From 8837ae13e17b6b90272e68c1cfe81b86e9ca630d Mon Sep 17 00:00:00 2001 From: Tim Molter <tim.molter@gmail.com> Date: Thu, 28 Mar 2013 20:15:47 +0100 Subject: [PATCH] fixed error-bar error --- .../xeiam/xchart/standalone/ErrorBarTest.java | 69 +++++++++++++++++++ .../main/java/com/xeiam/xchart/Series.java | 1 - .../chartpart/PlotContentLineChart.java | 21 ++++-- 3 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 xchart-demo/src/main/java/com/xeiam/xchart/standalone/ErrorBarTest.java diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/standalone/ErrorBarTest.java b/xchart-demo/src/main/java/com/xeiam/xchart/standalone/ErrorBarTest.java new file mode 100644 index 00000000..ed27b663 --- /dev/null +++ b/xchart-demo/src/main/java/com/xeiam/xchart/standalone/ErrorBarTest.java @@ -0,0 +1,69 @@ +/** + * Copyright (C) 2013 Xeiam LLC http://xeiam.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.xeiam.xchart.standalone; + +import java.awt.Color; + +import com.xeiam.xchart.Chart; +import com.xeiam.xchart.Series; +import com.xeiam.xchart.SeriesColor; +import com.xeiam.xchart.SeriesLineStyle; +import com.xeiam.xchart.SeriesMarker; +import com.xeiam.xchart.SwingWrapper; + +/** + * @author timmolter + */ +public class ErrorBarTest { + + public static void main(String[] args) throws Exception { + + // Test code for plotting + double[] xData = new double[] { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 }; + double[] yData1 = new double[] { 100, 100, 100, 50, 50, 50, 50 }; + double[] errdata = new double[] { 50, 20, 10, 50, 40, 20, 10 }; + + double[] yData2 = new double[] { 50, 80, 90, 0, 10, 30, 40 }; + double[] yData3 = new double[] { 150, 120, 110, 100, 90, 70, 60 }; + + Chart mychart = new Chart(900, 700); + mychart.getStyleManager().setYAxisLogarithmic(false); // set log or linear Y axis + mychart.getStyleManager().setYAxisMin(0); + mychart.getStyleManager().setYAxisMax(150); + mychart.getStyleManager().setErrorBarsColor(Color.black); + Series series1 = mychart.addSeries("Error bar test data", xData, yData1, errdata); + Series series2 = mychart.addSeries("Y+error", xData, yData2); + Series series3 = mychart.addSeries("Y-error", xData, yData3); + series1.setLineStyle(SeriesLineStyle.SOLID); + series1.setMarker(SeriesMarker.DIAMOND); + series1.setMarkerColor(Color.MAGENTA); + series2.setLineStyle(SeriesLineStyle.DASH_DASH); + series2.setMarker(SeriesMarker.NONE); + series2.setLineColor(SeriesColor.RED); + series3.setLineStyle(SeriesLineStyle.DASH_DASH); + series3.setMarker(SeriesMarker.NONE); + series3.setLineColor(SeriesColor.RED); + + new SwingWrapper(mychart).displayChart(); + } + +} diff --git a/xchart/src/main/java/com/xeiam/xchart/Series.java b/xchart/src/main/java/com/xeiam/xchart/Series.java index 94b9fe9d..4353cc2b 100644 --- a/xchart/src/main/java/com/xeiam/xchart/Series.java +++ b/xchart/src/main/java/com/xeiam/xchart/Series.java @@ -106,7 +106,6 @@ public class Series { yMax = yMinMax[1]; // System.out.println(yMin); // System.out.println(yMax); - } /** 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 1fdd1c50..ad49e28d 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 @@ -125,9 +125,9 @@ public class PlotContentLineChart extends PlotContent { } // System.out.println(y); - double eb = 0.0; + BigDecimal eb = BigDecimal.ZERO; if (errorBars != null) { - eb = ebItr.next().doubleValue(); + eb = new BigDecimal(ebItr.next().doubleValue()); } int xTransform = (int) (xLeftMargin + (x.subtract(xMin).doubleValue() / xMax.subtract(xMin).doubleValue() * xTickSpace)); @@ -179,11 +179,18 @@ public class PlotContentLineChart extends PlotContent { if (errorBars != null) { g.setColor(getChartPainter().getStyleManager().getErrorBarsColor()); g.setStroke(errorBarStroke); - int bottom = (int) (-1 * bounds.getHeight() * eb / (yMax.subtract(yMin).doubleValue())); - int top = (int) (bounds.getHeight() * eb / (yMax.subtract(yMin).doubleValue())); - g.drawLine(xOffset, yOffset + bottom, xOffset, yOffset + top); - g.drawLine(xOffset - 3, yOffset + bottom, xOffset + 3, yOffset + bottom); - g.drawLine(xOffset - 3, yOffset + top, xOffset + 3, yOffset + top); + + BigDecimal topValue = y.add(eb); + int topEBTransform = (int) (bounds.getHeight() - (yTopMargin + topValue.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace)); + int topEBOffset = (int) (bounds.getY() + topEBTransform); + + BigDecimal bottomValue = y.subtract(eb); + int bottomEBTransform = (int) (bounds.getHeight() - (yTopMargin + bottomValue.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace)); + int bottomEBOffset = (int) (bounds.getY() + bottomEBTransform); + + g.drawLine(xOffset, topEBOffset, xOffset, bottomEBOffset); + g.drawLine(xOffset - 3, bottomEBOffset, xOffset + 3, bottomEBOffset); + g.drawLine(xOffset - 3, topEBOffset, xOffset + 3, topEBOffset); } } -- GitLab