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();
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++;
}
}
}
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);