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?

Fraser
  • 15,275
  • 8
  • 53
  • 104
Shantanu Banerjee
  • 1,417
  • 6
  • 31
  • 51
  • 3
    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. – Bill K Oct 13 '16 at 16:26

2 Answers2

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));
    }
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • 1
    Yes this code is fine but the problem is I want to show the real time instead of incremented values in time axis!!! – Shantanu Banerjee Oct 26 '12 at 04:40
  • 1
    exactly ... that's what i want – Shantanu Banerjee Oct 26 '12 at 17:01
  • how can we set Range for time here? I mean, i only want last ten minutes. Is there a way to do that? – AloneInTheDark Aug 14 '14 at 14:33
  • 5
    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. – Kamil Jul 18 '18 at 22:09
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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045