JFreeCharts-制作一个图表,其中X为类别,Y为TimeInterval [英] JFreeCharts - Making a chart with X as category and Y as TimeInterval

查看:82
本文介绍了JFreeCharts-制作一个图表,其中X为类别,Y为TimeInterval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JFreeChart中查找api,这将允许我建立一个图表,其中长的X轴具有类别,而Y轴的间隔值对应于时间.

I am trying to find out apis in JFreeChart, that would allow me to build up a chart, where a long the X axis I would have categories and the Y axis interval values corresponding to times.

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

Y axis should represent time. X axis category.

这类似于Google日历方案,其中在X轴上(例如星期几).在Y轴上,您可以写下您在特定时间范围内所做的事情.

This is similar to a google calendar scenario, where in the X axis such as day of the week. And on the Y axis, you can write what you do along certain regions of time.

以下JFREE图表代表了我想代表的东西. 但是,我的Y轴将保持时间以毫秒为单位,而不是以时间单位(例如秒)为单位.

The following JFREE chart, represents what I would like to represent. However, my Y axis is holding time as miliseconds and not as a time unit (e.g. seconds).

在下图中,我说明了沿X的类别,我们可以想象它们类似于图上的边.

In the following image, i illustrate categories along the X that we can imagine they are something like an edge on a graph.

在Y轴上,例如有一些时间花在旅行上,然后在旅行后花了一些时间在说要耕种.

And on the Y axis, we have for example something time spend traveling it and then after traveling it time spent saying cultivating it.

Y轴由双精度值组成,因此最好能够使用适当的时间轴.使用类别数据集时,这似乎是个问题. 为了使图表风起云涌,必须采取许多措施,以使堆栈不被从0渲染-通过具有不可见的数据序列.

The Y axis is compised by double values, and it would be preferable to be able to use an appropriate time axis. This seems to be problem when using category data sets. Alreayd to make the chart bellow, many hacks had to be put in place, to make the stacks not being rendered from 0 - by having invisible data series.

推荐答案

基于我在评论中提到的demo7示例,现在如何使用JFreeChart解决上面发布的问题绝对不是很明显.绝对有可能使用XYBarChart作为引擎在您的问题的正确位置渲染条形,同时具有一个代表时间的轴和另一个代表您想要的东西(例如类别)的轴.

Althgout it is definitely not obvious how to use JFreeChart to solve the problem posted above, based on the demo7 sample mentinoed in my comments I can now say that it is definitely possible to use the XYBarChart as an engine to render the bars at the right places for your problem, while having an axis that represents time and another axis that represents whatever you want such as categories.

sample7示例使用一个数字轴,并分配数字轴的间隔作为渲染条形图的步骤.

The sample7 example uses a numberic axis and allocates intervals of the numeric axis as steps for rendering with of a bar.

通过这种方式,可以很容易地将问题适配到我上面的问题,并且这里有一个经过修改的应用程序,可以以适当的方式对我的问题的图表进行伪实现.

In such a way, the problem can easily be adapted to my question above, and here is the adapted application that pseudo-implements the chart on my question in a proper way.

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和category数据集,并进入XYBarChart和IntervalXYDataSets,我们可以在所需的位置渲染条.

And the image bellow illustrates how stepping away from the BarChart and category data sets, and moving into the XYBarChart and into IntervalXYDataSets, we can render bars where we want them.

请注意,如果我们垂直绘制图形,它将看起来像问题中的第一个图表.时间轴位于Y上,类别位于X上.

Note if we plot the graph vertically, it will look exactly like the first chart at the question. With time axis on the Y and and the cateogories on the X.

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

So, another satisfied user.

这篇关于JFreeCharts-制作一个图表,其中X为类别,Y为TimeInterval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