如何设置JFreeChart.Histogram对应数据

How to set JFreeChart.Histogram to corespond to data

我正在尝试显示数据中列表长度的直方图。我决定使用 JFreeCharts 对其进行可视化,但我将数据放入工具中,创建的直方图与该数据不对应。

起初,我尝试将相应的标签设置为x轴。不幸的是,我只能设置正确的开始 (1) 和结束 (dataAray.length)。但即便如此,我觉得这不是正确的方法。我希望 JFreeChart 能够自行管理它。

private JFreeChart getHistogramChart(String name, double[] dataArray) {
        String plotTitle = name;
        String xAxisLabel = "Length of transaction";
        String yAxis = "Frequency";
        PlotOrientation orientation = PlotOrientation.VERTICAL;


        HistogramDataset histogramDataset = new HistogramDataset();
        histogramDataset.setType(HistogramType.FREQUENCY);
        histogramDataset.addSeries(name, dataArray, dataArray.length,1,dataArray.length);

        boolean show = true;
        boolean toolTips = false;
        boolean urls = false;
        JFreeChart chart = ChartFactory.createHistogram(plotTitle, xAxisLabel, yAxis,
                histogramDataset, orientation, show, toolTips, urls);

        chart.setBackgroundPaint(Color.white);

        XYPlot plot = chart.getXYPlot();
        plot.setForegroundAlpha(0.75f);
        NumberAxis axis = (NumberAxis) plot.getDomainAxis();
        axis.setAutoRangeIncludesZero(false);
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setNumberFormatOverride(NumberFormat.getIntegerInstance());
        return chart;
    }

我有两种变体的数据:

作为列表 - {2=696, 3=229, 4=123, 5=74, 6=42, 7=29, 8=8, 9=9, 10=9, 11=2, 12=3, 13=2, 15 =1, 16=5, 17=1, 19=1, 22=1, 23=1, 24=2, 27=1, 28=1, 29=1, 39=2, 48=1, 56=2 , 57=1, 63=1, 93=1, 95=1, 99=1, 146=1, 435=1}

并且作为对应的双矩阵- [0.0, 696.0, 229.0, 123.0, 74.0, 42.0, 29.0, 8.0, 9.0, 9.0, 2.0, 3.0, 2.0, 0.0, 1.0, 5.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1,0, 2. , 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0,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不是我期望的 700 结束。

最近 也有人问过同样的问题。它不是重复的,因为语言和库完全不同。但是 原因 ... 比方说,"unexpected behavior" 是相同的:人们似乎不知道直方图实际上 是什么...

所以在这里重申一下:从视觉上看,直方图是条形图。不多。不低于。

使它如此特别的是此条形图中显示的 数据。即,落在某些箱子中的元素的频率。 (这些箱通常是值的范围,或单个(离散)值,如您的情况)。虽然 JFreeChart 提供了用于创建直方图的专用方法,但这些方法适用于您拥有 "raw" 数据并且仍然需要计算分箱的情况。在您的情况下,装箱已经完成。您已经拥有的数据 频率(以及条形高度)。

所以您拥有的数据可以简单地显示为条形图。

但是您应该考虑为此寻找不同的视觉表示。类似于链接的 python 示例:bins/bars 的数量稍微超过了仍然可以合理表示的条形数量:即使是 2500 像素宽,并且 非常 小字体,标签不再可读。鉴于有 435 个值,即使没有标签,条形在标准桌面显示器上也只能有几个像素宽。

无论如何,给定数组中的大多数值都是 0 - 准确地说是 403 个值。这就是图表中显示的内容:索引 0 处的条形高度为 403,因为这是数据中值 0 的频率。如果您的用例允许,您可以考虑省略这些值。

但是,这里有一个结果示例(作为适当的条形图):

使用此示例创建:

import java.awt.Color;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class FreeChartHistogram
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }

    private static void createAndShowGui()
    {
        JFrame frame = new JFrame();

        double dataArray[] =
        { 0.0, 696.0, 229.0, 123.0, 74.0, 42.0, 29.0, 8.0, 9.0, 9.0, 2.0, 3.0,
            2.0, 0.0, 1.0, 5.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 2.0, 0.0,
            0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 2.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
            0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
        JFreeChart chart = getHistogramChart("Test", dataArray);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setMaximumDrawHeight(3000);
        chartPanel.setMaximumDrawWidth(3000);
        frame.add(chartPanel);

        frame.setSize(2500, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static JFreeChart getHistogramChart(String name, double[] dataArray)
    {
        String plotTitle = name;
        String xAxisLabel = "Length of transaction";
        String yAxis = "Frequency";
        PlotOrientation orientation = PlotOrientation.VERTICAL;

        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        for (int i = 0; i < dataArray.length; i++)
        {
            dataSet.addValue(dataArray[i], (Integer) 0, (Integer) i);
        }
        boolean show = true;
        boolean toolTips = false;
        boolean urls = false;
        JFreeChart chart = ChartFactory.createBarChart(plotTitle, xAxisLabel,
            yAxis, dataSet, orientation, show, toolTips, urls);
        chart.setBackgroundPaint(Color.WHITE);

        // Set a very small font for the labels, and rotate them...
        CategoryPlot plot = chart.getCategoryPlot();
        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setTickLabelFont(new Font("Dialog", Font.PLAIN, 8));
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_90);

        return chart;
    }
}

(旁注:当您在问题中包含 MCVE 时,获得有用答案的可能性 。想象一下尝试回答 5 个 Swing 问题一天。你很快就会厌倦总是必须将问题中的组件代码嵌入到 JFrame 中并修复随机编译问题,这些问题只是由于提问者不愿意让回答变得更容易一些...... )