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

Different implementation of PR #107

parent 8504276c
No related branches found
No related tags found
No related merge requests found
......@@ -58,6 +58,7 @@ import com.xeiam.xchart.demo.charts.line.LineChart05;
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.realtime.RealtimeChart03;
import com.xeiam.xchart.demo.charts.scatter.ScatterChart01;
import com.xeiam.xchart.demo.charts.scatter.ScatterChart02;
import com.xeiam.xchart.demo.charts.scatter.ScatterChart03;
......@@ -86,6 +87,7 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
/** real-time chart example */
final RealtimeChart01 realtimeChart01 = new RealtimeChart01();
final RealtimeChart02 realtimeChart02 = new RealtimeChart02();
final RealtimeChart03 realtimeChart03 = new RealtimeChart03();
Timer timer = new Timer();
/**
......@@ -153,7 +155,7 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
public void run() {
realtimeChart01.updateData();
chartPanel.updateSeries(RealtimeChart01.SERIES_NAME, realtimeChart01.getyData());
chartPanel.updateSeries(RealtimeChart01.SERIES_NAME, null, realtimeChart01.getyData(), null);
}
};
timer = new Timer();
......@@ -167,7 +169,21 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
public void run() {
realtimeChart02.updateData();
chartPanel.updateSeries(RealtimeChart02.SERIES_NAME, realtimeChart02.getxData(), realtimeChart02.getyData());
chartPanel.updateSeries(RealtimeChart02.SERIES_NAME, realtimeChart02.getxData(), realtimeChart02.getyData(), null);
}
};
timer = new Timer();
timer.scheduleAtFixedRate(chartUpdaterTask, 0, 500);
}
else if (chartInfo.getExampleChartName().startsWith("RealtimeChart03")) {
// set up real-time chart simulated data feed
TimerTask chartUpdaterTask = new TimerTask() {
@Override
public void run() {
realtimeChart03.updateData();
chartPanel.updateSeries(RealtimeChart02.SERIES_NAME, null, realtimeChart03.getyData(), realtimeChart03.getErrorBars());
}
};
timer = new Timer();
......@@ -321,6 +337,9 @@ public class XChartDemo extends JPanel implements TreeSelectionListener {
defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("RealtimeChart02 - Updating X-Axis Window", realtimeChart02.getChart()));
category.add(defaultMutableTreeNode);
defaultMutableTreeNode = new DefaultMutableTreeNode(new ChartInfo("RealtimeChart03 - Updating With Error Bars", realtimeChart03.getChart()));
category.add(defaultMutableTreeNode);
}
/**
......
......@@ -70,7 +70,7 @@ public class RealtimeChart01 implements ExampleChart {
public void run() {
realtimeChart01.updateData();
chartPanel.updateSeries(SERIES_NAME, realtimeChart01.getyData());
chartPanel.updateSeries(SERIES_NAME, null, realtimeChart01.getyData(), null);
}
};
......
......@@ -72,7 +72,7 @@ public class RealtimeChart02 implements ExampleChart {
public void run() {
realtimeChart02.updateData();
chartPanel.updateSeries(SERIES_NAME, realtimeChart02.getxData(), realtimeChart02.getyData());
chartPanel.updateSeries(SERIES_NAME, realtimeChart02.getxData(), realtimeChart02.getyData(), null);
}
};
......
......@@ -141,4 +141,9 @@ public class RealtimeChart03 implements ExampleChart {
return yData;
}
public List<Double> getErrorBars() {
return errorBars;
}
}
......@@ -27,7 +27,7 @@ import com.xeiam.xchart.internal.style.SeriesColorMarkerLineStyle;
/**
* A Series containing X and Y data to be plotted on a Chart
*
*
* @author timmolter
*/
public class Series {
......@@ -77,7 +77,7 @@ public class Series {
/**
* Constructor
*
*
* @param name
* @param xData
* @param xAxisType
......@@ -110,7 +110,7 @@ public class Series {
/**
* Finds the min and max of a dataset
*
*
* @param data
* @return
*/
......@@ -150,7 +150,7 @@ public class Series {
/**
* Finds the min and max of a dataset accounting for error bars
*
*
* @param data
* @return
*/
......@@ -176,7 +176,7 @@ public class Series {
/**
* Set the line style of the series
*
*
* @param seriesLineStyle
*/
public Series setLineStyle(SeriesLineStyle seriesLineStyle) {
......@@ -187,7 +187,7 @@ public class Series {
/**
* Set the line style of the series
*
*
* @param basicStroke
*/
public Series setLineStyle(BasicStroke basicStroke) {
......@@ -198,7 +198,7 @@ public class Series {
/**
* Set the line color of the series
*
*
* @param seriesColor
*/
public Series setLineColor(SeriesColor seriesColor) {
......@@ -209,7 +209,7 @@ public class Series {
/**
* Set the line color of the series
*
*
* @param color
*/
public Series setLineColor(java.awt.Color color) {
......@@ -220,7 +220,7 @@ public class Series {
/**
* Sets the marker for the series
*
*
* @param seriesMarker
*/
public Series setMarker(SeriesMarker seriesMarker) {
......@@ -231,7 +231,7 @@ public class Series {
/**
* Sets the marker color for the series
*
*
* @param seriesColor
*/
public Series setMarkerColor(SeriesColor seriesColor) {
......@@ -242,7 +242,7 @@ public class Series {
/**
* Sets the marker color for the series
*
*
* @param color
*/
public Series setMarkerColor(java.awt.Color color) {
......@@ -331,20 +331,18 @@ public class Series {
return name;
}
public void replaceXData(Collection<?> newXData) {
xData = newXData;
calculateMinMax();
}
public void replaceData(Collection<?> newXData, Collection<? extends Number> newYData, Collection<? extends Number> newErrorBars) {
public void replaceYData(Collection<? extends Number> newYData) {
// Sanity check
if (newErrorBars != null && newErrorBars.size() != newYData.size()) {
throw new IllegalArgumentException("errorbars and Y-Axis sizes are not the same!!!");
}
if (newXData.size() != newYData.size()) {
throw new IllegalArgumentException("X and Y-Axis sizes are not the same!!!");
}
xData = newXData;
yData = newYData;
calculateMinMax();
}
public void replaceErrroBarData(Collection<? extends Number> newErrorBars) {
errorBars = newErrorBars;
calculateMinMax();
}
......
......@@ -275,76 +275,32 @@ public class XChartPanel extends JPanel {
}
/**
* update a series by only updating the Y-Axis data. The X-Axis data will be automatically generated as a list of increasing Integers starting from
* 1 and ending at the size of the new Y-Axis data list.
* Update a series by updating the X-Axis, Y-Axis and error bar data
*
* @param seriesName
* @param newXData - set null to be automatically generated as a list of increasing Integers starting from
* 1 and ending at the size of the new Y-Axis data list.
* @param newYData
* @param newErrorBarData - set null if there are no error bars
* @return
*/
public Series updateSeries(String seriesName, Collection<? extends Number> newYData) {
public Series updateSeries(String seriesName, Collection<?> newXData, List<? extends Number> newYData, List<? extends Number> newErrorBarData) {
Series series = chart.getSeriesMap().get(seriesName);
if (series == null) {
throw new IllegalArgumentException("Series name >" + seriesName + "< not found!!!");
}
series.replaceYData(newYData);
// generate X-Data
List<Integer> generatedXData = new ArrayList<Integer>();
for (int i = 1; i < newYData.size() + 1; i++) {
generatedXData.add(i);
}
series.replaceXData(generatedXData);
// Re-display the chart
revalidate();
repaint();
return series;
}
/**
* update a series by updating both the X-Axis and Y-Axis data
*
* @param seriesName
* @param newYData
* @return
*/
public Series updateSeries(String seriesName, Collection<?> newXData, List<? extends Number> newYData) {
Series series = chart.getSeriesMap().get(seriesName);
if (series == null) {
throw new IllegalArgumentException("Series name >" + seriesName + "< not found!!!");
if (newXData == null) {
// generate X-Data
List<Integer> generatedXData = new ArrayList<Integer>();
for (int i = 1; i <= newYData.size(); i++) {
generatedXData.add(i);
}
series.replaceData(generatedXData, newYData, newErrorBarData);
}
series.replaceXData(newXData);
series.replaceYData(newYData);
// Re-display the chart
revalidate();
repaint();
return series;
}
/**
* update a series by updating the X-Axis, Y-Axis and error bar data
*
* @param seriesName
* @param newXData
* @param newYData
* @param newErrorBarData
* @return
*/
public Series updateSeries(String seriesName, Collection<?> newXData, List<? extends Number> newYData, List<? extends Number> newErrorBarData) {
Series series = chart.getSeriesMap().get(seriesName);
if (series == null) {
throw new IllegalArgumentException("Series name >" + seriesName + "< not found!!!");
else {
series.replaceData(newXData, newYData, newErrorBarData);
}
series.replaceXData(newXData);
series.replaceYData(newYData);
series.replaceErrroBarData(newErrorBarData);
// Re-display the chart
revalidate();
......
......@@ -81,9 +81,18 @@ public class AxisPair implements ChartPart {
if (xData != null && xData.size() == 0) {
throw new IllegalArgumentException("X-Axis data cannot be empty!!!");
}
// Sanity check
if (errorBars != null && errorBars.size() != yData.size()) {
throw new IllegalArgumentException("errorbars and Y-Axis sizes are not the same!!!");
}
Series series = null;
if (xData != null) {
// Sanity check
if (xData.size() != yData.size()) {
throw new IllegalArgumentException("X and Y-Axis sizes are not the same!!!");
}
// inspect the series to see what kind of data it contains (Number, Date or String)
Iterator<?> itr = xData.iterator();
Object dataPoint = itr.next();
......@@ -123,14 +132,6 @@ public class AxisPair implements ChartPart {
series.setSeriesType(Series.SeriesType.Line);
}
// Sanity check
if (xData != null && xData.size() != yData.size()) {
throw new IllegalArgumentException("X and Y-Axis sizes are not the same!!!");
}
if (errorBars != null && errorBars.size() != yData.size()) {
throw new IllegalArgumentException("errorbars and Y-Axis sizes are not the same!!!");
}
if (seriesMap.keySet().contains(seriesName)) {
throw new IllegalArgumentException("Series name >" + seriesName + "< has already been used. Use unique names for each series!!!");
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment