【发布时间】:2020-09-20 21:36:54
【问题描述】:
我想为每个图表绘制一个系列,最后将所有系列放在一个图表中,但没有成功。我在寻求帮助。代码简单直接。这是我的代码:
-
主类;
public class TestChart extends Application { GenPlots genPlots =new GenPlots(); @Override public void start(Stage primaryStage) { Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(event -> { genPlots.GenPlots("Hello"); }); StackPane root = new StackPane(); root.getChildren().add(btn); Scene scene = new Scene(root, 200, 250); primaryStage.setTitle("TestCharts"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } } -
该课程旨在生成系列和图表:
public class GenPlots { public GenPlots() {}; Axis xAxis = new NumberAxis(); Axis yAxis = new NumberAxis(); LineChart<Number, Number> lineChart = new LineChart<Number, Number> (xAxis, yAxis); LineChart<Number, Number> lineChartMulti = new LineChart<Number, Number>(xAxis, yAxis); String serName="*"; // generate the linecharts public void GenPlots (String hello) { lineChart.getData().clear(); lineChartMulti.getData().clear(); for (int j=1; j<4;j++) { XYChart.Series serSIF = new XYChart.Series(); serSIF=getSeries(); serName=String.valueOf(j); serSIF.setName("Only one "+serName); lineChart.getData().add(serSIF); displayChart(lineChart,serName); lineChartMulti.getData().add(serSIF); } displayChart(lineChartMulti,serName+"All Series"); } // end method // get the series with values - sample public XYChart.Series getSeries() { double x=0.0; double fx=0.0; XYChart.Series serL = new XYChart.Series(); for (int k=1; k<5;k++) { x=x+2; fx=x*x*j; serL.getData().add(new XYChart.Data(x,fx)); } return serL; } // plot the lineCharts public void displayChart( LineChart<Number, Number>lineChart, String chartTitle ) { Stage window = new Stage(); window.initModality(Modality.NONE); StackPane vb = new StackPane(); vb.setPadding(new Insets(20, 20, 20, 20)); lineChart.setTitle(chartTitle); vb.getChildren().add(lineChart); Scene scene = new Scene(vb,500,600); window.setScene(scene); window.show(); } }
此外,所有系列的最后一个图都正确显示,但其他的 - 每个图表一个系列 - 被扭曲,或者根本没有绘制。每次生成折线图时,该系列似乎都重置为空。我认为是由于该系列是可观察的,但我不知道如何解决这个问题。恳请您的贡献
【问题讨论】:
-
不要转发问题,而是改进以前的问题..
-
您不能在多个图表中重复使用系列(就像您将 serSIf 添加到 lineChart 和 lineChartMulti 一样)。顺便说一句:在未来,坚持 java 命名约定并花更多的精力来格式化代码(没有制表符,每行至少有 4 个前导空格) - 否则它几乎不可读..
-
感谢您的回答。如何解决它,以不同的名称重新生成相同的系列?以某种方式,是否有可能将系列脱钩,或者类似的想法?
-
不理解您的评论 - 无论如何,只需使用新数据创建一个新系列(相同的值,但不同的实例)
标签: javafx axis series linechart redraw