Example: SplineData Chart

This example makes use of the SplineData class as well as the two spline smoothing classes in the package com.imsl.math. This class can be used either as an applet or as an application.
import com.imsl.math.*;
import com.imsl.chart.*;
import com.imsl.stat.Random;
import java.awt.Color;


public class SplineDataEx1 extends javax.swing.JApplet {
    static private final int nData = 21;
    static private final int nSpline = 100;
    
    private JPanelChart panel;
    
    public void init() {
        Chart chart = new Chart(this);
        panel = new JPanelChart(chart);
        getContentPane().add(panel, java.awt.BorderLayout.CENTER);
        setup(chart);
    }
    
    static private void setup(Chart chart) {
        chart.getChartTitle().setTitle(new Text("Smoothed Spline"));
        
        Legend legend = chart.getLegend();
        legend.setTitle(new Text("Legend"));
        legend.setViewport(0.7, 0.9, 0.1, 0.3);
        legend.setPaint(true);

        // Original data
        double xData[] = grid(nData);
        double yData[] = new double[nData];
        for (int k = 0;  k < nData;  k++) {
            yData[k] = f(xData[k]);
        }
        AxisXY axis = new AxisXY(chart);
        Data data = new Data(axis, xData, yData);
        data.setDataType(data.DATA_TYPE_MARKER);
        data.setMarkerType(Data.MARKER_TYPE_HOLLOW_CIRCLE);
        data.setMarkerColor(Color.red);
        data.setTitle("Original Data");
        
        // Noisy data
        Random random = new Random(123457);
        double yNoisy[] = new double[nData];
        for (int k = 0;  k < nData;  k++) {
            yNoisy[k] = yData[k] + (2.*random.nextDouble()-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.orange, "CsSmoothC2");
    }
    
    
    static 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 static void main(String argv[]) {
        JFrameChart frame = new JFrameChart();
        SplineDataEx1.setup(frame.getChart());
        frame.show();
    }
}



Output

eqn_0149

Link to Java source.