java - JFreeChart - 带日期轴的水平堆叠条形图

标签 java jfreechart

我有一组机器,对于管理应用程序,我需要制作一个图表来显示每台机器的状态。 数据库中有一个状态列,用于存储实际状态。该状态根据其他参数而变化,并且通过变化还存储事件的时间,这意味着事件的时间戳是该状态的开始时间。

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

enter image description here

如图所示,一条水平线(机器)上有很多状态,颜色将根据数据库中的值设置。
时间轴双向移动,过去和 future (预测),具有预定义的步骤(此处为 1/4 小时),因此用户应该能够以某种方式滚动它。

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

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

我真正需要的是操作方法或教程。
我在网上找到的源代码和示例几乎都是互相复制的,其中包含jfreechart lib的非常基本的用法。所以我真的不知道如何或从哪里开始......

我将非常感谢您的帮助。

最佳答案

您必须结合一些示例:

  • 为您的数据使用 CategoryDataset(例如 DefaultCategoryDataset)。使用 ChartFactory.createStackedBarChart()PlotOrientation.HORIZONTAL 创建图表。

  • 确保 addValue() 的第一个参数是 15 分钟的倍数(以毫秒为单位)。

  • 对域使用DateAxis;调用 setDateFormatOverride() 获取您想要的格式。

    DateAxis dateAxis = new DateAxis("Time");
    dateAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm"));
    categoryplot.setRangeAxis(dateAxis);
    
  • 对于滚动,请使用 SlidingCategoryDataset 就像他们建议的 here ,或使用上下文菜单中的缩放命令。

  • 对于颜色,请使用自定义的 getItemPaint(),就像它们显示的 here 一样。 .

I did set the DateFormat to show years, and it has 1970 as a year! Why?

您的示例是使用创建新的日期值,但是 Date预计毫秒。我将 000L 添加到时间值中,将范围计算移至 createDataset(),并让轴自动调整范围。

chart

