当前位置: 动力学知识库 > 问答 > 编程问答 >

java - JavaFx chart multiple Series, how to make them keep separate

问题描述:

I have 2 data series but the line looks like connecting each other and the first point connect to last point. how to make them separate ?

in sample site : http://docs.oracle.com/javafx/2/charts/line-chart.htm#CIHGBCFI, the chart keep separate and line color change automaticaly.

is there something wrong with my code ? please help

this is my code :

class dataXY

{

public double X;

public double Y;

}

class DataSeries

{

public String NamaSeries;

public List<dataXY> Data;

}

class DataChart

{

public String Title;

public String LabelX;

public String LabelY;

public int JumlahSeries;

public List<DataSeries> Datas;

}

public class FxChartDemo extends Application {

public static DataChart Grafik;

public static void main(String[] argsk) {

// GET DATA FROM ARGS

String _test = "NRT USAGE TREND;TANGGAL;ROWCOUNT;2;EMP;HIVE;0:20|1:300|2:200;0:20|1:30|2:200";

String[] args = _test.split(";");

DataChart DC = new DataChart();

DC.Title = args[0];

DC.LabelX = args[1];

DC.LabelY = args[2];

DC.JumlahSeries = Integer.parseInt(args[3]);

DC.Datas = new ArrayList<DataSeries>();

for(int i = 1; i <= DC.JumlahSeries; i++)

{

DataSeries DS = new DataSeries();

DS.NamaSeries = args[3+i];

DS.Data = new ArrayList<dataXY>();

for(int j = 0; j < DC.JumlahSeries; j++)

{

String[] _data = args[4+DC.JumlahSeries+j].split("\\|");

for(int k = 0; k < _data.length; k++)

{

dataXY a = new dataXY();

a.X = Double.parseDouble((_data[k].split(":"))[0]);

a.Y = Double.parseDouble((_data[k].split(":"))[1]);

DS.Data.add(a);

}

}

DC.Datas.add(DS);

}

Grafik = DC;

launch(args);

}

@Override

public void start(Stage stage) throws Exception {

final NumberAxis xAxis = new NumberAxis();

final NumberAxis yAxis = new NumberAxis();

xAxis.setLabel(Grafik.LabelX);

yAxis.setLabel(Grafik.LabelY);

xAxis.setAnimated(false);

yAxis.setAnimated(false);

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

lineChart.setTitle(Grafik.Title);

for(int i=0; i<Grafik.JumlahSeries; i++)

{

XYChart.Series series = new XYChart.Series();

series.setName(Grafik.Datas.get(i).NamaSeries);

for(int j = 0; j< Grafik.Datas.get(i).Data.size(); j++)

{

series.getData().add(new XYChart.Data(Grafik.Datas.get(i).Data.get(j).X, Grafik.Datas.get(i).Data.get(j).Y));

}

lineChart.getData().add(series);

}

lineChart.setAnimated(false);

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

saveAsPng(lineChart, "chart.png");

}

public static void saveAsPng(LineChart lineChart, String path) {

WritableImage image = lineChart.snapshot(new SnapshotParameters(), null);

File file = new File(path);

try {

ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);

} catch (IOException e) {

e.printStackTrace();

}

}

}

thx,

网友答案:

The parse logic in your main method is wrong: you have too many nested for loops. Consequently, each of the DataSeries instances you create contains all the data points. When the data series are plotted, they are identical (so the second one obscures the first and is the only one you see) and each joins all the points in the data set.

You need to eliminate the middle for loop:

public static void main(String[] argsk) {
    // GET DATA FROM ARGS
    String _test = "NRT USAGE TREND;TANGGAL;ROWCOUNT;2;EMP;HIVE;0:20|1:300|2:200;0:20|1:30|2:200";
    String[] args = _test.split(";");
    DataChart DC = new DataChart();
    DC.Title = args[0];
    DC.LabelX = args[1];
    DC.LabelY = args[2];
    DC.JumlahSeries = Integer.parseInt(args[3]);
    DC.Datas = new ArrayList<DataSeries>();
    for(int i = 1; i <= DC.JumlahSeries; i++)
    {
        DataSeries DS = new DataSeries();
        DS.NamaSeries = args[3+i];
        DS.Data = new ArrayList<dataXY>();
        String[] _data = args[3+DC.JumlahSeries+i].split("\\|");
        for(int k = 0; k < _data.length; k++)
        {
            dataXY a = new dataXY();
            a.X = Double.parseDouble((_data[k].split(":"))[0]);
            a.Y = Double.parseDouble((_data[k].split(":"))[1]);
            DS.Data.add(a);
        }
        DC.Datas.add(DS);
    }
    Grafik = DC;
    launch(args); 
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: