Partage
  • Partager sur Facebook
  • Partager sur Twitter

Histogramme avec 2 séries

Sujet résolu
    5 septembre 2019 à 10:54:24

    Bonjour,

    Je débute avec les graphiques sur java et j'ai un souci avec les histogrammes. Avec une série tout fonctionne correctement, mais lorsque j'ajoute la seconde série, elle apparaît derrière la première:

    package graphiques;
    import java.io.File;
    import java.io.IOException;
    import javax.swing.JPanel;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.plot.XYPlot;
    import org.jfree.chart.renderer.xy.XYBarRenderer;
    import org.jfree.data.statistics.HistogramDataset;
    import org.jfree.data.xy.IntervalXYDataset;
    import org.jfree.ui.ApplicationFrame;
    import org.jfree.ui.RefineryUtilities;
    
    public class Histograma extends ApplicationFrame {
    	public Histograma(String title) {
    		super(title);
    		JPanel chartPanel = crearPanel();
    		chartPanel.setPreferredSize(new java.awt.Dimension(500, 475));
    		setContentPane(chartPanel);
    	}
    	private static IntervalXYDataset crearDataset() {
    		HistogramDataset dataset = new HistogramDataset();
    		//vecto almacena los ingresos quincenales de 45 personas
    		double vector[] = {63, 89, 36, 49, 56, 64, 59, 35, 78,
    			43, 53, 70, 57, 62, 43, 68, 62, 26,
    			64, 72, 52, 51, 62, 60, 71, 61, 55,
    			59, 60, 67, 57, 67, 61, 67, 51, 81,
    			53, 64, 76, 44, 73, 56, 62, 63, 60};
    	
    		double vector2[] = {63, 90, 36, 49, 56, 64, 59, 35, 68,
    			43, 53, 70, 47, 62, 43, 68, 42, 26,
    			64, 72, 52, 51, 62, 60, 71, 61, 55,
    			59, 60, 67, 57, 67, 61, 67, 51, 81,
    			53, 64, 76, 44, 73, 56, 52, 63, 50};
    		//double vector[] = {63, 89};
    	
    		//En el ejercicio nos piden construir una distribución de frecuencias de 8 intervalos
    		//Por eso ponemos 8 en el tercer parámetro del addSeries
    		dataset.addSeries("Frecuencias de los ingresos", vector, 20);
    		dataset.addSeries("Frecuencias de los ingresos2", vector2, 20);
    		return dataset;
    	}
    	private static JFreeChart crearChart(IntervalXYDataset dataset) {
    		JFreeChart chart = ChartFactory.createHistogram(
    				"Histograma",null,null,dataset,PlotOrientation.VERTICAL,true,true,false
    		);
    		XYPlot plot = (XYPlot) chart.getPlot();
    		XYBarRenderer renderer = (XYBarRenderer) plot.getRenderer();
    		renderer.setDrawBarOutline(false);
    		try{
    			ChartUtilities.saveChartAsJPEG(new File("C:\\histograma.jpg"), chart, 500, 475);
    		}
    		catch(IOException e){
    			System.out.println("Error al abrir el archivo");
    		}
    		return chart;
    	}
    	public static JPanel crearPanel() {
    		JFreeChart chart = crearChart(crearDataset());
    		return new ChartPanel(chart);
    	}
    	public static void main(String[] args) throws IOException {
    		Histograma histo = new Histograma("Histograma");
    		histo.pack();
    		RefineryUtilities.centerFrameOnScreen(histo);
    		histo.setVisible(true);
    	}
    }
    
    

    Je voudrais avoir les barres bleues à côté des rouges et non derrière. Pourriez-vous m'aider s'il vous plaît?

    Merci

    -
    Edité par laggron078 5 septembre 2019 à 10:55:06

    • Partager sur Facebook
    • Partager sur Twitter
      9 septembre 2019 à 9:33:19

      Re,

      Finalement ça marche, voici mon code:

      package graphiques;
      import java.util.Random;
      
      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; 
      
      
      public class Proba extends ApplicationFrame{
      	
      	public static int nbexperiences = 2000;
      	public static int N = 4;
      	public static int nbbarres = 5;
      	
      	public Proba( String applicationTitle , String chartTitle ) {
      	      super( applicationTitle );        
      	      JFreeChart barChart = ChartFactory.createBarChart(
      	         chartTitle,           
      	         "Score",
      	         "Frequence",
      	         createDataset(),
      	         PlotOrientation.VERTICAL,
      	         true, true, false);
      	         
      	      ChartPanel chartPanel = new ChartPanel( barChart );        
      	      chartPanel.setPreferredSize(new java.awt.Dimension( 2000 , 367 ) );        
      	      setContentPane( chartPanel ); 
      	   }
      	
      	private CategoryDataset createDataset( ) {
      		final String f1_2 = "p=1/2";        
      	    final String f1_3 = "p=1/3";
      	    final String tabbarres[] = new String[nbbarres];
      	    for (int i=0;i<nbbarres;i++) {
      	    	int min;
      	    	if (i==0) {
      	    		min = 0;
      	    	} else {
      	    		min = N*i/nbbarres + 1;
      	    	}
      	    	
      	    	int max = N*(i+1)/nbbarres;
      	    	tabbarres[i] = min + " a " + max;
      	    }
      	    
      		final DefaultCategoryDataset dataset = new DefaultCategoryDataset( );  
      		//Binomiale, probas 1/2 et 1/3
      		Random r = new Random();
      		int tabcount[] = new int[nbbarres];
      		for (int i=0;i<nbbarres;i++) {
      			tabcount[i] = 0;
      		}
      		
      		for (int n=0;n<nbexperiences;n++) {
      			int count =0;
      			for (int j=0;j<N;j++) {
      				int ra = r.nextInt(2);
      				if (ra ==1) {
      					count ++;
      				}
      			}
      			int groupescore = 1;
      			while (count > N*(groupescore)/nbbarres) {
      				groupescore ++;
      			}
      			tabcount[groupescore-1]++;
      		}
      		
      		for (int i=0;i<nbbarres;i++) {
      			dataset.addValue( tabcount[i] , f1_2 , tabbarres[i] );
      		}
      		tabcount = new int[nbbarres];
      		for (int i=0;i<nbbarres;i++) {
      			tabcount[i] = 0;
      		}
      		
      		for (int n=0;n<nbexperiences;n++) {
      			int count =0;
      			for (int j=0;j<N;j++) {
      				int ra = r.nextInt(3);
      				if (ra ==2) {
      					count ++;
      				}
      			}
      			int groupescore = 1;
      			while (count > N*(groupescore)/nbbarres) {
      				groupescore ++;
      			}
      			tabcount[groupescore-1]++;
      		}
      		
      		for (int i=0;i<nbbarres;i++) {
      			dataset.addValue( tabcount[i] , f1_3, tabbarres[i] );
      		}
      		
      		return dataset;
      	}
      	
      	public static void main( String[ ] args ) {
      		Proba chart = new Proba("Statistiques", 
      	         "Probas score");
      	      chart.pack( );        
      	      RefineryUtilities.centerFrameOnScreen( chart );        
      	      chart.setVisible( true ); 
      	   }
      }
      



      • Partager sur Facebook
      • Partager sur Twitter

      Histogramme avec 2 séries

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown