在JavaFX中绘制数据集时,LineChart不是自动调整大小

时间:2018-06-18 10:39:58

标签: javafx linechart

我在JavaFX中使用LineChart。我使用与输入相同长度的数组进行绘图。 但是当我在我的应用程序中包含这个时,情节不是自动调整大小。 我已经将快照包含在其中。

enter image description here

我想将线宽设置得更小,也可以更改颜色。

以下是绘制此图表的代码

public class ChartPlot extends Application {
    static LineChart<Number, Number> linechart;
    static double[] xArray, yArray;

    public static LineChart linePlot(double[] x, double[] y) {
        xArray = new double[x.length];
        yArray = new double[y.length];

        xArray = x;
        yArray = y;

        // Defining the x axis
        final NumberAxis xAxis = new NumberAxis();
        xAxis.setLabel("Wavelength");

        // Defining the y axis
        final NumberAxis yAxis = new NumberAxis();
        yAxis.setLabel("Intensity");

        // Creating the line chart
        linechart = new LineChart<Number, Number>(xAxis, yAxis);

        // Prepare XYChart.Series objects by setting data
        XYChart.Series series = new XYChart.Series();
        // series.setName("No of schools in an year");

        // Setting the data to Line chart
        for (int i = 0; i < xArray.length; i++) {
            series.getData().add(new XYChart.Data(xArray[i], yArray[i]));
        }

        linechart.setCreateSymbols(false);
        linechart.getData().add(series);
        return linechart;
    }
}

请帮我解决这个问题。

提前致谢

1 个答案:

答案 0 :(得分:1)

NumerAxis need to be added with autoRange. Here is the code
public static LineChart linePlot(double[] x,double[] y)
{
    xArray=new double[x.length];
    yArray=new double[y.length];

    xArray=x;
    yArray=y;

    //Defining the x axis             
  final NumberAxis xAxis = new NumberAxis(); 
  xAxis.setLabel("Wavelength"); 

  //Defining the y axis   
  final NumberAxis yAxis = new NumberAxis(); 
  yAxis.setLabel("Intensity"); 

    //Creating the line chart 
  linechart= new LineChart<Number,Number>(xAxis,yAxis); 


  linechart.getData().clear();
  //Prepare XYChart.Series objects by setting data 
  XYChart.Series series = new XYChart.Series(); 
  //series.setName("No of schools in an year"); 

  //Setting the data to Line chart  
  for(int i = 0; i<xArray.length; i++) 
  {
        series.getData().add(new XYChart.Data(xArray[i], yArray[i]));
  }

  linechart.setCreateSymbols(false);

  linechart.getData().add(series); 

  *This is what I have Changed*

  **xAxis.setAutoRanging(true);
  xAxis.setForceZeroInRange(false);
  yAxis.setAutoRanging(true);
  yAxis.setForceZeroInRange(false);**
  linechart.autosize();
  linechart.applyCss();
    return linechart;

  }

}