JavaFX real-time LineChart with time axis
Asked Answered
P

2

12

I'm trying to plot real-time graph, with time axis, but I have found the LineChart constructor only has the signature.

LineChart(Axis<X> xAxis, Axis<Y> yAxis)  

I think embedding jfree chart in javafx is not a proper solution.

I want a few of the jfree features in a javafx LineChart, is this possible?

Pardew answered 25/10, 2012 at 8:17 Comment(1)
Fantastic question! Useful answer! Terrible Moderators! Why close perfectly good questions with valid answers? I'm just glad closed questions still come up in searches.Manifestation
P
19

Download Ensemble sample from http://www.oracle.com/technetwork/java/javafx/samples/index.html

There are several examples in it for dynamic charts, e.g. "Advanced Stock Line Chart". You can take a look at their source code directly in the application.

enter image description here

To show time on axis you can use string and DateFormatter:

    BarChart<String, Number> chart = new BarChart<>(new CategoryAxis(), new NumberAxis());

    final XYChart.Series<String, Number> series1 = new XYChart.Series<>();
    chart.getData().addAll(series1);

    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    Date date = new Date();
    for (int i = 0; i <= 10; i += 1) {
        date.setTime(date.getTime() + i * 11111);
        series1.getData().add(new XYChart.Data(dateFormat.format(date), Math.random() * 500));
    }
Publia answered 25/10, 2012 at 14:55 Comment(4)
Yes this code is fine but the problem is I want to show the real time instead of incremented values in time axis!!!Pardew
exactly ... that's what i wantPardew
how can we set Range for time here? I mean, i only want last ten minutes. Is there a way to do that?Achaemenid
The problem is, that you have to fill missing categories for time where you have no value. Otherwise you may have categories like this: 4:00, 11:00, 12:00 and they are placed in same distances from each other. JavaFx chart really sucks without time series support.Rectrix
I
0

The class org.jfree.chart.demo.TimeSeriesChartDemo1 is included with the distribution. It is pictured in the demo, and its source illustrates the use of the factory method ChartFactory.createTimeSeriesChart(). There's a related example here.

Incongruent answered 25/10, 2012 at 10:39 Comment(2)
I think you're supposed to use createTimeSeriesChart().Wotan
@ShantanuBanerjee: jfreechart works with javafx; alter the model, e.g. TimeSeries, and the view will update itself.Incongruent

© 2022 - 2024 — McMap. All rights reserved.