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

import ilog.views.chart.IlvChart;
import ilog.views.chart.IlvChartLayout;
import ilog.views.chart.IlvLegend;
import ilog.views.chart.data.IlvCombinedDataSet;
import ilog.views.chart.data.IlvDataSet;
import ilog.views.chart.data.IlvDefaultDataSource;
import ilog.views.chart.event.DataSetContentsEvent;
import ilog.views.chart.util.IlvArrays;
import ilog.views.util.IlvProductUtil;

public class DataSetCombination extends JFrame {

  /**
   * A data set that combines the y values of several other data sets.
   */
  public static abstract class YOperatorDataSet extends IlvCombinedDataSet {
    /**
     * Creates a new <code>YOperatorDataSet</code> with the specified name.
     */
    public YOperatorDataSet(String name) {
      setName(name);
    }

    /**
     * Returns the data resulting from an operation on the specified series.
     */
    protected abstract double getData(double[] values);

    /**
     * Returns the y value of the data point at the specified index.
     */
    Override
    public double getYData(int idx) {
      int count = getDataSetCount();
      double[] values = new double[count];
      for (int i = 0; i < count; ++i)
        values[i] = getDataSet(i).getYData(idx);
      return getData(values);
    }

    /**
     * Returns the x value of the data point at the specified index.
     */
    Override
    public double getXData(int idx) {
      return getDataSet(0).getXData(idx);
    }

    /**
     * Called when one of the data sets referenced by this object has been
     * modified.
     */
    Override
    protected void dataSetContentsChanged(DataSetContentsEvent evt) {
      super.dataSetContentsChanged(evt);
      int count = getDataCount();
      if (evt.getType() != DataSetContentsEvent.FULL_UPDATE && count == 0) // Not
                                                                           // enough
                                                                           // data
                                                                           // yet.
        return;
      if (evt.getType() == DataSetContentsEvent.DATA_ADDED && evt.getFirstIdx() != count)
        return; // Wait for data to be added to all original data sets.
      // Send a new event with this data set as source.
      fireDataSetContentsEvent(new DataSetContentsEvent(this, evt));
    }
  }

  /**
   * A combined data set that computes the mean value of a given category.
   */
  public static class Average extends YOperatorDataSet {
    /**
     * Creates a new <code>AverageCombinedDataSet</code> object.
     * 
     * @param name
     *          The name of this data set.
     * @param dataSets
     *          The referenced data sets.
     */
    public Average(String name, IlvDataSet[] dataSets) {
      super(name);
      setDataSets(dataSets);
    }

    /**
     * Returns the average value of the specified series.
     */
    Override
    protected double getData(double[] values) {
      double total = 0.;
      for (int i = 0; i < values.length; ++i) {
        total += values[i];
      }
      return total / values.length;
    }
  }

  static final int COUNT = 101;

  public DataSetCombination() {
    super("Computing a Data Set From Other Data Sets");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    IlvChart chart = new IlvChart();
    IlvDefaultDataSource ds = new IlvDefaultDataSource(
        new double[][] { IlvArrays.randomValues(COUNT, 0, 50), IlvArrays.randomValues(COUNT, 0, 50),
            IlvArrays.randomValues(COUNT, 0, 50) },
        -1, new String[] { "Data Set 1", "Data Set 2", "Data Set 3" }, null);
    ds.addDataSet(new Average("Average", ds.getDataSets()));
    chart.setDataSource(ds);
    chart.addLegend(new IlvLegend(), IlvChartLayout.SOUTH_BOTTOM);

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