JavaFX Adding multiple XYChart.Series to LineChart without explicating declaring each series

前提是你 提交于 2021-01-29 06:36:31

问题


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

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.

Here is my code

Main class that creates the LineChart and instantiate 10 different companies.

    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 Class for generating a new series to be added to 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;
  }

}

回答1:


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 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 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;
    }

}



回答2:


To not declare series1, series2, series3 ... you can create an ArrayList. In the code bellow I created an ArrayList with all the companies. I also created a MouseClickEvent. When you press a button on your mouse, the company, defined by variavble companysNumber, changes its values.

StockChart class

import java.util.ArrayList;

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.scene.input.MouseEvent;
import javafx.stage.Stage;

public class StockChart extends Application {

    private int companysNumber = 4;

    @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);

        ArrayList<CompanyStockData> companysLines = new ArrayList<CompanyStockData>();

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

        scene.setOnMousePressed(new javafx.event.EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                companysLines.get(companysNumber).changeStock();
            }
        });

        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++) {
            companysLines.add(new CompanyStockData(lineChart, timeOfDay, stockCompany[i], i+3));
            lineChart.getData().add(companysLines.get(i).generateStock());
        }

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

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

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<String, Number> series = new XYChart.Series<String, Number>();

    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<String, Number> 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<String, Number>(timeOfDay[i], i * 2 + yAddedValue));
        }

        return series;
    }

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

}


来源:https://stackoverflow.com/questions/30356385/javafx-adding-multiple-xychart-series-to-linechart-without-explicating-declaring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!