View Javadoc

1   package hoplugins.seriesstats;
2   
3   import java.awt.*;
4   import javax.swing.*;
5   /***
6    * Zeigt Statistiken in Form eins Liniendiagrames an
7    */
8   public class GraphicPanel extends JPanel
9   {
10      //Seitenabstände bis zum Koordinatenkreuzanfang
11      private int SL = 60; //Dynamisch berechnen
12      public int SR = 25;
13      public int SU = 50;
14      public int SO = 25;
15      public int SA = 50; //Abstand zwischen den Hilfslinien
16      public  boolean     print           = false;
17      
18      private String      xBezeichner     = "";
19      private String      yBezeichner     = "";
20  
21      private boolean     beschriftung    = false;
22      private boolean     hilfslinien     = true;
23      
24      private boolean     m_bMaxMinBerechnen       = false;
25      
26      private GraphicData[]  m_clStatistikModel = null;
27      
28      private String[]          m_clYAchseBeschriftung = null;
29      private java.text.NumberFormat m_clYAchseFormat  = null;
30  
31      public static final AlphaComposite DEFAULTALPHA = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);    
32      public static final AlphaComposite REDUCEDALPHA = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.7f);    
33      
34  //##Konstruktoren######################################################	
35  
36  /***
37   * Konstruktor
38   */
39      public GraphicPanel( boolean maxminBerechnen )
40      {
41          m_bMaxMinBerechnen = maxminBerechnen;
42          setDoubleBuffered(true);
43      }
44  	
45  
46  
47  //--Set und Get-methoden------------------------------
48  
49          
50      /***
51       * Maximaler Konstruktor
52       * 
53       * @param xBezeichner Bezeichnung der x-Achse
54       * @param yBezeichner Bezeichnung der y-Achse
55       * @param beschriftung Beschriftung des Graphen
56       * @param hilflinien Vertikale und Horizontale Linien durch den Graphen
57       * @param startwert Wert, mit der die x-Achse zu zählen anfängt
58       * @param abstand Der Wertabstand der x-Achse zwischen 2 Werten
59       * @param farbe Farbe des Graphen
60       */
61      public void setAllValues ( GraphicData[] models, String[] yAchseBeschriftung, java.text.NumberFormat yAchseFormat, String xBezeichner, String yBezeichner, boolean beschriftung, boolean hilfslinien )
62      {
63          this.m_clStatistikModel=models;
64          this.m_clYAchseBeschriftung=yAchseBeschriftung;
65          this.xBezeichner=xBezeichner;
66          this.yBezeichner=yBezeichner;
67          this.beschriftung=beschriftung;
68          this.hilfslinien=hilfslinien;
69          this.m_clYAchseFormat=yAchseFormat;
70          repaint();
71          //update(this.getGraphics());
72          
73      }
74          
75      public void setModel( GraphicData[] models )
76      {
77          m_clStatistikModel = models;
78          repaint();
79      }
80      
81      public GraphicData[] getModel()
82      {
83          return m_clStatistikModel;
84      }
85      
86      /*** 
87       * Ein bestimmtes Model holen
88       */
89      public GraphicData getModel( String name )
90      {
91          for ( int i = 0; m_clStatistikModel != null && m_clStatistikModel.length > i; i++ )
92          {
93              if ( m_clStatistikModel[i].getName ().equals ( name ) )
94              {
95                  return m_clStatistikModel[i];
96              }
97          }
98          return null;
99      }
100     
101     /***
102      * Einen bestimmten Graf sichtbar/unsichtbar machen
103      */
104     public void setShow( String name, boolean show )
105     {
106         for ( int i = 0; m_clStatistikModel != null && m_clStatistikModel.length > i; i++ )
107         {
108             if ( m_clStatistikModel[i].getName ().equals ( name ) )
109             {
110                 m_clStatistikModel[i].setShow ( show );
111                 break;
112             }
113         }
114         repaint();
115     }
116     
117 
118     /***
119     * Ein- oder Ausschalten der Beschriftung des Graphen
120     *
121     * @param beschriftung true: an / false: aus
122     */
123     public void setBeschriftung(boolean beschriftung)
124     {
125         this.beschriftung=beschriftung;
126         repaint();//update(this.getGraphics());
127     }
128 
129     /***
130     * Liefert den Zustand der Beschriftung (an/aus)
131     */
132     public boolean getBeschriftung()
133     {
134         return beschriftung;
135     }
136 
137     /***
138      * Ein- und Ausschalten der Hilfslinien
139      *
140      * @param hilfslinien true: an / false: aus
141      */
142     public void setHilfslinien(boolean hilfslinien)
143     {
144             this.hilfslinien=hilfslinien;
145             repaint();//update(this.getGraphics());
146     }
147 
148     /***
149     * Liefert den Zustand der Hilfslinien (an/aus)
150     */
151     public boolean getHilfslinien()
152     {
153             return hilfslinien;
154     }
155 
156             
157 //#####################################################################
158 
159         public void print( Graphics g )
160         {
161                 update(g);
162         }
163     
164 /***
165  * Zeichnet die GComponent
166  */
167 	public void paint(Graphics g)
168 	{
169 		update(g);
170 	}
171 
172 /***
173  * Zeichnet die GComponent
174  */
175 	public void update(Graphics g)
176 	{
177             if ( g != null )
178             {
179                 ( (java.awt.Graphics2D)g ).setRenderingHint ( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
180             
181 		//Initialsierung des Fensters
182 		Rectangle r = getBounds();
183 		int b = r.width-1;
184 		int h = r.height-1;
185 //		g.setColor(Color.lightGray);
186 //		g.fill3DRect(1,1,b-1,h-1,false);
187 		g.setColor(Color.white);		
188 		g.fillRect(1,1,b-1,h-1);
189 		g.setColor(Color.darkGray);
190                 int schriftgroesse = gui.UserParameter.instance ().schriftGroesse+2;
191                 if ( print )
192                 {
193                     schriftgroesse /=2;
194                 }
195 		g.setFont(new Font("SansSerif",Font.BOLD,schriftgroesse));
196 		g.drawString(yBezeichner,8,18);
197 		g.drawString(xBezeichner,b-150,h-8);
198 		
199 		//Höchster Wert in der Menge
200 		double max=20;
201 		double min=0;
202                 double maxohneFaktor=20;
203                 double minohneFaktor=0;
204                 //MaxMin berechnen
205                 if ( m_bMaxMinBerechnen )
206                 {
207                     max = maxFinder( true );
208                     min = minFinder( true );
209                     if (max<0) max=0;
210                     if (min>0) min=0;
211                 }
212                 maxohneFaktor = maxFinder( false );
213                 minohneFaktor = minFinder( false );
214                 
215 		//Höhe der x-Achse bestimmen
216 		int xHoehe=(int)((h-SU-SO)/2+SO+(max+min)*(((h-SU-SO)/2)/(max-min)));
217 		//#############SL abhängig von max###############################+++++++++++++
218 		
219                 
220                 if ( m_clStatistikModel != null && m_clStatistikModel.length > 0 && m_clStatistikModel[0] != null  )
221                 {
222                     //Massangaben einzeichnen
223                     //Menge der y-Striche bestimmen
224                     int f = 1;
225                     if ( print ) f = 2;
226                     int yStriche=(int)((h-SU-SO)/(SA/f));
227                     if (yStriche==0) yStriche=1;
228                     //Abstand der einzelnen Striche voneinander
229                     double yAbstand=(((double)(h-SU-SO))/yStriche);
230                     int smallschriftgroesse = gui.UserParameter.instance ().schriftGroesse;
231                     if ( print )
232                     {
233                         smallschriftgroesse /=2;
234                     }
235                     g.setFont(new Font("SansSerif",Font.BOLD,smallschriftgroesse));
236                     //Seitenabstand berechnen
237                     SL = smallschriftgroesse 
238                          + Math.max( (int)( ( g.getFontMetrics ().stringWidth ( m_clYAchseFormat.format ( max ) ) + 10 )  ), 
239                                      (int)( ( g.getFontMetrics ().stringWidth ( m_clYAchseFormat.format ( min ) ) + 10 )  ) );
240                     for (int i=yStriche;i>=0;i--)
241                     {
242                             //Striche y zeichnen: Höhe - Abstand vom unteren Rand - Vielfaches des Strichabstandes
243                             if (hilfslinien) 
244                             {
245                                     g.setColor(Color.lightGray);
246                                     g.drawLine(SL+5,(int)(h-SU-yAbstand*i),b-SR,(int)(h-SU-yAbstand*i));
247 
248                             }
249                             g.setColor(Color.black);
250                             g.drawLine(SL-5,(int)(h-SU-yAbstand*i),SL+5,(int)(h-SU-yAbstand*i));
251                             //Wert pro Strich
252                             int ypos = (int)(h-SU+smallschriftgroesse/2-yAbstand*i);
253                             g.drawString(m_clYAchseFormat.format(((double)(max-min)/(yStriche)*i+min)),smallschriftgroesse,ypos);
254                     }
255 
256                      //Menge der x-Striche bestimmen - Wird jetzt bei der BeschriftungXAchse gemacht
257                     
258 //                    int xStriche=(int)((b-SL-SR)/SA+1);
259 //                    if (xStriche>m_clStatistikModel[0].getWerte ().length) xStriche=m_clStatistikModel[0].getWerte ().length;
260                     //Abstand der einzelnen Striche voneinander
261 //                    double xAbstand=((double)(b-SL-SR)/(xStriche));
262 //                    for (int i=0;i<=xStriche;i++)
263 //                    {
264 //                            //Striche x zeichnen: Abstand vom linken Rand + Vielfaches vom Abstand
265 ////                            if (hilfslinien) 
266 ////                            {
267 ////                                    g.setColor(Color.lightGray);
268 ////                                    g.drawLine((int)(SL+xAbstand*(i)),h-SU,(int)(SL+xAbstand*(i)),SO);
269 ////                            }
270 ////                            g.setColor(Color.black);
271 ////                            g.drawLine((int)(SL+xAbstand*(i)),xHoehe-5,(int)(SL+xAbstand*(i)),xHoehe+5);
272 //                            //Wert pro Strich: Strichnummer * Wertemenge / Menge der Striche
273 //                            //###
274 //                            //g.drawString(m_clStatistikModel[0].getBezeichnungXAchse ()[i],(int)(SL+xAbstand*(i)-10),xHoehe+20);
275 //                    }
276 
277                     //Koordinatenkreuz
278                     g.drawLine(SL,SO,SL,h-SU);
279                     g.drawLine(SL,xHoehe,b-SR,xHoehe);
280                     
281                     int schriftbreite = 0;
282                     if ( m_clStatistikModel[0].getWerte ().length > 0 )
283                     {
284                         schriftbreite = Math.max( (int)( ( g.getFontMetrics ().stringWidth ( m_clStatistikModel[0].getFormat ().format ( maxohneFaktor ) ) + 10 )  ), 
285                                                   (int)( ( g.getFontMetrics ().stringWidth ( m_clStatistikModel[0].getFormat ().format ( minohneFaktor ) ) + 10 )  ) );
286                     }
287                     
288                     //Beschriftung XAchse
289                     showXAchseBeschriftung((Graphics2D)g,b,h);
290 
291                     //Wertelinie eintragen
292                     for ( int i = 0; m_clStatistikModel != null && i < m_clStatistikModel.length; i ++ )
293                     {
294                         if ( m_clStatistikModel[i] != null && m_clStatistikModel[i].isShow () )
295                         {
296                             wertLinien((Graphics2D)g,b,h,m_clStatistikModel[i].getWerte (),m_clStatistikModel[i].getFaktor(),max,min,beschriftung,m_clStatistikModel[i].getColor (),m_clStatistikModel[i].getFormat (), schriftbreite);
297                         }
298                     }
299                 }//Ende Keine Werte
300             }
301 	}
302 
303 	//Zeichnen eines Liniendiagrammes
304 	private void wertLinien(Graphics2D g, int b, int h, double[] werte,double faktor, double max, double min, boolean beschriftung, Color farbe, java.text.NumberFormat format, int schriftbreite)
305 	{
306             if ( werte.length > 0 )
307             {
308 		int x1,x2,y1,y2;
309 		int mengeBeschriftung=(int)((b-SL-SR)/schriftbreite)-1;
310 		if (mengeBeschriftung==0) mengeBeschriftung=1;
311 		int abstandBeschriftung=(int)(werte.length/mengeBeschriftung);
312 		if (abstandBeschriftung==0) abstandBeschriftung=1;
313 
314 		y2=(int)((h-SU-((double)(h-SU-SO)/(max-min)*(werte[werte.length-1]*faktor-min))));
315 		x2=SL;//(int)(((double)(b-SL-SR))/(werte.length)*(werte.length-1)+SL);
316 //		if (beschriftung && 0%abstandBeschriftung==0)
317 //		{
318 //			g.setColor(Color.darkGray);
319 //			g.drawString(format.format ( werte[werte.length-1] ),x2-10,y2-10);
320 //		}
321 		for(int i=1;i<werte.length;i++)
322 		{
323 			x1=x2;
324 			y1=y2;
325 			y2=(int)((h-SU-((double)(h-SU-SO)/(max-min)*(werte[werte.length-i-1]*faktor-min))));
326 			x2=(int)(((double)(b-SL-SR))/(werte.length)*(i)+SL);
327 //			y2=(int)((h-SU-((double)(h-SU-SO)/(max-min)*(werte[i]-min))));
328 //			x2=(int)(((double)(b-SL-SR))/(werte.length)*(i)+SL);
329 			g.setColor(farbe);
330                         //g.setComposite( REDUCEDALPHA );
331 			g.drawLine(x1,y1,x2,y2);
332                         g.drawLine(x1,y1+1,x2,y2+1);
333                         //g.setComposite( DEFAULTALPHA );
334 			if (beschriftung && i%abstandBeschriftung==0)
335 			{
336 				g.setColor(Color.darkGray);
337                                 int xpos = x2 - g.getFontMetrics ().stringWidth ( format.format ( werte[werte.length-i-1] ) ) / 2;
338                                 int ypos = y2 - g.getFont().getSize()/2;
339 				g.drawString(format.format ( werte[werte.length-i-1] ),xpos,ypos);
340 			}
341 		}
342             }
343 	}
344     
345 
346 	//Zeichnen eines Liniendiagrammes
347 	private void showXAchseBeschriftung(Graphics2D g, int b, int h)
348 	{
349             if ( m_clYAchseBeschriftung.length > 0 )
350             {
351                 int schriftbreite = (int)( ( g.getFontMetrics ().stringWidth ( m_clYAchseBeschriftung[0] ) + 10 ) * 1.5 );
352                 
353                 
354 		int x1,x2,y1,y2;
355 		int mengeBeschriftung=(int)((b-SL-SR)/schriftbreite);
356 		if (mengeBeschriftung==0) mengeBeschriftung=1;
357 		int abstandBeschriftung=(int)(m_clYAchseBeschriftung.length/mengeBeschriftung);
358 		if (abstandBeschriftung==0) abstandBeschriftung=1;
359 
360 		y2=this.getHeight ()-SU+25;
361 		x2=(int)(((double)(b-SL-SR))/(m_clYAchseBeschriftung.length)*(m_clYAchseBeschriftung.length-1)+SL);
362 //		if (0%abstandBeschriftung==0)
363 //		{
364 //                        g.setColor(Color.lightGray);
365 //                        g.drawLine(x2,this.getHeight ()-SU,x2,SO);
366 //                    
367 //			g.setColor(Color.black);
368 //			g.drawString( m_clYAchseBeschriftung[0], x2-20, y2 );
369 //                        g.drawLine ( x2, this.getHeight ()-SU-8, x2, this.getHeight ()-SU+8 );
370 //		}
371 		for(int i=0;i<m_clYAchseBeschriftung.length;i++)
372 		{
373 			x1=x2;
374 			y1=y2;
375 			y2=this.getHeight ()-SU+15;
376 			x2=(int)(((double)(b-SL-SR))/(m_clYAchseBeschriftung.length)*(m_clYAchseBeschriftung.length-i-1)+SL);
377 //			y2=(int)((h-SU-((double)(h-SU-SO)/(max-min)*(werte[i]-min))));
378 //			x2=(int)(((double)(b-SL-SR))/(werte.length)*(i)+SL);
379 			g.setColor(Color.black);
380                         //g.setComposite( REDUCEDALPHA );
381                         //g.setComposite( DEFAULTALPHA );
382 			if (i%abstandBeschriftung==0)
383 			{
384                             if ( hilfslinien )
385                             {
386                                 g.setColor(Color.lightGray);
387                                 g.drawLine(x2,this.getHeight ()-SU,x2,SO);
388                             }
389                             g.setColor(Color.black);
390                             int xpos = x2 - g.getFontMetrics ().stringWidth ( m_clYAchseBeschriftung[i] ) / 2;
391                             int ypos = y2 + g.getFont().getSize();
392                             g.drawString ( m_clYAchseBeschriftung[i], xpos, ypos );
393                             g.drawLine ( x2, this.getHeight ()-SU-8, x2, this.getHeight ()-SU+10 );
394 			}
395 		}
396             }
397 	}
398         
399         //Findet den Maximalwert in einem double-Array
400 	private double maxFinder( boolean usefaktor)
401 	{
402 		double max=1;
403 		for( int i=0; m_clStatistikModel != null && m_clStatistikModel.length > i && m_clStatistikModel[i] != null && i < m_clStatistikModel.length; i++ )
404 		{
405                     if ( m_clStatistikModel[i].isShow () )
406                     {
407                         if ( usefaktor )
408                         {
409                             if( m_clStatistikModel[i].getMaxValue () * m_clStatistikModel[i].getFaktor() > max ) max = m_clStatistikModel[i].getMaxValue () * m_clStatistikModel[i].getFaktor();
410                         }
411                         else
412                         {
413                             if( m_clStatistikModel[i].getMaxValue () > max ) max = m_clStatistikModel[i].getMaxValue ();
414                         }
415                     }
416 		}
417 		return (max);
418 	}
419 
420 	//Findet den Minimalwert in einem double-Array
421 	private double minFinder( boolean usefaktor )
422 	{
423 		double min=0;
424 		for( int i=0; m_clStatistikModel != null && m_clStatistikModel.length > i && m_clStatistikModel[i] != null && i < m_clStatistikModel.length; i++ )
425 		{
426                     if ( m_clStatistikModel[i].isShow () )
427                     {
428                         if ( usefaktor )
429                         {
430                             if( m_clStatistikModel[i].getMinValue () * m_clStatistikModel[i].getFaktor() < min ) min = m_clStatistikModel[i].getMinValue () * m_clStatistikModel[i].getFaktor();
431                         }
432                         else
433                         {
434                             if( m_clStatistikModel[i].getMinValue () < min ) min = m_clStatistikModel[i].getMinValue ();
435                         }
436                     }
437                 }
438 		return (min);
439 	}
440 
441         
442 	// gibt ein String zurück, der einen double-Wert mit n Nachkommastellen repräsentiert
443 	public String formatedDouble(double d, byte n)
444 	{
445 		
446 		String sHilf=Double.toString(d);
447 		String sResultValue=new String();
448 
449 
450 		int i=0;
451 		while((i<sHilf.length()) && (!(sHilf.charAt(i)=='.')))
452 		{
453 			sResultValue+=sHilf.charAt(i);
454 			i++;
455 		}
456 
457 		if(sHilf.charAt(i)=='.') 
458 		{
459 			sResultValue+=',';
460 			i++;
461 
462 			for(int j=0; (i<sHilf.length()) && (j<=n);j++)
463 			{
464 				sResultValue+=sHilf.charAt(i);
465 				i++;
466 				j++;
467 			}
468 		}
469 
470 		return (sResultValue);
471 	}
472 
473 }