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] Line Chart Multi Axes


Sujet :

2D Java

  1. #1
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut [JFreeChart] Line Chart Multi Axes
    salut,
    j'utilise Jfreechart pour obtenir des courbes,
    Comment je peut modifier mon code pour avoir un "LINE CHART" avec un axe pour chaque série(variable) car chaque série varie dans une plage de valeur différente des autres
    voici mon code
    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
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
     
    package multiple;
     
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
     
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    //import java.text.DecimalFormat;
     
    public class LineChartTests extends ApplicationFrame {
    	public LineChartTests(String title) {
    	super(title);
    		CategoryDataset dataset = createDataset();
    			JFreeChart chart = createChart(dataset);
    			// add the chart to a panel...
    			ChartPanel chartPanel = new ChartPanel(chart);
    			chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    			setContentPane(chartPanel);
    	}
    	private String url= "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/pfe.mdb";
    	private String user = "user";
    	private String pass = "pass";
    	private String []temps = new String [100];
    	private float[] Traf= new float [100];
    	private float[] SRVLINES= new float [100];
    	private float[] CULC= new float [100];
    	private int y=0;
     
    	private CategoryDataset createDataset() {
    		// row keys...
    		String series1 = "TGRP:TRAFCARR";
    		String series2 = "TGRP:SRVLINES";
    		String series3 = "TGRP:CULC";
     
    //		--connection à la base donnée
    		try {
    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
    			}
    			catch(ClassNotFoundException e) {
    			System.err.println("Erreur de chargement du driver : + e") ;
    			}
    			try {
    				Connection connect = DriverManager.getConnection(url,user,pass) ;
    				Statement statement = connect.createStatement() ;
    				String Str="2007/05/20";
    				String Str02="MSC09:TBTN16";
     
     
     
    	       		String query = "select \"TGRP:TRAFCARR\",\"TGRP:SRVLINES\",\"TGRP:CULC\",Time from TGRPDetailed where (Date=\'"+Str+"\' and Object=\'"+Str02+"\') ";		       		
    	       		//String query = "select \"TGRP:TRAFCARR\",Time from TGRPDetailed where (Date=\'"+Str+"\' and Object=\'"+Str02+"\') ";
    	       		ResultSet resultset = statement.executeQuery(query);				
    				while (resultset.next()!= false){				 	
    					 String Str1 = resultset.getString(1);
    					 String Str2 = resultset.getString(2);
    					 String Str3 = resultset.getString(3);
    					 String Str4 = resultset.getString(4);
    					 Traf[y]= Float.parseFloat(Str1);
    					 SRVLINES[y]= Float.parseFloat(Str2);
    					 CULC[y]= Float.parseFloat(Str3);
    					 temps[y]=Str4;
    					 y++;
    						} 	
    				System.out.println(y);
    			connect.close();// fermuture de la connection à la BD
    				}
    			catch(SQLException sqle) {
    			System.err.println("Erreur lors de la connexion : " + sqle) ;
    			}
     
    		//column keys...
     
     
    		//String type1 = "Type 1";
    		//String type2 = "Type 2";
    		//String type3 = "Type 3";
    		//String type4 = "Type 4";
    		//String type5 = "Type 5";
    		//String type6 = "Type 6";
    		//String type7 = "Type 7";
    		//String type8 = "Type 8";
     
    		// create the dataset...
     
     
     
    		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    		for (int i=0;i<y;i++){
    			dataset.addValue(Traf[i], series1, temps[i]);
    			dataset.addValue(SRVLINES[i], series2, temps[i]);
    			dataset.addValue(CULC[i], series3, temps[i]);
    		}
     
    		//dataset.addValue(5.0, series2, type1);
    		/*
    		dataset.addValue(7.0, series2, type2);
    		dataset.addValue(6.0, series2, type3);
    		dataset.addValue(8.0, series2, type4);
    		dataset.addValue(4.0, series2, type5);
    		dataset.addValue(4.0, series2, type6);
    		dataset.addValue(2.0, series2, type7);
    		dataset.addValue(1.0, series2, type8);
    		dataset.addValue(4.0, series3, type1);
    		dataset.addValue(3.0, series3, type2);
    		dataset.addValue(2.0, series3, type3);
    		dataset.addValue(3.0, series3, type4);
    		dataset.addValue(6.0, series3, type5);
    		dataset.addValue(3.0, series3, type6);
    		dataset.addValue(4.0, series3, type7);
    		dataset.addValue(3.0, series3, type8);
    		*/
    		return dataset;
    	}
    	private JFreeChart createChart(CategoryDataset dataset) {
    		// create the chart...
    		JFreeChart chart = ChartFactory.createLineChart(
    				"Line Chart Demo 1", // chart title
    				"Type", // domain axis label
    				"Value", // range axis label
    				dataset, // data
    				PlotOrientation.VERTICAL, // orientation
    				true, // include legend
    				true, // tooltips
    				false // urls
    		);
    		/*
    		// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
    		StandardLegend legend = (StandardLegend) chart.getLegend();
    		legend.setDisplaySeriesShapes(true);
    		legend.setDisplaySeriesLines(true);
    		chart.setBackgroundPaint(new Color(0xCC, 0xCC, 0xFF));
    		CategoryPlot plot = chart.getCategoryPlot();
    		// customise the range axis...
    		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    		rangeAxis.setAutoRangeIncludesZero(true);
    		rangeAxis.setUpperMargin(0.20);
    		rangeAxis.setLabelAngle(Math.PI / 2.0);
    		//customise the renderer...
    		LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    		renderer.setDrawOutlines(true);
    		renderer.setSeriesStroke(0, new BasicStroke(2.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND,
    				1.0f,new float[] {10.0f, 6.0f},0.0f));
    		renderer.setSeriesStroke(1, new BasicStroke(2.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND,
    				1.0f,new float[] {6.0f, 6.0f},0.0f));
    		renderer.setSeriesStroke(2, new BasicStroke(2.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND,
    				1.0f,new float[] {2.0f, 6.0f},0.0f));
    		renderer.setItemLabelsVisible(true);
    		renderer.setPositiveItemLabelPosition(new ItemLabelPosition());
    		renderer.setNegativeItemLabelPosition(new ItemLabelPosition());
    		// OPTIONAL CUSTOMISATION COMPLETED.
    		 * 
    		 */
    		return chart;
    	}
    	public static void main(String[] args) {
    		LineChartTests demo = new LineChartTests("Line Chart Demo");
    		demo.pack();
    		RefineryUtilities.centerFrameOnScreen(demo);
    		demo.setVisible(true);
    	}
    }

  2. #2
    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 devrais t'inspirer de la classe MultipleAxisDemo1 présente dans le jar de demo de JFreeChart dont le code est le suivant :
    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
    import java.awt.Color;
    import java.awt.Dimension;
    import javax.swing.JPanel;
    import org.jfree.chart.*;
    import org.jfree.chart.axis.*;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
    import org.jfree.chart.renderer.xy.XYItemRenderer;
    import org.jfree.chart.title.TextTitle;
    import org.jfree.data.time.*;
    import org.jfree.data.xy.XYDataset;
    import org.jfree.ui.*;
     
    public class MultipleAxisDemo1 extends ApplicationFrame
    {
     
        public MultipleAxisDemo1(String s)
        {
            super(s);
            JFreeChart jfreechart = createChart();
            ChartPanel chartpanel = new ChartPanel(jfreechart);
            chartpanel.setPreferredSize(new Dimension(600, 270));
            chartpanel.setDomainZoomable(true);
            chartpanel.setRangeZoomable(true);
            setContentPane(chartpanel);
        }
     
        private static JFreeChart createChart()
        {
            XYDataset xydataset = createDataset("Series 1", 100D, new Minute(), 200);
            JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("Multiple Axis Demo 1", "Time of Day", "Primary Range Axis", xydataset, true, true, false);
            jfreechart.setBackgroundPaint(Color.white);
            jfreechart.addSubtitle(new TextTitle("Four datasets and four range axes."));
            XYPlot xyplot = (XYPlot)jfreechart.getPlot();
            xyplot.setOrientation(PlotOrientation.VERTICAL);
            xyplot.setBackgroundPaint(Color.lightGray);
            xyplot.setDomainGridlinePaint(Color.white);
            xyplot.setRangeGridlinePaint(Color.white);
            xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
            xyplot.getRangeAxis().setFixedDimension(15D);
            XYItemRenderer xyitemrenderer = xyplot.getRenderer();
            xyitemrenderer.setPaint(Color.black);
            NumberAxis numberaxis = new NumberAxis("Range Axis 2");
            numberaxis.setFixedDimension(10D);
            numberaxis.setAutoRangeIncludesZero(false);
            numberaxis.setLabelPaint(Color.red);
            numberaxis.setTickLabelPaint(Color.red);
            xyplot.setRangeAxis(1, numberaxis);
            xyplot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
            XYDataset xydataset1 = createDataset("Series 2", 1000D, new Minute(), 170);
            xyplot.setDataset(1, xydataset1);
            xyplot.mapDatasetToRangeAxis(1, 1);
            StandardXYItemRenderer standardxyitemrenderer = new StandardXYItemRenderer();
            standardxyitemrenderer.setSeriesPaint(0, Color.red);
            xyplot.setRenderer(1, standardxyitemrenderer);
            NumberAxis numberaxis1 = new NumberAxis("Range Axis 3");
            numberaxis1.setLabelPaint(Color.blue);
            numberaxis1.setTickLabelPaint(Color.blue);
            xyplot.setRangeAxis(2, numberaxis1);
            XYDataset xydataset2 = createDataset("Series 3", 10000D, new Minute(), 170);
            xyplot.setDataset(2, xydataset2);
            xyplot.mapDatasetToRangeAxis(2, 2);
            StandardXYItemRenderer standardxyitemrenderer1 = new StandardXYItemRenderer();
            standardxyitemrenderer1.setSeriesPaint(0, Color.blue);
            xyplot.setRenderer(2, standardxyitemrenderer1);
            NumberAxis numberaxis2 = new NumberAxis("Range Axis 4");
            numberaxis2.setLabelPaint(Color.green);
            numberaxis2.setTickLabelPaint(Color.green);
            xyplot.setRangeAxis(3, numberaxis2);
            XYDataset xydataset3 = createDataset("Series 4", 25D, new Minute(), 200);
            xyplot.setDataset(3, xydataset3);
            xyplot.mapDatasetToRangeAxis(3, 3);
            StandardXYItemRenderer standardxyitemrenderer2 = new StandardXYItemRenderer();
            standardxyitemrenderer2.setSeriesPaint(0, Color.green);
            xyplot.setRenderer(3, standardxyitemrenderer2);
            return jfreechart;
        }
     
        private static XYDataset createDataset(String s, double d, RegularTimePeriod regulartimeperiod, int i)
        {
            TimeSeries timeseries = new TimeSeries(s, regulartimeperiod.getClass());
            RegularTimePeriod regulartimeperiod1 = regulartimeperiod;
            double d1 = d;
            for(int j = 0; j < i; j++)
            {
                timeseries.add(regulartimeperiod1, d1);
                regulartimeperiod1 = regulartimeperiod1.next();
                d1 *= 1.0D + (Math.random() - 0.495D) / 10D;
            }
     
            TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
            timeseriescollection.addSeries(timeseries);
            return timeseriescollection;
        }
     
        public static JPanel createDemoPanel()
        {
            JFreeChart jfreechart = createChart();
            return new ChartPanel(jfreechart);
        }
     
        public static void main(String args[])
        {
            MultipleAxisDemo1 multipleaxisdemo1 = new MultipleAxisDemo1("Multiple Axis Demo 1");
            multipleaxisdemo1.pack();
            RefineryUtilities.centerFrameOnScreen(multipleaxisdemo1);
            multipleaxisdemo1.setVisible(true);
        }
    }
    Apparemment, il faut créer autant d'instance de XYDataset que de séries et les ajouter au XYPlot via la méthode setDataset.
    Modératrice Java - Struts, Servlets/JSP, ...

  3. #3
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut
    Citation Envoyé par c_nvy
    Apparemment, il faut créer autant d'instance de XYDataset que de séries et les ajouter au XYPlot via la méthode setDataset.
    mais est ce que c'est pareil pour "Line chart" je n'utilise pas "xyplot"

  4. #4
    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
    Effectivement, dans ton cas, il faut plutôt créer autant d'instance de CategoryDataset que de séries et les ajouter au CategoryPlot via la méthode setDataset.

    J'ai fait un essai en m'inspirant de ton code et de la classe MultipleAxisDemo1 et ça donne ceci :
    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
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    import java.awt.Color;
     
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.AxisLocation;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.renderer.category.LineAndShapeRenderer;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RectangleInsets;
    import org.jfree.ui.RefineryUtilities;
     
     
    public class LineChartTests extends ApplicationFrame {
    	public LineChartTests(String title)
    	{
    		super(title);
    		CategoryDataset dataset = createDataset();
    		JFreeChart chart = createChart(dataset);
    		// add the chart to a panel...
    		ChartPanel chartPanel = new ChartPanel(chart);
    		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    		setContentPane(chartPanel);
    	}
     
    	private CategoryDataset createDataset() {
    		// row keys...
    		String series1 = "TGRP:TRAFCARR";
    		// create the dataset...
    		String type1 = "Type 1";
    		String type2 = "Type 2";
    		String type3 = "Type 3";
    		String type4 = "Type 4";
    		String type5 = "Type 5";
    		String type6 = "Type 6";
    		String type7 = "Type 7";
    		String type8 = "Type 8";
     
    		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    		dataset.addValue(5.0, series1, type1);
    		dataset.addValue(6.0, series1, type2);
    		dataset.addValue(4.0, series1, type3);
    		dataset.addValue(7.0, series1, type4);
    		dataset.addValue(8.0, series1, type5);
    		dataset.addValue(9.0, series1, type6);
    		dataset.addValue(6.0, series1, type7);
    		dataset.addValue(4.0, series1, type8);
     
    		return dataset;
    	}
    	private CategoryDataset createDataset1() {
    		// row keys...
    		String series2 = "TGRP:SRVLINES";
    		String series3 = "TGRP:CULC";
     
    		// create the dataset...
    		String type1 = "Type 1";
    		String type2 = "Type 2";
    		String type3 = "Type 3";
    		String type4 = "Type 4";
    		String type5 = "Type 5";
    		String type6 = "Type 6";
    		String type7 = "Type 7";
    		String type8 = "Type 8";
     
    		DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
    		dataset1.addValue(1.0, series2, type1);
    		dataset1.addValue(3.0, series2, type2);
    		dataset1.addValue(0.5, series2, type3);
    		dataset1.addValue(4.0, series2, type4);
    		dataset1.addValue(2.0, series2, type5);
    		dataset1.addValue(1.0, series2, type6);
    		dataset1.addValue(2.0, series2, type7);
    		dataset1.addValue(1.0, series2, type8);
     
    		return dataset1;
    	}
    	private CategoryDataset createDataset2() {
    		// row keys...
    		String series3 = "TGRP:CULC";
    		// create the dataset...
    		String type1 = "Type 1";
    		String type2 = "Type 2";
    		String type3 = "Type 3";
    		String type4 = "Type 4";
    		String type5 = "Type 5";
    		String type6 = "Type 6";
    		String type7 = "Type 7";
    		String type8 = "Type 8";
     
    		DefaultCategoryDataset  dataset2 = new DefaultCategoryDataset ();
    		dataset2.addValue(4.0, series3, type1);
    		dataset2.addValue(3.0, series3, type2);
    		dataset2.addValue(2.0, series3, type3);
    		dataset2.addValue(3.0, series3, type4);
    		dataset2.addValue(6.0, series3, type5);
    		dataset2.addValue(3.0, series3, type6);
    		dataset2.addValue(4.0, series3, type7);
    		dataset2.addValue(3.0, series3, type8);
     
    		return dataset2;
    	}
    	private JFreeChart createChart(CategoryDataset dataset) {
    		// create the chart...
    		JFreeChart jfreechart = ChartFactory.createLineChart(
    				"Line Chart Test", // chart title
    				"Type", // domain axis label
    				"TGRP:TRAFCARR", // range axis label
    				dataset, // data
    				PlotOrientation.VERTICAL, // orientation
    				true, // include legend
    				true, // tooltips
    				false // urls
    		);
           jfreechart.setBackgroundPaint(Color.white);
     
           CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
           categoryplot.setOrientation(PlotOrientation.VERTICAL);
           categoryplot.setBackgroundPaint(Color.lightGray);
           categoryplot.setDomainGridlinePaint(Color.white);
           categoryplot.setRangeGridlinePaint(Color.white);
           categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
           categoryplot.getRangeAxis().setFixedDimension(15D);
     
           LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer() ;
           lineandshaperenderer.setSeriesPaint(0, Color.black);
           categoryplot.setRenderer(0, lineandshaperenderer);
     
           CategoryDataset dataset1 = createDataset1();
           categoryplot.setDataset(1,dataset1) ;
           categoryplot.mapDatasetToRangeAxis(1, 1);
           LineAndShapeRenderer lineandshaperenderer1 = new LineAndShapeRenderer();
           lineandshaperenderer1.setSeriesPaint(1, Color.red);
           categoryplot.setRenderer(1, lineandshaperenderer1);
           NumberAxis numberaxis = new NumberAxis("TGRP:SRVLINES");
           numberaxis.setFixedDimension(10D);
           numberaxis.setAutoRangeIncludesZero(false);
           numberaxis.setLabelPaint(Color.red);
           numberaxis.setTickLabelPaint(Color.red);
           categoryplot.setRangeAxis(1, numberaxis);
           categoryplot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
     
           CategoryDataset dataset2 = createDataset2();
           categoryplot.setDataset(2,dataset2) ;
           categoryplot.mapDatasetToRangeAxis(2, 2);
           LineAndShapeRenderer lineandshaperenderer2 = new LineAndShapeRenderer();
           lineandshaperenderer2.setSeriesPaint(2, Color.blue);
           categoryplot.setRenderer(2, lineandshaperenderer2);
           NumberAxis numberaxis1 = new NumberAxis("TGRP:CULC");
           numberaxis1.setLabelPaint(Color.blue);
           numberaxis1.setTickLabelPaint(Color.blue);
           categoryplot.setRangeAxis(2, numberaxis1);
           categoryplot.setRangeAxisLocation(2, AxisLocation.BOTTOM_OR_RIGHT);
     
    	return jfreechart;
    	}
    	public static void main(String[] args) {
    		LineChartTests demo = new LineChartTests("Line Chart Test");
    		demo.pack();
    		RefineryUtilities.centerFrameOnScreen(demo);
    		demo.setVisible(true);
    	}
    }
    Il y a sans doute quelques améliorations à apporter mais cela semble correspondre à ton besoin non ?
    Modératrice Java - Struts, Servlets/JSP, ...

  5. #5
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut
    Citation Envoyé par c_nvy
    Effectivement, dans ton cas, il faut plutôt créer autant d'instance de CategoryDataset que de séries et les ajouter au CategoryPlot via la méthode setDataset.

    J'ai fait un essai en m'inspirant de ton code et de la classe MultipleAxisDemo1 et ça donne ceci :
    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
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    import java.awt.Color;
     
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.AxisLocation;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.renderer.category.LineAndShapeRenderer;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RectangleInsets;
    import org.jfree.ui.RefineryUtilities;
     
     
    public class LineChartTests extends ApplicationFrame {
    	public LineChartTests(String title)
    	{
    		super(title);
    		CategoryDataset dataset = createDataset();
    		JFreeChart chart = createChart(dataset);
    		// add the chart to a panel...
    		ChartPanel chartPanel = new ChartPanel(chart);
    		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
    		setContentPane(chartPanel);
    	}
     
    	private CategoryDataset createDataset() {
    		// row keys...
    		String series1 = "TGRP:TRAFCARR";
    		// create the dataset...
    		String type1 = "Type 1";
    		String type2 = "Type 2";
    		String type3 = "Type 3";
    		String type4 = "Type 4";
    		String type5 = "Type 5";
    		String type6 = "Type 6";
    		String type7 = "Type 7";
    		String type8 = "Type 8";
     
    		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    		dataset.addValue(5.0, series1, type1);
    		dataset.addValue(6.0, series1, type2);
    		dataset.addValue(4.0, series1, type3);
    		dataset.addValue(7.0, series1, type4);
    		dataset.addValue(8.0, series1, type5);
    		dataset.addValue(9.0, series1, type6);
    		dataset.addValue(6.0, series1, type7);
    		dataset.addValue(4.0, series1, type8);
     
    		return dataset;
    	}
    	private CategoryDataset createDataset1() {
    		// row keys...
    		String series2 = "TGRP:SRVLINES";
    		String series3 = "TGRP:CULC";
     
    		// create the dataset...
    		String type1 = "Type 1";
    		String type2 = "Type 2";
    		String type3 = "Type 3";
    		String type4 = "Type 4";
    		String type5 = "Type 5";
    		String type6 = "Type 6";
    		String type7 = "Type 7";
    		String type8 = "Type 8";
     
    		DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
    		dataset1.addValue(1.0, series2, type1);
    		dataset1.addValue(3.0, series2, type2);
    		dataset1.addValue(0.5, series2, type3);
    		dataset1.addValue(4.0, series2, type4);
    		dataset1.addValue(2.0, series2, type5);
    		dataset1.addValue(1.0, series2, type6);
    		dataset1.addValue(2.0, series2, type7);
    		dataset1.addValue(1.0, series2, type8);
     
    		return dataset1;
    	}
    	private CategoryDataset createDataset2() {
    		// row keys...
    		String series3 = "TGRP:CULC";
    		// create the dataset...
    		String type1 = "Type 1";
    		String type2 = "Type 2";
    		String type3 = "Type 3";
    		String type4 = "Type 4";
    		String type5 = "Type 5";
    		String type6 = "Type 6";
    		String type7 = "Type 7";
    		String type8 = "Type 8";
     
    		DefaultCategoryDataset  dataset2 = new DefaultCategoryDataset ();
    		dataset2.addValue(4.0, series3, type1);
    		dataset2.addValue(3.0, series3, type2);
    		dataset2.addValue(2.0, series3, type3);
    		dataset2.addValue(3.0, series3, type4);
    		dataset2.addValue(6.0, series3, type5);
    		dataset2.addValue(3.0, series3, type6);
    		dataset2.addValue(4.0, series3, type7);
    		dataset2.addValue(3.0, series3, type8);
     
    		return dataset2;
    	}
    	private JFreeChart createChart(CategoryDataset dataset) {
    		// create the chart...
    		JFreeChart jfreechart = ChartFactory.createLineChart(
    				"Line Chart Test", // chart title
    				"Type", // domain axis label
    				"TGRP:TRAFCARR", // range axis label
    				dataset, // data
    				PlotOrientation.VERTICAL, // orientation
    				true, // include legend
    				true, // tooltips
    				false // urls
    		);
           jfreechart.setBackgroundPaint(Color.white);
     
           CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();
           categoryplot.setOrientation(PlotOrientation.VERTICAL);
           categoryplot.setBackgroundPaint(Color.lightGray);
           categoryplot.setDomainGridlinePaint(Color.white);
           categoryplot.setRangeGridlinePaint(Color.white);
           categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
           categoryplot.getRangeAxis().setFixedDimension(15D);
     
           LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer() ;
           lineandshaperenderer.setSeriesPaint(0, Color.black);
           categoryplot.setRenderer(0, lineandshaperenderer);
     
           CategoryDataset dataset1 = createDataset1();
           categoryplot.setDataset(1,dataset1) ;
           categoryplot.mapDatasetToRangeAxis(1, 1);
           LineAndShapeRenderer lineandshaperenderer1 = new LineAndShapeRenderer();
           lineandshaperenderer1.setSeriesPaint(1, Color.red);
           categoryplot.setRenderer(1, lineandshaperenderer1);
           NumberAxis numberaxis = new NumberAxis("TGRP:SRVLINES");
           numberaxis.setFixedDimension(10D);
           numberaxis.setAutoRangeIncludesZero(false);
           numberaxis.setLabelPaint(Color.red);
           numberaxis.setTickLabelPaint(Color.red);
           categoryplot.setRangeAxis(1, numberaxis);
           categoryplot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
     
           CategoryDataset dataset2 = createDataset2();
           categoryplot.setDataset(2,dataset2) ;
           categoryplot.mapDatasetToRangeAxis(2, 2);
           LineAndShapeRenderer lineandshaperenderer2 = new LineAndShapeRenderer();
           lineandshaperenderer2.setSeriesPaint(2, Color.blue);
           categoryplot.setRenderer(2, lineandshaperenderer2);
           NumberAxis numberaxis1 = new NumberAxis("TGRP:CULC");
           numberaxis1.setLabelPaint(Color.blue);
           numberaxis1.setTickLabelPaint(Color.blue);
           categoryplot.setRangeAxis(2, numberaxis1);
           categoryplot.setRangeAxisLocation(2, AxisLocation.BOTTOM_OR_RIGHT);
     
    	return jfreechart;
    	}
    	public static void main(String[] args) {
    		LineChartTests demo = new LineChartTests("Line Chart Test");
    		demo.pack();
    		RefineryUtilities.centerFrameOnScreen(demo);
    		demo.setVisible(true);
    	}
    }
    Il y a sans doute quelques améliorations à apporter mais cela semble correspondre à ton besoin non ?

    merci infinément
    ça marche

  6. #6
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut
    juste une question quelle est partie du code responsable du traçage de courbe rouge car j'aime pas avoir des point sur la courbe

  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
    Remplace :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    LineAndShapeRenderer lineandshaperenderer1 = new LineAndShapeRenderer();
    par
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    LineAndShapeRenderer lineandshaperenderer1 = new LineAndShapeRenderer(true,false);
    et de même pour lineandshaperenderer2 si tu ne veux pas les triangles sur la courbe bleue.
    Modératrice Java - Struts, Servlets/JSP, ...

  8. #8
    Membre régulier Avatar de wiss20000
    Inscrit en
    Août 2006
    Messages
    225
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 225
    Points : 82
    Points
    82
    Par défaut
    merci encore une fois

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

Discussions similaires

  1. Réponses: 1
    Dernier message: 28/04/2009, 10h00
  2. [JFreeChart] GanttChart probleme des axes
    Par nabil148911 dans le forum 2D
    Réponses: 13
    Dernier message: 04/08/2008, 12h17
  3. chart owc, axes et numberformat
    Par mohcultiv dans le forum ASP
    Réponses: 2
    Dernier message: 11/07/2007, 17h10
  4. [JFreeChart] Pareto Chart
    Par bibx dans le forum 2D
    Réponses: 2
    Dernier message: 08/12/2006, 10h46
  5. Line Chart vide
    Par LaNat dans le forum iReport
    Réponses: 5
    Dernier message: 08/11/2006, 13h59

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