mercoledì 2 gennaio 2013

Uso di JFreeChart in Java/NetBeans

L'utilizzo di JFreeChart in NetBeans/Java e' sostanzialmente identico a quello di Android

Per prima cosa si deve creare un progetto come New Java Application e poi si devono linkare le librerie jcommon e jfreechart (le altre librerie comprese nel file zip si possono omettere)


In seguito si deve editare la funzione main come segue
Barre
----------------------------------------

package barre;

import java.awt.Color;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author l.innocenti
 */
public class Barre {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  dataset.setValue(2, "classe1", "1");
  dataset.setValue(7, "classe1", "2");
  dataset.setValue(4, "classe1", "3");
  dataset.setValue(9, "classe1", "4");
  dataset.setValue(6, "classe1", "5");
  JFreeChart chart;
 chart = ChartFactory.createBarChart
("Grafico a barre","Classe", "Valore", dataset,
PlotOrientation.VERTICAL, false,true, false);
  //chart.setBackgroundPaint(Color.yellow);
  //chart.getTitle().setPaint(Color.blue); 
  CategoryPlot p = chart.getCategoryPlot(); 
  p.setRangeGridlinePaint(Color.red); 
  ChartFrame frame1=new ChartFrame("Bar Chart",chart);
  frame1.pack();
  frame1.setVisible(true);
  
//  frame1.setSize(400,350);
    }
}

----------------------------------------


Linee
----------------------------------------

package javaapplication7;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author l.innocenti
 */
public class JavaApplication7 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
   XYSeries series = new XYSeries("XYGraph");
   series.add(1, 1);
   series.add(1, 2);
   series.add(2, 1);
   series.add(3, 9);
   series.add(4, 10);

// Add the series to your data set
   XYSeriesCollection dataset = new XYSeriesCollection();
   dataset.addSeries(series);

// Generate the graph
   JFreeChart chart;   
        chart = ChartFactory.createXYLineChart(
"Grafico XY", // Title
"asse X", // x-axis Label
"asse Y", // y-axis Label
dataset, // Dataset
PlotOrientation.VERTICAL, // Plot Orientation
false, // Show Legend
false, // Use tooltips
false // Configure chart to generate URLs?
);
        
ChartFrame frame = new ChartFrame("Esempio", chart);
frame.pack();
frame.setVisible(true);
    }
}


----------------------------------------