gpt4 book ai didi

java - JFreeChart 以折线图显示值

转载 作者:太空宇宙 更新时间:2023-11-04 06:51:37 25 4
gpt4 key购买 nike

如何在折线图中显示系列值。

我当前的代码

public void renderChart(String variation, OutputStream stream) throws Exception {
boolean rotate = "rotate".equals(variation); // add ?variation=rotate to the URL to rotate the chart
JFreeChart chart = generateChart(rotate);
ChartUtilities.writeChartAsPNG(stream, chart, 750, 400);
}

private JFreeChart generateChart(boolean rotate) {
DefaultCategoryDataset data = ChartData.getDataset();
JFreeChart chart = ChartFactory.createLineChart("example graph", // title
"x-axis", // x-axis label
"y-axis", // y-axis label
data, rotate ? PlotOrientation.HORIZONTAL : PlotOrientation.VERTICAL, true, // legend displayed
true, // tooltips displayed
false); // no URLs*/
CategoryPlot plot = (CategoryPlot) chart.getPlot();
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
DecimalFormat decimalformat1 = new DecimalFormat("##");
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator("{2}", decimalformat1));
renderer.setItemLabelsVisible(true);
renderer.setSeriesVisible(true);
return chart;
}

谁能告诉我如何在此显示值

最佳答案

为了运行您的代码,我创建了一个 main 方法,并创建了一个创建一些示例数据的方法 ChartData.getDataset() 。而且你的代码运行得很好。所以我猜你的问题是你没有我刚才提到的这两种方法。

以下是创建简单测试数据集的方法:

class ChartData {
public static DefaultCategoryDataset getDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1, "Foo", "A");
dataset.addValue(10, "Foo", "B");
dataset.addValue(5, "Foo", "C");
dataset.addValue(2, "Bar", "A");
dataset.addValue(3, "Bar", "B");
dataset.addValue(8, "Bar", "C");
return dataset;
}
}

这是一个 main 方法:

public class JFreeChartSnippet {

//// ////
// Copy your code from your question here //
//// ////

public static void main(String[] args) throws Exception {
new JFreeChartSnippet().renderChart("rotate", new FileOutputStream("foobar.png"));
}
}

现在您应该在文件 foobar.png 中看到一些输出:

Output from above code

关于java - JFreeChart 以折线图显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23242433/

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