get series from a chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Nisbo
Posts: 10
Joined: Sun Aug 14, 2016 5:52 pm
antibot: No, of course not.

get series from a chart

Post by Nisbo » Sun Aug 14, 2016 6:07 pm

Hello from Germany

1st, I am new on Java

I create dynamically several Charts in several JInternalFrame's (on a JDesktopPane)
1 Chart per JInternalFrame

This works with a fixed time range and of course also if I put it into a SwingWorker but in this case its blinking / flashing due to the UI repaint so I decided to update each chart instead of repainting it like in this tutorial
http://www.java2s.com/Code/Java/Chart/J ... taDemo.htm

My Problem is to get the TimeSeries, I get the Names of the TimeSeries but it does not work correct

1st I check all components on my JInternalFrame to get the Chart I want to update, this works.
After this I try to get the timeseries on the chart, I get the count of the series (getSeriesCount()) and put it in a while loop.
In this loop I am able to get the Name of the timeseries

Code: Select all

String timeSeriesName = jFreeChart.getXYPlot().getDataset().getSeriesKey(i).toString();
and after this my problem begins at this line

Code: Select all

 ((TimeSeries) jFreeChart.getXYPlot().getDataset().getSeriesKey(i)).addOrUpdate(new Second(myDate), result.getInt(6));

Code: Select all

for(Component chartPanel : ((JInternalFrame) internalFrameToUse).getContentPane().getComponents()){
    // get the ChartPanel
    if(chartPanel instanceof ChartPanel){
        // get the Chart
        JFreeChart jFreeChart = ((ChartPanel) chartPanel).getChart();
       
        int i = 0;
        while(i < jFreeChart.getXYPlot().getDataset().getSeriesCount()){
            // get the TimeSeries(Collection)
            String timeSeriesName = jFreeChart.getXYPlot().getDataset().getSeriesKey(i).toString();
           
            // check if the READING is equal the name of the TimeSerie --> this work
            System.out.println("READING: " + result.getString("READING") + " - Name of the TimeSerie: " + timeSeriesName);
           
            if(result.getString("READING").equals(timeSeriesName)){
                System.out.println("Add or Update the value");
                // This will stop my SwingWorker, done() is called
                ((TimeSeries) jFreeChart.getXYPlot().getDataset().getSeriesKey(i)).addOrUpdate(new Second(myDate), result.getInt(6));
               
                System.out.println("Das wird nicht mehr angezeigt");
               
               
                if (result.getString("DEVICE").equals("Arbeitszimmer.Steckdose.PC")){
                    System.out.println(result.getString("TIMESTAMP") + " - READING: " + result.getString("READING") + " Value: " + result.getInt(6) +
                            " - Count: " + ((TimeSeries) jFreeChart.getXYPlot().getDataset().getSeriesKey(i)).getItemCount());
                }
            }
           
            i++;
        }
    }
}
Any Idea ?

EDIT:

Maybe helpful, snippet how I create the chart the first time (code is cut to keep only the imported lines)

Code: Select all

ArrayList<TimeSeries> series = new ArrayList<>();
...
series.add(new TimeSeries(sss.trim()));
....
TimeSeriesCollection dataset = new TimeSeriesCollection();
for(TimeSeries s : series){
       dataset.addSeries(s);
}
final JFreeChart chart = ChartFactory.createTimeSeriesChart("" + value, "Time", "Value", dataset, true, true, false);
			
			final XYPlot plot = chart.getXYPlot();
	        ValueAxis    axis = plot.getDomainAxis();
	        axis.setAutoRange(true);
	        axis.setFixedAutoRange(360000.0);  // 60000 = 60 seconds
	        //axis = plot.getRangeAxis();
	        //axis.setRange(0.0, 200.0); 
			
			ChartPanel chartPanel = new ChartPanel(chart);
			           chartPanel.setPreferredSize(new Dimension(800, 550));
					   chartPanel.setEnforceFileExtensions(false);
					   chartPanel.setBounds(0, 0, 800, 550);
					   chartPanel.setVisible(true);
					   chartPanel.setMouseZoomable(true, false);
					   
			f.getContentPane().removeAll();
			f.getContentPane().add(chartPanel);

Nisbo
Posts: 10
Joined: Sun Aug 14, 2016 5:52 pm
antibot: No, of course not.

Re: get series from a chart

Post by Nisbo » Sun Aug 14, 2016 9:13 pm

I solved the problem

create a new class: AvailableTimeSeries

Code: Select all

import java.util.ArrayList;
import org.jfree.data.time.TimeSeries;

public class AvailableTimeSeries {
    private String nameOfTheSet;
    private ArrayList<TimeSeries> timeSerie = new ArrayList<TimeSeries>();
   
    public AvailableTimeSeries(String nameOfTheSet){
        this.nameOfTheSet = nameOfTheSet;
    }

    public void addTimeSerie(TimeSeries timeSerie) {
        this.timeSerie.add(timeSerie);
    }

    public ArrayList<TimeSeries> getTimeSerie() {
        return timeSerie;
    }

    public String getNameOfTheSet() {
        return nameOfTheSet;
    }

    public void setNameOfTheSet(String nameOfTheSet) {
        this.nameOfTheSet = nameOfTheSet;
    }
}
Code for your MainClass

Code: Select all

private ArrayList<AvailableTimeSeries> timeSeriesMegaArray = new ArrayList<AvailableTimeSeries>();
dont forget to clear the ArrayList before a restart of your Method

Code: Select all

this.timeSeriesMegaArray.clear();
set the Data to the object (qName is a String with the name of the Set)

Code: Select all

availableTimeSeries = new AvailableTimeSeries(qName);
TimeSeries ts1 = new TimeSeries("Serie 1");
TimeSeries ts1 = new TimeSeries("Serie 2");
TimeSeries ts1 = new TimeSeries("Serie 3");
availableTimeSeries.addTimeSerie(ts1);
availableTimeSeries.addTimeSerie(ts2);
availableTimeSeries.addTimeSerie(ts3);

this.timeSeriesMegaArray.add(availableTimeSeries);

Code to get the TimeSeries back, the qName is the Name of the Set, result.getString("READING") in my case is the name of the TimeSerie

Code: Select all

for(AvailableTimeSeries setOfTimeSeries : this.timeSeriesMegaArray){
    if(setOfTimeSeries.getNameOfTheSet().equals(qName)){
        for(TimeSeries timeSerie : setOfTimeSeries.getTimeSerie()){
            if(result.getString("READING").equals(timeSerie.getKey())){
                timeSerie.addOrUpdate(new Second(myDate), result.getInt(6));
            }
        }
    }
}

Locked