我正在使用 jfreechart 并尝试制作一个运行良好的 XYLineChart。
我的问题是,y 标签是双值,我需要字符串。
我的代码:
DefaultXYDataset result = new DefaultXYDataset();
XYSeries series1 = new XYSeries("Words");
series1.add(0, 0.3);
series1.add(1, 0.5);
series1.add(2, 0.6);
series1.add(3, 0.3);
series1.add(4, 0.2);
series1.add(5, 1);
result.addSeries(getTitle(), series1.toArray());
我想要类似的东西:
XYSeries series1 = new XYSeries("Words");
series1.add("word 1", 0.3);
series1.add("word 2", 0.5);
...
使用符号轴的更新代码:
private void test2() {
XYDataset dataset = createDataset2();
JFreeChart chart = createChart2(dataset, "NN");
ChartPanel chartPanel = new ChartPanel(chart);
chartPanel.setPreferredSize(new java.awt.Dimension(500, 250));
panel_visualize.add(chartPanel);
}
private DefaultXYDataset createDataset2()
{
DefaultXYDataset result = new DefaultXYDataset();
XYSeries series1 = new XYSeries("Words");
series1.add(0.3, 0);
series1.add(0.5, 1);
series1.add(0.6, 2);
series1.add(0.3, 3);
series1.add(0.2, 4);
result.addSeries(getTitle(), series1.toArray());
return result;
}
private JFreeChart createChart2(XYDataset dataset, String title)
{
JFreeChart chart = ChartFactory.createXYLineChart(title, // chart title
"Words",
"Activation",
dataset, // data
PlotOrientation.HORIZONTAL,
true, // include legend
true,
false);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setForegroundAlpha(0.5f);
String[] grade = new String[5];
grade[0] = "Temp 0";
grade[1] = "Temp 1";
grade[2] = "Temp 2";
grade[3] = "Temp 3";
grade[4] = "Temp 4";
SymbolAxis rangeAxis = new SymbolAxis("Words", grade);
rangeAxis.setTickUnit(new NumberTickUnit(1));
rangeAxis.setRange(0,grade.length);
plot.setRangeAxis(rangeAxis);
return chart;
}