我有数据表示我想表示为方框和胡须图,并设置了类别图表。但是,此外,我还想添加一个折线图,显示每个数据点的平均值累积。
现在我的代码是这样的
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时,我只看到框和胡须呈现(没有线条和形状图)。
在输出图像中使用渲染器和数据集时还需要做些什么吗?另外,有没有更好的方法来获得我的盒子和胡须图中的平均值的累积线?
答案 0 :(得分:2)
我正在试图找出如何在盒子顶部放置点和正确的胡须点。
如CategoryDataset
here所示,每个rowKey
都是一个系列,每个columnKey
都是系列中的一个元素。在下面的示例中,"Mean"
行catData
包含columnKey
中每个boxData
的条目。使用setDatasetRenderingOrder()
指定DatasetRenderingOrder
。
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);
}
}