package testes;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.io.File;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
   
   /**
    * A simple demonstration application showing how to create a bar chart.
    */
   public class BarChartDemo1 extends ApplicationFrame {
   
       /**
        * Creates a new demo instance.
        *
        * @param title  the frame title.
     * @throws IOException 
        */
       public BarChartDemo1(String title) throws IOException {
           super(title);
           CategoryDataset dataset = createDataset();
           JFreeChart chart = createChart(dataset);
           ChartPanel chartPanel = new ChartPanel(chart, false);
           chartPanel.setPreferredSize(new Dimension(500, 270));
           setContentPane(chartPanel);
       }
   
       /**
        * Returns a sample dataset.
        * 
        * @return The dataset.
       */
       private static CategoryDataset createDataset() {
           
           // row keys...
    	   //String series1 =  "Acesso à água Potável";
           //String series2 =  "Litros de água por pessoa";
           String series3 =  "Tratamento de esgoto";
   
          // column keys...
           String category1 = "PERNAMBUCO";
           String category2 = "PARAÍBA";
           String category3 = "ALAGOAS";
           String category4 = "RIO GRANDE DO NORTE";
           String category5 = "MARANHÂO";
           String category6 = "PIAUÍ";
           String category7 = "CEARÁ";
           String category8 = "SERGIPE";
           String category9 = "BAHIA";
           
           
           
 
          // create the dataset...
          DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  
          //dataset.addValue(10, series1, category1);
          //dataset.addValue(40, series1, category2);
          //dataset.addValue(90, series1, category3);
          //dataset.addValue(58, series1, category4);
          
 
          //dataset.addValue(300, series2, category1);
          //dataset.addValue(410, series2, category2);
          //dataset.addValue(180, series2, category3);
          //dataset.addValue(18, series2, category4);
          
  
          dataset.addValue(40, series3, category1);
          dataset.addValue(30, series3, category2);
          dataset.addValue(20, series3, category3);
          dataset.addValue(36, series3, category4);
          dataset.addValue(40, series3, category5);
          dataset.addValue(30, series3, category6);
          dataset.addValue(20, series3, category7);
          dataset.addValue(36, series3, category8);
          dataset.addValue(36, series3, category9);
          
          return dataset;
         
      }
      
      /**
    * Creates a sample chart.
   * 
       * @param dataset  the dataset.
      * 
       * @return The chart.
     * @throws IOException 
      */
      private static JFreeChart createChart(CategoryDataset dataset) throws IOException {
          
          // create the chart...
          JFreeChart chart = ChartFactory.createBarChart(
             "Tratamento de Esgoto X País",       // chart title
              "Legenda",               // domain axis label
              "Percentual",                  // range axis label
              dataset,                  // data
              PlotOrientation.VERTICAL, // orientation
             true,                     // include legend
             true,                     // tooltips?
           false                     // URLs?
          );
 
          // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
  
          // set the background color for the chart...
          chart.setBackgroundPaint(Color.white);
  
          // get a reference to the plot for further customisation...
          CategoryPlot plot = (CategoryPlot) chart.getPlot();
          plot.setBackgroundPaint(Color.lightGray);
          plot.setDomainGridlinePaint(Color.white);
          plot.setDomainGridlinesVisible(true);
          plot.setRangeGridlinePaint(Color.white);
          ChartRenderingInfo info = new ChartRenderingInfo
          (new StandardEntityCollection());
          File file1 = new File("F:/jspTeste/WebContent/testes/listrosPorPessoa.jpg");
          ChartUtilities.saveChartAsJPEG(file1, chart, 600, 400, info);
  
          // ******************************************************************
         //  More than 150 demo applications are included with the JFreeChart
          //  Developer Guide...for more information, see:
         //
          //  >   http://www.object-refinery.com/jfreechart/guide.html
          //
          // ******************************************************************
       
         // set the range axis to display integers only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
         rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
  
          // disable bar 
          BarRenderer renderer = (BarRenderer) plot.getRenderer();
          renderer.setDrawBarOutline(false);
          
        // set up gradient paints for series...
          GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 
                  0.0f, 0.0f, new Color(0, 0, 64));
          GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 
                  0.0f, 0.0f, new Color(0, 64, 0));
          GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 
                  0.0f, 0.0f, new Color(64, 0, 0));
          renderer.setSeriesPaint(0, gp0);
          renderer.setSeriesPaint(1, gp1);
          renderer.setSeriesPaint(2, gp2);
 
         CategoryAxis domainAxis = plot.getDomainAxis();
         domainAxis.setCategoryLabelPositions(
                 CategoryLabelPositions.createUpRotationLabelPositions(
                         Math.PI / 6.0));
         // OPTIONAL CUSTOMISATION COMPLETED.
         
         return chart;
          
     }
     
    /**
      * Starting point for the demonstration application.
       *
      * @param args  ignored.
       */
     public static void main(String[] args) {
         BarChartDemo1 demo;
		try {
			demo = new BarChartDemo1("Bar Chart Demo 1");
			demo.pack();
	          RefineryUtilities.centerFrameOnScreen(demo);
	          demo.setVisible(true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    }
  
}