我有2个数据系列,但该线看起来像是相互连接,第一个点连接到最后一个点。如何让它们分开?
在示例网站:http://docs.oracle.com/javafx/2/charts/line-chart.htm#CIHGBCFI中,图表会自动保持单独和线条颜色更改。
我的代码有问题吗?请帮忙
这是我的代码:
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,
答案 0 :(得分:1)
main方法中的解析逻辑是错误的:您有太多嵌套的hours = [["Alice", 43], ["Bob", 37], ["Fred", 15]]
titles = [["Alice", "Manager"], ["Betty", "Consultant"], ["Bob", "Assistant"]]
managers = [(x, y) for ((x, y), (z, w)) in zip(hours, titles) if w == "Manager"]
循环。因此,您创建的每个for
实例都包含 all 数据点。绘制数据系列时,它们是相同的(因此第二个模糊了第一个并且是您看到的唯一一个),并且每个连接数据集中的所有点。
您需要消除中间DataSeries
循环:
for