JFreeCharts - 创建一个以X为类别,Y为TimeInterval的图表

时间:2017-05-10 08:50:14

标签: jfreechart

我试图找出JFreeChart中的api,这将允许我建立一个图表,其中X轴长,我将有类别,Y轴间隔值对应于时间。

Y轴应代表时间。 X轴类别。

这类似于谷歌日历场景,其中在X轴上,例如星期几。在Y轴上,您可以写出您在某些时间区域所做的事情。

以下JFREE图表代表了我想表达的内容。 但是,我的Y轴保持时间为毫秒而不是时间单位(例如秒)。

在下图中,我展示了沿X的类别,我们可以想象它们就像是图形上的边缘。

在Y轴上,我们有一些时间花在旅行上,然后在旅行之后花费时间去训练它。

Y轴由双值组成,并且最好能够使用适当的时间轴。使用类别数据集时,这似乎是个问题。 Alreayd要使图表咆哮,必须放置许多黑客,使堆栈不是从0渲染 - 通过隐藏数据系列。

enter image description here

1 个答案:

答案 0 :(得分:1)

Althgout绝对不明白如何使用JFreeChart来解决上面发布的问题,根据我的评论中的demo7示例,我现在可以说,绝对可以使用XYBarChart作为引擎来渲染条形图适合您的问题的地方,同时有一个代表时间的轴和另一个代表你想要的东西的轴,比如类别。

sample7示例使用数字轴并将数值轴的间隔分配为使用条形渲染的步骤。

通过这种方式,问题可以很容易地适应我上面的问题,这里是适应的应用程序,以适当的方式伪造我的问题上的图表。

import java.awt.Color;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.SymbolAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.RegularTimePeriod;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYIntervalSeries;
import org.jfree.data.xy.XYIntervalSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class XYBarChartDemo7VarianRepresentingStackOverFlowQuestion extends ApplicationFrame {

    /**
     * Constructs the demo application.
     *
     * @param title
     *            the frame title.
     */
    public XYBarChartDemo7VarianRepresentingStackOverFlowQuestion(String title) {
        super(title);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 300));
        setContentPane(chartPanel);
    }

    private static JFreeChart createChart(IntervalXYDataset dataset) {
        JFreeChart chart = ChartFactory.createXYBarChart("XYBarChartDemo7", "Date", true, "Y", dataset,
                PlotOrientation.HORIZONTAL, true, false, false);

        XYPlot plot = (XYPlot) chart.getPlot();

        // The Y axis is turned into a date axis
        plot.setRangeAxis(new DateAxis("Date"));

        // The X axis is turned is turned into a label axis
        SymbolAxis xAxis = new SymbolAxis("Series",
                new String[] { "R107 => R101", "R101 => R5", "R5 => R15", "R15 => R16" });
        xAxis.setGridBandsVisible(false);
        plot.setDomainAxis(xAxis);

        // Enables using Y interval
        XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
        renderer.setUseYInterval(true);
        plot.setRenderer(renderer);
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);

        ChartUtilities.applyCurrentTheme(chart);

        return chart;
    }

    /**
     * Creates a sample dataset.
     *
     * @return A dataset.
     */
    private static IntervalXYDataset createDataset() {
        // Time points to represnet
        // Edge: "R107 => R101"
        RegularTimePeriod d0 = new Day(12, 6, 2007);
        RegularTimePeriod d1 = new Day(13, 6, 2007);
        RegularTimePeriod d2 = new Day(14, 6, 2007);

        // "R101 => R5"
        RegularTimePeriod d3 = new Day(15, 6, 2007);
        RegularTimePeriod d4 = new Day(16, 6, 2007);
        RegularTimePeriod d5 = new Day(17, 6, 2007);

        // "R5 => R15"
        RegularTimePeriod d6 = new Day(18, 6, 2007);
        RegularTimePeriod d7 = new Day(19, 6, 2007);
        RegularTimePeriod d8 = new Day(20, 6, 2007);

        // "R15 => R16"
        RegularTimePeriod d9 = new Day(21, 6, 2007);
        RegularTimePeriod d10 = new Day(22, 6, 2007);
        RegularTimePeriod d11 = new Day(23, 6, 2007);

        /// Next edge that is not part of the path
        RegularTimePeriod d12 = new Day(24, 6, 2007);

        // Create three interval series (each series has a different color)
        XYIntervalSeriesCollection dataset = new XYIntervalSeriesCollection();
        XYIntervalSeries s1 = new XYIntervalSeries("ProductiveTime");
        XYIntervalSeries s2 = new XYIntervalSeries("WaitTime");

        // Series 1 and series2 along edge1
        addItem(s1, d0, d1, 0);
        addItem(s2, d1, d3, 0);
        // Series 1 and series2 along edge2
        addItem(s1, d3, d4, 1);
        addItem(s2, d4, d6, 1);
        // Series 1 and series2 along edge3
        addItem(s1, d6, d7, 2);
        addItem(s2, d7, d9, 2);
        // Series 1 and series2 along edge4
        addItem(s1, d9, d10, 3);
        addItem(s2, d10, d12, 3);

        // puts in the data set the data series
        dataset.addSeries(s1);
        dataset.addSeries(s2);
        return dataset;
    }

    private static void addItem(XYIntervalSeries s, RegularTimePeriod p0, RegularTimePeriod p1, int index) {
        s.add(index,
                // xLow x xHigh we see this on the Y because chart is horizontal
                index - 0.45, index + 0.45, p0.getFirstMillisecond(),
                // yLow yHigh (time interval) - we see this on the X because chart is set as horizontal
                // NOTE: - notice how miliseconds are being used to make the xy interval and not
                // actually a date object. But then on the date axis we shall have a proper DateAxis for rendering
                // these miliseconds values
                p0.getFirstMillisecond(), p1.getFirstMillisecond());
    }

    /**
     * Creates a panel for the demo.
     *
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        return new ChartPanel(createChart(createDataset()));
    }

    /**
     * Starting point for the demonstration application.
     *
     * @param args
     *            ignored.
     */
    public static void main(String[] args) {
        XYBarChartDemo7VarianRepresentingStackOverFlowQuestion demo = new XYBarChartDemo7VarianRepresentingStackOverFlowQuestion(
                "JFreeChart : XYBarChartDemo7.java");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}

下面的图片说明了如何离开BarChart和类别数据集,然后进入XYBarChart和IntervalXYDataSets,我们可以在我们想要的地方渲染条形图。

请注意,如果我们垂直绘制图形,它看起来与问题的第一个图表完全相同。 Y轴上的时间轴和X上的类别。

所以,另一个满意的用户。

enter image description here