Skip to content
Snippets Groups Projects
Commit 04637f30 authored by Tim Molter's avatar Tim Molter
Browse files

added real-time samples to XChartDemo

parent 11e04483
Branches
No related tags found
No related merge requests found
...@@ -17,6 +17,8 @@ package com.xeiam.xchart.demo; ...@@ -17,6 +17,8 @@ package com.xeiam.xchart.demo;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.GridLayout; import java.awt.GridLayout;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
...@@ -49,6 +51,8 @@ import com.xeiam.xchart.demo.charts.line.LineChart03; ...@@ -49,6 +51,8 @@ import com.xeiam.xchart.demo.charts.line.LineChart03;
import com.xeiam.xchart.demo.charts.line.LineChart04; import com.xeiam.xchart.demo.charts.line.LineChart04;
import com.xeiam.xchart.demo.charts.line.LineChart05; import com.xeiam.xchart.demo.charts.line.LineChart05;
import com.xeiam.xchart.demo.charts.line.LineChart06; import com.xeiam.xchart.demo.charts.line.LineChart06;
import com.xeiam.xchart.demo.charts.realtime.RealtimeChart01;
import com.xeiam.xchart.demo.charts.realtime.RealtimeChart02;
import com.xeiam.xchart.demo.charts.scatter.ScatterChart01; import com.xeiam.xchart.demo.charts.scatter.ScatterChart01;
import com.xeiam.xchart.demo.charts.scatter.ScatterChart02; import com.xeiam.xchart.demo.charts.scatter.ScatterChart02;
import com.xeiam.xchart.demo.charts.scatter.ScatterChart03; import com.xeiam.xchart.demo.charts.scatter.ScatterChart03;
...@@ -71,7 +75,12 @@ public class XChartDemo extends JPanel implements TreeSelectionListener { ...@@ -71,7 +75,12 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
private JTree tree; private JTree tree;
/** The panel for chart */ /** The panel for chart */
private JPanel chartPanel; private XChartPanel chartPanel;
/** real-time chart example */
final RealtimeChart01 realtimeChart01 = new RealtimeChart01();;
final RealtimeChart02 realtimeChart02 = new RealtimeChart02();;
Timer timer = new Timer();
/** /**
* Constructor * Constructor
...@@ -102,12 +111,13 @@ public class XChartDemo extends JPanel implements TreeSelectionListener { ...@@ -102,12 +111,13 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
splitPane.setTopComponent(treeView); splitPane.setTopComponent(treeView);
splitPane.setBottomComponent(chartPanel); splitPane.setBottomComponent(chartPanel);
Dimension minimumSize = new Dimension(100, 125); Dimension minimumSize = new Dimension(130, 160);
treeView.setMinimumSize(minimumSize); treeView.setMinimumSize(minimumSize);
splitPane.setPreferredSize(new Dimension(700, 700)); splitPane.setPreferredSize(new Dimension(700, 700));
// Add the split pane to this panel. // Add the split pane to this panel.
add(splitPane); add(splitPane);
} }
@Override @Override
...@@ -126,6 +136,37 @@ public class XChartDemo extends JPanel implements TreeSelectionListener { ...@@ -126,6 +136,37 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
// displayURL(chartInfo.bookURL); // displayURL(chartInfo.bookURL);
chartPanel = new XChartPanel(chartInfo.getExampleChart()); chartPanel = new XChartPanel(chartInfo.getExampleChart());
splitPane.setBottomComponent(chartPanel); splitPane.setBottomComponent(chartPanel);
// start running a simulated data feed for the sample real-time plot
timer.cancel(); // just in case
if (chartInfo.getExampleChartName().startsWith("RealtimeChart01")) {
// set up real-time chart simulated data feed
TimerTask chartUpdaterTask = new TimerTask() {
@Override
public void run() {
realtimeChart01.updateData();
chartPanel.updateSeries(RealtimeChart01.SERIES_NAME, realtimeChart01.getyData());
}
};
timer = new Timer();
timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);
}
else if (chartInfo.getExampleChartName().startsWith("RealtimeChart02")) {
// set up real-time chart simulated data feed
TimerTask chartUpdaterTask = new TimerTask() {
@Override
public void run() {
realtimeChart02.updateData();
chartPanel.updateSeries(RealtimeChart02.SERIES_NAME, realtimeChart02.getxData(), realtimeChart02.getyData());
}
};
timer = new Timer();
timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);
}
} }
} }
...@@ -139,112 +180,122 @@ public class XChartDemo extends JPanel implements TreeSelectionListener { ...@@ -139,112 +180,122 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
// categories // categories
DefaultMutableTreeNode category = null; DefaultMutableTreeNode category = null;
// leaves // leaves
DefaultMutableTreeNode chart = null; DefaultMutableTreeNode defaultMutableTreeNode = null;
// Area category // Area category
category = new DefaultMutableTreeNode("Area Charts"); category = new DefaultMutableTreeNode("Area Charts");
top.add(category); top.add(category);
chart = new DefaultMutableTreeNode(new ChartInfo("AreaChart01 - 3-Series", new AreaChart01().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("AreaChart01 - 3-Series", new AreaChart01().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("AreaChart02 - Null Y-Axis Data Points", new AreaChart02().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("AreaChart02 - Null Y-Axis Data Points", new AreaChart02().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
// First category // First category
category = new DefaultMutableTreeNode("Line Charts"); category = new DefaultMutableTreeNode("Line Charts");
top.add(category); top.add(category);
chart = new DefaultMutableTreeNode(new ChartInfo("LineChart01 - Logarithmic Y-Axis", new LineChart01().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("LineChart01 - Logarithmic Y-Axis", new LineChart01().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("LineChart02 - Customized Series Style", new LineChart02().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("LineChart02 - Customized Series Style", new LineChart02().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("LineChart03 - Extensive Chart Customization", new LineChart03().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("LineChart03 - Extensive Chart Customization", new LineChart03().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("LineChart04 - Hundreds of Series on One Plot", new LineChart04().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("LineChart04 - Hundreds of Series on One Plot", new LineChart04().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("LineChart05 - Scatter and Line", new LineChart05().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("LineChart05 - Scatter and Line", new LineChart05().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("LineChart06 - Logarithmic Y-Axis with Error Bars", new LineChart06().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("LineChart06 - Logarithmic Y-Axis with Error Bars", new LineChart06().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
// Scatter category // Scatter category
category = new DefaultMutableTreeNode("Scatter Charts"); category = new DefaultMutableTreeNode("Scatter Charts");
top.add(category); top.add(category);
chart = new DefaultMutableTreeNode(new ChartInfo("ScatterChart01 - Gaussian Blob", new ScatterChart01().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("ScatterChart01 - Gaussian Blob", new ScatterChart01().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("ScatterChart02 - Logarithmic Data", new ScatterChart02().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("ScatterChart02 - Logarithmic Data", new ScatterChart02().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("ScatterChart03 - Single point", new ScatterChart03().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("ScatterChart03 - Single point", new ScatterChart03().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("ScatterChart04 - Error Bars", new ScatterChart04().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("ScatterChart04 - Error Bars", new ScatterChart04().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
// Bar category // Bar category
category = new DefaultMutableTreeNode("Bar Charts"); category = new DefaultMutableTreeNode("Bar Charts");
top.add(category); top.add(category);
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart01 - Basic Bar Chart", new BarChart01().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("BarChart01 - Basic Bar Chart", new BarChart01().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart02 - Date Categories", new BarChart02().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("BarChart02 - Date Categories", new BarChart02().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart03 - Positive and Negative", new BarChart03().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("BarChart03 - Positive and Negative", new BarChart03().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart04 - Missing Point in Series", new BarChart04().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("BarChart04 - Missing Point in Series", new BarChart04().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("BarChart05 - GGPlot2 Theme", new BarChart05().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("BarChart05 - GGPlot2 Theme", new BarChart05().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
// Theme category // Theme category
category = new DefaultMutableTreeNode("Chart Themes"); category = new DefaultMutableTreeNode("Chart Themes");
top.add(category); top.add(category);
chart = new DefaultMutableTreeNode(new ChartInfo("ThemeChart01 - Default XChart Theme", new ThemeChart01().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("ThemeChart01 - Default XChart Theme", new ThemeChart01().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("ThemeChart02 - GGPlot2 Theme", new ThemeChart02().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("ThemeChart02 - GGPlot2 Theme", new ThemeChart02().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("ThemeChart03 - Matlab Theme", new ThemeChart03().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("ThemeChart03 - Matlab Theme", new ThemeChart03().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
// Date category // Date category
category = new DefaultMutableTreeNode("Date Charts"); category = new DefaultMutableTreeNode("Date Charts");
top.add(category); top.add(category);
chart = new DefaultMutableTreeNode(new ChartInfo("DateChart01 - Millisecond Scale", new DateChart01().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("DateChart01 - Millisecond Scale", new DateChart01().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("DateChart02 - Second Scale", new DateChart02().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("DateChart02 - Second Scale", new DateChart02().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("DateChart03 - Minute Scale", new DateChart03().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("DateChart03 - Minute Scale", new DateChart03().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("DateChart04 - Hour Scale", new DateChart04().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("DateChart04 - Hour Scale", new DateChart04().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("DateChart05 - Day Scale", new DateChart05().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("DateChart05 - Day Scale", new DateChart05().getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("DateChart06 - Month Scale", new DateChart06().getChart()));
category.add(defaultMutableTreeNode);
defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("DateChart07 - Year Scale", new DateChart07().getChart()));
category.add(defaultMutableTreeNode);
// Real-time category
category = new DefaultMutableTreeNode("Real-time Charts");
top.add(category);
chart = new DefaultMutableTreeNode(new ChartInfo("DateChart06 - Month Scale", new DateChart06().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("RealtimeChart01 - Fixed X-Axis Window", realtimeChart01.getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
chart = new DefaultMutableTreeNode(new ChartInfo("DateChart07 - Year Scale", new DateChart07().getChart())); defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("RealtimeChart02 - Updating X-Axis Window", realtimeChart02.getChart()));
category.add(chart); category.add(defaultMutableTreeNode);
} }
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.xeiam.xchart.standalone; package com.xeiam.xchart.demo.charts.realtime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -21,26 +21,26 @@ import java.util.Timer; ...@@ -21,26 +21,26 @@ import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel;
import com.xeiam.xchart.Chart; import com.xeiam.xchart.Chart;
import com.xeiam.xchart.XChartPanel; import com.xeiam.xchart.XChartPanel;
import com.xeiam.xchart.demo.charts.ExampleChart;
/** /**
* Realtime
*
* @author timmolter * @author timmolter
*/ */
public class RealtimeAttempt2 { public class RealtimeChart01 implements ExampleChart {
private Chart chart; private List<Double> yData;
private XChartPanel chartPanel; public static final String SERIES_NAME = "series1";
private static final String SERIES_NAME = "series1";
private List<Number> yData;
public static void main(String[] args) throws Exception { public static void main(String[] args) {
// Setup the panel // Setup the panel
final RealtimeAttempt2 realtimeAttempt = new RealtimeAttempt2(); final RealtimeChart01 realtimeChart01 = new RealtimeChart01();
realtimeAttempt.buildPanel(); final XChartPanel chartPanel = realtimeChart01.buildPanel();
// Schedule a job for the event-dispatching thread: // Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI. // creating and showing this application's GUI.
...@@ -52,7 +52,7 @@ public class RealtimeAttempt2 { ...@@ -52,7 +52,7 @@ public class RealtimeAttempt2 {
// Create and set up the window. // Create and set up the window.
JFrame frame = new JFrame("XChart"); JFrame frame = new JFrame("XChart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(realtimeAttempt.getChartPanel()); frame.add(chartPanel);
// Display the window. // Display the window.
frame.pack(); frame.pack();
...@@ -66,7 +66,9 @@ public class RealtimeAttempt2 { ...@@ -66,7 +66,9 @@ public class RealtimeAttempt2 {
@Override @Override
public void run() { public void run() {
realtimeAttempt.updateData(); realtimeChart01.updateData();
chartPanel.updateSeries(SERIES_NAME, realtimeChart01.getyData());
} }
}; };
...@@ -75,24 +77,40 @@ public class RealtimeAttempt2 { ...@@ -75,24 +77,40 @@ public class RealtimeAttempt2 {
} }
public void buildPanel() { public XChartPanel buildPanel() {
yData = getRandomData(5);
return new XChartPanel(getChart());
}
@Override
public Chart getChart() {
yData = getRandomData(5); yData = getRandomData(5);
// Create Chart // Create Chart
chart = new Chart(500, 400); Chart chart = new Chart(500, 400);
chart.setChartTitle("Sample Real-time Chart"); chart.setChartTitle("Sample Real-time Chart");
chart.setXAxisTitle("X"); chart.setXAxisTitle("X");
chart.setYAxisTitle("Y"); chart.setYAxisTitle("Y");
chart.addSeries(SERIES_NAME, null, yData); chart.addSeries(SERIES_NAME, null, yData);
chartPanel = new XChartPanel(chart); return chart;
}
private static List<Double> getRandomData(int numPoints) {
List<Double> data = new ArrayList<Double>();
for (int i = 0; i < numPoints; i++) {
data.add(Math.random() * 100);
}
return data;
} }
public void updateData() { public void updateData() {
// Get some new data // Get some new data
List<Number> newData = getRandomData(1); List<Double> newData = getRandomData(1);
yData.addAll(newData); yData.addAll(newData);
...@@ -101,25 +119,10 @@ public class RealtimeAttempt2 { ...@@ -101,25 +119,10 @@ public class RealtimeAttempt2 {
yData.remove(0); yData.remove(0);
} }
chartPanel.updateSeries(SERIES_NAME, yData);
} }
public Chart getChart() { public List<Double> getyData() {
return chart; return yData;
}
public JPanel getChartPanel() {
return chartPanel;
}
private static List<Number> getRandomData(int numPoints) {
List<Number> data = new ArrayList<Number>();
for (int i = 0; i < numPoints; i++) {
data.add(Math.random() * 100);
}
return data;
} }
} }
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.xeiam.xchart.standalone; package com.xeiam.xchart.demo.charts.realtime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -21,27 +21,29 @@ import java.util.Timer; ...@@ -21,27 +21,29 @@ import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel;
import com.xeiam.xchart.Chart; import com.xeiam.xchart.Chart;
import com.xeiam.xchart.XChartPanel; import com.xeiam.xchart.XChartPanel;
import com.xeiam.xchart.demo.charts.ExampleChart;
/** /**
* Realtime
*
* @author timmolter * @author timmolter
*/ */
public class RealtimeAttempt3 { public class RealtimeChart02 implements ExampleChart {
private Chart chart; private Chart chart;
private XChartPanel chartPanel; // private XChartPanel chartPanel;
private static final String SERIES_NAME = "series1"; public static final String SERIES_NAME = "series1";
private List<Integer> xData; private List<Integer> xData;
private List<Double> yData; private List<Double> yData;
public static void main(String[] args) throws Exception { public static void main(String[] args) {
// Setup the panel // Setup the panel
final RealtimeAttempt3 realtimeAttempt = new RealtimeAttempt3(); final RealtimeChart02 realtimeChart02 = new RealtimeChart02();
realtimeAttempt.buildPanel(); final XChartPanel chartPanel = realtimeChart02.buildPanel();
// Schedule a job for the event-dispatching thread: // Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI. // creating and showing this application's GUI.
...@@ -53,7 +55,7 @@ public class RealtimeAttempt3 { ...@@ -53,7 +55,7 @@ public class RealtimeAttempt3 {
// Create and set up the window. // Create and set up the window.
JFrame frame = new JFrame("XChart"); JFrame frame = new JFrame("XChart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(realtimeAttempt.getChartPanel()); frame.add(chartPanel);
// Display the window. // Display the window.
frame.pack(); frame.pack();
...@@ -67,7 +69,9 @@ public class RealtimeAttempt3 { ...@@ -67,7 +69,9 @@ public class RealtimeAttempt3 {
@Override @Override
public void run() { public void run() {
realtimeAttempt.updateData(); realtimeChart02.updateData();
chartPanel.updateSeries(SERIES_NAME, realtimeChart02.getxData(), realtimeChart02.getyData());
} }
}; };
...@@ -76,65 +80,73 @@ public class RealtimeAttempt3 { ...@@ -76,65 +80,73 @@ public class RealtimeAttempt3 {
} }
public void buildPanel() { public XChartPanel buildPanel() {
yData = getRandomData(5); yData = getRandomData(5);
xData = getMonotonicallyIncreasingData(5); xData = getMonotonicallyIncreasingData(5);
return new XChartPanel(getChart());
}
@Override
public Chart getChart() {
xData = getMonotonicallyIncreasingData(5);
yData = getRandomData(5);
// Create Chart // Create Chart
chart = new Chart(500, 400); Chart chart = new Chart(500, 400);
chart.setChartTitle("Sample Real-time Chart"); chart.setChartTitle("Sample Real-time Chart");
chart.setXAxisTitle("X"); chart.setXAxisTitle("X");
chart.setYAxisTitle("Y"); chart.setYAxisTitle("Y");
chart.addSeries(SERIES_NAME, xData, yData); chart.addSeries(SERIES_NAME, xData, yData);
chartPanel = new XChartPanel(chart); return chart;
}
private List<Double> getRandomData(int numPoints) {
List<Double> data = new ArrayList<Double>();
for (int i = 0; i < numPoints; i++) {
data.add(Math.random() * 100);
}
return data;
}
private List<Integer> getMonotonicallyIncreasingData(int numPoints) {
List<Integer> data = new ArrayList<Integer>();
for (int i = 0; i < numPoints; i++) {
data.add(i);
}
return data;
} }
public void updateData() { public void updateData() {
// Get some new data // Get some new data
List<Double> newYData = getRandomData(1); List<Double> newData = getRandomData(1);
yData.addAll(newYData);
xData.add(xData.get(xData.size() - 1) + 1); yData.addAll(newData);
// Limit the total number of points // Limit the total number of points
while (yData.size() > 20) { while (yData.size() > 20) {
yData.remove(0); yData.remove(0);
} }
xData.add(xData.get(xData.size() - 1) + 1);
while (xData.size() > 20) { while (xData.size() > 20) {
xData.remove(0); xData.remove(0);
} }
chartPanel.updateSeries(SERIES_NAME, xData, yData);
} }
public Chart getChart() { public List<Double> getyData() {
return chart; return yData;
} }
public JPanel getChartPanel() { public List<Integer> getxData() {
return chartPanel; return xData;
}
private List<Double> getRandomData(int numPoints) {
List<Double> data = new ArrayList<Double>();
for (int i = 0; i < numPoints; i++) {
data.add(Math.random() * 100);
}
return data;
}
private List<Integer> getMonotonicallyIncreasingData(int numPoints) {
List<Integer> data = new ArrayList<Integer>();
for (int i = 0; i < numPoints; i++) {
data.add(i);
}
return data;
} }
} }
/**
* Copyright 2011 - 2014 Xeiam LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.xeiam.xchart.standalone;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.Series;
import com.xeiam.xchart.SeriesMarker;
import com.xeiam.xchart.XChartPanel;
/**
* @author rossjourdain
*/
public class RealtimeAttempt1 {
private Chart chart;
private JPanel chartPanel;
public static void main(String[] args) throws Exception {
// Setup the panel
final RealtimeAttempt1 realtimeAttempt = new RealtimeAttempt1();
realtimeAttempt.buildPanel();
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// Create and set up the window.
JFrame frame = new JFrame("XChart");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(realtimeAttempt.getChartPanel());
// Display the window.
frame.pack();
frame.setVisible(true);
}
});
// Simulate a data feed
TimerTask chartUpdaterTask = new TimerTask() {
@Override
public void run() {
realtimeAttempt.updateData();
}
};
Timer timer = new Timer();
timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);
}
public void buildPanel() {
List<Double> yData = getRandomData(5);
// Create Chart
chart = new Chart(500, 400);
chart.setChartTitle("Sample Chart");
chart.setXAxisTitle("X");
chart.setYAxisTitle("Y");
Series series = chart.addSeries("y(x)", null, yData);
series.setMarker(SeriesMarker.CIRCLE);
chartPanel = new XChartPanel(chart);
}
public void updateData() {
// Get some new data
List<Double> newData = getRandomData(1);
// Replace the existing
List<Double> replacementData = new ArrayList<Double>();
Series oldSeries = (Series) chart.getSeriesMap().values().toArray()[0];
List<Double> oldData = (List<Double>) oldSeries.getYData();
replacementData.addAll(oldData);
replacementData.addAll(newData);
// Limit the total number of points
while (replacementData.size() > 20) {
replacementData.remove(0);
}
// Swap out the data
chart.getSeriesMap().clear();
Series newSeries = chart.addSeries(oldSeries.getName(), null, replacementData);
newSeries.setLineColor(oldSeries.getStrokeColor());
newSeries.setLineStyle(oldSeries.getStroke());
newSeries.setMarkerColor(oldSeries.getMarkerColor());
// etc
// Re-display the chart
chartPanel.revalidate();
chartPanel.repaint();
}
public Chart getChart() {
return chart;
}
public JPanel getChartPanel() {
return chartPanel;
}
private static List<Double> getRandomData(int numPoints) {
List<Double> data = new ArrayList<Double>();
for (int i = 0; i < numPoints; i++) {
data.add(Math.random() * 100);
}
return data;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment