【问题标题】:JFreechart Boxplot changing size of boxes when coloring the boxesJFreechart Boxplot 在为盒子着色时改变盒子的大小
【发布时间】:2016-09-02 09:05:20
【问题描述】:

我正在使用 JFreeChart 制作箱线图(代码在底部)。当我不为每个盒子添加颜色时,它们会被画宽并正确居中(如我所愿):

但是,当我通过 x 轴标签为它们着色时,它们会变小并且不再正确居中:

我怎样才能得到第二个图的颜色,但第一个图的盒子大小?


package test;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.BoxAndWhiskerToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BoxAndWhiskerRenderer;
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;

public class test {
    public static void main(String[] args) throws Exception {
    DefaultBoxAndWhiskerCategoryDataset dataset = new DefaultBoxAndWhiskerCategoryDataset();

    // example data
    HashMap<String, ArrayList<Double>> test = new HashMap<String, ArrayList<Double>>();
    test.put("A",new ArrayList<Double>(Arrays.asList(0.8, 1.4, 0.8, 1.9, 1.2)));
    test.put("B",new ArrayList<Double>(Arrays.asList(0.8, 1.4, 0.8, 1.9, 1.2)));
    test.put("C",new ArrayList<Double>(Arrays.asList(0.8, 1.4, 0.8, 1.9, 1.2)));
    test.put("D",new ArrayList<Double>(Arrays.asList(0.8, 1.4, 0.8, 1.9, 1.2)));
    test.put("E",new ArrayList<Double>(Arrays.asList(0.8, 1.4, 0.8, 1.9, 1.2)));
    for (String k : test.keySet()){
        /* change to 
         *     String xAxisLabel = "";
         * to get wide plot
         */
        String xAxisLabel = k;
        dataset.add(test.get(k), xAxisLabel, k);// + beta of interactionterm");
    }
    final CategoryAxis xAxis = new CategoryAxis("Example x-axis");
    final NumberAxis yAxis = new NumberAxis("Example y-axis");
    yAxis.setAutoRangeIncludesZero(false);
    final BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
    renderer.setFillBox(true);
    renderer.setSeriesToolTipGenerator(1, new BoxAndWhiskerToolTipGenerator());
    renderer.setMeanVisible(false);
    final CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);

    final JFreeChart chart = new JFreeChart(
            "Example",
            plot
            );
    final ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new java.awt.Dimension(3000,1800));
    ChartUtilities.saveChartAsPNG(new File("test.png"), chart, 1000, 600);
    }
}

【问题讨论】:

    标签: java jfreechart


    【解决方案1】:

    不同之处在于您的第一张图片有一个系列,而您的第二张图片有五个系列。与其添加大量系列,不如添加一个系列,其中包含五个项目,例如您的顶部图片。您可以使用覆盖getItemPaint() 的自定义BoxAndWhiskerRenderer 来获得不同的颜色,就像它们为here 显示XYLineAndShapeRenderer 一样。

    编辑:要获得匹配的图例,您需要一个新的DrawingSupplier,例如this

    【讨论】:

    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 2017-09-23
    • 2013-06-02
    • 1970-01-01
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    相关资源
    最近更新 更多