/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import ilog.views.chart.IlvChart;
import ilog.views.chart.IlvChartLayout;
import ilog.views.chart.IlvChartRenderer;
import ilog.views.chart.IlvColor;
import ilog.views.chart.IlvDataInterval;
import ilog.views.chart.IlvLegend;
import ilog.views.chart.IlvStyle;
import ilog.views.chart.data.IlvDataSource;
import ilog.views.chart.data.IlvDefaultDataSource;
import ilog.views.chart.graphic.IlvDataIndicator;
import ilog.views.chart.graphic.IlvDataLabelAnnotation;
import ilog.views.chart.renderer.IlvHiLoChartRenderer;
import ilog.views.chart.renderer.IlvPolylineChartRenderer;
import ilog.views.util.IlvProductUtil;

public class CustomCartesian extends JFrame {

  /** Creates new Cartesian */
  public CustomCartesian() {
    super("Customized Cartesian Chart");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    // creates the initial values array
    double[][] temps = { { 10, 8, 12, 19, 10, 14, 13 }, { 16, 12, 20, 15, 18, 24, 26 } };
    String[] names = { "Morning Temperatures", "Afternoon Temperatures" };
    String[] labels = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
    // Create one data source to store the temperatures. No x series since
    // the x values are actually the index of the y value in the data set
    IlvDataSource tempDataSource = new IlvDefaultDataSource(temps, -1, names, labels);

    // Create a new cartesian chart
    IlvChart chart = new IlvChart();
    chart.setAntiAliasing(true);

    // Create the hilo renderer. This renderer shares the same data source
    // than the polyline renderer. This is a clear illustration of the
    // model-views design of the chart library : one model, two graphical
    // representations.
    IlvHiLoChartRenderer hiloRenderer = new IlvHiLoChartRenderer();
    hiloRenderer.setWidthPercent(40);
    hiloRenderer.setDataSource(tempDataSource);
    IlvStyle[] styles = { new IlvStyle(Color.black, IlvColor.indianRed),
        new IlvStyle(Color.black, IlvColor.cornflowerBlue) };
    hiloRenderer.setStyles(styles);
    hiloRenderer.getChild(0).setName("Morning/Afternoon Temperatures");
    chart.addRenderer(hiloRenderer);

    // Create the renderer that will draw the graphical representation of
    // the morning and afternoon temperatures as two distinct polylines.
    IlvChartRenderer tempRenderer = new IlvPolylineChartRenderer();
    tempRenderer.setDataSource(tempDataSource);
    chart.addRenderer(tempRenderer);

    // Customize the scales
    chart.getXScale().setCategory(tempDataSource.getDataSet(0), true);
    chart.getYScale(0).setStepUnit(Double.valueOf(5.), Double.valueOf(1.));
    chart.getYScale(0).setTitle("Celsius", -90);
    chart.getYScale(0).setTitlePlacement(80);

    IlvDataInterval inter = new IlvDataInterval(chart.getXScale().getStepsDefinition().previousStep(5),
        chart.getXScale().getStepsDefinition().incrementStep(6));
    IlvDataIndicator weInd = new IlvDataIndicator(-1, inter, null);
    weInd.setStyle(new IlvStyle(Color.black, IlvColor.wheat));
    chart.addDecoration(weInd);

    // Add a label annotation for every data points of the renderer series.
    // We want to display the min or max temperature, so we set the
    // labelling mode to Y_VALUE_LABEL.
    tempRenderer.setDataLabeling(IlvChartRenderer.Y_VALUE_LABEL);
    tempRenderer.setAnnotation(new IlvDataLabelAnnotation());

    // Adding a legend to the chart. The legend is placed within the
    // chart area, at an absolute position. When a legend is added with
    // the ABSOLUTE constraint and its moveable property value is true (the
    // default value), it can be dragged within the chart. The legend is
    // said in "floating" mode.
    IlvLegend legend = new IlvLegend();
    legend.setLocation(50, 30);
    chart.addLegend(legend, IlvChartLayout.ABSOLUTE);

    // Add a title to the chart. We use a JLabel initialize with the title
    // text and add it to the chart as the header. Note that the IlvChart
    // class provides two convenience methods that can be used in this special
    // case when you just want to set a label as a header or footer.
    // These method are the IlvChart.setHeaderText() and
    // IlvChart.setFooterText()
    // methods. The lines below could be resume to
    // chart.setHeaderText("Temperatures of the week");
    JLabel label = new JLabel("Temperatures of the week", JLabel.CENTER);
    chart.setHeader(label);

    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(chart, BorderLayout.CENTER);
    setSize(500, 400);
  }

  public static void main(String[] args) {
    // This sample uses JViews Charts features. When deploying an
    // application that includes this code, you need to be in possession
    // of a Perforce JViews Charts Deployment license.
    IlvProductUtil.DeploymentLicenseRequired(IlvProductUtil.JViews_Charts_Deployment);

    SwingUtilities.invokeLater(new Runnable() {
      Override
      public void run() {
        CustomCartesian frame = new CustomCartesian();
        frame.setVisible(true);
      }
    });
  }
}