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

made a simplifying change: removed need for multiple add series methods for differenty types

parent fd973abb
Branches
No related tags found
No related merge requests found
Showing
with 55 additions and 81 deletions
...@@ -75,7 +75,7 @@ public class BarChart02 implements ExampleChart { ...@@ -75,7 +75,7 @@ public class BarChart02 implements ExampleChart {
xData.add(date); xData.add(date);
yData.add(-1 * ((random.nextInt(i) + 1))); yData.add(-1 * ((random.nextInt(i) + 1)));
} }
Series series = chart.addDateSeries("Model 77", xData, yData); Series series = chart.addSeries("Model 77", xData, yData);
series.setLineColor(SeriesColor.RED); series.setLineColor(SeriesColor.RED);
chart.getStyleManager().setPlotGridLinesVisible(false); chart.getStyleManager().setPlotGridLinesVisible(false);
......
...@@ -48,8 +48,8 @@ public class BarChart05 implements ExampleChart { ...@@ -48,8 +48,8 @@ public class BarChart05 implements ExampleChart {
// Create Chart // Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Temperature vs. Color").xAxisTitle("Color").yAxisTitle("Temperature").theme(ChartTheme.GGPlot2).build(); Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Temperature vs. Color").xAxisTitle("Color").yAxisTitle("Temperature").theme(ChartTheme.GGPlot2).build();
chart.addCategorySeries("fish", new ArrayList<String>(Arrays.asList(new String[] { "Blue", "Red", "Green", "Yellow" })), new ArrayList<Number>(Arrays.asList(new Number[] { -40, 30, 20, 60 }))); chart.addSeries("fish", new ArrayList<String>(Arrays.asList(new String[] { "Blue", "Red", "Green", "Yellow" })), new ArrayList<Number>(Arrays.asList(new Number[] { -40, 30, 20, 60 })));
chart.addCategorySeries("worms", new ArrayList<String>(Arrays.asList(new String[] { "Blue", "Red", "Green", "Yellow" })), new ArrayList<Number>(Arrays.asList(new Number[] { 50, 10, -20, 40 }))); chart.addSeries("worms", new ArrayList<String>(Arrays.asList(new String[] { "Blue", "Red", "Green", "Yellow" })), new ArrayList<Number>(Arrays.asList(new Number[] { 50, 10, -20, 40 })));
return chart; return chart;
} }
......
...@@ -75,7 +75,7 @@ public class DateChart01 implements ExampleChart { ...@@ -75,7 +75,7 @@ public class DateChart01 implements ExampleChart {
yData.add(Math.random() * i); yData.add(Math.random() * i);
} }
Series series = chart.addDateSeries("blah", xData, yData); Series series = chart.addSeries("blah", xData, yData);
series.setMarker(SeriesMarker.NONE); series.setMarker(SeriesMarker.NONE);
return chart; return chart;
......
...@@ -65,7 +65,7 @@ public class DateChart02 implements ExampleChart { ...@@ -65,7 +65,7 @@ public class DateChart02 implements ExampleChart {
yData.add(Math.random() * i); yData.add(Math.random() * i);
} }
chart.addDateSeries("blah", xData, yData); chart.addSeries("blah", xData, yData);
return chart; return chart;
......
...@@ -67,7 +67,7 @@ public class DateChart03 implements ExampleChart { ...@@ -67,7 +67,7 @@ public class DateChart03 implements ExampleChart {
yData.add(Math.random() * i); yData.add(Math.random() * i);
} }
chart.addDateSeries("blah", xData, yData); chart.addSeries("blah", xData, yData);
return chart; return chart;
......
...@@ -65,7 +65,7 @@ public class DateChart04 implements ExampleChart { ...@@ -65,7 +65,7 @@ public class DateChart04 implements ExampleChart {
yData.add(Math.random() * i); yData.add(Math.random() * i);
} }
chart.addDateSeries("blah", xData, yData); chart.addSeries("blah", xData, yData);
return chart; return chart;
......
...@@ -65,7 +65,7 @@ public class DateChart05 implements ExampleChart { ...@@ -65,7 +65,7 @@ public class DateChart05 implements ExampleChart {
yData.add(Math.random() * i); yData.add(Math.random() * i);
} }
chart.addDateSeries("blah", xData, yData); chart.addSeries("blah", xData, yData);
return chart; return chart;
......
...@@ -65,7 +65,7 @@ public class DateChart06 implements ExampleChart { ...@@ -65,7 +65,7 @@ public class DateChart06 implements ExampleChart {
yData.add(Math.random() * i); yData.add(Math.random() * i);
} }
chart.addDateSeries("blah", xData, yData); chart.addSeries("blah", xData, yData);
return chart; return chart;
......
...@@ -65,7 +65,7 @@ public class DateChart07 implements ExampleChart { ...@@ -65,7 +65,7 @@ public class DateChart07 implements ExampleChart {
yData.add(Math.random() * i); yData.add(Math.random() * i);
} }
chart.addDateSeries("blah", xData, yData); chart.addSeries("blah", xData, yData);
return chart; return chart;
......
...@@ -100,7 +100,7 @@ public class LineChart03 implements ExampleChart { ...@@ -100,7 +100,7 @@ public class LineChart03 implements ExampleChart {
chart.getStyleManager().setNormalDecimalPattern("#0.000"); chart.getStyleManager().setNormalDecimalPattern("#0.000");
chart.getStyleManager().setLocale(Locale.GERMAN); chart.getStyleManager().setLocale(Locale.GERMAN);
Series series = chart.addDateSeries("Fake Data", xData, yData); Series series = chart.addSeries("Fake Data", xData, yData);
series.setLineColor(SeriesColor.BLUE); series.setLineColor(SeriesColor.BLUE);
series.setMarkerColor(Color.ORANGE); series.setMarkerColor(Color.ORANGE);
series.setMarker(SeriesMarker.CIRCLE); series.setMarker(SeriesMarker.CIRCLE);
......
...@@ -28,8 +28,11 @@ import com.xeiam.xchart.demo.charts.ExampleChart; ...@@ -28,8 +28,11 @@ import com.xeiam.xchart.demo.charts.ExampleChart;
/** /**
* Realtime * Realtime
* * <p>
* @author timmolter * Demonstrates the following:
* <ul>
* <li>real-time chart updates
* <li>fixed window
*/ */
public class RealtimeChart01 implements ExampleChart { public class RealtimeChart01 implements ExampleChart {
......
...@@ -28,8 +28,11 @@ import com.xeiam.xchart.demo.charts.ExampleChart; ...@@ -28,8 +28,11 @@ import com.xeiam.xchart.demo.charts.ExampleChart;
/** /**
* Realtime * Realtime
* * <p>
* @author timmolter * Demonstrates the following:
* <ul>
* <li>real-time chart updates
* <li>dynamic window
*/ */
public class RealtimeChart02 implements ExampleChart { public class RealtimeChart02 implements ExampleChart {
......
...@@ -15,9 +15,9 @@ ...@@ -15,9 +15,9 @@
*/ */
package com.xeiam.xchart.demo.charts.scatter; package com.xeiam.xchart.demo.charts.scatter;
import java.util.ArrayList; import java.util.HashSet;
import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.Set;
import com.xeiam.xchart.Chart; import com.xeiam.xchart.Chart;
import com.xeiam.xchart.StyleManager.ChartType; import com.xeiam.xchart.StyleManager.ChartType;
...@@ -27,8 +27,11 @@ import com.xeiam.xchart.demo.charts.ExampleChart; ...@@ -27,8 +27,11 @@ import com.xeiam.xchart.demo.charts.ExampleChart;
/** /**
* Gaussian Blob * Gaussian Blob
* * <p>
* @author timmolter * Demonstrates the following:
* <ul>
* <li>ChartType.Scatter
* <li>Series data as a Set
*/ */
public class ScatterChart01 implements ExampleChart { public class ScatterChart01 implements ExampleChart {
...@@ -42,8 +45,8 @@ public class ScatterChart01 implements ExampleChart { ...@@ -42,8 +45,8 @@ public class ScatterChart01 implements ExampleChart {
@Override @Override
public Chart getChart() { public Chart getChart() {
List<Double> xData = new ArrayList<Double>(); Set<Double> xData = new HashSet<Double>();
List<Double> yData = new ArrayList<Double>(); Set<Double> yData = new HashSet<Double>();
Random random = new Random(); Random random = new Random();
int size = 1000; int size = 1000;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
......
...@@ -103,9 +103,9 @@ public class ThemeChart03 implements ExampleChart { ...@@ -103,9 +103,9 @@ public class ThemeChart03 implements ExampleChart {
e.printStackTrace(); e.printStackTrace();
} }
Series series1 = chart.addDateSeries("downloads", xData, y1Data); Series series1 = chart.addSeries("downloads", xData, y1Data);
series1.setLineStyle(SeriesLineStyle.DOT_DOT); series1.setLineStyle(SeriesLineStyle.DOT_DOT);
chart.addDateSeries("price", xData, y2Data); chart.addSeries("price", xData, y2Data);
return chart; return chart;
} }
......
...@@ -18,7 +18,7 @@ package com.xeiam.xchart; ...@@ -18,7 +18,7 @@ package com.xeiam.xchart;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.List;
import java.util.Map; import java.util.Map;
import com.xeiam.xchart.StyleManager.ChartTheme; import com.xeiam.xchart.StyleManager.ChartTheme;
...@@ -104,60 +104,20 @@ public class Chart { ...@@ -104,60 +104,20 @@ public class Chart {
} }
/** /**
* Add a Category series to the chart * Add a series to the chart using Collections
*
* @param seriesName
* @param xData
* @param yData
* @return
*/
public Series addCategorySeries(String seriesName, Collection<String> xData, Collection<? extends Number> yData) {
return chartPainter.getAxisPair().addSeries(seriesName, xData, yData, null);
}
/**
* Add a Date series to the chart
*
* @param seriesName
* @param xData the X-Axis data
* @param yData the Y-Axis data
* @return A Series object that you can set properties on
*/
public Series addDateSeries(String seriesName, Collection<Date> xData, Collection<? extends Number> yData) {
return chartPainter.getAxisPair().addSeries(seriesName, xData, yData, null);
}
/**
* Add a Date series to the chart with error bars
*
* @param seriesName
* @param xData the X-Axis data
* @param yData the Y-Axis data
* @param errorBars the error bar data
* @return A Series object that you can set properties on
*/
public Series addDateSeries(String seriesName, Collection<Date> xData, Collection<? extends Number> yData, Collection<? extends Number> errorBars) {
return chartPainter.getAxisPair().addSeries(seriesName, xData, yData, errorBars);
}
/**
* Add a Number series to the chart using Collection<Number>
* *
* @param seriesName * @param seriesName
* @param xData the X-Axis data * @param xData the X-Axis data
* @param yData the Y-Axis data * @param yData the Y-Axis data
* @return A Series object that you can set properties on * @return A Series object that you can set properties on
*/ */
public Series addSeries(String seriesName, Collection<? extends Number> xData, Collection<? extends Number> yData) { public Series addSeries(String seriesName, Collection<?> xData, Collection<? extends Number> yData) {
return chartPainter.getAxisPair().addSeries(seriesName, xData, yData, null); return chartPainter.getAxisPair().addSeries(seriesName, xData, yData, null);
} }
/** /**
* Add a Number series to the chart using Collection<Number> with error bars * Add a Number series to the chart using Collections with error bars
* *
* @param seriesName * @param seriesName
* @param xData the X-Axis data * @param xData the X-Axis data
...@@ -165,7 +125,7 @@ public class Chart { ...@@ -165,7 +125,7 @@ public class Chart {
* @param errorBars the error bar data * @param errorBars the error bar data
* @return A Series object that you can set properties on * @return A Series object that you can set properties on
*/ */
public Series addSeries(String seriesName, Collection<? extends Number> xData, Collection<? extends Number> yData, Collection<? extends Number> errorBars) { public Series addSeries(String seriesName, Collection<?> xData, Collection<? extends Number> yData, Collection<? extends Number> errorBars) {
return chartPainter.getAxisPair().addSeries(seriesName, xData, yData, errorBars); return chartPainter.getAxisPair().addSeries(seriesName, xData, yData, errorBars);
} }
...@@ -194,20 +154,20 @@ public class Chart { ...@@ -194,20 +154,20 @@ public class Chart {
*/ */
public Series addSeries(String seriesName, double[] xData, double[] yData, double[] errorBars) { public Series addSeries(String seriesName, double[] xData, double[] yData, double[] errorBars) {
Collection<Number> xDataNumber = null; List<Double> xDataNumber = null;
if (xData != null) { if (xData != null) {
xDataNumber = new ArrayList<Number>(); xDataNumber = new ArrayList<Double>();
for (double d : xData) { for (double d : xData) {
xDataNumber.add(new Double(d)); xDataNumber.add(new Double(d));
} }
} }
Collection<Number> yDataNumber = new ArrayList<Number>(); List<Double> yDataNumber = new ArrayList<Double>();
for (double d : yData) { for (double d : yData) {
yDataNumber.add(new Double(d)); yDataNumber.add(new Double(d));
} }
Collection<Number> errorBarDataNumber = null; List<Double> errorBarDataNumber = null;
if (errorBars != null) { if (errorBars != null) {
errorBarDataNumber = new ArrayList<Number>(); errorBarDataNumber = new ArrayList<Double>();
for (double d : errorBars) { for (double d : errorBars) {
errorBarDataNumber.add(new Double(d)); errorBarDataNumber.add(new Double(d));
} }
......
...@@ -20,7 +20,6 @@ import java.awt.Color; ...@@ -20,7 +20,6 @@ import java.awt.Color;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import com.xeiam.xchart.internal.chartpart.Axis.AxisType; import com.xeiam.xchart.internal.chartpart.Axis.AxisType;
import com.xeiam.xchart.internal.markers.Marker; import com.xeiam.xchart.internal.markers.Marker;
...@@ -303,13 +302,13 @@ public class Series { ...@@ -303,13 +302,13 @@ public class Series {
return name; return name;
} }
void replaceXData(List<?> newXData) { void replaceXData(Collection<?> newXData) {
xData = newXData; xData = newXData;
calculateMinMax(); calculateMinMax();
} }
void replaceYData(List<? extends Number> newYData) { void replaceYData(Collection<? extends Number> newYData) {
yData = newYData; yData = newYData;
calculateMinMax(); calculateMinMax();
......
...@@ -27,6 +27,7 @@ import java.awt.event.MouseListener; ...@@ -27,6 +27,7 @@ import java.awt.event.MouseListener;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
...@@ -251,7 +252,7 @@ public class XChartPanel extends JPanel { ...@@ -251,7 +252,7 @@ public class XChartPanel extends JPanel {
* @param newYData * @param newYData
* @return * @return
*/ */
public Series updateSeries(String seriesName, List<? extends Number> newYData) { public Series updateSeries(String seriesName, Collection<? extends Number> newYData) {
Series series = chart.getSeriesMap().get(seriesName); Series series = chart.getSeriesMap().get(seriesName);
if (series == null) { if (series == null) {
...@@ -280,7 +281,7 @@ public class XChartPanel extends JPanel { ...@@ -280,7 +281,7 @@ public class XChartPanel extends JPanel {
* @param newYData * @param newYData
* @return * @return
*/ */
public Series updateSeries(String seriesName, List<?> newXData, List<? extends Number> newYData) { public Series updateSeries(String seriesName, Collection<?> newXData, List<? extends Number> newYData) {
Series series = chart.getSeriesMap().get(seriesName); Series series = chart.getSeriesMap().get(seriesName);
if (series == null) { if (series == null) {
......
...@@ -59,11 +59,13 @@ public class AxisPair implements ChartPart { ...@@ -59,11 +59,13 @@ public class AxisPair implements ChartPart {
} }
/** /**
* @param <T> * @param seriesName
* @param xData * @param xData
* @param yData * @param yData
* @param errorBars
* @return Series
*/ */
public <T> Series addSeries(String seriesName, Collection<T> xData, Collection<? extends Number> yData, Collection<? extends Number> errorBars) { public Series addSeries(String seriesName, Collection<?> xData, Collection<? extends Number> yData, Collection<? extends Number> errorBars) {
// Sanity checks // Sanity checks
if (seriesName == null) { if (seriesName == null) {
...@@ -81,7 +83,7 @@ public class AxisPair implements ChartPart { ...@@ -81,7 +83,7 @@ public class AxisPair implements ChartPart {
Series series = null; Series series = null;
if (xData != null) { if (xData != null) {
// Check if xAxis series contains Number or Date data // inspect the series to see what kind of data it contains (Number, Date or String)
Iterator<?> itr = xData.iterator(); Iterator<?> itr = xData.iterator();
Object dataPoint = itr.next(); Object dataPoint = itr.next();
if (dataPoint instanceof Number) { if (dataPoint instanceof Number) {
...@@ -93,6 +95,9 @@ public class AxisPair implements ChartPart { ...@@ -93,6 +95,9 @@ public class AxisPair implements ChartPart {
else if (dataPoint instanceof String) { else if (dataPoint instanceof String) {
xAxis.setAxisType(AxisType.String); xAxis.setAxisType(AxisType.String);
} }
else {
throw new RuntimeException("Series data must be either Number, Date or String type!!!");
}
yAxis.setAxisType(AxisType.Number); yAxis.setAxisType(AxisType.Number);
series = new Series(seriesName, xData, xAxis.getAxisType(), yData, yAxis.getAxisType(), errorBars, seriesColorMarkerLineStyleCycler.getNextSeriesColorMarkerLineStyle()); series = new Series(seriesName, xData, xAxis.getAxisType(), yData, yAxis.getAxisType(), errorBars, seriesColorMarkerLineStyleCycler.getNextSeriesColorMarkerLineStyle());
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment