有没有办法在JavaFX LineChart中断开2个串联串? [英] Is there a way to disconnect 2 dots in series in JavaFX LineChart?

查看:129
本文介绍了有没有办法在JavaFX LineChart中断开2个串联串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在LineChart上有四个系列。每个系列都包含一些时间分割的图形。默认情况下,LineChart连接这些图形。它看起来很丑,在上下文中没有任何意义,所以我想将它们分开,但保留颜色和图例。换句话说,我想要的是删除两个特定点之间的连接。有没有办法在不借助向图表中添加新系列的情况下执行此操作(图表是逻辑连接的,添加新系列会让用户感到困惑并使图表混乱)?

I have four series on LineChart. Each series consists of some amount of graphs split in time. By default, LineChart connects those graphs. It looks ugly and makes no sense in the context, so I want to separate them, but keep the color and the legend. In other words what I want is to remove connection between two specific points. Is there a way to do this without resorting to adding new series to the chart (the graphs are logically connected, and adding new series would confuse the user and clutter the chart)?

for(int j = 0; j < 4; j++) {
    XYChart.Series<Float, Float> series = new XYChart.Series<>();
    series.setName("Канал " + (j + 1));
    fillWithData(series);
    chart.getData().add(series);
}

推荐答案

是的,你可以

https://docs.oracle.com/javase/8/javafx /api/javafx/scene/chart/XYChart.Series.html#nodeProperty 系列节点用于存储该行。节点是你可以修改的路径。

As stated in https://docs.oracle.com/javase/8/javafx/api/javafx/scene/chart/XYChart.Series.html#nodeProperty the serie node is use to store the line. the node is a path that you can modify.

看看这个小应用程序:

    package application;

import java.util.Arrays;
import java.util.List;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.Axis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.shape.Circle;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {

            NumberAxis xAxis = new NumberAxis();
            NumberAxis yAxis = new NumberAxis();
            MyLineChart chart = new MyLineChart(xAxis, yAxis, Arrays.asList(5));


            XYChart.Series<Number, Number> serie = new Series<>();

            serie.getData().add(new Data<Number, Number>(0, 5));
            serie.getData().add(new Data<Number, Number>(1, 5));
            serie.getData().add(new Data<Number, Number>(2, 5));
            serie.getData().add(new Data<Number, Number>(3, 5));
            serie.getData().add(new Data<Number, Number>(4, 5));
            serie.getData().add(new Data<Number, Number>(5, -5));
            serie.getData().add(new Data<Number, Number>(6, -5));
            serie.getData().add(new Data<Number, Number>(7, -5));

            chart.getData().add(serie);

            Path p = (Path) serie.getNode();        

            Scene scene = new Scene(chart,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();



    }

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

    private static class MyLineChart extends LineChart<Number,Number>{


        private List<Integer> breakpointIndex;

        public MyLineChart(Axis<Number> xAxis, Axis<Number> yAxis, List<Integer> breakpointIndex) {
            super(xAxis, yAxis);
            this.breakpointIndex = breakpointIndex;
        }

        @Override
        protected void layoutPlotChildren() {
            super.layoutPlotChildren();

            Path p = (Path) getData().get(0).getNode();

            breakpointIndex.forEach(i ->{

                Data<Number, Number> discontinuousPoint = getData().get(0).getData().get(i+1);
                p.getElements().add(i+1, new MoveTo(getXAxis().getDisplayPosition( discontinuousPoint.getXValue()), getYAxis().getDisplayPosition(discontinuousPoint.getYValue())));

            });

            System.out.println("\nnew Path :");
            p.getElements().forEach(e -> System.out.println("p : " + e));


            //getPlotChildren().add(new Circle(50));


        }

    }
}

你必须覆盖负责绘制图表的方法layoutplotchildren(如果可以的话,看看原始方法的代码很有意思。)

You have to override the method layoutplotchildren that is responsible to draw the chart (it's interesting to see the code of the origin method if you can).

然后你可以在这里画出你想要的东西。

and then you can draw what you want here.

在我的小应用程序中,我只是修改已经创建的节点,但你可以省略超级调用并绘制你想要什么。

In my small app i'am just modifying the node that are already create but you can omit the super call and draw what you want.

看看你有访问图表元素的方法,比如getPlotChildren(),getchartchildren()。

look at the doc you have method to access chartelement like getPlotChildren(), getchartchildren().

您可以使用getdata()

and you can access to the serie added to the chart with getdata()

这篇关于有没有办法在JavaFX LineChart中断开2个串联串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