我对JFreeChart有疑问:是否可以将PlotOrientationBoxAndWhiskerChart更改为水平?我有一个直方图,我想在下面添加一个BoxAndWhiskerChart。我需要水平放置,以便可以使用相同的轴刻度。我试图更改PlotChartPanel中的方向。

最佳答案

@Catalina Island显示了更改PlotOrientation here的正确方法,但是您可能在下面显示的BoxAndWhiskerRendererPlotOrientation.HORIZONTAL中遇到错误。请注意下晶须上的截断线。

java - BoxAndWhiskerChart JFreeChart的图方向-LMLPHP

问题是drawHorizontalItem()中的here

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


应该是这样的:

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


java - BoxAndWhiskerChart JFreeChart的图方向-LMLPHP

经过测试的代码:

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);
    }
}

10-06 07:02