/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.vutbr.feec.utko.fojtoval.krypto.windows.data;

import cz.vutbr.feec.utko.fojtoval.krypto.funkce.Constant;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.image.BufferedImage;
import org.jfree.chart.ChartFactory;
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.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author fojtoval
 */
public class Graf {
    
    /**
   * Slouzi k vykresleni grafu poctu vyskytu pismen v zadanem textu. Zadany text se ziskava z textoveho okna
   * jTextPane1 (vstup), NavrhView. K vykresleni dojde v novem okne, ktere se otevre po stisku menu Frekvencni analyza.
     * @param data pole obsahuji procentuelni vyskyt pismen v textu.
     * @return obrazek grafu
   */

    public static BufferedImage zobrazGraf (double[] data) {

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    for(int i=0; i < 26; i++) {
      dataset.addValue(Constant.cj[i], "český jazyk", Character.toString((char)(i+97)));
      dataset.addValue(data[i], "šifra", Character.toString((char)(i+97)));
    }

    JFreeChart chart = ChartFactory.createBarChart(
        "Četnost písmen v zadaném textu",         // chart title
        "Písmena abecedy",               // domain axis label
        "Procentuelní četnost v textu",                  // range axis label
        dataset,                  // data
        PlotOrientation.VERTICAL, // orientation
        true,                     // include legend
        false,                     // tooltips
        false                     // URL
    );

    // OPTIONAL CUSTOMISATION OF THE CHART.

    // background color for the chart
    chart.setBackgroundPaint(Color.white);

    // get a reference to the plot for further customisation
    CategoryPlot plot = chart.getCategoryPlot();
    plot.setNoDataMessage("NO DATA!");
    plot.setBackgroundPaint(Color.white);
    plot.setDomainGridlinePaint(Color.white);
    plot.setDomainGridlinesVisible(false);
    plot.setRangeGridlinePaint(Color.white);

    // set the range axis to display integers only
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    // disable bar outlines
    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)
    );


    int imx = 600;
    int imy = 300;

    BufferedImage image = chart.createBufferedImage(imx,imy);
    return(image);

  }

}
