1:
57:
58: package ;
59:
60: import ;
61: import ;
62: import ;
63: import ;
64: import ;
65: import ;
66:
67: import ;
68: import ;
69: import ;
70: import ;
71: import ;
72: import ;
73: import ;
74: import ;
75: import ;
76: import ;
77: import ;
78: import ;
79: import ;
80:
81:
84: public class StackedXYAreaRenderer2 extends XYAreaRenderer2
85: implements Cloneable,
86: PublicCloneable,
87: Serializable {
88:
89:
90: private static final long serialVersionUID = 7752676509764539182L;
91:
92:
101: private boolean roundXCoordinates;
102:
103:
106: public StackedXYAreaRenderer2() {
107: this(null, null);
108: }
109:
110:
117: public StackedXYAreaRenderer2(XYToolTipGenerator labelGenerator,
118: XYURLGenerator urlGenerator) {
119: super(labelGenerator, urlGenerator);
120: this.roundXCoordinates = true;
121: }
122:
123:
133: public boolean getRoundXCoordinates() {
134: return this.roundXCoordinates;
135: }
136:
137:
148: public void setRoundXCoordinates(boolean round) {
149: this.roundXCoordinates = round;
150: notifyListeners(new RendererChangeEvent(this));
151: }
152:
153:
162: public Range findRangeBounds(XYDataset dataset) {
163: if (dataset == null) {
164: return null;
165: }
166: double min = Double.POSITIVE_INFINITY;
167: double max = Double.NEGATIVE_INFINITY;
168: TableXYDataset d = (TableXYDataset) dataset;
169: int itemCount = d.getItemCount();
170: for (int i = 0; i < itemCount; i++) {
171: double[] stackValues = getStackValues((TableXYDataset) dataset,
172: d.getSeriesCount(), i);
173: min = Math.min(min, stackValues[0]);
174: max = Math.max(max, stackValues[1]);
175: }
176: if (min == Double.POSITIVE_INFINITY) {
177: return null;
178: }
179: return new Range(min, max);
180: }
181:
182:
187: public int getPassCount() {
188: return 1;
189: }
190:
191:
208: public void drawItem(Graphics2D g2,
209: XYItemRendererState state,
210: Rectangle2D dataArea,
211: PlotRenderingInfo info,
212: XYPlot plot,
213: ValueAxis domainAxis,
214: ValueAxis rangeAxis,
215: XYDataset dataset,
216: int series,
217: int item,
218: CrosshairState crosshairState,
219: int pass) {
220:
221:
222: Shape entityArea = null;
223: EntityCollection entities = null;
224: if (info != null) {
225: entities = info.getOwner().getEntityCollection();
226: }
227:
228: TableXYDataset tdataset = (TableXYDataset) dataset;
229:
230:
231: double x1 = dataset.getXValue(series, item);
232: double y1 = dataset.getYValue(series, item);
233: if (Double.isNaN(y1)) {
234: y1 = 0.0;
235: }
236: double[] stack1 = getStackValues(tdataset, series, item);
237:
238:
239:
240: double x0 = dataset.getXValue(series, Math.max(item - 1, 0));
241: double y0 = dataset.getYValue(series, Math.max(item - 1, 0));
242: if (Double.isNaN(y0)) {
243: y0 = 0.0;
244: }
245: double[] stack0 = getStackValues(tdataset, series, Math.max(item - 1,
246: 0));
247:
248: int itemCount = dataset.getItemCount(series);
249: double x2 = dataset.getXValue(series, Math.min(item + 1,
250: itemCount - 1));
251: double y2 = dataset.getYValue(series, Math.min(item + 1,
252: itemCount - 1));
253: if (Double.isNaN(y2)) {
254: y2 = 0.0;
255: }
256: double[] stack2 = getStackValues(tdataset, series, Math.min(item + 1,
257: itemCount - 1));
258:
259: double xleft = (x0 + x1) / 2.0;
260: double xright = (x1 + x2) / 2.0;
261: double[] stackLeft = averageStackValues(stack0, stack1);
262: double[] stackRight = averageStackValues(stack1, stack2);
263: double[] adjStackLeft = adjustedStackValues(stack0, stack1);
264: double[] adjStackRight = adjustedStackValues(stack1, stack2);
265:
266: RectangleEdge edge0 = plot.getDomainAxisEdge();
267:
268: float transX1 = (float) domainAxis.valueToJava2D(x1, dataArea, edge0);
269: float transXLeft = (float) domainAxis.valueToJava2D(xleft, dataArea,
270: edge0);
271: float transXRight = (float) domainAxis.valueToJava2D(xright, dataArea,
272: edge0);
273:
274: if (this.roundXCoordinates) {
275: transX1 = Math.round(transX1);
276: transXLeft = Math.round(transXLeft);
277: transXRight = Math.round(transXRight);
278: }
279: float transY1;
280:
281: RectangleEdge edge1 = plot.getRangeAxisEdge();
282:
283: GeneralPath left = new GeneralPath();
284: GeneralPath right = new GeneralPath();
285: if (y1 >= 0.0) {
286: transY1 = (float) rangeAxis.valueToJava2D(y1 + stack1[1], dataArea,
287: edge1);
288: float transStack1 = (float) rangeAxis.valueToJava2D(stack1[1],
289: dataArea, edge1);
290: float transStackLeft = (float) rangeAxis.valueToJava2D(
291: adjStackLeft[1], dataArea, edge1);
292:
293:
294: if (y0 >= 0.0) {
295: double yleft = (y0 + y1) / 2.0 + stackLeft[1];
296: float transYLeft
297: = (float) rangeAxis.valueToJava2D(yleft, dataArea, edge1);
298: left.moveTo(transX1, transY1);
299: left.lineTo(transX1, transStack1);
300: left.lineTo(transXLeft, transStackLeft);
301: left.lineTo(transXLeft, transYLeft);
302: left.closePath();
303: }
304: else {
305: left.moveTo(transX1, transStack1);
306: left.lineTo(transX1, transY1);
307: left.lineTo(transXLeft, transStackLeft);
308: left.closePath();
309: }
310:
311: float transStackRight = (float) rangeAxis.valueToJava2D(
312: adjStackRight[1], dataArea, edge1);
313:
314: if (y2 >= 0.0) {
315: double yright = (y1 + y2) / 2.0 + stackRight[1];
316: float transYRight
317: = (float) rangeAxis.valueToJava2D(yright, dataArea, edge1);
318: right.moveTo(transX1, transStack1);
319: right.lineTo(transX1, transY1);
320: right.lineTo(transXRight, transYRight);
321: right.lineTo(transXRight, transStackRight);
322: right.closePath();
323: }
324: else {
325: right.moveTo(transX1, transStack1);
326: right.lineTo(transX1, transY1);
327: right.lineTo(transXRight, transStackRight);
328: right.closePath();
329: }
330: }
331: else {
332: transY1 = (float) rangeAxis.valueToJava2D(y1 + stack1[0], dataArea,
333: edge1);
334: float transStack1 = (float) rangeAxis.valueToJava2D(stack1[0],
335: dataArea, edge1);
336: float transStackLeft = (float) rangeAxis.valueToJava2D(
337: adjStackLeft[0], dataArea, edge1);
338:
339:
340: if (y0 >= 0.0) {
341: left.moveTo(transX1, transStack1);
342: left.lineTo(transX1, transY1);
343: left.lineTo(transXLeft, transStackLeft);
344: left.clone();
345: }
346: else {
347: double yleft = (y0 + y1) / 2.0 + stackLeft[0];
348: float transYLeft = (float) rangeAxis.valueToJava2D(yleft,
349: dataArea, edge1);
350: left.moveTo(transX1, transY1);
351: left.lineTo(transX1, transStack1);
352: left.lineTo(transXLeft, transStackLeft);
353: left.lineTo(transXLeft, transYLeft);
354: left.closePath();
355: }
356: float transStackRight = (float) rangeAxis.valueToJava2D(
357: adjStackRight[0], dataArea, edge1);
358:
359:
360: if (y2 >= 0.0) {
361: right.moveTo(transX1, transStack1);
362: right.lineTo(transX1, transY1);
363: right.lineTo(transXRight, transStackRight);
364: right.closePath();
365: }
366: else {
367: double yright = (y1 + y2) / 2.0 + stackRight[0];
368: float transYRight = (float) rangeAxis.valueToJava2D(yright,
369: dataArea, edge1);
370: right.moveTo(transX1, transStack1);
371: right.lineTo(transX1, transY1);
372: right.lineTo(transXRight, transYRight);
373: right.lineTo(transXRight, transStackRight);
374: right.closePath();
375: }
376: }
377:
378:
379: Paint itemPaint = getItemPaint(series, item);
380: if (pass == 0) {
381: g2.setPaint(itemPaint);
382: g2.fill(left);
383: g2.fill(right);
384: }
385:
386:
387: if (entities != null) {
388: GeneralPath gp = new GeneralPath(left);
389: gp.append(right, false);
390: entityArea = gp;
391: addEntity(entities, entityArea, dataset, series, item,
392: transX1, transY1);
393: }
394:
395: }
396:
397:
410: private double[] getStackValues(TableXYDataset dataset,
411: int series, int index) {
412: double[] result = new double[2];
413: for (int i = 0; i < series; i++) {
414: double v = dataset.getYValue(i, index);
415: if (!Double.isNaN(v)) {
416: if (v >= 0.0) {
417: result[1] += v;
418: }
419: else {
420: result[0] += v;
421: }
422: }
423: }
424: return result;
425: }
426:
427:
436: private double[] averageStackValues(double[] stack1, double[] stack2) {
437: double[] result = new double[2];
438: result[0] = (stack1[0] + stack2[0]) / 2.0;
439: result[1] = (stack1[1] + stack2[1]) / 2.0;
440: return result;
441: }
442:
443:
453: private double[] adjustedStackValues(double[] stack1, double[] stack2) {
454: double[] result = new double[2];
455: if (stack1[0] == 0.0 || stack2[0] == 0.0) {
456: result[0] = 0.0;
457: }
458: else {
459: result[0] = (stack1[0] + stack2[0]) / 2.0;
460: }
461: if (stack1[1] == 0.0 || stack2[1] == 0.0) {
462: result[1] = 0.0;
463: }
464: else {
465: result[1] = (stack1[1] + stack2[1]) / 2.0;
466: }
467: return result;
468: }
469:
470:
477: public boolean equals(Object obj) {
478: if (obj == this) {
479: return true;
480: }
481: if (!(obj instanceof StackedXYAreaRenderer2)) {
482: return false;
483: }
484: StackedXYAreaRenderer2 that = (StackedXYAreaRenderer2) obj;
485: if (this.roundXCoordinates != that.roundXCoordinates) {
486: return false;
487: }
488: return super.equals(obj);
489: }
490:
491:
498: public Object clone() throws CloneNotSupportedException {
499: return super.clone();
500: }
501:
502: }