Bonjour,

Je suis nouveau dans l'utilisation de JFreeChart, et je crois que je commence à comprendre un peu comment ça marche.

Mais j'ai une petite question "théorique" : quand on a besoin de mettre à jour un graphe illustré dans un ChartPanel, faut-il toujours impérativement passer par une modification du dataset ? C'est toujours comme ça qu'il faut travailler ?

J'ai fait un petit exemple complet qui semble fonctionner parfaitement : des JSpinners me permettent de mettre à jour mon camembert à 5 parties.

Le voici :

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
 
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
 
import org.jfree.chart.*;
import org.jfree.data.general.DefaultPieDataset;
 
public class Jfreesimple extends JFrame implements ChangeListener
{
    public ChartPanel chartPanel;
    public DefaultPieDataset dataset;
 
    SpinnerNumberModel model1 = new SpinnerNumberModel(10, 10, 100, 10);
    SpinnerNumberModel model2 = new SpinnerNumberModel(10, 10, 100, 10);
    SpinnerNumberModel model3 = new SpinnerNumberModel(10, 10, 100, 10);
    SpinnerNumberModel model4 = new SpinnerNumberModel(10, 10, 100, 10);
    SpinnerNumberModel model5 = new SpinnerNumberModel(10, 10, 100, 10);
 
    public JSpinner spin1 = new JSpinner(model1);
    public JSpinner spin2 = new JSpinner(model2);
    public JSpinner spin3 = new JSpinner(model3);
    public JSpinner spin4 = new JSpinner(model4);
    public JSpinner spin5 = new JSpinner(model5);
 
    public Jfreesimple()
    {
        JPanel lepanel = new JPanel();
 
        lepanel.setLayout(new BorderLayout());
 
        //création du chartPanel avec ses valeurs initiales :
        dataset = new DefaultPieDataset();
        dataset.setValue("Catégorie 1",  10.0);
        dataset.setValue("Catégorie 2",  10.0);
        dataset.setValue("Catégorie 3",  10.0);
        dataset.setValue("Catégorie 4",  10.0);
        dataset.setValue("Catégorie 5",  10.0);
 
        JFreeChart mongraphe = ChartFactory.createPieChart(
                "Mon 1er graphe bien compris", dataset,
                true, true, false );
 
        chartPanel = new ChartPanel(mongraphe, false);
 
        lepanel.add(chartPanel, BorderLayout.CENTER);
 
        // Ajout du panel avec les JSpinner :
        JPanel panelSpinners = new JPanel();
        panelSpinners.setLayout(new FlowLayout());
 
        spin1.addChangeListener(this);
        panelSpinners.add(new JLabel(" Catégorie 1 "));
        panelSpinners.add(spin1);
 
        spin2.addChangeListener(this);
        panelSpinners.add(new JLabel(" Catégorie 2 "));
        panelSpinners.add(spin2);
 
        spin3.addChangeListener(this);
        panelSpinners.add(new JLabel(" Catégorie 3 "));
        panelSpinners.add(spin3);
 
        spin4.addChangeListener(this);
        panelSpinners.add(new JLabel(" Catégorie 4 "));
        panelSpinners.add(spin4);
 
        spin5.addChangeListener(this);
        panelSpinners.add(new JLabel(" Catégorie 5 "));
        panelSpinners.add(spin5);
 
        lepanel.add(panelSpinners, BorderLayout.SOUTH);
 
        this.setContentPane(lepanel);
    }
 
    public static void main(String[] args)
    {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            }
        catch (Exception e)
            {
            System.out.println(e);
            }
        Jfreesimple applic = new Jfreesimple();
        applic.setTitle("Mon premier Graphe");
        applic.setSize(new Dimension(800,500));
        applic.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        applic.setVisible(true);
    }
 
    public void stateChanged(ChangeEvent e)
    {
        if( (JSpinner)(e.getSource()) == spin1)
        {
            dataset.setValue("Catégorie 1",  (Integer) spin1.getValue());
        }
 
        if( (JSpinner)(e.getSource()) == spin2)
        {
            dataset.setValue("Catégorie 2",  (Integer) spin2.getValue());
        }
 
        if( (JSpinner)(e.getSource()) == spin3)
        {
            dataset.setValue("Catégorie 3",  (Integer) spin3.getValue());
        }
 
        if( (JSpinner)(e.getSource()) == spin4)
        {
            dataset.setValue("Catégorie 4",  (Integer) spin4.getValue());
        }
 
        if( (JSpinner)(e.getSource()) == spin5)
        {
            dataset.setValue("Catégorie 5",  (Integer) spin5.getValue());
        }
    }
}
Est-ce que la philosophie d'utilisation de JfreeChart est correcte ainsi ?