From 504956fe8a7f91d99405b7275de3f0040df93b3e Mon Sep 17 00:00:00 2001 From: Tim Molter <tim.molter@gmail.com> Date: Wed, 6 Mar 2013 23:52:47 +0100 Subject: [PATCH] bug fix issue 27: https://github.com/timmolter/XChart/issues/27 --- .../com/xeiam/xchart/demo/XChartDemo.java | 6 +- .../xchart/demo/charts/line/LineChart04.java | 4 +- .../xchart/demo/charts/line/LineChart05.java | 83 +++++++++++++++++++ .../main/java/com/xeiam/xchart/Series.java | 3 + .../java/com/xeiam/xchart/StyleManager.java | 4 +- .../chartpart/AxisTickCalculator.java | 8 +- .../chartpart/PlotContentBarChart.java | 12 +-- .../chartpart/PlotContentLineChart.java | 26 +++--- 8 files changed, 116 insertions(+), 30 deletions(-) create mode 100644 xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart05.java diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/XChartDemo.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/XChartDemo.java index 671ac3b2..d6ba7804 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/XChartDemo.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/XChartDemo.java @@ -46,6 +46,7 @@ import com.xeiam.xchart.demo.charts.line.LineChart01; import com.xeiam.xchart.demo.charts.line.LineChart02; import com.xeiam.xchart.demo.charts.line.LineChart03; import com.xeiam.xchart.demo.charts.line.LineChart04; +import com.xeiam.xchart.demo.charts.line.LineChart05; import com.xeiam.xchart.demo.charts.scatter.ScatterChart01; import com.xeiam.xchart.demo.charts.scatter.ScatterChart02; import com.xeiam.xchart.demo.charts.scatter.ScatterChart03; @@ -157,7 +158,10 @@ public class XChartDemo extends JPanel implements TreeSelectionListener { chart = new DefaultMutableTreeNode(new ChartInfo("LineChart03 - Extensive Chart Customization", new LineChart03().getChart())); category.add(chart); - chart = new DefaultMutableTreeNode(new ChartInfo("LineChart04 - Plots Hundreds of Series on One Plot", new LineChart04().getChart())); + chart = new DefaultMutableTreeNode(new ChartInfo("LineChart04 - Hundreds of Series on One Plot", new LineChart04().getChart())); + category.add(chart); + + chart = new DefaultMutableTreeNode(new ChartInfo("LineChart05 - Scatter and Line", new LineChart05().getChart())); category.add(chart); // Scatter category diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java index 2f345c29..84528ce1 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java @@ -24,7 +24,7 @@ import com.xeiam.xchart.SwingWrapper; import com.xeiam.xchart.demo.charts.ExampleChart; /** - * Plots Hundreds of Series on One Plot + * Hundreds of Series on One Plot */ public class LineChart04 implements ExampleChart { @@ -48,7 +48,7 @@ public class LineChart04 implements ExampleChart { chart.getStyleManager().setLegendVisible(false); for (int i = 0; i < 200; i++) { - Series series = chart.addSeries("", new double[] { Math.random(), Math.random() }, new double[] { Math.random(), Math.random() }); + Series series = chart.addSeries("A", new double[] { Math.random(), Math.random() }, new double[] { Math.random(), Math.random() }); series.setLineColor(SeriesColor.BLUE); series.setLineStyle(SeriesLineStyle.SOLID); series.setMarker(SeriesMarker.CIRCLE); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart05.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart05.java new file mode 100644 index 00000000..d93a1829 --- /dev/null +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart05.java @@ -0,0 +1,83 @@ +/** + * Copyright 2011-2013 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.demo.charts.line; + +import java.awt.Color; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.Series; +import com.xeiam.xchart.SeriesLineStyle; +import com.xeiam.xchart.SeriesMarker; +import com.xeiam.xchart.StyleManager.LegendPosition; +import com.xeiam.xchart.SwingWrapper; +import com.xeiam.xchart.demo.charts.ExampleChart; + +/** + * Scatter and Line + * <p> + * Demonstrates the following: + * <ul> + * <li>Customizing the series style properties + * <li>Scatter and Line overlay + * <li>Logarithmic Y-Axis + */ +public class LineChart05 implements ExampleChart { + + public static void main(String[] args) { + + ExampleChart exampleChart = new LineChart05(); + Chart chart = exampleChart.getChart(); + new SwingWrapper(chart).displayChart(); + } + + @Override + public Chart getChart() { + + // Create Chart + Chart chart = new Chart(800, 600); + + // Customize Chart + chart.setChartTitle("LineChart05"); + chart.setXAxisTitle("X"); + chart.setYAxisTitle("Y"); + chart.getStyleManager().setLegendPosition(LegendPosition.InsideSW); + + double[] xData = new double[] { 0.0, 1.0, 2.0, 3.0, 4.0, 5, 6 }; + double[] yData = new double[] { 106, 44, 26, 10, 7.5, 3.4, .88 }; + double[] yData2 = new double[] { 102, 49, 23.6, 11.3, 5.4, 2.6, 1.25 }; + + Series series = chart.addSeries("A", xData, yData); + series.setLineStyle(SeriesLineStyle.NONE); + series.setMarker(SeriesMarker.DIAMOND); + series.setMarkerColor(Color.BLACK); + + Series series2 = chart.addSeries("B", xData, yData2); + series2.setMarker(SeriesMarker.NONE); + series2.setLineStyle(SeriesLineStyle.DASH_DASH); + series2.setLineColor(Color.BLACK); + + chart.getStyleManager().setYAxisLogarithmic(true); + + chart.getStyleManager().setYAxisMin(0.01); + chart.getStyleManager().setYAxisMax(1000); + + chart.getStyleManager().setXAxisMin(-2); + chart.getStyleManager().setXAxisMax(8); + + return chart; + } + +} diff --git a/xchart/src/main/java/com/xeiam/xchart/Series.java b/xchart/src/main/java/com/xeiam/xchart/Series.java index 2e0a55ca..94b9fe9d 100644 --- a/xchart/src/main/java/com/xeiam/xchart/Series.java +++ b/xchart/src/main/java/com/xeiam/xchart/Series.java @@ -77,6 +77,9 @@ public class Series { */ public Series(String name, Collection<?> xData, AxisType xAxisType, Collection<Number> yData, AxisType yAxisType, Collection<Number> errorBars, SeriesColorMarkerLineStyle seriesColorMarkerLineStyle) { + if (name == null || name.length() < 1) { + throw new IllegalArgumentException("Series name cannot be null or zero-length!!!"); + } this.name = name; this.xData = xData; this.yData = yData; diff --git a/xchart/src/main/java/com/xeiam/xchart/StyleManager.java b/xchart/src/main/java/com/xeiam/xchart/StyleManager.java index d712b24f..11f1df71 100644 --- a/xchart/src/main/java/com/xeiam/xchart/StyleManager.java +++ b/xchart/src/main/java/com/xeiam/xchart/StyleManager.java @@ -738,7 +738,7 @@ public class StyleManager { this.xAxisMin = xAxisMin; } - public Double getxAxisMin() { + public Double getXAxisMin() { return xAxisMin; } @@ -748,7 +748,7 @@ public class StyleManager { this.xAxisMax = xAxisMax; } - public Double getxAxisMax() { + public Double getXAxisMax() { return xAxisMax; } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickCalculator.java index d514f35b..c0f128c2 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickCalculator.java @@ -79,14 +79,14 @@ public abstract class AxisTickCalculator { } } // override min and maxValue if specified - if (axisDirection == Direction.X && styleManager.getxAxisMin() != null) { - overrideMinValue = new BigDecimal(styleManager.getxAxisMin()); + if (axisDirection == Direction.X && styleManager.getXAxisMin() != null) { + overrideMinValue = new BigDecimal(styleManager.getXAxisMin()); } if (axisDirection == Direction.Y && styleManager.getYAxisMin() != null) { overrideMinValue = new BigDecimal(styleManager.getYAxisMin()); } - if (axisDirection == Direction.X && styleManager.getxAxisMax() != null) { - overrideMaxValue = new BigDecimal(styleManager.getxAxisMax()); + if (axisDirection == Direction.X && styleManager.getXAxisMax() != null) { + overrideMaxValue = new BigDecimal(styleManager.getXAxisMax()); } if (axisDirection == Direction.Y && styleManager.getYAxisMax() != null) { overrideMaxValue = new BigDecimal(styleManager.getYAxisMax()); diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java index 853131a3..713b3963 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java @@ -101,17 +101,11 @@ public class PlotContentBarChart extends PlotContent { // figure out the general form of the chart int chartForm = 1; // 1=positive, -1=negative, 0=span if (yMin.compareTo(BigDecimal.ZERO) > 0 && yMax.compareTo(BigDecimal.ZERO) > 0) { - // positive chart - chartForm = 1; - System.out.println("positive chart"); + chartForm = 1; // positive chart } else if (yMin.compareTo(BigDecimal.ZERO) < 0 && yMax.compareTo(BigDecimal.ZERO) < 0) { - // negative chart - chartForm = -1; - System.out.println("negative chart"); + chartForm = -1; // negative chart } else { - // span chart - chartForm = 0; - System.out.println("span chart"); + chartForm = 0;// span chart } Iterator<?> categoryItr = categories.iterator(); diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java index 3bc22faa..1fdd1c50 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java @@ -64,32 +64,34 @@ public class PlotContentLineChart extends PlotContent { Collection<?> xData = series.getxData(); BigDecimal xMin = getChartPainter().getAxisPair().getxAxis().getMin(); BigDecimal xMax = getChartPainter().getAxisPair().getxAxis().getMax(); - if (getChartPainter().getStyleManager().isXAxisLogarithmic()) { - xMin = new BigDecimal(Math.log10(xMin.doubleValue())); - xMax = new BigDecimal(Math.log10(xMax.doubleValue())); - } + Collection<Number> yData = series.getyData(); BigDecimal yMin = getChartPainter().getAxisPair().getyAxis().getMin(); BigDecimal yMax = getChartPainter().getAxisPair().getyAxis().getMax(); - if (getChartPainter().getStyleManager().isYAxisLogarithmic()) { - yMin = new BigDecimal(Math.log10(yMin.doubleValue())); - yMax = new BigDecimal(Math.log10(yMax.doubleValue())); - } // override min and maxValue if specified - if (getChartPainter().getStyleManager().getxAxisMin() != null) { - xMin = new BigDecimal(getChartPainter().getStyleManager().getxAxisMin()); + if (getChartPainter().getStyleManager().getXAxisMin() != null) { + xMin = new BigDecimal(getChartPainter().getStyleManager().getXAxisMin()); } if (getChartPainter().getStyleManager().getYAxisMin() != null) { yMin = new BigDecimal(getChartPainter().getStyleManager().getYAxisMin()); } - if (getChartPainter().getStyleManager().getxAxisMax() != null) { - xMin = new BigDecimal(getChartPainter().getStyleManager().getxAxisMax()); + if (getChartPainter().getStyleManager().getXAxisMax() != null) { + xMax = new BigDecimal(getChartPainter().getStyleManager().getXAxisMax()); } if (getChartPainter().getStyleManager().getYAxisMax() != null) { yMax = new BigDecimal(getChartPainter().getStyleManager().getYAxisMax()); } + // logarithmic + if (getChartPainter().getStyleManager().isXAxisLogarithmic()) { + xMin = new BigDecimal(Math.log10(xMin.doubleValue())); + xMax = new BigDecimal(Math.log10(xMax.doubleValue())); + } + if (getChartPainter().getStyleManager().isYAxisLogarithmic()) { + yMin = new BigDecimal(Math.log10(yMin.doubleValue())); + yMax = new BigDecimal(Math.log10(yMax.doubleValue())); + } Collection<Number> errorBars = series.getErrorBars(); int previousX = Integer.MIN_VALUE; -- GitLab