移动刻度标签JavaFx 2

2023-11-29

是否可以将刻度标签移动/移动到图表中。目前我看到隐藏/显示刻度标签的 API 是否有可以在图表内移动刻度标签的 API?如果没有 API,那么我可以使用/应用某种技术来完成此任务吗?

当前代码

public class Graph extends Application{
private NumberAxis xAxis;
private NumberAxis yAxis;

public static void main(final String[] args)
{
    launch(args);
}

@Override
public void start(final Stage primaryStage) throws Exception
{
    xAxis = new NumberAxis(0, 300, 20);
    xAxis.setAutoRanging(false);
    xAxis.setAnimated(false);
    xAxis.setMinorTickVisible(false);
    xAxis.setTickLabelsVisible(false);
    xAxis.setTickMarkVisible(false);

    yAxis = new NumberAxis(30, 240, 30);
    yAxis.setAutoRanging(false);
    yAxis.setAnimated(false);
    yAxis.setTickMarkVisible(false);
    yAxis.setMinorTickVisible(false);
    yAxis.setMinorTickCount(3);

    final LineChart<Number, Number> ctg = new LineChart<>(xAxis, yAxis);

    ctg.setAnimated(false);
    ctg.setCreateSymbols(false);
    ctg.setAlternativeRowFillVisible(false);
    ctg.setLegendVisible(false);
    ctg.setHorizontalGridLinesVisible(true);
    ctg.setVerticalGridLinesVisible(true);

    Series<Number, Number> series = new LineChart.Series<>();
    ctg.getData().add(series);

    for (int i = 0; i < 300; i += 5) {

        XYChart.Series minorYGrid = new XYChart.Series();
        minorYGrid.getData().add(new XYChart.Data(i, 30));
        minorYGrid.getData().add(new XYChart.Data(i, 240));
        ctg.getData().add(minorYGrid);
    }

    for (int i = 40; i <= 240; i += 10) {

        XYChart.Series minorXGrid = new XYChart.Series();
        minorXGrid.getData().add(new XYChart.Data(0, i));
        minorXGrid.getData().add(new XYChart.Data(500, i));
        ctg.getData().add(minorXGrid);
    }

    final Scene scene = new Scene(ctg, 1600, 400);
    scene.getStylesheets().add("resources/application.css");
    primaryStage.setScene(scene);
    primaryStage.show();
}
}

目前结果

enter image description here

Expected result enter image description here


平移单轴

您可以平移图表上的 y 轴。

例如:

yAxis.translateXProperty().bind(
    xAxis.widthProperty().divide(2)
);

为了确保轴显示在图表顶部,您可以在场景中将深度缓冲区设置为 true,并将 yAxis 的 z 坐标设置为 -1。

yAxis.setTranslateZ(-1);

平移多个轴

您的“预期结果”实际上有多个垂直轴。不幸的是,JavaFX 中没有克隆方法来克隆节点。因此,您必须创建一个新轴并将其分层在图表顶部。实现这一目标的一种方法(这有点矫枉过正且效率低下)是创建一个全新的图表并将其分层在旧图表之上,类似于解决方案中所做的操作绘制 XYCharts 图层。另一种方法可能更好,但有点棘手,就是创建另一个轴并将其堆叠在原始图表上。

示例代码

multi-axis chart

