JFreeChart-具有日期轴的水平堆叠条形图 [英] JFreeChart - horizontal stacked bar chart with date axis

查看:60
本文介绍了JFreeChart-具有日期轴的水平堆叠条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组计算机,对于管理应用程序,我需要制作一张图或图表以显示每台计算机的状态. 在数据库中,存在一个列状态,其中存储了实际状态.该状态根据其他参数而改变,并且通过改变事件的时间也被存储,这意味着事件的时间戳是该状态的开始时间.

I have a set of machines and for the management application I need to make a diagram or chart to show the state of each machine. In the database there is a column state where the actual state is stored. This state changes according other parameters and by change the time of the event is also stored, that means the timestamp of the event is the start time for the state.

machine-id | event_time (long) | status (int)  

正如您在图像中看到的那样,一条水平线上(机器)有很多状态,颜色将根据数据库中的值进行设置.
时间轴以预定的步骤(此处为1/4小时)沿过去和将来的两个方向(预测)运行,因此用户应该能够以某种方式滚动它.

As you can see in the image there are many states on one horizontal line (machine), the colors will be set accourding the value in database.
The time axis goes in both directions, past and future (forecast), with predefined steps (here 1/4 hour) so the user should be able to scroll it somehow.

作为一个库,我可以使用JFreeChart,但我以前从未使用过它,而且我也不是经验丰富的Java开发人员.

As a library I am allowed to use JFreeChart, but I've never worked with it before and also I am not such an experienced Java-Developer.

我的问题:
-我需要做什么? (jfreechart的哪些类)
-如何设置以1/4小时为间隔的第一时间轴
-如何设置带有日期的第二时间轴(格式为dd:mm:yyyy)(很高兴)
-如何使时间轴可滚动(过去和将来)
-如何为每种状态设置颜色

My questions:
- What do I need to do it? (which classes of jfreechart)
- How can I set a first time axis separated in 1/4 hour steps
- How to set a second time axis with the date (formated dd:mm:yyyy) (nice to have)
- How can I make the time axis scrollable (past and future)
- How to set the color for each status

我真正需要的是方法或教程.
我在网上发现的资源和示例几乎都是彼此的副本,并且具有jfreechart lib的非常基本的用法.所以我真的不知道如何或从哪里开始...

What I really need is a how-to or a tutorial.
The sources and examples I found on the net are almost all a copy of eachother with a very basic usage of the jfreechart lib. So I don't really know how or where to start...

非常感谢您的帮助.

推荐答案

我使用XYIntervalSeries和XYPlot做过.
如果有人需要提示,则Hier是示例代码:

I did with the XYIntervalSeries and XYPlot.
Hier is the example code if someone needs a hint on that:

