gpt4 book ai didi

java - JFreeChart XYSeries 作为字符串

转载 作者:行者123 更新时间:2023-11-30 04:26:46 25 4
gpt4 key购买 nike

我正在使用 jfreechart 并尝试制作一个运行良好的 XYLineChart。

我的问题是,y 标签是 double 值,我需要字符串。

我的代码:

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

最佳答案

使用:

plot.setDomainAxis(rangeAxis); 

解决了我的问题。

感谢垃圾神的帮助。

关于java - JFreeChart XYSeries 作为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15688496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com