Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2009
    Messaggi
    20

    [Java] Creare un grafico di linea

    Salve ho un programma in java che ha alcuni dati, vorrei creare un grafico lineare che segna un'evoluzione, similie a questo (immagine presa a caso da google giusto per rendere l'idea):

    Ho cercato su internet e ho trovato librerie come JFreeChart, ma non sono riuscito bene a capire come funzionano per la mancaza di esempi.

    Qualcuno ha mai fatto cose del genere, qualsiasi metodo per diegnare il grafico va bene.
    Grazie mille dell'aiuto.
    Campus
    OS: Mac Os X
    Linguaggi C/Java

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2009
    Messaggi
    20
    grazie, ho risolto, se vi interessa ecco il codice, ho usato JFreeChart:
    codice:
    import java.awt.Font;
    import java.awt.Color;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PiePlot;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.plot.CategoryPlot;
    
    import java.util.ArrayList;
    
    
    public class LLineGraph extends JPanel
    	{
    		
    		public LLineGraph(ArrayList<Double> datas, ArrayList<String> type, String title, String typeLabel, String valueLabel)
    		{
    			CategoryDataset dataset = createDataset(title, datas, type);
    			this.add(new ChartPanel(setGraph(title, dataset, typeLabel, valueLabel)));
    			
    		}
    		
    		private CategoryDataset createDataset(String title, ArrayList<Double> datas, ArrayList<String> type) 
    		{
    			final DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
    			int i = 0;
    			for(double aData : datas)	
    			{
    				dataset2.addValue(aData, title, type.get(i));
    				i += 1;
    			}
    
    			return dataset2;
    			
    		}
    		public JFreeChart setGraph(String title, CategoryDataset dataset, String typeLabel, String valueLabel)
    		{
    			JFreeChart jfc = ChartFactory.createLineChart(title,       // chart title
    											   typeLabel,                    // domain axis label
    											   valueLabel,                   // range axis label
    											   dataset,                   // data
    											   PlotOrientation.VERTICAL,  // orientation
    											   false,                      // legend
    											   true,                      // tooltips
    											   false                      // urls
    											   );
    			//jfc.setBackgroundPaint(Color.yellow); // Set the background colour of the chart
    			jfc.getTitle().setPaint(Color.red); // Adjust the colour of the title
    			CategoryPlot p = jfc.getCategoryPlot(); // Get the Plot object for a bar graph
    
    			p.setBackgroundPaint(new Color(253, 252, 237));// Modify the plot background
    			p.setRangeGridlinePaint(Color.black); // Modify the colour of the plot gridlines Modify Chart 			
    
    			return jfc;
    		}
    	}
    OS: Mac Os X
    Linguaggi C/Java

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.