Mercurial > hg > blitz_condensed
comparison src/org/dancres/blitz/tools/dash/graph/Chart.java @ 8:77db38eb3042
Remove some dead code (Dave Brosius)
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 23 May 2009 11:28:26 +0100 |
parents | 48228766ed4c |
children |
comparison
equal
deleted
inserted
replaced
7:fa7203ea1622 | 8:77db38eb3042 |
---|---|
1 package org.dancres.blitz.tools.dash.graph; | 1 package org.dancres.blitz.tools.dash.graph; |
2 | 2 |
3 import org.dancres.blitz.tools.dash.ColorScheme; | 3 import org.dancres.blitz.tools.dash.ColorScheme; |
4 | 4 |
5 import java.lang.*; | |
6 import java.awt.*; | 5 import java.awt.*; |
7 import java.awt.event.*; | |
8 import java.text.*; | 6 import java.text.*; |
9 import javax.swing.*; | 7 import javax.swing.*; |
10 | 8 |
11 public class Chart extends JPanel | 9 public class Chart extends JPanel |
12 implements ChartItemEnabler | 10 implements ChartItemEnabler |
59 private Dimension minSize=new Dimension(200,200); | 57 private Dimension minSize=new Dimension(200,200); |
60 private Dimension currentSize=null; | 58 private Dimension currentSize=null; |
61 private Rectangle chartRect= new Rectangle(); | 59 private Rectangle chartRect= new Rectangle(); |
62 private Rectangle zoomRect; | 60 private Rectangle zoomRect; |
63 private boolean isShown=false; //at least one dataset visiable | 61 private boolean isShown=false; //at least one dataset visiable |
62 | |
64 //public methods | 63 //public methods |
65 public Chart() | 64 public Chart() |
66 { | 65 { |
67 idDrawer[0]=new SqDrawer(SOLID); | 66 idDrawer[0]=new SqDrawer(SOLID); |
68 idDrawer[1]=new XDrawer(); | 67 idDrawer[1]=new XDrawer(); |
69 idDrawer[2]=new RoundRectDrawer(SOLID); | 68 idDrawer[2]=new RoundRectDrawer(SOLID); |
70 | 69 |
71 | 70 |
72 } | 71 } |
72 | |
73 synchronized public void addData(final String name,final double [] data, | 73 synchronized public void addData(final String name,final double [] data, |
74 final String [] tags) | 74 final String [] tags) |
75 throws ChartException | 75 throws ChartException |
76 { | 76 { |
77 addData(name,data,tags,null); | 77 addData(name,data,tags,null); |
78 } | 78 } |
79 | |
79 synchronized public void addData(final String name,final double [] data, | 80 synchronized public void addData(final String name,final double [] data, |
80 final String [] tags,final int [] dates) | 81 final String [] tags,final int [] dates) |
81 throws ChartException | 82 throws ChartException |
82 { | 83 { |
83 if(dsCounter==MAX_DATA_SETS) | 84 if(dsCounter==MAX_DATA_SETS) |
84 throw new ChartException("Maximum number of data sets exceeded!"); | 85 throw new ChartException("Maximum number of data sets exceeded!"); |
85 | 86 |
86 setDataAt(dsCounter++,name,data,tags,dates); | 87 setDataAt(dsCounter++,name,data,tags,dates); |
87 } | 88 } |
89 | |
88 public void setDataAt(int index,final String name,final double [] data, | 90 public void setDataAt(int index,final String name,final double [] data, |
89 final String [] tags,final int [] dates) | 91 final String [] tags,final int [] dates) |
90 throws ChartException | 92 throws ChartException |
91 { | 93 { |
92 dirtyCache=true; //flag that calcFactors must be called internally | 94 dirtyCache=true; //flag that calcFactors must be called internally |
108 } | 110 } |
109 d.pointFactor= new double[ tags.length ]; | 111 d.pointFactor= new double[ tags.length ]; |
110 dataSet[ index ]=d; | 112 dataSet[ index ]=d; |
111 } | 113 } |
112 public void setLabelEvery(int every) {labelEvery=every;} | 114 public void setLabelEvery(int every) {labelEvery=every;} |
115 | |
113 public static int getMaxSupportedDataSets() {return MAX_DATA_SETS;} | 116 public static int getMaxSupportedDataSets() {return MAX_DATA_SETS;} |
117 | |
114 public Dimension getMinimumSize() | 118 public Dimension getMinimumSize() |
115 { | 119 { |
116 return minSize; | 120 return minSize; |
117 } | 121 } |
122 | |
118 public Dimension getPreferredSize() | 123 public Dimension getPreferredSize() |
119 { | 124 { |
120 if(currentSize==null) | 125 if(currentSize==null) |
121 currentSize= getSize(); | 126 currentSize= getSize(); |
122 | 127 |
123 return currentSize; | 128 return currentSize; |
124 } | 129 } |
130 | |
125 public void setSize(Dimension d) | 131 public void setSize(Dimension d) |
126 { | 132 { |
127 super.setSize(d); | 133 super.setSize(d); |
128 currentSize=d; | 134 currentSize=d; |
129 } | 135 } |
133 int ty=yOffset; | 139 int ty=yOffset; |
134 yOffset=yoffset; | 140 yOffset=yoffset; |
135 super.print(g); | 141 super.print(g); |
136 yOffset=ty; | 142 yOffset=ty; |
137 } | 143 } |
144 | |
138 public void enableData(final String name,final boolean yesno) | 145 public void enableData(final String name,final boolean yesno) |
139 { | 146 { |
140 for( int i=0;i<dsCounter;i++) | 147 for( int i=0;i<dsCounter;i++) |
141 { | 148 { |
142 if(dataSet[i].label.compareTo(name)==0) | 149 if(dataSet[i].label.compareTo(name)==0) |
146 repaint(); | 153 repaint(); |
147 return; | 154 return; |
148 } | 155 } |
149 } | 156 } |
150 } | 157 } |
158 | |
151 public void paint(Graphics g) | 159 public void paint(Graphics g) |
152 { | 160 { |
153 Dimension dim=getSize(); | 161 Dimension dim=getSize(); |
154 g.setColor(Color.white); | 162 g.setColor(Color.white); |
155 g.fillRect(0,0,dim.width,dim.height); | 163 g.fillRect(0,0,dim.width,dim.height); |
156 | |
157 boolean isPrinting=g instanceof PrintGraphics; | |
158 | 164 |
159 if(dirtyCache) | 165 if(dirtyCache) |
160 { | 166 { |
161 calcFactors(); | 167 calcFactors(); |
162 dirtyCache=false; | 168 dirtyCache=false; |
202 g.drawString(dslabel,legx+5,legy); | 208 g.drawString(dslabel,legx+5,legy); |
203 | 209 |
204 legx+=strWidth+12; | 210 legx+=strWidth+12; |
205 } | 211 } |
206 } | 212 } |
213 | |
207 //now calc the drawable area for the chart | 214 //now calc the drawable area for the chart |
208 // draw zoomable area | 215 // draw zoomable area |
209 yoff=legy+15; | 216 yoff=legy+15; |
210 hi-=(legy-baseY)+5; | 217 hi-=(legy-baseY)+5; |
211 | 218 |
212 if(zoomRect!=null) | 219 if(zoomRect!=null) |
213 { | 220 { |
214 g.setColor(Color.lightGray); | 221 g.setColor(Color.lightGray); |
215 g.fillRect(zoomRect.x,yoff,zoomRect.width,hi); | 222 g.fillRect(zoomRect.x,yoff,zoomRect.width,hi); |
216 } | 223 } |
224 | |
217 //border | 225 //border |
218 g.setColor(axisColor); | 226 g.setColor(axisColor); |
219 g.drawRect(xoff,yoff,wid,hi); | 227 g.drawRect(xoff,yoff,wid,hi); |
228 | |
220 //cache for zooming | 229 //cache for zooming |
221 chartRect.setBounds(xoff,yoff,wid,hi); | 230 chartRect.setBounds(xoff,yoff,wid,hi); |
231 | |
222 //draw the points | 232 //draw the points |
223 for(int i=0;i<ns;i++) | 233 for(int i=0;i<ns;i++) |
224 { | 234 { |
225 if(dataSet[i].isVisible) | 235 if(dataSet[i].isVisible) |
226 { | 236 { |
247 lastx=xpos;lasty=ypos; | 257 lastx=xpos;lasty=ypos; |
248 } | 258 } |
249 if(xpos>(prevx+25)) | 259 if(xpos>(prevx+25)) |
250 { | 260 { |
251 g.setColor(gridColor); | 261 g.setColor(gridColor); |
252 g.drawLine((int)xpos,(int)yoff, | 262 g.drawLine((int) xpos,yoff, |
253 (int)xpos,(int)yoff+hi+2); | 263 (int) xpos,yoff+hi+2); |
254 g.setColor(axisColor); | 264 g.setColor(axisColor); |
255 if(labelCounter%labelEvery==0 && | 265 if(labelCounter%labelEvery==0 && |
256 xpos>(lastLabelPos+labelWidth+10)) | 266 xpos>(lastLabelPos+labelWidth+10)) |
257 { | 267 { |
258 xTagPosition[j]=(int)xpos-5; | 268 xTagPosition[j]=(int)xpos-5; |
259 g.drawString(dataSet[i].getTag(j),xTagPosition[j],(int)yoff+hi+20); | 269 g.drawString(dataSet[i].getTag(j),xTagPosition[j],yoff+hi+20); |
260 lastLabelPos=xpos; | 270 lastLabelPos=xpos; |
261 } | 271 } |
262 else | 272 else |
263 { | 273 { |
264 xTagPosition[j]=-1; //invalid | 274 xTagPosition[j]=-1; //invalid |
398 } | 408 } |
399 maxValue=adjustMax(maxValue); | 409 maxValue=adjustMax(maxValue); |
400 minValue=adjustMin(minValue); | 410 minValue=adjustMin(minValue); |
401 | 411 |
402 } | 412 } |
413 | |
403 private double adjustMax(double d) | 414 private double adjustMax(double d) |
404 { | 415 { |
405 double x=d; | 416 double x=d; |
406 if(x>0) | 417 if(x>0) |
407 x+=0.05; | 418 x+=0.05; |
408 else | 419 else |
409 x-=0.05; | 420 x-=0.05; |
410 | 421 |
411 return Math.rint(x*10)/10; | 422 return Math.rint(x*10)/10; |
412 } | 423 } |
424 | |
413 private double adjustMin(double d) | 425 private double adjustMin(double d) |
414 { | 426 { |
415 double x=d; | 427 double x=d; |
416 x-=0.05; | 428 x-=0.05; |
417 return Math.rint(x*10)/10; | 429 return Math.rint(x*10)/10; |
418 } | 430 } |
431 | |
419 //private impl classes internal double-dispatch | 432 //private impl classes internal double-dispatch |
420 private interface IdDrawer | 433 private interface IdDrawer |
421 { | 434 { |
422 public void draw(final Graphics g,final int xpos,final int ypos); | 435 public void draw(final Graphics g,final int xpos,final int ypos); |
423 } | 436 } |
437 g.fillRect(xpos+1,ypos,4,4); | 450 g.fillRect(xpos+1,ypos,4,4); |
438 else | 451 else |
439 g.drawRect(xpos,ypos,4,4); | 452 g.drawRect(xpos,ypos,4,4); |
440 } | 453 } |
441 } | 454 } |
455 | |
442 private class RoundRectDrawer | 456 private class RoundRectDrawer |
443 implements IdDrawer | 457 implements IdDrawer |
444 { | 458 { |
445 private int drawMode; | 459 private int drawMode; |
446 | 460 |
454 g.fillRoundRect(xpos+1,ypos,4,4,4,4); | 468 g.fillRoundRect(xpos+1,ypos,4,4,4,4); |
455 else | 469 else |
456 g.drawRoundRect(xpos+1,ypos,4,4,4,4); | 470 g.drawRoundRect(xpos+1,ypos,4,4,4,4); |
457 } | 471 } |
458 } | 472 } |
473 | |
459 private class XDrawer | 474 private class XDrawer |
460 implements IdDrawer | 475 implements IdDrawer |
461 { | 476 { |
462 public XDrawer() | 477 public XDrawer() |
463 { | 478 { |
468 | 483 |
469 g.drawLine(xpos-2+os,ypos-2+os,xpos+2+os,ypos+2+os); | 484 g.drawLine(xpos-2+os,ypos-2+os,xpos+2+os,ypos+2+os); |
470 g.drawLine(xpos+2+os,ypos-2+os,xpos-2+os,ypos+2+os); | 485 g.drawLine(xpos+2+os,ypos-2+os,xpos-2+os,ypos+2+os); |
471 } | 486 } |
472 } | 487 } |
473 //zoomer | |
474 private int dragStartXPos; | |
475 private int dragStartYPos; | |
476 private int lastXPos; | |
477 //private int lastYPos; | |
478 private boolean isDragging=false; | |
479 | |
480 private void setZoomEndPos(int xend) | |
481 { | |
482 Dimension d=getSize(); | |
483 int s=dragStartXPos; | |
484 int e=xend; | |
485 if(s>e) //right to lseft highlight | |
486 { | |
487 s=xend; | |
488 e=dragStartXPos; | |
489 } | |
490 int oldstart=startAt; | |
491 int oldend=endAt; | |
492 calcNewStartAndEnd(s,e); | |
493 if(startAt<endAt) | |
494 { | |
495 calcFactors(); //recalculate the factors | |
496 } | |
497 else | |
498 { | |
499 startAt=oldstart; | |
500 endAt=oldend; | |
501 } | |
502 // | |
503 /* | |
504 double startFactor=(double)s/(double)d.width; | |
505 double endFactor=(double)e/(double)d.width; | |
506 int oldstart=startAt; | |
507 int oldend=endAt; | |
508 startAt+=(int)(endAt*startFactor); | |
509 endAt=(int)(endAt*endFactor); | |
510 if(startAt<endAt) | |
511 { | |
512 calcFactors(); //recalculate the factors | |
513 } | |
514 else | |
515 { | |
516 startAt=oldstart; | |
517 endAt=oldend; | |
518 } | |
519 */ | |
520 repaint(); | |
521 } | |
522 //calculate startAt, endAt based xTagPositions[] | |
523 private void calcNewStartAndEnd(int xStart,int xEnd) | |
524 { | |
525 //TEST | |
526 //System.out.println("xStart="+xStart+" End="+xEnd); | |
527 | |
528 int lowerBound=0; | |
529 int upperBound=0; | |
530 int xLower=0; | |
531 int xHigher=0; | |
532 int lowestXBound=Integer.MAX_VALUE; | |
533 int highestXBound=Integer.MAX_VALUE; | |
534 | |
535 int size=xTagPosition.length; | |
536 for(int i=0;i<size;i++) | |
537 { | |
538 if(xTagPosition[i]!=-1) | |
539 { | |
540 | |
541 xLower=xStart-xTagPosition[i]; | |
542 xLower=Math.abs(xLower); | |
543 if(xLower<lowestXBound) | |
544 { | |
545 lowestXBound=xLower; | |
546 lowerBound=i; | |
547 } | |
548 xHigher=xEnd-xTagPosition[i]; | |
549 xHigher=Math.abs(xHigher); | |
550 if(xHigher<highestXBound) | |
551 { | |
552 highestXBound=xHigher; | |
553 upperBound=i; | |
554 } | |
555 } | |
556 } | |
557 startAt=lowerBound; | |
558 endAt=upperBound; | |
559 | |
560 } | |
561 | |
562 } | 488 } |
563 | 489 |