/*
 * -------------------------------------------------------------------------
 *      $Id: ChartSeries.java,v 1.7 2004/05/26 18:54:54 estewart Exp $
 * -------------------------------------------------------------------------
 *      Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved.
 *
 *      This software is confidential information which is proprietary to
 *      and a trade secret of Visual Numerics, Inc.  Use, duplication or
 *      disclosure is subject to the terms of an appropriate license
 *      agreement.
 *
 *      VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 *      SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 *      BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 *      FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL
 *      NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 *      AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 *      ITS DERIVATIVES.
 *--------------------------------------------------------------------------
 */

package com.imsl.demo.risk;
import com.imsl.chart.*;
import java.text.*;
import java.util.*;

/**
 *  Chart the raw timeseries.
 *
 * @author  brophy
 * @created January 31, 2002
 */
public class ChartSeries extends JFrameChart {
    static private final DateFormat DATE_FORMAT_YEAR = new SimpleDateFormat("MMMyy");
    private String color[] = {"black", "red", "blue", "green", "gold"};

    boolean isRaw = true;

    /** Creates new ChartSeries */
    public ChartSeries(Database db) {
        setTitle("Risk Analysis");
        Object l[] = getListeners(java.awt.event.WindowListener.class);
        for (int k = 0;  k < l.length;  k++) {
            removeWindowListener((java.awt.event.WindowListener)l[k]);
        }

        Chart chart = getChart();
        //chart.getLegend().setPaint(true);
        //chart.getBackground().setFillColor(new java.awt.Color(240,240,240));
        if (isRaw) {
            chart.getChartTitle().setTitle("Raw Time Series");
        } else {
            chart.getChartTitle().setTitle("Daily Returns");
        }

        Date dateStart = new GregorianCalendar(1990, 0, 1).getTime();
        Date dateEnd = new GregorianCalendar(2002, 11, 31).getTime();
        double ticks[] = new double[7];
        for (int j = 0;  j < ticks.length;  j++) {
            ticks[j] = new GregorianCalendar(1990+2*j, 0, 1).getTimeInMillis();
        }
        double tCross = new GregorianCalendar(1990, 0, 1).getTimeInMillis();

        String ticker[] = db.getTickers();
        for (int k = 0;  k < ticker.length;  k++) {
            Database.Series series = db.getSeries(ticker[k]);
            AxisXY axis = new AxisXY(chart);
            axis.setViewport(0.35, 0.98, 0.2, 0.9);
            axis.setLineColor(color[k]);
            axis.setTextColor(color[k]);

            axis.getAxisX().setAutoscaleInput(AxisXY.AUTOSCALE_OFF);
            axis.getAxisX().setWindow(dateStart.getTime(), dateEnd.getTime());
            axis.getAxisX().getAxisLabel().setTextFormat(DATE_FORMAT_YEAR);
            axis.getAxisX().setTicks(ticks);

            axis.getAxisY().getAxisLabel().setTextFormat(new java.text.DecimalFormat("0.E0"));
            axis.getAxisY().getAxisUnit().setTitle(ticker[k]);
            //axis.getAxisY().getAxisUnit().setFontStyle(java.awt.Font.BOLD);

            axis.setCross(tCross, 0.0);
            tCross -= 1000.*3600.*24.*500.;
            if (!ticker[k].equals("spx")) {
                axis.getAxisX().setPaint(false);
            }
            Data data;
            if (isRaw) {
                data = new Data(axis, series.date, series.close);
            } else {
                double x[] = new double[series.close.length-2];
                double y[] = new double[series.close.length-2];
                for (int j = 0;  j < y.length;  j++) {
                    x[j] = series.date[j+1];
                    y[j] = (series.close[j+1]-series.close[j])/series.close[j];
                }
                data = new Data(axis, x, y);
            }
            data.setTitle(ticker[k]);
        }
    }
}