public class XYIntervalBarChart extends ApplicationFrame{

private static final String NODATA_SERIES   = "NODATA";
private static final String STANDBY_SERIES  = "STANDBY";
private static final String HEATING_SERIES  = "HEATING";
private static final String HOLDING_SERIES  = "HOLDING";
private static final String COOLING_SERIES  = "COOLING";
private static final String LOWERING_SERIES = "LOWERING";

ArrayList<EventStatus> testData = null;
String[] catArray;

JFreeChart chart;
DateAxis dateAxis;

Date chartStartDate;
Date chartEndDate;

public XYIntervalBarChart(String title) {
    super(title);
    // set up some test data
    initData();

    chartStartDate  = new Date(1477461600000L);
    chartEndDate = new Date(1477497600000L);
    chart = createIntervalStackedChart();
    ChartPanel chartPanel = new ChartPanel(chart);
    chartPanel.setPreferredSize(new Dimension(600, 450));
    setContentPane(chartPanel);
}

private JFreeChart createIntervalStackedChart() {
    XYIntervalSeriesCollection dataset = createXYIntervalDataset();
    XYBarRenderer xyRend = new XYBarRenderer();
    xyRend.setShadowVisible(false);
    xyRend.setUseYInterval(true);
    xyRend.setBarPainter(new StandardXYBarPainter());
    xyRend.setSeriesPaint(0, Color.BLACK);
    xyRend.setSeriesPaint(1, Color.DARK_GRAY);
    xyRend.setSeriesPaint(2, Color.RED);
    xyRend.setSeriesPaint(3, Color.YELLOW);
    xyRend.setSeriesPaint(4, Color.CYAN);
    xyRend.setSeriesPaint(5, Color.GREEN);

    dateAxis = new DateAxis();
    dateAxis.setVerticalTickLabels(true);
    dateAxis.setDateFormatOverride(new SimpleDateFormat("dd.MM.yy HH:mm"));
    XYPlot plot = new XYPlot(dataset, new SymbolAxis("", catArray), dateAxis, xyRend);
    plot.setOrientation(PlotOrientation.HORIZONTAL);
    plot.setBackgroundPaint(Color.LIGHT_GRAY);
    return new JFreeChart(plot);
}

private XYIntervalSeriesCollection createXYIntervalDataset() {
    XYIntervalSeriesCollection dataset = new XYIntervalSeriesCollection();

    int statesCount = 6;
    String[] states = new String[] {NODATA_SERIES, STANDBY_SERIES, HEATING_SERIES, HOLDING_SERIES, COOLING_SERIES, LOWERING_SERIES};

    XYIntervalSeries[] series = new XYIntervalSeries[statesCount];
    for (int i = 0; i < statesCount; i++) {
        series[i] = new XYIntervalSeries(states[i]);
        dataset.addSeries(series[i]);
    }

    for (int i = 0; i < testData.size(); i++) {
        EventStatus es = testData.get(i);
        int machNo = es.getPlanningNo();
        int state = es.getStatus();
        long eventStart = es.getTime();
        long eventEnd = 0;
        if (testData.indexOf(es) == testData.size() - 1) {
            eventEnd = chartEndDate.getTime();
        }
        else {
            EventStatus nextEs = testData.get(i + 1);
            if (nextEs.getTime() > eventStart) {
                eventEnd = nextEs.getTime();
            }
            else {
                eventEnd = chartEndDate.getTime();
            }
        }

        long duration = TimeUnit.MILLISECONDS.convert(eventEnd - eventStart, TimeUnit.MILLISECONDS);
        series[state].add(machNo, machNo - 0.2, machNo + 0.2, eventStart, eventStart, eventStart + duration);
    }

    return dataset;
}

private void initData() {
    testData = new ArrayList<EventStatus>();
    testData.add(new EventStatus("Mach-1", 1477468500000L, 1, 0)); // 26.10.16 09:55  standby
    testData.add(new EventStatus("Mach-1", 1477472100000L, 2, 0)); // 26.10.16 10:55  heating
    testData.add(new EventStatus("Mach-1", 1477474200000L, 5, 0)); // 26.10.16 11:30  lowering
    testData.add(new EventStatus("Mach-1", 1477476000000L, 3, 0)); // 26.10.16 12:00  holding
    testData.add(new EventStatus("Mach-1", 1477479600000L, 4, 0)); // 26.10.16 13:00  cooling
    testData.add(new EventStatus("Mach-1", 1477486800000L, 1, 0)); // 26.10.16 15:00  standby

    testData.add(new EventStatus("Mach-2", 1477465200000L, 3, 1)); // 26.10.16 09:00  holding
    testData.add(new EventStatus("Mach-2", 1477472400000L, 2, 1)); // 26.10.16 11:00  heating
    testData.add(new EventStatus("Mach-2", 1477474200000L, 5, 1)); // 26.10.16 11:30  lowering
    testData.add(new EventStatus("Mach-2", 1477476000000L, 2, 1)); // 26.10.16 12:00  heating
    testData.add(new EventStatus("Mach-2", 1477479600000L, 3, 1)); // 26.10.16 13:00  holding
    testData.add(new EventStatus("Mach-2", 1477486800000L, 4, 1)); // 26.10.16 15:00  cooling


    ArrayList<String> list = new ArrayList<>();
    for (EventStatus eventStatus : testData) {
        if (list.contains(eventStatus.getName()))
            continue;
        else
            list.add(eventStatus.getName());
    }

    catArray = new String[list.size()];
    catArray = list.toArray(catArray);
}

public static void main(String[] args) {
    XYIntervalBarChart demo = new XYIntervalBarChart("XYIntervalBarChart");
    demo.pack();
    RefineryUtilities.centerFrameOnScreen(demo);
    demo.setVisible(true);

}}

结果:

这篇关于JFreeChart-具有日期轴的水平堆叠条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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