
package bicat.gui;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.ui.ApplicationFrame;
import java.awt.Font;
import org.jfree.chart.axis.CategoryAnchor;

import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;
import java.awt.Color;
import java.awt.Paint;
import org.jfree.chart.annotations.*;
/**
 * A simple demonstration application showing how to create a vertical 3D bar chart using data
 * from a {@link CategoryDataset}.
 *
 */
public class NewClass1 extends ApplicationFrame {

    
    
      /**
     * A custom renderer that returns a different color for each item in a single series.
     */
    class CustomRenderer extends BarRenderer3D {

        /** The colors. */
        private Paint[] colors;

        /**
         * Creates a new renderer.
         *
         * @param colors  the colors.
         */
        public CustomRenderer(final Paint[] colors) {
            this.colors = colors;
        }

        /**
         * Returns the paint for an item.  Overrides the default behaviour inherited from
         * AbstractSeriesRenderer.
         *
         * @param row  the series.
         * @param column  the category.
         *
         * @return The item color.
         */
        public Paint getItemPaint(final int row, final int column) {
            return this.colors[column % this.colors.length];
        }
    }

    /**
     * Creates a new demo.
     *
     * @param title  the frame title.
     */
    public NewClass1(final String title) {

        super(title);
        
        final CategoryDataset dataset = createDataset();
        final JFreeChart chart = createChart(dataset);

        // add the chart to a panel...
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);

    }

    /**
     * Creates a sample dataset.
     *
     * @return a sample dataset.
     */
   private CategoryDataset createDataset() {

        final double[][] data = new double[][]
            {{10.0, 4.0, 15.0, 14.0,20,12,11}};
             
        String [] arr={"GO1","GO 2","GO3","a2","d3","f4","f5"};
        return DatasetUtilities.createCategoryDataset(new String []{"Series"},arr, data);

    }
    
    /**
     * Creates a chart.
     * 
     * @param dataset  the dataset.
     * 
     * @return The chart.
     */
    private JFreeChart createChart(final CategoryDataset dataset) {
        
        final JFreeChart chart = ChartFactory.createBarChart3D(
            "3D Bar Chart Demo",      // chart title
            "Category",               // domain axis label
            "Value",                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL, // orientation
            false,                     // include legend
            true,                     // tooltips
            false                     // urls
        );

        
        
         chart.setBackgroundPaint(Color.lightGray);
           // get a reference to the plot for further customisation...
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setNoDataMessage("NO DATA!");

        final CustomRenderer  renderer = new CustomRenderer(
            new Paint[] {Color.red, Color.blue, Color.green,
                Color.yellow, Color.orange, Color.cyan,
                Color.magenta, Color.blue,Color.BLACK,Color.ORANGE,Color.YELLOW}
        );

        //        renderer.setLabelGenerator(new StandardCategoryLabelGenerator());
        renderer.setItemLabelsVisible(true);
        renderer.setOutlinePaint(Color.BLACK);
        renderer.setDrawBarOutline(false);
       
        //XYTextAnnotation xyan=new XYTextAnnotation("50%", 100,100);

             double value=dataset.getValue(dataset.getRowKey(0),dataset.getColumnKey(4)).doubleValue();
             double position=value-(value*0.5);
                     
                     
                     
             
             
        CategoryTextAnnotation an=new CategoryTextAnnotation("70%",dataset.getColumnKey(4),position);
        
        an.setTextAnchor(TextAnchor.BOTTOM_CENTER);
        an.setFont(new Font("SansSerif", Font.BOLD, 12));
        an.setCategoryAnchor(CategoryAnchor.MIDDLE); 
        
        plot.addAnnotation(an);
        plot.setRenderer(renderer);
                
        
//        final ItemLabelPosition p = new ItemLabelPosition(
//            ItemLabelAnchor.OUTSIDE12, TextAnchor.TOP_CENTER, TextAnchor.BOTTOM_CENTER, 9);
//        renderer.set
//        
//        renderer.setcaPositiveItemLabelPosition(p);
//        plot.setRenderer(renderer);
//
//        // change the margin at the top of the range axis...
//        final ValueAxis rangeAxis = plot.getRangeAxis();
//        
//        rangeAxis.se
//        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//        rangeAxis.setLowerMargin(0.15);
//        rangeAxis.setUpperMargin(0.15);
//
//        return chart;
        
        
        
        
        
        
        
       
        final CategoryAxis axis = plot.getDomainAxis();
        axis.setCategoryLabelPositions(
            CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / dataset.getColumnCount())
        );
        
        System.out.println( dataset.getColumnCount());
        axis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
        axis.setCategoryMargin(0.4);
        axis.setUpperMargin(0.05);
        axis.setLowerMargin(0.05);
        

        
        
        return chart;

    }
    
    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    * 
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************
    
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(final String[] args) {

        final NewClass1 demo = new NewClass1("3D Bar Chart Demo 1");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}
