00001
00002
00003
00004
00005
00006
00007 package org.swtchart.internal.series;
00008
00009 import org.eclipse.swt.SWT;
00010 import org.eclipse.swt.graphics.Color;
00011 import org.eclipse.swt.graphics.GC;
00012 import org.eclipse.swt.widgets.Display;
00013 import org.swtchart.IErrorBar;
00014 import org.swtchart.internal.axis.Axis;
00015
00019 public class ErrorBar implements IErrorBar {
00020
00022 private static final int DEFAULT_LINE_WIDTH = 1;
00023
00025 private static final double DEFAULT_ERROR = 1d;
00026
00028 private static final int DEFAULT_COLOR = SWT.COLOR_DARK_GRAY;
00029
00031 private static final ErrorBarType DEFAULT_TYPE = ErrorBarType.BOTH;
00032
00034 private Color color;
00035
00037 private int lineWidth;
00038
00040 private double error;
00041
00043 private double[] plusErrors;
00044
00046 private double[] minusErrors;
00047
00049 private ErrorBarType type;
00050
00052 private boolean isVisible;
00053
00057 public ErrorBar() {
00058 color = Display.getDefault().getSystemColor(DEFAULT_COLOR);
00059 lineWidth = DEFAULT_LINE_WIDTH;
00060 error = DEFAULT_ERROR;
00061 type = ErrorBarType.BOTH;
00062 isVisible = false;
00063 plusErrors = new double[0];
00064 minusErrors = new double[0];
00065 }
00066
00067
00068
00069
00070 public ErrorBarType getType() {
00071 return type;
00072 }
00073
00074
00075
00076
00077 public void setType(ErrorBarType type) {
00078 if (type == null) {
00079 this.type = DEFAULT_TYPE;
00080 } else {
00081 this.type = type;
00082 }
00083 }
00084
00085
00086
00087
00088 public Color getColor() {
00089 if (color.isDisposed()) {
00090 color = Display.getDefault().getSystemColor(DEFAULT_COLOR);
00091 }
00092 return color;
00093 }
00094
00095
00096
00097
00098 public void setColor(Color color) {
00099 if (color != null && color.isDisposed()) {
00100 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
00101 }
00102
00103 if (color == null) {
00104 this.color = Display.getDefault().getSystemColor(DEFAULT_COLOR);
00105 } else {
00106 this.color = color;
00107 }
00108 }
00109
00110
00111
00112
00113 public int getLineWidth() {
00114 return lineWidth;
00115 }
00116
00117
00118
00119
00120 public void setLineWidth(int width) {
00121 if (width <= 0) {
00122 this.lineWidth = DEFAULT_LINE_WIDTH;
00123 } else {
00124 this.lineWidth = width;
00125 }
00126 }
00127
00128
00129
00130
00131 public double getError() {
00132 return error;
00133 }
00134
00135
00136
00137
00138 public void setError(double error) {
00139 if (error < 0) {
00140 throw new IllegalArgumentException(
00141 "positive value must be given for error.");
00142 }
00143
00144 this.error = error;
00145 }
00146
00147
00148
00149
00150 public double[] getPlusErrors() {
00151 double[] copiedSeries = new double[plusErrors.length];
00152 System.arraycopy(plusErrors, 0, copiedSeries, 0, plusErrors.length);
00153
00154 return copiedSeries;
00155 }
00156
00157
00158
00159
00160 public void setPlusErrors(double[] errors) {
00161 if (errors == null) {
00162 SWT.error(SWT.ERROR_NULL_ARGUMENT);
00163 return;
00164 }
00165
00166 this.plusErrors = new double[errors.length];
00167 System.arraycopy(errors, 0, plusErrors, 0, errors.length);
00168 }
00169
00170
00171
00172
00173 public double[] getMinusErrors() {
00174 double[] copiedSeries = new double[minusErrors.length];
00175 System.arraycopy(minusErrors, 0, copiedSeries, 0, minusErrors.length);
00176
00177 return copiedSeries;
00178 }
00179
00180
00181
00182
00183 public void setMinusErrors(double[] errors) {
00184 if (errors == null) {
00185 SWT.error(SWT.ERROR_NULL_ARGUMENT);
00186 return;
00187 }
00188
00189 this.minusErrors = new double[errors.length];
00190 System.arraycopy(errors, 0, minusErrors, 0, errors.length);
00191 }
00192
00193
00194
00195
00196 public boolean isVisible() {
00197 return isVisible;
00198 }
00199
00200
00201
00202
00203 public void setVisible(boolean visible) {
00204 this.isVisible = visible;
00205 }
00206
00221 protected void draw(GC gc, int h, int v, Axis axis, int seriesIndex) {
00222 if (!isVisible) {
00223 return;
00224 }
00225
00226 int oldLineWidth = gc.getLineWidth();
00227 gc.setLineWidth(lineWidth);
00228 gc.setLineStyle(SWT.LINE_SOLID);
00229 Color oldForeground = gc.getForeground();
00230 gc.setForeground(getColor());
00231
00232
00233 double plusError = error;
00234 double minusError = error;
00235 if (plusErrors.length > seriesIndex) {
00236 plusError = plusErrors[seriesIndex];
00237 }
00238 if (minusErrors.length > seriesIndex) {
00239 minusError = minusErrors[seriesIndex];
00240 }
00241
00242
00243 draw(gc, h, v, axis, plusError, minusError);
00244
00245 gc.setLineWidth(oldLineWidth);
00246 gc.setForeground(oldForeground);
00247 }
00248
00265 private void draw(GC gc, int h, int v, Axis axis, double plusError,
00266 double minusError) {
00267 if (axis.isHorizontalAxis()) {
00268 double dataCoordinate = axis.getDataCoordinate(h);
00269 int plusErrorInPixels = axis.getPixelCoordinate(dataCoordinate
00270 + plusError)
00271 - h;
00272 int miusErrorInPixels = h
00273 - axis.getPixelCoordinate(dataCoordinate - minusError);
00274 if (axis.isLogScaleEnabled() && dataCoordinate - plusError < 0) {
00275 miusErrorInPixels = h
00276 - axis.getPixelCoordinate(axis.getRange().lower);
00277 }
00278
00279 if (type != ErrorBarType.MINUS) {
00280 gc.drawLine(h, v, h + plusErrorInPixels, v);
00281 gc.drawLine(h + plusErrorInPixels, v + 1 + lineWidth, h
00282 + plusErrorInPixels, v - 1 - lineWidth);
00283 }
00284 if (type != ErrorBarType.PLUS) {
00285 gc.drawLine(h - miusErrorInPixels, v, h, v);
00286 gc.drawLine(h - miusErrorInPixels, v + 1 + lineWidth, h
00287 - miusErrorInPixels, v - 1 - lineWidth);
00288 }
00289 } else {
00290 double dataCoordinate = axis.getDataCoordinate(v);
00291 int plusErrorInPixels = v
00292 - axis.getPixelCoordinate(dataCoordinate + plusError);
00293 int miusErrorInPixels = axis.getPixelCoordinate(dataCoordinate
00294 - minusError)
00295 - v;
00296 if (axis.isLogScaleEnabled() && dataCoordinate - plusError < 0) {
00297 miusErrorInPixels = axis
00298 .getPixelCoordinate(axis.getRange().lower)
00299 - v;
00300 }
00301
00302 if (type != ErrorBarType.MINUS) {
00303 gc.drawLine(h, v - plusErrorInPixels, h, v);
00304 gc.drawLine(h + 1 + lineWidth, v - plusErrorInPixels, h - 1
00305 - lineWidth, v - plusErrorInPixels);
00306 }
00307 if (type != ErrorBarType.PLUS) {
00308 gc.drawLine(h, v, h, v + miusErrorInPixels);
00309 gc.drawLine(h + 1 + lineWidth, v + miusErrorInPixels, h - 1
00310 - lineWidth, v + miusErrorInPixels);
00311 }
00312 }
00313 }
00314 }