Home >Database >Mysql Tutorial >How to Visualize Time Series Data with JDBCXYDataset and JFreeChart?

How to Visualize Time Series Data with JDBCXYDataset and JFreeChart?

DDD
DDDOriginal
2024-11-02 17:00:03716browse

How to Visualize Time Series Data with JDBCXYDataset and JFreeChart?

JDBCXYDataset Represents Time Series Data

This example demonstrates how to create a JDBCXYDataset representing time series data and visualize it using JFreeChart.

Database Setup

We create an in-memory database (using H2) with a table named inventory that has three columns: when (timestamp), n1 (integer), and n2 (integer).

Code Overview

  • createDataset():

    • Initializes the database and inserts sample data.
    • Creates a JDBCXYDataset by executing a query on the inventory table. The time values are in milliseconds since the epoch, so they need to be converted to java.util.Date objects.
  • display():

    • Creates a JFrame to display the chart.
    • Creates a JFreeChart using createTimeSeriesChart() and the JDBCXYDataset.
    • Adds the chart to the frame and prints the date values for each data point.

Time Series Representation

JDBCXYDataset recognizes time series data. When querying the dataset, the returned values are instances of Number or org.jfree.date.Date. The latter represents timestamps as java.util.Date objects. This allows JFreeChart to handle the display of time values correctly in time series charts.

Output

The chart will display a plot of the n1 and n2 values over time. The x-axis will show the date and time of each data point in hourly intervals.

Notes

  • The example uses random values for illustration purposes. Real-world applications would retrieve data from a database or other source.
  • The dataset can be filtered by time range or other criteria by modifying the query used to create it.
  • JDBCXYDataset can also be used to visualize other types of data, e.g., scatter plots.

The above is the detailed content of How to Visualize Time Series Data with JDBCXYDataset and JFreeChart?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn