一尘不染

具有多个类别的Boxplots的JFreeChart缩放

java

我目前正在使用JFreeChart显示框线图的基于Java的项目。

我的问题是如何显示包含带有大约20个类别和5个以上系列的CategoryDataset的箱形图的图表。

当前,如果未设置ChartPanel的首选大小,则图例,标签和注释可读取,但Boxplots太小。或设置ChartPanel的大小,以使Boxplots具有可接受的大小,然后将图例,标签和注释水平拉伸。

我的问题是,如何在不缩放图例,轴标签和图表注释的情况下正确缩放框线图?是否可以在不缩放图表所有元素的情况下缩放图表?

代码示例

import java.awt.Color;
import java.awt.Dimension;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JFrame;
import javax.swing.JScrollPane;

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.renderer.category.BoxAndWhiskerRenderer;
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;


public class StretchedBoxAndWhiskerExample{

DefaultBoxAndWhiskerCategoryDataset dataset;
JFreeChart chart;
ChartPanel chartPanel;
JFrame frame;
JScrollPane scrollPane;

public StretchedBoxAndWhiskerExample() {
    createCategoryBoxplot();

    frame = new JFrame();
    scrollPane = new JScrollPane(chartPanel);
    scrollPane.setPreferredSize(new Dimension(800,700));
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
}

private void createCategoryBoxplot(){   
    dataset = createCategoryDataset();
    CategoryAxis xAxis = new CategoryAxis("");
    NumberAxis yAxis = new NumberAxis("Score");

    BoxAndWhiskerRenderer renderer = new BoxAndWhiskerRenderer();
    CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
    createJFreeChart(plot,"Test");

    // Design
    renderer.setFillBox(false);
    renderer.setMeanVisible(false);

    chart.setBackgroundPaint(Color.white); 
    plot.setBackgroundPaint(Color.lightGray); 
    plot.setDomainGridlinePaint(Color.white); 
    plot.setDomainGridlinesVisible(true); 
    plot.setRangeGridlinePaint(Color.white);
    plot.getRangeAxis().setRange(-10.5, 10.5);


    chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(3250,600));
}

private DefaultBoxAndWhiskerCategoryDataset createCategoryDataset() {

    dataset = new DefaultBoxAndWhiskerCategoryDataset();
    ArrayList<Double> values = createSampleData();
    ArrayList<String> categories = createSampleCategories();
    for (int i=0;i<=5;i++){
        for (String category : categories){
            dataset.add(values,i,category);
        }
    }
    return dataset;
}


private ArrayList<String> createSampleCategories() {
    ArrayList<String> tmp = new ArrayList<String>();
    for (int i=0;i<=20;i++){
        tmp.add("Category"+i);
    }
    return tmp;
}

private ArrayList<Double> createSampleData() {
    ArrayList<Double> tmp = new ArrayList<Double>();
    for (int i=0;i<10;i++){
        tmp.add(5.0);
        tmp.add(7.0);
        tmp.add(2.0);
        tmp.add(4.0);
    }
    return tmp;
}

private void createJFreeChart(CategoryPlot plot, String title){
    chart = new JFreeChart(title, plot);
}

public static void main(String[] args) throws IOException { 
    StretchedBoxAndWhiskerExample demo = new StretchedBoxAndWhiskerExample();

}
}

阅读 489

收藏
2020-03-11

共1个答案

一尘不染

设置包含容器ChartPanel(而不是图表)的首选大小,如此处和此处所示。

附录:我认为你无法将图表添加到滚动窗格中。而是创建一个与SlidingCategoryDatasetImplements类似的类BoxAndWhiskerCategoryDataset。将滚动条添加到控制第一个显示的索引的框架。

附录:一种不太雄心勃勃的方法是使用一些适当的控件来分页数据集的一部分,如下例所示。

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
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.renderer.category.BoxAndWhiskerRenderer;
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;

/** @see https://stackoverflow.com/questions/6844759 */
public class BoxAndWhiskerDemo {

    private static final int COLS = 20;
    private static final int VISIBLE = 4;
    private static final int ROWS = 5;
    private static final int VALUES = 10;
    private static final Random rnd = new Random();
    private List<String> columns;
    private List<List<List<Double>>> data;
    private DefaultBoxAndWhiskerCategoryDataset dataset;
    private CategoryPlot plot;
    private ChartPanel chartPanel;
    private JPanel controlPanel;
    private int start = 0;

    public BoxAndWhiskerDemo() {
2020-03-11