本文介绍了JavaFX将多个XYChart.Series添加到LineChart,而无需重复声明每个系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过以下教程来创建JavaFX图 https://docs.oracle.com/javafx/2/charts/line -chart.htm

I have been going through the following tutorial for creating JavaFX graphshttps://docs.oracle.com/javafx/2/charts/line-chart.htm

我的问题是,如何动态创建新数据系列并将其添加到LineChart中,而不必重复声明每个系列(即series1,series2)?

My question is, how can I dynamically create and add new series of data to my LineChart without explicating declaring each series i.e. series1, series2?

当我编译并运行程序时,仅添加了一个系列.

When I compile and run my program only a single series has been added.

这是我的代码

创建LineChart并实例化10个不同公司的主类.

    public class StockChart extends Application {

    @Override public void start(Stage stage) {

        stage.setTitle("Stock Market");
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
         xAxis.setLabel("Time of Day");
         yAxis.setLabel("Price Per Stock");
         LineChart<String,Number> lineChart = 
                new LineChart<String,Number>(xAxis,yAxis);
        Scene scene  = new Scene(lineChart,800,600); 
        lineChart.setTitle("Stock Exchange");

        String[] timeOfDay = {"8:00AM", "9:00AM", "10:00AM", "161:00AM", "12:00PM",
                              "1:00PM", "2:00PM", "3:00PM", "4:00PM" };
        String[] stockCompany = { "AAPL", "ORCL", "MSFT", "GOOG", "AMZN", "FB", "HPQ", "YHOO", "ADSK", "ATVI"};

        for(int i = 0; i < stockCompany.length; i++) {
          CompanyStockData tmpCompany = new CompanyStockData(lineChart, timeOfDay, stockCompany[i]);
          lineChart.getData().add(tmpCompany.generateStock());
          // lineChart.getData().add(line.generateStock());
          // lineChart.seriesRemoved(line.series);
        }



        stage.setScene(scene);
        stage.show();
    }


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

CompanyStockData类,用于生成要添加到LineChart的新系列

public class CompanyStockData extends StockChart {
  protected   LineChart<String,Number> lineChart;
  protected   String[] timeOfDay;
  protected  String companyName;
  public  XYChart.Series series = new XYChart.Series();

  public CompanyStockData(LineChart<String,Number> lineChart, String[] timeOfDay, String companyName) {
    this.lineChart = lineChart;
    this.timeOfDay = timeOfDay;
    this.companyName = companyName;
  }

  public XYChart.Series generateStock() {
    series.setName(companyName);
    System.out.println(lineChart);
    for(int i = 0; i < timeOfDay.length; i++) {
      System.out.println(timeOfDay[i]);
      series.getData().add(new XYChart.Data(timeOfDay[i], i*2));
    }
    return series;
  }

  public static void main(String[] args) {

  }

  @Override
  public String toString() {
    return "Company : " + this.companyName +
            " Time Of Day Length " + this.timeOfDay.length;
  }

}

推荐答案

您的代码在10家公司中排名第一.它们只是彼此覆盖,您看不到它们全部.为了解决这个问题,我在 CompanyStockData 类中添加了 yAddedValue 变量.这个变量只会使公司的生产线提高一点.

Your code is putting on chart 10 companies. They are just covered by each other and you are not able to see them all.To fix it I added yAddedValue variable in CompanyStockData class. This variable just moves the company's line a bit higher.

StockChart

StockChart class

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

public class StockChart extends Application {

    @Override public void start(Stage stage) {

        stage.setTitle("Stock Market");

        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();

        xAxis.setLabel("Time of Day");
        yAxis.setLabel("Price Per Stock");

        LineChart<String, Number> lineChart = new LineChart<String,Number>(xAxis,yAxis);

        Scene scene  = new Scene(lineChart,800,600); 

        lineChart.setTitle("Stock Exchange");

        String[] timeOfDay = {"8:00AM", "9:00AM", "10:00AM", "161:00AM", "12:00PM",
                            "1:00PM", "2:00PM", "3:00PM", "4:00PM" };
        String[] stockCompany = { "AAPL", "ORCL", "MSFT", "GOOG", "AMZN", "FB", "HPQ",
                            "YHOO", "ADSK", "ATVI"};

        for(int i = 0; i < stockCompany.length; i++) {
            CompanyStockData tmpCompany = new CompanyStockData(lineChart, timeOfDay, stockCompany[i], i+3);
            lineChart.getData().add(tmpCompany.generateStock());
        }

        stage.setScene(scene);
        stage.show();
    }    

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

CompanyStockData

CompanyStockData class

import javafx.scene.chart.LineChart;
import javafx.scene.chart.XYChart;

public class CompanyStockData {

    protected LineChart<String,Number> lineChart;
    protected String[] timeOfDay;
    protected String companyName;
    public XYChart.Series series = new XYChart.Series();

    private int yAddedValue;

    public CompanyStockData(LineChart<String,Number> lineChart, String[] timeOfDay, String companyName, int yAddedValue) {
        this.lineChart = lineChart;
        this.timeOfDay = timeOfDay;
        this.companyName = companyName;
        this.yAddedValue = yAddedValue;
    }

    public XYChart.Series generateStock() {
        series.setName(companyName);
        System.out.println(lineChart);

        for(int i = 0; i < timeOfDay.length; i++) {
            System.out.println(timeOfDay[i]);
            series.getData().add(new XYChart.Data(timeOfDay[i], i*2 + yAddedValue));
        }

        return series;
    }

}

这篇关于JavaFX将多个XYChart.Series添加到LineChart,而无需重复声明每个系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 12:46