PlotOrientation of BoxAndWhiskerChart JFreeChart

丶灬走出姿态 提交于 2019-12-20 03:38:04

问题


I have a question about JFreeChart: Is it possible to change the PlotOrientation of a BoxAndWhiskerChart to horizontal? I have a histogram, and I want to add a BoxAndWhiskerChart below. I need it horizontal so I can use the same axis scale. I tried to change the orientation in the Plot and ChartPanel.


回答1:


@Catalina Island shows the correct way to change the PlotOrientation here, but you may run into a bug in the BoxAndWhiskerRenderer shown below for PlotOrientation.HORIZONTAL. Note the truncated line on the lower whisker.

The problem is here in drawHorizontalItem():

g2.draw(new Line2D.Double(xxMin, yymid - halfW, xxMin, yy + halfW));

which should be this:

g2.draw(new Line2D.Double(xxMin, yymid - halfW, xxMin, yymid + halfW));

Code as tested:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.Arrays;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.statistics.DefaultBoxAndWhiskerCategoryDataset;

/**
 * @see https://stackoverflow.com/a/38407595/230513
 */
public class BoxPlot {


    private void display() {
        JFrame f = new JFrame("BoxPlot");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DefaultBoxAndWhiskerCategoryDataset data = new DefaultBoxAndWhiskerCategoryDataset();
        data.add(Arrays.asList(30, 36, 46, 55, 65, 76, 81, 80, 71, 59, 44, 34), "Planet", "Endor");
        JFreeChart chart = ChartFactory.createBoxAndWhiskerChart(
            "Box and Whisker Chart", "Planet", "Temperature", data, false);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setOrientation(PlotOrientation.HORIZONTAL);
        f.add(new ChartPanel(chart) {

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(500, 300);
            }
        });
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

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



回答2:


You can change the PlotOrientation on the CategoryPlot like this.

CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setOrientation(PlotOrientation.HORIZONTAL);


来源:https://stackoverflow.com/questions/38394629/plotorientation-of-boxandwhiskerchart-jfreechart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!