Search code examples
jfreechart

Show newest inserted values in chart with TimeSeriesCollection when zoomed


I have such like a live chart realized with JFreeChart. The x-axis shows the time and there are several TimeSeriesCollections in the chart. If the a value is added to the TimeSeries the charts updates and shows the value only if it is not zoomed. Is there a possibility in the zoomed mode to show automatically new values (means to keep the zoom range and jump to the right).

Thanks Oli


Solution

  • I found a possible solution:

    this.chart.addChangeListener((ChartChangeEvent cce) -> {
            if (scrollToNewest) {
                Number max = DatasetUtilities.findMaximumDomainValue(this.plot.getDataset(0));
                ValueAxis va = this.plot.getDomainAxis();
                Range r = va.getRange();
                double curUpperBound = r.getUpperBound();
                double diff = max.doubleValue() - curUpperBound;
                Range newR = new Range(r.getLowerBound() + diff, r.getUpperBound() + diff);
                va.setRange(newR);
            }
        });