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

add cateogory series method

parent c2aa29ff
No related branches found
No related tags found
No related merge requests found
Showing
with 202 additions and 70 deletions
......@@ -16,6 +16,8 @@
*/
package org.knowm.xchart.demo.charts.bar;
import java.util.Arrays;
import org.knowm.xchart.Chart;
import org.knowm.xchart.ChartBuilder;
import org.knowm.xchart.StyleManager.ChartType;
......@@ -47,7 +49,7 @@ public class BarChart01 implements ExampleChart {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Score Histogram").xAxisTitle("Score").yAxisTitle("Number").build();
chart.addSeries("test 1", new double[] { 0, 1, 2, 3, 4 }, new double[] { 4, 5, 9, 6, 5 });
chart.addCategorySeries("test 1", Arrays.asList(new Integer[] { 0, 1, 2, 3, 4 }), Arrays.asList(new Integer[] { 4, 5, 9, 6, 5 }));
// Customize Chart
chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW);
......
......@@ -75,7 +75,7 @@ public class BarChart02 implements ExampleChart {
xData.add(date);
yData.add(-1 * 0.00000001 * ((random.nextInt(i) + 1)));
}
Series series = chart.addSeries("Model 77", xData, yData);
Series series = chart.addCategorySeries("Model 77", xData, yData);
series.setLineColor(SeriesColor.RED);
chart.getStyleManager().setPlotGridLinesVisible(false);
chart.getStyleManager().setBarFilled(false);
......
......@@ -16,6 +16,8 @@
*/
package org.knowm.xchart.demo.charts.bar;
import java.util.Arrays;
import org.knowm.xchart.Chart;
import org.knowm.xchart.ChartBuilder;
import org.knowm.xchart.StyleManager.ChartType;
......@@ -45,7 +47,7 @@ public class BarChart03 implements ExampleChart {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Score vs. Age").xAxisTitle("Age").yAxisTitle("Score").build();
chart.addSeries("males", new double[] { 10, 20, 30, 40 }, new double[] { 40, -30, -20, -60 });
chart.addCategorySeries("males", Arrays.asList(new Integer[] { 10, 20, 30, 40 }), Arrays.asList(new Integer[] { 40, -30, -20, -60 }));
return chart;
}
......
......@@ -16,6 +16,8 @@
*/
package org.knowm.xchart.demo.charts.bar;
import java.util.Arrays;
import org.knowm.xchart.Chart;
import org.knowm.xchart.ChartBuilder;
import org.knowm.xchart.StyleManager.ChartType;
......@@ -47,8 +49,8 @@ public class BarChart04 implements ExampleChart {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("XFactor vs. Age").xAxisTitle("Age").yAxisTitle("XFactor").build();
chart.addSeries("female", new double[] { 10, 20, 30, 40, 50 }, new double[] { 50, 10, 20, 40, 35 });
chart.addSeries("male", new double[] { 10, 20, 30, 40, 50 }, new double[] { 40, 30, 20, 0, 60 });
chart.addCategorySeries("female", Arrays.asList(new Integer[] { 10, 20, 30, 40, 50 }), Arrays.asList(new Integer[] { 50, 10, 20, 40, 35 }));
chart.addCategorySeries("male", Arrays.asList(new Integer[] { 10, 20, 30, 40, 50 }), Arrays.asList(new Integer[] { 40, 30, 20, 0, 60 }));
chart.getStyleManager().setYAxisMin(5);
chart.getStyleManager().setYAxisMax(70);
......
......@@ -49,10 +49,10 @@ public class BarChart05 implements ExampleChart {
// 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.addSeries("fish", new ArrayList<String>(Arrays.asList(new String[] { "Blue", "Red", "Green", "Yellow", "Yellow" })), new ArrayList<Number>(Arrays.asList(new Number[] { -40, 30, 20, 60,
60 })));
chart.addSeries("worms", new ArrayList<String>(Arrays.asList(new String[] { "Blue", "Red", "Green", "Yellow", "Yellow" })), new ArrayList<Number>(Arrays.asList(new Number[] { 50, 10, -20, 40,
60 })));
chart.addCategorySeries("fish", new ArrayList<String>(Arrays.asList(new String[] { "Blue", "Red", "Green", "Yellow", "Yellow" })), new ArrayList<Number>(Arrays.asList(new Number[] { -40, 30, 20,
60, 60 })));
chart.addCategorySeries("worms", new ArrayList<String>(Arrays.asList(new String[] { "Blue", "Red", "Green", "Yellow", "Yellow" })), new ArrayList<Number>(Arrays.asList(new Number[] { 50, 10, -20,
40, 60 })));
return chart;
}
......
......@@ -53,8 +53,8 @@ public class BarChart06 implements ExampleChart {
Histogram histogram1 = new Histogram(getGaussianData(10000), 30, -30, 30);
Histogram histogram2 = new Histogram(getGaussianData(5000), 30, -30, 30);
chart.addSeries("histogram 1", histogram1.getxAxisData(), histogram1.getyAxisData());
chart.addSeries("histogram 2", histogram2.getxAxisData(), histogram2.getyAxisData());
chart.addCategorySeries("histogram 1", histogram1.getxAxisData(), histogram1.getyAxisData());
chart.addCategorySeries("histogram 2", histogram2.getxAxisData(), histogram2.getyAxisData());
// Customize Chart
chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW);
......
......@@ -53,9 +53,9 @@ public class BarChart07 implements ExampleChart {
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Score Histogram").xAxisTitle("Mean").yAxisTitle("Count").build();
Histogram histogram1 = new Histogram(getGaussianData(1000), 10, -30, 30);
chart.addSeries("histogram 1", histogram1.getxAxisData(), histogram1.getyAxisData());
chart.addCategorySeries("histogram 1", histogram1.getxAxisData(), histogram1.getyAxisData());
Histogram histogram2 = new Histogram(getGaussianData(1000), 10, -30, 30);
chart.addSeries("histogram 2", histogram2.getxAxisData(), histogram2.getyAxisData());
chart.addCategorySeries("histogram 2", histogram2.getxAxisData(), histogram2.getyAxisData());
// Customize Chart
chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW);
......
......@@ -52,7 +52,7 @@ public class BarChart08 implements ExampleChart {
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Histogram").xAxisTitle("Mean").yAxisTitle("Count").build();
Histogram histogram1 = new Histogram(getGaussianData(10000), 10, -10, 10);
chart.addSeries("histogram", histogram1.getxAxisData(), histogram1.getyAxisData(), getFakeErrorData(histogram1.getxAxisData().size()));
chart.addCategorySeries("histogram", histogram1.getxAxisData(), histogram1.getyAxisData(), getFakeErrorData(histogram1.getxAxisData().size()));
// Customize Chart
chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW);
......
......@@ -51,9 +51,9 @@ public class BarChart09 implements ExampleChart {
// Create Chart
Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Value vs. Letter").xAxisTitle("Letter").yAxisTitle("Value").theme(ChartTheme.GGPlot2).build();
chart.addSeries("China", new ArrayList<String>(Arrays.asList(new String[] { "A", "B", "C", "D", "E" })), new ArrayList<Number>(Arrays.asList(new Number[] { 11, 23, 20, 36, 5 })));
Series series2 = chart.addSeries("World Ave.", new ArrayList<String>(Arrays.asList(new String[] { "A", "B", "C", "D", "E" })), new ArrayList<Number>(Arrays.asList(new Number[] { 13, 25, 22, 38,
7 })));
chart.addCategorySeries("China", new ArrayList<String>(Arrays.asList(new String[] { "A", "B", "C", "D", "E" })), new ArrayList<Number>(Arrays.asList(new Number[] { 11, 23, 20, 36, 5 })));
Series series2 = chart.addCategorySeries("World Ave.", new ArrayList<String>(Arrays.asList(new String[] { "A", "B", "C", "D", "E" })), new ArrayList<Number>(Arrays.asList(new Number[] { 13, 25,
22, 38, 7 })));
series2.setSeriesType(SeriesType.Line);
// Customize Chart
......
......@@ -106,7 +106,7 @@ public class Chart {
}
/**
* Add a Number series to the chart using Collections with error bars
* Add a series for a X-Y type chart using Lists with error bars
*
* @param seriesName
* @param xData the X-Axis data
......@@ -120,7 +120,7 @@ public class Chart {
}
/**
* Add a series to the chart using Collections
* Add a series for a X-Y type chart using Lists
*
* @param seriesName
* @param xData the X-Axis data
......@@ -133,7 +133,34 @@ public class Chart {
}
/**
* Add a series to the chart using double arrays
* Add a series for a Category type chart using Lists 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 addCategorySeries(String seriesName, List<?> xData, List<? extends Number> yData, List<? extends Number> errorBars) {
return chartInternal.addCategorySeries(seriesName, xData, yData, errorBars);
}
/**
* Add a series for a Category type chart using Lists
*
* @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 addCategorySeries(String seriesName, List<?> xData, List<? extends Number> yData) {
return addCategorySeries(seriesName, xData, yData, null);
}
/**
* Add a series for a X-Y type chart using using double arrays
*
* @param seriesName
* @param xData the X-Axis data
......@@ -146,7 +173,7 @@ public class Chart {
}
/**
* Add a series to the chart using double arrays with error bars
* Add a series for a X-Y type chart using using double arrays with error bars
*
* @param seriesName
* @param xData the X-Axis data
......@@ -179,7 +206,7 @@ public class Chart {
}
/**
* Add a series to the chart using int arrays
* Add a series for a X-Y type chart using using int arrays
*
* @param seriesName
* @param xData the X-Axis data
......@@ -192,7 +219,7 @@ public class Chart {
}
/**
* Add a series to the chart using int arrays with error bars
* Add a series for a X-Y type chart using using int arrays with error bars
*
* @param seriesName
* @param xData the X-Axis data
......
......@@ -24,6 +24,7 @@ import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import org.knowm.xchart.StyleManager.LegendPosition;
import org.knowm.xchart.internal.chartpart.ChartInternal.ChartInternalType;
/**
* Axis
......@@ -342,24 +343,25 @@ public class Axis implements ChartPart {
private AxisTickCalculator getAxisTickCalculator(double workingSpace) {
if (getDirection() == Direction.X && getAxisType() == AxisType.String) {
if (getDirection() == Direction.X && getChartInternal().getChartInternalType() == ChartInternalType.Category) {
return new AxisTickCategoryChartCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartInternal());
}
else if (getDirection() == Direction.X && getChartInternal().getStyleManager().isXAxisLogarithmic() && getAxisType() != AxisType.Date) {
else if (getChartInternal().getChartInternalType() == ChartInternalType.XY && getAxisType() == AxisType.Date) {
return new AxisTickLogarithmicCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartInternal().getStyleManager());
return new AxisTickDateCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartInternal().getStyleManager());
}
else if (getDirection() == Direction.Y && getChartInternal().getStyleManager().isYAxisLogarithmic() && getAxisType() != AxisType.Date) {
else if (getDirection() == Direction.X && getChartInternal().getChartInternalType() == ChartInternalType.XY && getChartInternal().getStyleManager().isXAxisLogarithmic()
&& getAxisType() != AxisType.Date) {
return new AxisTickLogarithmicCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartInternal().getStyleManager());
}
else if (getAxisType() == AxisType.Date) {
else if (getDirection() == Direction.Y && getChartInternal().getStyleManager().isYAxisLogarithmic() && getAxisType() != AxisType.Date) {
return new AxisTickDateCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartInternal().getStyleManager());
return new AxisTickLogarithmicCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartInternal().getStyleManager());
}
else { // number
......
......@@ -40,6 +40,13 @@ public class ChartInternal {
private int width;
private int height;
protected enum ChartInternalType {
XY, Category
}
private ChartInternalType chartInternalType = null;
private Map<String, Series> seriesMap = new LinkedHashMap<String, Series>();
private SeriesColorMarkerLineStyleCycler seriesColorMarkerLineStyleCycler = new SeriesColorMarkerLineStyleCycler();
......@@ -79,23 +86,13 @@ public class ChartInternal {
*/
public Series addSeries(String seriesName, List<?> xData, List<? extends Number> yData, List<? extends Number> errorBars) {
// Sanity checks
if (seriesName == null) {
throw new IllegalArgumentException("Series Name cannot be null!!!");
}
if (yData == null) {
throw new IllegalArgumentException("Y-Axis data cannot be null!!!");
}
if (yData.size() == 0) {
throw new IllegalArgumentException("Y-Axis data cannot be empty!!!");
}
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!!!");
if (chartInternalType != null && chartInternalType != ChartInternalType.XY) {
throw new IllegalArgumentException("Cannot mix x-y and category series types! Don't call addSeries() and addCategorySeries() for the same chart!");
}
chartInternalType = ChartInternalType.XY;
// Sanity checks
sanityCheck(seriesName, xData, yData, errorBars);
Series series = null;
if (xData != null) {
......@@ -104,21 +101,13 @@ public class ChartInternal {
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();
if (dataPoint instanceof Number) {
axisPair.getXAxis().setAxisType(AxisType.Number);
}
else if (dataPoint instanceof Date) {
axisPair.getXAxis().setAxisType(AxisType.Date);
}
else if (dataPoint instanceof String) {
axisPair.getXAxis().setAxisType(AxisType.String);
}
else {
throw new IllegalArgumentException("Series data must be either Number, Date or String type!!!");
// inspect the series to see what kind of data it contains (Number, Date)
AxisType axisType = setXAxisType(xData);
if (!(axisType == AxisType.Number || axisType == AxisType.Date)) {
throw new IllegalArgumentException("X-Axis data must be of type Number or Date!!!");
}
axisPair.getYAxis().setAxisType(AxisType.Number);
series = new Series(seriesName, xData, axisPair.getXAxis().getAxisType(), yData, axisPair.getYAxis().getAxisType(), errorBars, seriesColorMarkerLineStyleCycler
.getNextSeriesColorMarkerLineStyle());
......@@ -134,7 +123,117 @@ public class ChartInternal {
.getNextSeriesColorMarkerLineStyle());
}
// set series type based on chart type, but only if it's not explicitly set on the series yet.
// set series type
setSeriesType(series);
if (seriesMap.keySet().contains(seriesName)) {
throw new IllegalArgumentException("Series name >" + seriesName + "< has already been used. Use unique names for each series!!!");
}
seriesMap.put(seriesName, series);
return series;
}
/**
* @param seriesName
* @param xData
* @param yData
* @param errorBars
* @return Series
*/
public Series addCategorySeries(String seriesName, List<?> xData, List<? extends Number> yData, List<? extends Number> errorBars) {
if (chartInternalType != null && chartInternalType != ChartInternalType.Category) {
throw new IllegalArgumentException("Cannot mix x-y and category series types! Don't call addSeries() and addCategorySeries() for the same chart!");
}
chartInternalType = ChartInternalType.Category;
// Sanity checks
sanityCheck(seriesName, xData, yData, errorBars);
if (xData == null) {
throw new IllegalArgumentException("Y-Axis data cannot be null!!!");
}
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, String)
setXAxisType(xData);
axisPair.getYAxis().setAxisType(AxisType.Number);
Series series = new Series(seriesName, xData, axisPair.getXAxis().getAxisType(), yData, axisPair.getYAxis().getAxisType(), errorBars, seriesColorMarkerLineStyleCycler
.getNextSeriesColorMarkerLineStyle());
// set series type
setSeriesType(series);
if (seriesMap.keySet().contains(seriesName)) {
throw new IllegalArgumentException("Series name >" + seriesName + "< has already been used. Use unique names for each series!!!");
}
seriesMap.put(seriesName, series);
return series;
}
private void sanityCheck(String seriesName, List<?> xData, List<? extends Number> yData, List<? extends Number> errorBars) {
if (seriesName == null) {
throw new IllegalArgumentException("Series Name cannot be null!!!");
}
if (yData == null) {
throw new IllegalArgumentException("Y-Axis data cannot be null!!!");
}
if (yData.size() == 0) {
throw new IllegalArgumentException("Y-Axis data cannot be empty!!!");
}
if (xData != null && xData.size() == 0) {
throw new IllegalArgumentException("X-Axis data cannot be empty!!!");
}
if (errorBars != null && errorBars.size() != yData.size()) {
throw new IllegalArgumentException("errorbars and Y-Axis sizes are not the same!!!");
}
}
/**
* Sets the axis type based on the data contained in the xData argument
*
* @param xData
* @return
*/
private AxisType setXAxisType(List<?> xData) {
AxisType axisType;
Iterator<?> itr = xData.iterator();
Object dataPoint = itr.next();
if (dataPoint instanceof Number) {
axisType = AxisType.Number;
axisPair.getXAxis().setAxisType(axisType);
}
else if (dataPoint instanceof Date) {
axisType = AxisType.Date;
axisPair.getXAxis().setAxisType(axisType);
}
else if (dataPoint instanceof String) {
axisType = AxisType.String;
axisPair.getXAxis().setAxisType(axisType);
}
else {
throw new IllegalArgumentException("Series data must be either Number, Date or String type!!!");
}
return axisType;
}
/**
* set series type based on chart type, but only if it's not explicitly set on the series yet.
*
* @param series
*/
private void setSeriesType(Series series) {
switch (getStyleManager().getChartType()) {
case Line:
if (series.getSeriesType() == null) {
......@@ -152,6 +251,7 @@ public class ChartInternal {
}
break;
case Bar:
// TODO Not Yet Supported
if (series.getSeriesType() == null) {
series.setSeriesType(Series.SeriesType.Bar);
}
......@@ -162,14 +262,6 @@ public class ChartInternal {
}
break;
}
if (seriesMap.keySet().contains(seriesName)) {
throw new IllegalArgumentException("Series name >" + seriesName + "< has already been used. Use unique names for each series!!!");
}
seriesMap.put(seriesName, series);
return series;
}
/**
......@@ -247,7 +339,7 @@ public class ChartInternal {
*
* @return
*/
public Legend getChartLegend() {
protected Legend getChartLegend() {
return chartLegend;
}
......@@ -267,11 +359,16 @@ public class ChartInternal {
*
* @return
*/
public Plot getPlot() {
protected Plot getPlot() {
return plot;
}
public ChartInternalType getChartInternalType() {
return chartInternalType;
}
public int getWidth() {
return width;
......
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