Home  >  Article  >  Database  >  How to Populate a JFreechart TimeSeriesCollection with Time Series Data from a MySQL Database?

How to Populate a JFreechart TimeSeriesCollection with Time Series Data from a MySQL Database?

Barbara Streisand
Barbara StreisandOriginal
2024-10-30 22:58:30170browse

How to Populate a JFreechart TimeSeriesCollection with Time Series Data from a MySQL Database?

Populating JFreechart TimeSeriesCollection from MySQL DB

This question aims to display the temperature variation over days in a month using a JFreechart TimeSeriesCollection. However, the original implementation faced challenges with reading data accurately from the database.

Accurate Data Reading for Time Series Data

To resolve the data reading issue, it's important to consider precision in the conversion between String and Date. In the provided example, strings are directly converted to Hour.

JDBCXYDataset

To handle time series data efficiently, the JDBCXYDataset should be utilized. This dataset is designed specifically for querying and displaying time series data. Implementing JDBCXYDataset ensures that:

  • Date values are recognized and displayed correctly.
  • Data is retrieved from the database in a format compatible with time series charts.
  • The correct time format is used (Date and Timestamp in Java).

Example Implementation

Consider the following code snippet that leverages JDBCXYDataset to resolve the issue:

<code class="java">Connection conn = DriverManager.getConnection(...);
JDBCXYDataset jds = new JDBCXYDataset(conn);
jds.executeQuery("SELECT `data_registo`, `hora_registo`, `temperatura` FROM `registos` WHERE `idSensor` = 'BrgTH001'");</code>

This query retrieves data from the registos table, where data_registro and hora_registro are concatenated to form the timestamp and temperatura is the value. The jds object can then be used to create a time series chart using JFreechart's createTimeSeriesChart method.

The above is the detailed content of How to Populate a JFreechart TimeSeriesCollection with Time Series Data from a MySQL Database?. 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