package demoGrafici;

import org.jfree.data.xy.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.data.category.*;
import org.jfree.data.general.*;

import java.io.*;

public class Esercizio
{

  
  static void esercizio()
  {
    DefaultPieDataset dataSet = caricaDatiEsercizio();
    
    JFreeChart grafo = ChartFactory.createPieChart(
             "Fonti energetiche", 
             dataSet, 
             true, 
             false, 
             false);
    
    
    ChartFrame frame = new ChartFrame("Esercizio", grafo);
    frame.setSize(400,350);
    frame.setVisible(true);
  }
  
  
  static DefaultPieDataset caricaDatiEsercizio()
  {
    String s, a[];
    DefaultPieDataset dataSet = new DefaultPieDataset();
    
    try
    {
      FileReader in = new FileReader("esercizio.dat");
      BufferedReader reader = new BufferedReader(in);

      while (reader.ready())
      {
        s = reader.readLine();
        if (s.length() > 0 && s.charAt(0) != '#')
        {
          a = s.split("\t");
          dataSet.setValue(
                a[0] + " " +  a[1] + "%", 
                Double.parseDouble(a[1]));
        }
      }

      reader.close();
      in.close();
    }
    catch (IOException ioe)
    {
      System.out.println(ioe.getMessage());
    }

    return dataSet;
  }
  
  public static void main (String[] args)
  {
    esercizio();
  }

} /* end of class declaration */

