/*
 * 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.BorderFactory;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import ilog.views.chart.IlvChart;
import ilog.views.chart.IlvChartRenderer;
import ilog.views.chart.IlvDataInterval;
import ilog.views.chart.IlvStyle;
import ilog.views.chart.data.IlvDefaultDataSource;
import ilog.views.chart.graphic.IlvDataIndicator;
import ilog.views.chart.renderer.IlvBarChartRenderer;
import ilog.views.util.IlvProductUtil;

public class DataIndicator extends JFrame {
  private static final Color CHART_FOREGROUND_COLOR = new Color(102, 102, 153);
  private static final IlvStyle INDICATOR_STYLE = new IlvStyle(new Color(49, 106, 197), new Color(198, 214, 239, 128));
  private static final Color INDIC_FILL_COLOR = new Color(204, 204, 255);

  public DataIndicator() {
    super("Creating a Data Indicator");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    init();
  }

  private void init() {
    // Initialize a new IlvChart
    IlvChart chart = createChart();
    double[][] values = {
        { 19.0, 15.0, 13.0, 18.0, 12.7, 16.3, 21.0, 13.4, 15.3, 13.8, 25.8, 16.1, 21.2, 23.6, 27.1, 24.1 },
        { 13.4, 16, 16.8, 19.8, 23.1, 23.0, 21.3, 22.5, 21.2, 23.6, 27.1, 24.1, 17.0, 19.0, 16.0, 13.0 } };
    IlvDefaultDataSource ds = new IlvDefaultDataSource(values, -1, new String[] { "DataSet 1", "DataSet 2" }, null);
    IlvChartRenderer r = createRenderer();
    r.setDataSource(ds);
    chart.addRenderer(r);

    // ----------------------------------------------------------------------
    // -- Data Indicators

    // A data indictor that highlights the range [5,13] along the x-axis.
    IlvDataIndicator indic = new IlvDataIndicator(-1, new IlvDataInterval(5, 13), null);
    // set the rendering style
    indic.setStyle(INDICATOR_STYLE);
    chart.addDecoration(indic);

    // A data indicator that indicates the value 23 as a threshold line
    // along the y-axis. It displays the 'Threshold' value.
    indic = new IlvDataIndicator(0, 23, "Threshold");
    indic.setStyle(INDICATOR_STYLE);
    // change its draw order so that it is drawn ABOVE renderers
    indic.setDrawOrder(IlvChart.DRAW_ABOVE);
    // customizer its label renderer
    indic.getLabelRenderer().setOpaque(true);
    indic.getLabelRenderer().setBorder(BorderFactory.createLineBorder(CHART_FOREGROUND_COLOR));
    indic.getLabelRenderer().setBackground(INDIC_FILL_COLOR);
    chart.addDecoration(indic);

    getContentPane().add(chart, BorderLayout.CENTER);
    setSize(400, 320);

  }

  protected IlvChart createChart() {
    IlvChart chart = new IlvChart();
    chart.setForeground(CHART_FOREGROUND_COLOR);
    chart.setAntiAliasing(true);
    chart.setBackground(Color.white);
    chart.setOpaque(true);
    chart.getChartArea().setPlotBackground(new Color(255, 245, 235));
    return chart;
  }

  protected IlvChartRenderer createRenderer() {
    IlvChartRenderer r = new IlvBarChartRenderer(IlvBarChartRenderer.SUPERIMPOSED);
    r.setStyles(new IlvStyle[] { new IlvStyle(new Color(249, 128, 114), new Color(183, 225, 204, 180)) });
    return r;
  }

  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() {
        DataIndicator frame = new DataIndicator();
        frame.setVisible(true);
      }
    });
  }

}