多轴图表.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class MultiAxisChart extends Application {

    @Override
    public void start(final Stage primaryStage) throws Exception {
        final StackPane chartStack = createChartStack();

        final Scene scene = new Scene(chartStack, 1600, 400, true);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private StackPane createChartStack() {
        LineChart<Number, Number> bottomChart = createChart();
        LineChart<Number, Number> topChart    = createChart();

        bottomChart.getYAxis().translateXProperty().bind(
                bottomChart.getXAxis().widthProperty().multiply(1.0/3)
        );
        topChart.getYAxis().translateXProperty().bind(
                topChart.getXAxis().widthProperty().multiply(2.0/3)
        );

        bottomChart.getYAxis().setTranslateZ(-1);
        topChart.getYAxis().setTranslateZ(-1);

        topChart.getStylesheets().addAll(getClass().getResource(
                "overlay-chart.css"
        ).toExternalForm());

        return new StackPane(bottomChart, topChart);
    }

    private LineChart<Number, Number> createChart() {
        NumberAxis xAxis = new NumberAxis(0, 300, 20);
        xAxis.setAutoRanging(false);
        xAxis.setAnimated(false);
        xAxis.setMinorTickVisible(false);
        xAxis.setTickLabelsVisible(false);
        xAxis.setTickMarkVisible(false);

        NumberAxis yAxis = new NumberAxis(30, 240, 30);
        yAxis.setAutoRanging(false);
        yAxis.setAnimated(false);
        yAxis.setTickMarkVisible(false);
        yAxis.setMinorTickVisible(false);
        yAxis.setMinorTickCount(3);

        final LineChart<Number, Number> ctg = new LineChart<>(xAxis, yAxis);

        ctg.setAnimated(false);
        ctg.setCreateSymbols(false);
        ctg.setAlternativeRowFillVisible(false);
        ctg.setLegendVisible(false);
        ctg.setHorizontalGridLinesVisible(true);
        ctg.setVerticalGridLinesVisible(true);

        return ctg;
    }

    public static void main(final String[] args) {
        launch(args);
    }
}

覆盖图.css

/** file: overlay-chart.css (place in same directory as MultiAxisChart) */
.chart-plot-background {
    -fx-background-color: transparent;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

移动刻度标签JavaFx 2 的相关文章

  • 如何在itext中设置自定义颜色?

    感谢您花时间回答我的问题 我正在使用 Java 中的 iText 生成 PDF 文档 我需要将表的列标题设置为与值列中的颜色不同的颜色 我有来自 Photoshop 的颜色十六进制值 我正在使用带有块和段落的 PdfPTable 除了 Ba
  • Tomcat 7 停止接收 HTTP 请求

    我有一个Tomcat 7接收大量数据的服务器GET 要求 这种方法在一段时间内效果很好 然后突然停止工作 7 8 小时后 当它停止工作时 我收到此错误 五月 06 2015 12 47 58 AM org apache coyote htt
  • 协助持续进行 Java 到 C# 转换的工具

    如今 许多项目都是用 Java 编写的 其中一些最终转换为 C 以合并到 NET 中 我想到的例子有 log4net nhibernate 和 db4o 包括 Sharpen db4o 的工具 在内 您是否见过和 或使用过任何使连续转换变得
  • 在 Postgres 中为特定查询设置 work_mem

    我正在使用一个委托给 JDBC 驱动程序的库PostgreSQL 而且有些查询非常复杂 需要更多内存 我不想设置work mem对于所有查询来说都是大的 只是这个子集 问题是执行以下代码会导致错误 pseudo code for what
  • 如何使用 Java 本机接口将字节数组传递到以 char* 作为参数的 C 函数中?

    所以我需要使用JNI从java调用C函数 当传入不同的数据类型 创建本机变量 头文件 共享库等等 时 我已经能够成功地做到这一点 但无法让它与字节数组一起使用 这是我的 C 函数 include
  • jUnit 中每个 @Test 的不同拆卸

    有没有办法为 jUnit 中的每个 Test 定义不同的拆卸 Use the After注释来指示每个之后要运行的方法 Test 像这样的全套注释是 BeforeClass 首先 Tests are run Before 在每个之前 Tes
  • Java 内存错误:无法创建新的本机线程

    运行 java 服务器时 我在 UNIX 服务器上收到此错误 Exception in thread Thread 0 java lang OutOfMemoryError unable to create new native threa
  • 在 Java 中停止线程? [复制]

    这个问题在这里已经有答案了 我正在编写一段代码 该代码连接到服务器 使用该连接生成一堆线程并执行一堆 东西 在某些情况下 连接会失败 我需要停止一切并从头开始使用新对象 我想在对象之后进行清理 但在线程上调用 thread stop 但此方
  • Spring 在 AuthenticationSuccessHandler 中自动装配会话范围 bean 不起作用

    我正在使用 spring security 我想初始化一个对象User在用户成功登录后的会话中 安全配置如下 Configuration EnableWebSecurity PropertySource classpath configs
  • 测试正确的时区处理

    我们正在处理大量数据 所有数据均以 UTC Java 语言 标记 在读取这些数据 将其存储在数据库中以及再次将其取出之间 发生了一些数据在夏令时期间关闭一小时的情况 由于 UTC 没有夏令时的概念 这显然是软件中的一个错误 一旦知道 就很容
  • Android Studio安装JDK错误

    In Android Studio I am facing bellow error 当我按下时会显示此弹出窗口Alt Enter对于缺少的类 符号 当我点击 setup SDK 时 它显示两个选项 1 8 Java版本 1 8 0 60
  • 应返回带有 html 代码的字符串的支持 bean 属性返回空字符串

    我的支持 bean 中有一个返回 html 代码的属性 public String getHtmlPrevisualizar return Hello world 我想要做的是在 iframe 中显示这个 html 代码 我用 JavaSc
  • 选择活动时运行时崩溃

    首先我想说我几乎没有 Android 经验 这是我在 Android 中的第一个项目 而且我的老师不太擅长教学 所以我对任何过度的无知表示歉意 在进一步讨论之前先解释一下 我的应用程序的目标本质上是能够记录您在某些活动上花费了多少时间 记录
  • 如何列出所有已加载的 Spring bean 定义文件

    在大型企业系统中 并不总是清楚在 ApplicationContext 构建期间导入了哪些文件 有没有办法列出过程中加载的所有文件 我知道如何列出加载的属性文件 但不知道导入的 bean 文件 更新示例 文件 1 applicationCo
  • 访问 JAR 资源

    我有一个jar包含我想要分发的资源 主要是缓存 日志记录等配置 的文件 我对这些资源的相对路径有问题 所以我做了我在另一个 stackoverflow 问题中发现的问题 该问题说这是一种有效的方法 ClassInTheSamePackage
  • 当列表中不存在 X 时,从列表中查找大于 X 的值

    我试图从列表中查找大于特定值 在我的情况下已知 的值 Example Given list 1 2 5 10 15 list is sorted 查找大于的值X 7在这种情况下 期望的结果 返回一个包含值的列表 10 15 我尝试使用jav
  • 为什么我得到:没有有效的 JFX 运行时

    我有一个使用 java 1 6 编译并使用 jnlp webstart 运行的现有应用程序 如果我使用 JRE 1 6 从客户端运行此应用程序 一切都会很好 但是 当我使用 java JDK 7 编译代码并使用 JRE 1 7 67 运行客
  • 如何在 Hibernate 中使用 SELECT 进行 INSERT

    我需要在休眠中实现以下请求 insert into my table max column values select max id from special table where 如何在休眠中使用注释来做到这一点 Special tab
  • 如何在android中使用Room Persistence ORM工具实现created_at和updated_at列

    我该如何实施created at and updated at在Android中使用Room Persistence ORM工具的列 可以在创建或更新表中的行时自动更新时间戳 我研究了很多网站 但仍然没有找到任何可以处理的结果middlew
  • java有类似C#的属性吗? [复制]

    这个问题在这里已经有答案了 C 属性 我的意思是 get 和 set 方法 是一个非常有用的功能 java 也有类似 C 的属性吗 我的意思是我们如何在 java 中实现类似以下 C 代码的内容 public string Name get

随机推荐