import java.awt.Color;
import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.data.Range;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class StackedTimeBarChart extends ApplicationFrame {

    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";
    private static final int STANDBY_SERIES_INDEX = 0;
    private static final int HEATING_SERIES_INDEX = 1;
    private static final int HOLDING_SERIES_INDEX = 2;
    private static final int COOLING_SERIES_INDEX = 3;
    private static final int LOWERING_SERIES_INDEX = 4;
    private static final Color STANDBY_COLOR = Color.DARK_GRAY;
    private static final Color HEATING_COLOR = Color.ORANGE;
    private static final Color HOLDING_COLOR = Color.YELLOW;
    private static final Color COOLING_COLOR = Color.CYAN;
    private static final Color LOWERING_COLOR = Color.GREEN;

    ArrayList<EventStatus> testData = null;
    CategoryPlot plot;

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

        // set the start and end date of the chart
        plot = new CategoryPlot();
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
        plot.setOrientation(PlotOrientation.HORIZONTAL);

        // create dataset
        CategoryDataset dataset = createDataset();

        // create the axis
        CategoryAxis catAxis = new CategoryAxis();
        DateAxis dateAxis = new DateAxis();
        dateAxis.setVerticalTickLabels(true);
        //dateAxis.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 1));
        dateAxis.setDateFormatOverride(new SimpleDateFormat("dd.MM.yyyy HH:mm"));

        // set up the renderer
        StackedBarRenderer rend = new StackedBarRenderer();
        //rend.setBase(chartRange.getLowerBound());
        rend.setSeriesPaint(STANDBY_SERIES_INDEX, STANDBY_COLOR);
        rend.setSeriesPaint(HEATING_SERIES_INDEX, HEATING_COLOR);
        rend.setSeriesPaint(HOLDING_SERIES_INDEX, HOLDING_COLOR);
        rend.setSeriesPaint(COOLING_SERIES_INDEX, COOLING_COLOR);
        rend.setSeriesPaint(LOWERING_SERIES_INDEX, LOWERING_COLOR);

        // set up the plot
        plot.setDataset(dataset);
        plot.setDomainAxis(catAxis);
        plot.setRangeAxis(dateAxis);
        plot.setRenderer(rend);

        // create the chart and add it
        JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new Dimension(600, 450));
        setContentPane(chartPanel);
    }

    private CategoryDataset createDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        Date chartStartDate = new Date(testData.get(0).getTime());
        Date chartEndDate = new Date(testData.get(testData.size() - 1).getTime());
        Range chartRange = new Range(chartStartDate.getTime(), chartEndDate.getTime());
        if (testData != null) {
            for (int i = 0; i < testData.size(); i++) {
                // is there data?
                if (testData.size() > 0) {
                    for (int j = 0; j < testData.size(); j++) {
                        EventStatus es = testData.get(j);
                        long eventTime = es.getTime();
                        int status = es.getStatus();
                        String name = es.getName();

                        // if data event time is in the range of the chart then show it
                        // THIS DOES NOT WORK PROPERLY!!!!
                        if (eventTime >= chartStartDate.getTime() && eventTime < chartEndDate.getTime()) {
                            // create series and categories
                            if (es.getStatus() == STANDBY_SERIES_INDEX) {
                                dataset.addValue(new Double(es.getTime()), STANDBY_SERIES, es.getName());
                            } else if (es.getStatus() == HEATING_SERIES_INDEX) {
                                dataset.addValue(new Double(es.getTime()), HEATING_SERIES, es.getName());
                            } else if (es.getStatus() == HOLDING_SERIES_INDEX) {
                                dataset.addValue(new Double(es.getTime()), HOLDING_SERIES, es.getName());
                            } else if (es.getStatus() == COOLING_SERIES_INDEX) {
                                dataset.addValue(new Double(es.getTime()), COOLING_SERIES, es.getName());
                            } else if (es.getStatus() == LOWERING_SERIES_INDEX) {
                                dataset.addValue(new Double(es.getTime()), LOWERING_SERIES, es.getName());
                            } else {
                                dataset.addValue(chartRange.getUpperBound() - chartRange.getLowerBound(), STANDBY_SERIES, es.getName());
                            }
                        } else {
                            continue;
                        }
                    }
                }
            }
        } else {
            plot.setNoDataMessage("NO DATA AVAILABLE");
        }

        return dataset;
    }

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

    }

    private void initData() {
        testData = new ArrayList<EventStatus>();
        testData.add(new EventStatus("Mach-1", 1476950160000L, 1));
        testData.add(new EventStatus("Mach-1", 1476952200000L, 2));
        testData.add(new EventStatus("Mach-1", 1476964800000L, 4));
        testData.add(new EventStatus("Mach-1", 1476966600000L, 3));
        testData.add(new EventStatus("Mach-2", 1476943200000L, 1));
        testData.add(new EventStatus("Mach-2", 1476946800000L, 4));
        testData.add(new EventStatus("Mach-2", 1476954000000L, 2));
        testData.add(new EventStatus("Mach-2", 1476955800000L, 1));
        testData.add(new EventStatus("Mach-2", 1476973800000L, 3));
        testData.add(new EventStatus("Mach-3", 1476959400000L, 2));
        testData.add(new EventStatus("Mach-3", 1476966600000L, 1));
        testData.add(new EventStatus("Mach-3", 1476970200000L, 4));
        testData.add(new EventStatus("Mach-3", 1476972000000L, 1));
        testData.add(new EventStatus("Mach-3", 1476986400000L, 2));
    }

// Chart object class that hold category, event time and status
    private class EventStatus {

        private String name;
        private long time;
        private int status;

        public EventStatus(String name, long time, int status) {
            this.name = name;
            this.time = time;
            this.status = status;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public long getTime() {
            return time;
        }

        public void setTime(long time) {
            this.time = time;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }
    }
}

关于java - JFreeChart - 带日期轴的水平堆叠条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40105094/

相关文章:

java - 在 ArrayList 中查找最大数

java - 如何使用 Java Spark 结构化流从 Kafka 主题正确消费

java - 如何使用jfreechart计算折线图中添加的系列数?

java - 如何在 ChartPanel 顶部绘制图形?

java - 在 OS X 上运行 Java 6 有哪些选择?

java - 使用 TouchImageView.java 类查找 TouchImageView 的中心点

java - Java 指针写入是原子的吗?

java - 无法更改图表上线条的颜色 : JFreeChart

java - JFreeChart 正在绘制数据但未绘制线性回归

java - 使用 JFreeChart 创建图表所需的权限