Graphique en bâton

Contenu du snippet

Une classe permettant de créer des graphiques en bâton , plusieurs mises en forme sont disponnibles ( avec ou sans repères/légendes , couleurs déradée ou unies , couleurs différentes pour chaque bâton ou non , valeures inscrites au dessu des batons ou pas ....) , ce n'est rien d'extraordinaire mais ca montre quand meme l'utilisation de la methode paint(Graphics g) et la converstion en Graphics2D.

Certainement à ameliorer quand j'aurais plus de temps .

Source / Exemple :


/*

  • @author Tlaloc
  • /
import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JPanel; public class Graph extends JPanel{ //le type de graphique que l'on souhaite public static final int VALUES_ONLY = 0; public static final int VALUES_AND_STRING = 1; public static final int VALUES_STRING_AND_COLOR = 2; public static final int VALUES_STRING_COLOR_AND_LADDER = 3; public static final int VALUES_AND_COLOR = 4; public static final int VALUES_AND_LADDER = 5; //les proprietées private int CURRENT_TYPE = 0; private Object[] VALUES = null ; private Object[][] _VALUES = null ; private Object[][] LADDER = null ; public Graph(){ super(); setOpaque ( true ); setBackground(Color.WHITE); setSize(800,600); setBounds(0,0,800,600); setLayout(null); } public void createGraph(){ repaint(); } @Override public void paint(Graphics g){ //pour avoir le fonds blanc g.setColor(Color.WHITE); g.fillRect(0, 0, getSize().width, getSize().height); //on place la légende avant d'ajouter les valeurs try{ if(CURRENT_TYPE == VALUES_STRING_COLOR_AND_LADDER || CURRENT_TYPE == VALUES_AND_LADDER ){ for ( int i = 0 ; i < LADDER.length ; i++){ try{ g.setColor(Color.RED); g.drawLine(0, getSize().height-(Integer)LADDER[i][0],getSize().width , getSize().height-(Integer)LADDER[i][0]); g.setColor(Color.BLACK); g.drawString( LADDER[i][1].toString(), 10,getSize().height-(Integer)LADDER[i][0]); } catch(Exception e){System.out.println("!");} } } } catch (NullPointerException e){ System.err.println( "Missing type Exception_ Veuillez renseigner le type de graphique"); e.printStackTrace(); return ; } //on dessine apres les valeurs en fonction de la demande if ( CURRENT_TYPE == VALUES_ONLY || CURRENT_TYPE == VALUES_AND_LADDER){ //recuperation de la valeur maximale des stats a traiter int max = getMaxValue(true); //on recupere le rapport proportionel valeur / hauteur du graph float rapport = (float)((float)(getSize().height-50 )/ (float)max); //calcul de la position x des batons int sizeX = ((getSize().width)-((VALUES.length*20)+20))/VALUES.length; //convertion du graphics en grahics2D pour l'effet de dégradé Graphics2D g2 = (Graphics2D)g; for ( int i = 0 ; i < VALUES.length ; i ++ ){ int locationY = (getSize().height-(int)(rapport * (Integer)VALUES[i])); int locationX = (i*sizeX)+(i*20)+20; g2.setPaint(new GradientPaint(0,0,Color.BLUE,getSize().width,getSize().height,Color.GREEN,true)); g2.fillRect( locationX , locationY , sizeX, 600 ); g.setColor(Color.BLACK); g.drawRect(locationX , locationY , sizeX, 600 ); } } if (CURRENT_TYPE == VALUES_AND_STRING){ //recuperation de la valeur maximale des stats a traiter int max = getMaxValue(false); //on recupere le rapport proportionel valeur / hauteur du graph float rapport = (float)((float)(getSize().height-50 )/ (float)max); //calcul de la position x des batons int sizeX = ((getSize().width)-((_VALUES.length*20)+20))/_VALUES.length; //convertion du graphics en grahics2D pour l'effet de dégradé Graphics2D g2 = (Graphics2D)g; for ( int i = 0 ; i < _VALUES.length ; i ++ ){ int locationY = (getSize().height-(int)(rapport * (Integer)_VALUES[i][0])); int locationX = (i*sizeX)+(i*20)+20; g2.setPaint(new GradientPaint(0,0,Color.BLUE,getSize().width,getSize().height,Color.GREEN,true)); g2.fillRect( locationX , locationY , sizeX, 600 ); g.setColor(Color.BLACK); g.drawRect(locationX , locationY , sizeX, 600 ); g.drawString(_VALUES[i][1].toString(), locationX, locationY-10); } } if(CURRENT_TYPE == VALUES_STRING_AND_COLOR || CURRENT_TYPE == VALUES_STRING_COLOR_AND_LADDER ){ //recuperation de la valeur maximale des stats a traiter int max = getMaxValue(false); //on recupere le rapport proportionel valeur / hauteur du graph float rapport = (float)((float)(getSize().height-50 )/ (float)max); //calcul de la position x des batons int sizeX = ((getSize().width)-((_VALUES.length*20)+20))/_VALUES.length; for ( int i = 0 ; i < _VALUES.length ; i ++ ){ int locationY = (getSize().height-(int)(rapport * (Integer)_VALUES[i][0])); int locationX = (i*sizeX)+(i*20)+20; g.setColor((Color)_VALUES[i][2]); g.fillRect(locationX , locationY , sizeX, 600 ); g.setColor(Color.BLACK); g.drawRect(locationX , locationY , sizeX, 600 ); g.drawString(_VALUES[i][1].toString(), locationX, locationY-10); } } if(CURRENT_TYPE == VALUES_AND_COLOR){ //recuperation de la valeur maximale des stats a traiter int max = getMaxValue(false); //on recupere le rapport proportionel valeur / hauteur du graph float rapport = (float)((float)(getSize().height-50 )/ (float)max); //calcul de la position x des batons int sizeX = ((getSize().width)-((_VALUES.length*20)+20))/_VALUES.length; for ( int i = 0 ; i < _VALUES.length ; i ++ ){ int locationY = (getSize().height-(int)(rapport * (Integer)_VALUES[i][0])); int locationX = (i*sizeX)+(i*20)+20; g.setColor((Color)_VALUES[i][1]); g.fillRect(locationX , locationY , sizeX, 600 ); g.setColor(Color.BLACK); g.drawRect(locationX , locationY , sizeX, 600 ); } } } private int getMaxValue(boolean isSingleTable){ if (isSingleTable){ int max = 0 ; for ( int i = 0 ; i < VALUES.length ; i ++ ){ if( (Integer)VALUES[i] > max){ max = (Integer)VALUES[i]; } } return max ; } else{ int max = 0 ; for ( int i = 0 ; i < _VALUES.length ; i ++ ){ if( (Integer)_VALUES[i][0] > max){ max = (Integer)_VALUES[i][0]; } } return max ; } } //les getters public int getType(){ return CURRENT_TYPE ; } public Object[] getValues(){ return VALUES ; } public Object[][]getMiltipleValues(){ return _VALUES; } public Object[] getLadder(){ return LADDER; } //les setters public void setType(int type){ CURRENT_TYPE = type ; } public void setValues( Object [] values){ VALUES = values ; } public void setValues( Object [][] values){ _VALUES = values ; } public void setLadder(Object [][] ladder){ LADDER = ladder ; } }

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.