/*
 * 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.IlvLabelRenderer;
import ilog.views.chart.IlvScaleAnnotation;
import ilog.views.chart.IlvStyle;
import ilog.views.chart.data.IlvDefaultDataSource;
import ilog.views.chart.renderer.IlvBubbleChartRenderer;
import ilog.views.util.IlvProductUtil;

public class ScaleAnnotation extends JFrame {
  private static final Color ANNO_BORDER_COLOR = new Color(102, 102, 153);
  private static final Color ANNO_FILL_COLOR = new Color(204, 204, 255);

  public ScaleAnnotation() {
    super("Adding Annotations in Scales");
    setDefaultCloseOperation(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);

    // ----------------------------------------------------------------------
    // -- Scale Annotations

    // An X-Scale annotation displaying the value of the abscisse of a given
    // data point (here the x-value is 7)
    IlvScaleAnnotation anno = new IlvScaleAnnotation(7);
    // customize the annotation rendering
    initAnno(anno);
    // add the annotation to the x-scale
    chart.getXScale().addAnnotation(anno);

    // A Y-Scale annotation displaying the string "Threshold" at y = 20.
    anno = new IlvScaleAnnotation(20);
    initAnno(anno);
    // Set the annotation text (could have been set in the ctor too).
    anno.setLabel("Threshold");
    // add the annotation to the y-scale
    chart.getYScale(0).addAnnotation(anno);
    // A Y-Scale annotation at y = 25
    anno = new IlvScaleAnnotation(25);
    initAnno(anno);
    chart.getYScale(0).addAnnotation(anno);

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

  }

  protected IlvChart createChart() {
    IlvChart chart = new IlvChart();
    chart.setForeground(ANNO_BORDER_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 IlvBubbleChartRenderer();
    r.setStyles(new IlvStyle[] { new IlvStyle(new Color(249, 128, 114), new Color(183, 225, 204, 180)) });
    return r;
  }

  protected void initAnno(IlvScaleAnnotation anno) {
    IlvLabelRenderer labelR = anno.getLabelRenderer();
    labelR.setBorder(BorderFactory.createLineBorder(ANNO_BORDER_COLOR));
    labelR.setBackground(ANNO_FILL_COLOR);
    labelR.setOpaque(true);
  }

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