IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

2D Java Discussion :

[jCharts] [débutant] rafraîchissement d'un graphe


Sujet :

2D Java

  1. #1
    Membre confirmé Avatar de calogerogigante
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    602
    Détails du profil
    Informations personnelles :
    Âge : 53
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2003
    Messages : 602
    Points : 497
    Points
    497
    Par défaut [jCharts] [débutant] rafraîchissement d'un graphe
    J'essaie d'apprendre les particularités de jCharts dans le but de l'intégrer à mon projet en cours.

    http://jcharts.krysalis.org/

    Je commence à bien comprendre comment doivent être organisés les différents composants d'un graphe (titre, légendes, données...) mais j'ai un problème de rafraîchissement.

    En effet, l'exemple ci-dessous dessine un graphe parfaitement, mais lorsque je retaille la fenêtre, le graphe disparaît et ne s'affiche plus.

    Comment dois-je programmer la création d'un graphe, pour que celui-ci soit toujours affiché quand un utilisateur va bouger la fenêtre de mon programme ?

    EXEMPLE jCharts :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    // TEST DES GRAPHES BAR
     
    import org.jCharts.axisChart.*;
    import org.jCharts.test.*;
    import org.jCharts.types.*;
    import org.jCharts.chartData.*;
    import org.jCharts.nonAxisChart.*;
    import org.jCharts.properties.*;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class SwingDemo2 extends JFrame
    {
    private JPanel panel;
     
    //*******************************************************************************
    public SwingDemo2() throws ChartDataException, PropertyException
      {
      initComponents();
      }
    //********************************************************************************/
    private void initComponents() throws ChartDataException, PropertyException
      {
      setTitle("Test SwingDemo 2");
      setSize( 600, 400 );
      panel=new JPanel( true ); // construit un panel qui est DoubbleBuffered
      panel.setSize( 600, 400 );
      panel.setBackground(Color.yellow);
      getContentPane().add( panel );
      setVisible( true );
     
      // --------------------------------------------------------------------------------
      String[] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" };
      String xAxisTitle= "Années";
      String yAxisTitle= "Problèmes";
      String title= "Succès dans l'étude de JAVA";
      DataSeries dataSeries = new DataSeries( xAxisLabels,
                                              xAxisTitle,
                                              yAxisTitle,
                                              title );
     
      double[][] data= new double[][]{ { 250, 45, -36, 66, 145, 80, 55  } };
      String[] legendLabels= { "Réussite" };
      Paint[] paints= TestDataGenerator.getRandomPaints( 1 );
      BarChartProperties barChartProperties= new BarChartProperties();
      AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data,
                                                               legendLabels,
                                                               paints,
                                                               ChartType.BAR,
                                                               barChartProperties );
      dataSeries.addIAxisPlotDataSet( axisChartDataSet );
     
      ChartProperties chartProperties= new ChartProperties();
      AxisProperties axisProperties= new AxisProperties();
      LegendProperties legendProperties= new LegendProperties();
      AxisChart axisChart= new AxisChart( dataSeries,
                                          chartProperties,
                                          axisProperties,
                                          legendProperties,
                                          500,
                                          350 );
     
      axisChart.setGraphics2D( (Graphics2D) panel.getGraphics() );
      axisChart.render();
     
      addWindowListener( new WindowAdapter()
               {
               public void windowClosing( WindowEvent e )
                  {
                  exitForm( e );
                  }
               }
            );
      }
    // **********************************************************************************
      private void exitForm( WindowEvent windowEvent )
      {
        System.exit( 0 );
      }
    //***********************************************************
      public static void main( String args[] ) throws ChartDataException, PropertyException
      {
      new SwingDemo2();
      }
    }

  2. #2
    Membre confirmé Avatar de calogerogigante
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    602
    Détails du profil
    Informations personnelles :
    Âge : 53
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2003
    Messages : 602
    Points : 497
    Points
    497
    Par défaut
    Bon, en ayant été potasser un peu la théorie, j'ai appris que les JComponent utilise la méthode paintComponent(Graphics g) pour redessiner leur élément.

    J'ai donc fait une modification à l'exemple ci-dessus. Mais cette fois-ci le problème est différent : on voit bien que le panel prend une couleur jaune, puis on voit bien aussi que le panel dessine le graphe sur le panel jaune, mais ensuite, on voit aussi hélas que le panel jaune est "redessiné" sur le graphe en 3ème étape... J'ai donc au final un écran tout jaune. Quand je retaille la fenêtre, on arrive à voir que les 3 étapes se reproduisent à nouveau...

    Comment faire alors ? Peut-être certains éléments ne sont pas dans le bon ordre ?

    Voici ma modification :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    // SwingDemo.java
     
    import org.jCharts.axisChart.*;
    import org.jCharts.test.*;
    import org.jCharts.types.*;
    import org.jCharts.chartData.*;
    import org.jCharts.nonAxisChart.*;
    import org.jCharts.properties.*;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class SwingDemo extends JFrame
    {
    private MonPanel panel;
     
    //*******************************************************************************
    public SwingDemo() throws ChartDataException, PropertyException
      {
      initComponents();
      }
    //********************************************************************************/
    private void initComponents() throws ChartDataException, PropertyException
      {
      setTitle("Test SwingDemo");
      setSize( 600, 400 );
      MonPanel panel=new MonPanel();
      panel.setSize( 600, 400 );
      panel.setBackground(Color.yellow);
     
    	panel.construireUnAxisChart();
     
      getContentPane().add( panel );
      setVisible( true );
     
      addWindowListener( new WindowAdapter()
               {
               public void windowClosing( WindowEvent e )
                  {
                  System.exit( 0 );
                  }
               }
            );
      }
     
    //***********************************************************
      public static void main( String args[] ) throws ChartDataException, PropertyException
      {
      new SwingDemo();
      }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    import org.jCharts.axisChart.*;
    import org.jCharts.test.*;
    import org.jCharts.types.*;
    import org.jCharts.chartData.*;
    import org.jCharts.nonAxisChart.*;
    import org.jCharts.properties.*;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class MonPanel extends JPanel
    {
    // CHAMPS
    AxisChart axisChart;
    //**********************************************************************
    public void construireUnAxisChart() throws ChartDataException, PropertyException
      {
      // -----------------------------------------------------------------------------
      String[] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" };
      String xAxisTitle= "Années";
      String yAxisTitle= "Problèmes";
      String title= "Réussites en JAVA";
      DataSeries dataSeries = new DataSeries( xAxisLabels,
                                              xAxisTitle,
                                              yAxisTitle,
                                              title );
     
      double[][] data= new double[][]{ { 650, 45, -36, 352, 145, 80, 420  } };
      String[] legendLabels= { "Données" };
      Paint[] paints= TestDataGenerator.getRandomPaints( 1 );
      BarChartProperties barChartProperties= new BarChartProperties();
      AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data,
                                                               legendLabels,
                                                               paints,
                                                               ChartType.BAR,
                                                               barChartProperties );
      dataSeries.addIAxisPlotDataSet( axisChartDataSet );
     
      ChartProperties chartProperties= new ChartProperties();
      AxisProperties axisProperties= new AxisProperties();
      LegendProperties legendProperties= new LegendProperties();
      axisChart= new AxisChart( dataSeries,
                                chartProperties,
                                axisProperties,
                                legendProperties,
                                550,
                                350 );	
      }
    //**********************************************************************
    public void paintComponent(Graphics g)
      {
      super.paintComponent(g);
     
      axisChart.setGraphics2D( (Graphics2D) this.getGraphics() );
    	try {
          axisChart.render();
    			}
    	catch (ChartDataException cde)
    	   {
    		 System.out.println( cde.toString() );
    		 }
    	catch (PropertyException pe)
    	   {
    		 System.out.println( pe.toString() );
    		 }
      }
    //**********************************************************************
    }

  3. #3
    Membre confirmé Avatar de calogerogigante
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    602
    Détails du profil
    Informations personnelles :
    Âge : 53
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2003
    Messages : 602
    Points : 497
    Points
    497
    Par défaut
    OK, c'est bon, j'ai résolu mon problème. Il fallait donner avec plus de précision une instance de Graphics2D, et cela, même dans la méthode paintComponent.

    Je mets la version qui gère parfaitement le rafraîchissement, en espérant que cela serve aux autres :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    // SwingDemo.java  
     
    import org.jCharts.axisChart.*;
    import org.jCharts.test.*;
    import org.jCharts.types.*;
    import org.jCharts.chartData.*;
    import org.jCharts.nonAxisChart.*;
    import org.jCharts.properties.*;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class SwingDemo extends JFrame
    {
    private MonPanel panel;
     
    //*******************************************************************************
    public SwingDemo() throws ChartDataException, PropertyException
      {
      initComponents();
      }
    //********************************************************************************/
    private void initComponents() throws ChartDataException, PropertyException
      {
      setTitle("Test Swing et JCharts");
      setSize( 600, 400 );
      MonPanel panel=new MonPanel();
      panel.setSize( 600, 400 );
      panel.construireUnAxisChart();
     
      getContentPane().add( panel, BorderLayout.CENTER );
      setVisible( true );
     
      addWindowListener( new WindowAdapter()
               {
               public void windowClosing( WindowEvent e )
                  {
                  System.exit( 0 );
                  }
               }
            );
      }
     
    //***********************************************************
      public static void main( String args[] ) throws ChartDataException, PropertyException
      {
      // En tout premier, on place le LookAndFeel
      try {
          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
          }
      catch (Exception e)
          {
          System.out.println(e.toString());
          }
     
      new SwingDemo();
      }
    }
    Et aussi

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    import org.jCharts.axisChart.*;
    import org.jCharts.test.*;
    import org.jCharts.types.*;
    import org.jCharts.chartData.*;
    import org.jCharts.nonAxisChart.*;
    import org.jCharts.properties.*;
     
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class MonPanel extends JPanel
    {
    // CHAMPS
    AxisChart axisChart;
     
    // Constructeur :
    public MonPanel()
      {
      super();
      setBackground(Color.yellow);
      }
    //**********************************************************************
    public void construireUnAxisChart() throws ChartDataException, PropertyException
      {
      // -----------------------------------------------------------------------------
      String[] xAxisLabels= { "1998", "1999", "2000", "2001", "2002", "2003", "2004" };
      String xAxisTitle= "Années";
      String yAxisTitle= "Problèmes";
      String title= "Réussites en JAVA";
      DataSeries dataSeries = new DataSeries( xAxisLabels,
                                              xAxisTitle,
                                              yAxisTitle,
                                              title );
     
      double[][] data= new double[][]{ { 650, 45, -36, 352, 145, 80, 420  } };
      String[] legendLabels= { "Données" };
      Paint[] paints= TestDataGenerator.getRandomPaints( 1 );
      BarChartProperties barChartProperties= new BarChartProperties();
      AxisChartDataSet axisChartDataSet= new AxisChartDataSet( data,
                                                               legendLabels,
                                                               paints,
                                                               ChartType.BAR,
                                                               barChartProperties );
      dataSeries.addIAxisPlotDataSet( axisChartDataSet );
     
      ChartProperties chartProperties= new ChartProperties();
      AxisProperties axisProperties= new AxisProperties();
      LegendProperties legendProperties= new LegendProperties();
      axisChart= new AxisChart( dataSeries,
                                chartProperties,
                                axisProperties,
                                legendProperties,
                                550,
                                350 );
      Graphics   g  = this.getGraphics();
      Graphics2D g2 = (Graphics2D) g;
     
      axisChart.setGraphics2D( g2) ;
      }
    //**********************************************************************
    public void paintComponent(Graphics g)
      {
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D) g;
     
      axisChart.setGraphics2D( g2) ;
      try {
          axisChart.render();
          }
      catch (ChartDataException cde)
         {
         System.out.println( cde.toString() );
         }
      catch (PropertyException pe)
         {
         System.out.println( pe.toString() );
         }
      }
    //**********************************************************************
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant] C/OCaml XML + graphe
    Par Yyukk dans le forum Caml
    Réponses: 8
    Dernier message: 16/04/2011, 21h23
  2. (débutant)Afficher un seul graphe sur quatre
    Par Tite-Phil dans le forum LabVIEW
    Réponses: 0
    Dernier message: 26/04/2010, 12h32
  3. Réponses: 1
    Dernier message: 09/06/2008, 18h38
  4. [Débutant] Affichage d'un Graph
    Par Carlitox dans le forum LabVIEW
    Réponses: 6
    Dernier message: 08/06/2007, 15h54
  5. [débutant]Représentation d'un graphe : besoin d'un avis
    Par Barbara_04 dans le forum Langage
    Réponses: 2
    Dernier message: 10/05/2007, 18h47

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo