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 :

[JFreeChart] afficher plusieurs courbes selon le nombre récupéré


Sujet :

2D Java

  1. #1
    Membre averti Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Points : 355
    Points
    355
    Par défaut [JFreeChart] afficher plusieurs courbes selon le nombre récupéré
    Bonjour,

    Je tente de faire plusieurs courbes correspondant à chaque mesure de glycemie du patient issue d'une bdd.

    Le probleme, est que je ne sais pas comment créer un TimesSeries à chaque id du patient.
    un patient à ses propres mesures et moi, je rajoute tous dans un seul Timesseries ce qui fait que j'ai une seule courbe, voir pas du tout quand les coordonnées se croisent.
    En effet chaque TimesSeries doit etre rajouté au TimeSeriesCollection.

    Comment créer un TimesSeries pour chaque patient ?

    voici le code de mon graphique:
    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
     
    public GraphiqueEtatPatient(){
    		JPanel p = new JPanel();
     
    		// Couleurs des lignes
    		Color COULEURLIGNE1 = Color.black;
    		//Color COULEURLIGNE2 = Color.red;
     
    		// Creation du graphique
    		chart = ChartFactory.createTimeSeriesChart("Taux de glycémie des patients","Période","Taux de glycémie",dataSet,true,true,false);
    		chartPanel = new ChartPanel(chart, true);
     
    		// Les dimensions du graphique
    		Dimension d = new Dimension(350,250);
    		chartPanel.setMaximumSize(d); 
    		chartPanel.setPreferredSize(d); 
    		chartPanel.setMinimumSize(d);
    		p.add(chartPanel);
     
    		plot = (XYPlot) chart.getPlot(); 
    		// On definie une couleur pour les lignes
    		plot.getRenderer().setSeriesPaint(0,COULEURLIGNE1); 
    		//plot.getRenderer().setSeriesPaint(1,COULEURLIGNE2); 
     
    		// On definie une couleur de fond pour le graphique
    		plot.setBackgroundPaint(Color.white);
    		rangeAxis = (NumberAxis) plot.getRangeAxis();
     
    		// On fixe une taille pour l'axe des ordonnées
    		rangeAxis.setUpperBound(5.0);
    		add(p);
    		try {
    			connection = DriverManager.getConnection(URL + USER + PWD);
    			Statement stmt = connection.createStatement();
    			ResultSet rs = stmt.executeQuery("SELECT mesure.*, ref, nom FROM mesure, patient WHERE id_patient=ref");
    			int nbreMesure = 0;
    			int day, month, year;
     
    			// Creation des lignes
    			TimeSeries mesure_patient = null;
    			//series_ligne_2 = new TimeSeries("Patient 2", Hour.class);
    			dataSet.removeAllSeries();
    			while (rs.next()){
    				String nom_patient = (String)rs.getObject(10);
    				System.out.println("nom_patient: "+nom_patient);
    				mesure_patient = new TimeSeries(nom_patient, Hour.class);
    				if (rs.getObject(8).equals(rs.getObject(9))){
    					nbreMesure++;
    					System.out.println("nbreMesure: "+nbreMesure);
     
    					date=(String) rs.getObject(4);
    					heure=(String) rs.getObject(6);
    					glycemie=(Double) rs.getObject(5);
     
    					hour= Integer.parseInt(heure.substring(0,2));
    					System.out.println("heur: "+hour);
    					day = Integer.parseInt(date.substring(0,2));
    					System.out.println("dat: "+day);
    					month = Integer.parseInt(date.substring(3,5));
    					System.out.println("mois: "+month);
    					year = Integer.parseInt(date.substring(6,10));
    					System.out.println("annee: "+year);
    					System.out.println("glycemie: "+glycemie);
     
    					mesure_patient.setNotify(true);
    					mesure_patient.add(new Hour(nbreMesure,day,month,year), glycemie);
    				}
    			}
    			// Ajout des ligne dans le dataset
    			dataSet.addSeries(mesure_patient);
    			//dataSet.addSeries(series_ligne_2);
    			chart.setNotify(true);
    			chartPanel.setChart(chart);	
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		this.setVisible(true);
    	}
    }

  2. #2
    Membre averti Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Points : 355
    Points
    355
    Par défaut
    créer un TimesSeries par patient suivant le nombre de patient dans ma table, ca doit pas etre compliqué quand meme...?
    Comment créer une instance TimesSeries suivant le nombre de patient ?

  3. #3
    Membre averti Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Points : 355
    Points
    355
    Par défaut
    Après quelques rectification, j'ai obtenu un graphique sans courbes mais avec les legendes des patients ce qui est plutôt pas mal.

    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
    121
    122
    123
    124
    125
    126
     
    package vue;
     
    import ...;
     
    @SuppressWarnings("serial")
    public class GraphiqueEtatPatient extends JPanel{
     
    	private Connection connection;
    	private final String URL = "jdbc:derby://localhost:1527/DiabetoMedical";
    	private final String USER = ";user=user";
    	private final String PWD = ";password=user";
    	private double glycemie=1.80;
    	NumberAxis rangeAxis;
     
    	ChartPanel chartPanel;
    	JPanel p;
    	JFreeChart chart;
     
    	RegularTimePeriod rtp;
     
    	TimeSeriesCollection dataSet = new TimeSeriesCollection();
    	XYPlot plot;
    	Fenetre fen;
    	int hour = 0;
    	int nbreMesure = 0;
    	private String date;
    //	private Double mixtard30, axtrapide;
     
     
    	public GraphiqueEtatPatient(){
    		p = new JPanel();
     
    		// Couleurs des lignes
    		Color COULEURLIGNE1 = Color.black;
    		//Color COULEURLIGNE2 = Color.red;
     
    		// Creation du graphique
    		chart = ChartFactory.createTimeSeriesChart("Taux de glycémie des patients","Période","Taux de glycémie",dataSet,true,true,false);
    		chartPanel = new ChartPanel(chart, true);
    		System.out.println("ici");
    		try {
    			connection = DriverManager.getConnection(URL + USER + PWD);
     
    			int nbreMesure = 0;
    			int day, month, year;
    			// Creation des lignes
    			//TimeSeries mesure_patient = null;
    			//series_ligne_2 = new TimeSeries("Patient 2", Hour.class);
    			dataSet.removeAllSeries();
    			String nom_patient="";
    			Statement stm = connection.createStatement();
    			ResultSet res = stm.executeQuery("SELECT count(ref) FROM patient");
    			int count = 0;
    			while (res.next()){
    				count= res.getInt(1);
    			}
    			System.out.println("count: "+count);
    			int r = 1;
     
    			Statement stmt = connection.createStatement();
    			ResultSet rs = stmt.executeQuery("SELECT mesure.*, ref, nom FROM mesure, patient WHERE id_patient=ref");
    			while (rs.next()){
    					System.out.println("r"+r);
    				//mesure_patient = new TimeSeries(nom_patient, Hour.class);
    				if (rs.getInt(8)==r){
    					nom_patient = (String)rs.getObject(9);
    					System.out.println("nom_patient: "+nom_patient);
    					int id_patient = (int)rs.getInt(7);
    					System.out.println("id_patient: "+id_patient);
    					nbreMesure++;
    					System.out.println("nbreMesure: "+nbreMesure);
     
    					date=(String) rs.getObject(4);
    					glycemie=(Double) rs.getObject(5);
     
    					day = Integer.parseInt(date.substring(0,2));
    					System.out.println("dat: "+day);
    					month = Integer.parseInt(date.substring(3,5));
    					System.out.println("mois: "+month);
    					year = Integer.parseInt(date.substring(6,10));
    					System.out.println("annee: "+year);
    					System.out.println("glycemie: "+glycemie);
     
    					new TimeSeries(nom_patient, Hour.class).setNotify(true);
    					new TimeSeries(nom_patient, Hour.class).add(new Hour(nbreMesure,day,month,year), glycemie);
    				}else{
    						// Ajout des ligne dans le dataset
    						dataSet.addSeries(new TimeSeries(nom_patient, Hour.class));
    						//dataSet.addSeries(series_ligne_2);
    						r++;
    				}
    			}
    			// Ajout des ligne dans le dataset
    			dataSet.addSeries(new TimeSeries(nom_patient, Hour.class));
    			//dataSet.addSeries(series_ligne_2);
    			chart.setNotify(true);
    			chartPanel.setChart(chart);	
    			chartPanel.removeAll();
    			chartPanel.repaint();
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		// Les dimensions du graphique
    		Dimension d = new Dimension(350,250);
    		chartPanel.setMaximumSize(d); 
    		chartPanel.setPreferredSize(d); 
    		chartPanel.setMinimumSize(d);
    		p.add(chartPanel);
     
    		plot = (XYPlot) chart.getPlot(); 
    		// On definie une couleur pour les lignes
    		plot.getRenderer().setSeriesPaint(0,COULEURLIGNE1); 
    		//plot.getRenderer().setSeriesPaint(1,COULEURLIGNE2); 
     
    		// On definie une couleur de fond pour le graphique
    		plot.setBackgroundPaint(Color.white);
    		rangeAxis = (NumberAxis) plot.getRangeAxis();
     
    		// On fixe une taille pour l'axe des ordonnées
    		rangeAxis.setUpperBound(5.0);
    		add(p);
     
    		this.setVisible(true);
    	}
    }
    A en croire l'éxécution du graphique, je me suis apercu qu'il s'éxécutait 3 fois de suite.
    Je suppose que cela reprend les memes TimeSeries et récrit par dessus ce qui fait que je n'apercois pas les courbes dans le graphique mais plutot leur légendes.
    La question est:
    Pourquoi cette classe s'éxécutent-elles 3 fois de suite et empechent d'afficher les courbes ?

    s'il y'a un génie en JFreechart, c'est ici qu'il faut répondre.

  4. #4
    Membre averti Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Points : 355
    Points
    355
    Par défaut
    toujours aucun génie
    Bon, autre question:
    Quelqu'un aurait-il un exemple de LineChart qui affiche plusieurs?
    Ou bien un autre graphique qui affiche plusieurs courbes ?

    Ca m'aiderait enormement.
    merci

  5. #5
    Membre du Club
    Inscrit en
    Décembre 2007
    Messages
    64
    Détails du profil
    Informations forums :
    Inscription : Décembre 2007
    Messages : 64
    Points : 64
    Points
    64
    Par défaut
    Une idée comme ca, mais as-tu essayé en appelant :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    chart = ChartFactory.createTimeSeriesChart("Taux de glycémie des patients","Période","Taux de glycémie",dataSet,true,true,false);
    après avoir ajouté tes séries dans ton dataset ?

  6. #6
    Membre averti Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Points : 355
    Points
    355
    Par défaut
    non, ca ne marche pas.

  7. #7
    Expert éminent

    Femme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    5 793
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 5 793
    Points : 7 778
    Points
    7 778
    Par défaut
    Tu as un exemple de TimeSeriesChart avec deux courbes dans le jar de demo de JFreeChart, soit TimeSeriesDemo1 :
    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
    import java.awt.Color;
    import java.awt.Dimension;
    import java.text.SimpleDateFormat;
    import javax.swing.JPanel;
    import org.jfree.chart.*;
    import org.jfree.chart.axis.DateAxis;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
    import org.jfree.data.time.*;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.ui.*;
     
    public class TimeSeriesDemo1 extends ApplicationFrame
    {
     
        public TimeSeriesDemo1(String s)
        {
            super(s);
            XYDataset xydataset = createDataset();
            JFreeChart jfreechart = createChart(xydataset);
            ChartPanel chartpanel = new ChartPanel(jfreechart, false);
            chartpanel.setPreferredSize(new Dimension(500, 270));
            chartpanel.setMouseZoomable(true, false);
            setContentPane(chartpanel);
        }
     
        private static JFreeChart createChart(XYDataset xydataset)
        {
            JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Legal & General Unit Trust Prices", "Date", "Price Per Unit", xydataset, true, true, false);
            jfreechart.setBackgroundPaint(Color.white);
            XYPlot xyplot = (XYPlot)jfreechart.getPlot();
            xyplot.setBackgroundPaint(Color.lightGray);
            xyplot.setDomainGridlinePaint(Color.white);
            xyplot.setRangeGridlinePaint(Color.white);
            xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
            xyplot.setDomainCrosshairVisible(true);
            xyplot.setRangeCrosshairVisible(true);
            org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = xyplot.getRenderer();
            if(xyitemrenderer instanceof XYLineAndShapeRenderer)
            {
                XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyitemrenderer;
                xylineandshaperenderer.setBaseShapesVisible(true);
                xylineandshaperenderer.setBaseShapesFilled(true);
            }
            DateAxis dateaxis = (DateAxis)xyplot.getDomainAxis();
            dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
            return jfreechart;
        }
     
        private static XYDataset createDataset()
        {
            TimeSeries timeseries = new TimeSeries("L&G European Index Trust", org.jfree.data.time.Month.class);
            timeseries.add(new Month(2, 2001), 181.80000000000001D);
            timeseries.add(new Month(3, 2001), 167.30000000000001D);
            timeseries.add(new Month(4, 2001), 153.80000000000001D);
            timeseries.add(new Month(5, 2001), 167.59999999999999D);
            timeseries.add(new Month(6, 2001), 158.80000000000001D);
            timeseries.add(new Month(7, 2001), 148.30000000000001D);
            timeseries.add(new Month(8, 2001), 153.90000000000001D);
            timeseries.add(new Month(9, 2001), 142.69999999999999D);
            timeseries.add(new Month(10, 2001), 123.2D);
            timeseries.add(new Month(11, 2001), 131.80000000000001D);
            timeseries.add(new Month(12, 2001), 139.59999999999999D);
            timeseries.add(new Month(1, 2002), 142.90000000000001D);
            timeseries.add(new Month(2, 2002), 138.69999999999999D);
            timeseries.add(new Month(3, 2002), 137.30000000000001D);
            timeseries.add(new Month(4, 2002), 143.90000000000001D);
            timeseries.add(new Month(5, 2002), 139.80000000000001D);
            timeseries.add(new Month(6, 2002), 137D);
            timeseries.add(new Month(7, 2002), 132.80000000000001D);
            TimeSeries timeseries1 = new TimeSeries("L&G UK Index Trust", org.jfree.data.time.Month.class);
            timeseries1.add(new Month(2, 2001), 129.59999999999999D);
            timeseries1.add(new Month(3, 2001), 123.2D);
            timeseries1.add(new Month(4, 2001), 117.2D);
            timeseries1.add(new Month(5, 2001), 124.09999999999999D);
            timeseries1.add(new Month(6, 2001), 122.59999999999999D);
            timeseries1.add(new Month(7, 2001), 119.2D);
            timeseries1.add(new Month(8, 2001), 116.5D);
            timeseries1.add(new Month(9, 2001), 112.7D);
            timeseries1.add(new Month(10, 2001), 101.5D);
            timeseries1.add(new Month(11, 2001), 106.09999999999999D);
            timeseries1.add(new Month(12, 2001), 110.3D);
            timeseries1.add(new Month(1, 2002), 111.7D);
            timeseries1.add(new Month(2, 2002), 111D);
            timeseries1.add(new Month(3, 2002), 109.59999999999999D);
            timeseries1.add(new Month(4, 2002), 113.2D);
            timeseries1.add(new Month(5, 2002), 111.59999999999999D);
            timeseries1.add(new Month(6, 2002), 108.8D);
            timeseries1.add(new Month(7, 2002), 101.59999999999999D);
            TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
            timeseriescollection.addSeries(timeseries);
            timeseriescollection.addSeries(timeseries1);
            return timeseriescollection;
        }
     
        public static JPanel createDemoPanel()
        {
            JFreeChart jfreechart = createChart(createDataset());
            return new ChartPanel(jfreechart);
        }
     
        public static void main(String args[])
        {
            TimeSeriesDemo1 timeseriesdemo1 = new TimeSeriesDemo1("Time Series Demo 1");
            timeseriesdemo1.pack();
            RefineryUtilities.centerFrameOnScreen(timeseriesdemo1);
            timeseriesdemo1.setVisible(true);
        }
    }

  8. #8
    Membre à l'essai
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    8
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 8
    Points : 10
    Points
    10
    Par défaut
    Ce code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    new TimeSeries(nom_patient, Hour.class).add(new Hour(nbreMesure,day,month,year), glycemie);
    ...il crée un nouveau TimeSeries objet, puis il ajoute une observation au TimeSeries, mais le TimeSeries vous n'assignez jemais.

    Ce code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    dataSet.addSeries(new TimeSeries(nom_patient, Hour.class));
    ...il ajoute un nouveau TimeSeries object, mais vous n'ajoutez jamais les observations au TimeSeries.

    Pouvez-vous voir le probleme maintenant?

    (S’il vous plaît excusez mon français, je suis Néo-Zélandais).

  9. #9
    Membre averti Avatar de mouss4rs
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    884
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 884
    Points : 355
    Points
    355
    Par défaut
    NON , je n'ai pas compris du tout.
    Pour vous, qu'est ce que je devrai écrire ?

    merci

Discussions similaires

  1. Afficher plusieurs courbes sur un état
    Par AmineBadry dans le forum IHM
    Réponses: 1
    Dernier message: 12/08/2013, 14h56
  2. centrer plusieurs images selon leur nombre
    Par Rafapouf dans le forum Mise en page CSS
    Réponses: 4
    Dernier message: 30/10/2012, 10h06
  3. Je peux afficher plusieurs courbes avec chart?
    Par makin_toch dans le forum ASP.NET
    Réponses: 1
    Dernier message: 25/04/2011, 14h20
  4. [Débutant] Afficher plusieurs courbes (Hold on ne marche pas)
    Par jparc dans le forum MATLAB
    Réponses: 9
    Dernier message: 28/03/2011, 17h09
  5. Réponses: 6
    Dernier message: 05/03/2010, 22h07

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