/*
 * -------------------------------------------------------------------------
 *    $Id: SplineChart.java,v 1.6 2004/09/01 18:03:10 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.chart;
import com.imsl.chart.*;
import com.imsl.math.*;
import java.awt.*;
import javax.swing.*;


public class SplineChart extends JPanel///JApplet
{
    private Chart    chart = null;
    static private final int nData = 21, nSpline = 100;

    public void init()
    {
        chart = new Chart(this);
        chart.setDoubleBuffering(true);
        chart.getChartTitle().setTitle(new Text("Smoothed Spline"));
        
        Legend legend = chart.getLegend();
        legend.setTitle(new Text("Legend"));
        //gend.setFillType(legend.FILL_TYPE_SOLID);
        //legend.setFillColor(Color.yellow);
        legend.setPaint(true);
        
        AxisXY axis = new AxisXY(chart);
        Data    data;

        // Original data
        double xData[] = grid(nData);
        double yData[] = new double[nData];
        for (int k = 0;  k < nData;  k++) {
            yData[k] = f(xData[k]);
        }
        data = new Data(axis, xData, yData);
        data.setDataType(Data.DATA_TYPE_MARKER);
        data.setMarkerType(Data.MARKER_TYPE_HOLLOW_CIRCLE);
        data.setMarkerColor(Color.green);
        data.setTitle("Original Data");

        // Noisy data
        double yNoisy[] = new double[nData];
        for (int k = 0;  k < nData;  k++) {
            yNoisy[k] = yData[k] + (2.*Math.random()-1.);
        }
        data = new Data(axis, xData, yNoisy);
        data.setDataType(Data.DATA_TYPE_MARKER);
        data.setMarkerType(Data.MARKER_TYPE_FILLED_SQUARE);
        data.setMarkerSize(0.75);
        data.setMarkerColor(Color.blue);
        data.setTitle("Noisy Data");

        chartSpline(axis, new CsSmooth(xData, yData), Color.red, "CsSmooth");
        chartSpline(axis, new CsSmoothC2(xData, yData, nData), Color.green, "CsSmoothC2");

        new ToolTip(chart);
    }
    

    private void chartSpline(AxisXY axis, Spline spline, Color color, String title)
    {
        Data data = new SplineData(axis, spline);
        data.setDataType(Data.DATA_TYPE_LINE);
        data.setLineColor(color);
        data.setTitle(title);
    }


    static private double[] grid(int nData)
    {
        double xData[] = new double[nData];
        for (int k = 0;  k < nData;  k++) {
            xData[k] = 3.0*k / (double)(nData-1);
        }
        return xData;
    }

    static private double f(double x)
    {
        return 1.0/(0.1+Math.pow(3.0*(x-1.0),4));
    }


    public void update(Graphics g)
    {
        chart.update(g);
    }
    
    public void paint(Graphics g)
    {
        chart.paint(g);
    }


    public static void main(String argv[])
    {
        SplineChart ex = null;

        JFrame frame = new JFrame();
        ex = new SplineChart();
        frame.getContentPane().add(ex);
        ex.init();
        frame.setSize(new Dimension(400,400));
        frame.show();
    }

}