gpt4 book ai didi

java - 在 JFreechart 中使用多个渲染器

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

我想将数据表示为盒须图,并设置了类别图来实现此目的。不过,此外,我想添加一个折线图,显示每个数据点平均值的累积。

现在我的代码是这样的

DefaultBoxAndWhiskerCategoryDataset dataSet = new DefaultBoxAndWhiskerCategoryDataset();
int i = 0;
for (List<Integer> categoryList : categoryLists) {
dataSet.add(categoryList, i, "BoxAndWhiskers");
i++;
}
BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();

double sum = 0;
i = 0;
DefaultCategoryDataset averageDataSet = new DefaultCategoryDataset();
for (double average : averages) {
sum += average;
averageDataSet.add(sum, i, "Average");
i++;
}
LineAndShapeRenderer lineAndShapeRenderer = new LineAndShapeRenderer();


CategoryAxis xAxis = new CategoryAxis("Type");
NumberAxis yAxis = new NumberAxis("Value");
CategoryPlot plot = new CategoryPlot(dataSet, xAxis, yAxis, renderer);

plot.setDataset(1, averageDataSet);
plot.setRenderer(1, lineAndShapeRenderer);

JFreeChart chart = new JFreeChart(
"Box-and-Whisker",
new Font("SansSerif", Font.BOLD, 14),
plot,
true
);

BufferedImage outputImage = chart.createBufferedImage(800, 600);

ImageIO.write(outputImage, "png", new File("output.png"));

但是,当我查看我的output.png时,我只看到渲染的框和 mustache (没有线和形状图)。

我还需要做些什么才能在输出图像中使用渲染器和数据集吗?此外,是否有更好的方法来获取方框和 mustache 图中平均值的累积线?

最佳答案

I'm trying to figure out how to lay the points on top of the box and whiskers points properly.

如图here对于CategoryDataset,每个rowKey是一个系列,每个columnKey是该系列中的一个元素。在下面的示例中,catData"Mean" 行包含 boxData 中每个 columnKey 的条目。使用setDatasetRenderingOrder()指定DatasetRenderingOrder

image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.Arrays;
import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;

/**
* @see https://stackoverflow.com/a/38242696/230513
*/
public class Test {

private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DefaultBoxAndWhiskerCategoryDataset boxData = new DefaultBoxAndWhiskerCategoryDataset();
boxData.add(Arrays.asList(30, 36, 46, 55, 65, 76, 81, 80, 71, 59, 44, 34), "Planet", "Endor");
boxData.add(Arrays.asList(22, 25, 34, 44, 54, 63, 69, 67, 59, 48, 38, 28), "Planet", "Hoth");
BoxAndWhiskerRenderer boxRenderer = new BoxAndWhiskerRenderer();
DefaultCategoryDataset catData = new DefaultCategoryDataset();
catData.addValue(boxData.getMeanValue(0, 0), "Mean", boxData.getColumnKey(0));
catData.addValue(boxData.getMeanValue(0, 1), "Mean", boxData.getColumnKey(1));
LineAndShapeRenderer lineRenderer = new LineAndShapeRenderer();
CategoryAxis xAxis = new CategoryAxis("Type");
NumberAxis yAxis = new NumberAxis("Value");
yAxis.setAutoRangeIncludesZero(false);
CategoryPlot plot = new CategoryPlot(boxData, xAxis, yAxis, boxRenderer);
plot.setDataset(1, catData);
plot.setRenderer(1, lineRenderer);
plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
JFreeChart chart = new JFreeChart("Test", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
f.add(new ChartPanel(chart){
@Override
public Dimension getPreferredSize() {
return new Dimension(320, 480);
}
});
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Test()::display);
}
}

关于java - 在 JFreechart 中使用多个渲染器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38205149/

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