Example: Fisher Iris Data marked by spheres

The classic Fisher iris data is plotted in this chart. This example shows use of the 3D marker type. A sphere has been chosen in this example to highlight data points.


import com.imsl.chart3d.*;
import com.imsl.io.FlatFile;
import java.awt.Color;
import java.io.*;
import java.sql.SQLException;
import java.util.StringTokenizer;

public class DataEx2 extends JFrameChart3D {

    private int species[];
    private double sepalLength[];
    private double sepalWidth[];
    private double petalLength[];
    private double petalWidth[];

    public DataEx2() throws IOException, SQLException {
        read();

        Chart3D chart = getChart3D();
        chart.getBackground().setFillColor("lightyellow");
        AxisXYZ axis = new AxisXYZ(chart);
        axis.setAxisTitlePosition(AxisXYZ.AXIS_TITLE_PARALLEL);

        axis.getAxisX().getAxisTitle().setTitle("Sepal Length");
        axis.getAxisY().getAxisTitle().setTitle("Sepal Width");
        axis.getAxisZ().getAxisTitle().setTitle("Petal Length");

        axis.setDataType(Data.DATA_TYPE_MARKER);
        axis.setMarkerType(Data.MARKER_TYPE_SPHERE);
        Color color[] = {java.awt.Color.red, java.awt.Color.green,
            java.awt.Color.blue};

        for (int k = 0; k < species.length; k++) {
            // marker type = Species
            // x = Sepal Length
            // y = Sepal Width
            // z = Petal Length
            // marker size = Petal Width
            double xp[] = {sepalLength[k]};
            double yp[] = {sepalWidth[k]};
            double zp[] = {petalLength[k]};
            Data data = new Data(axis, xp, yp, zp);
            data.setMarkerSize(Math.sqrt(petalWidth[k]));
            data.setMarkerColor(color[species[k] - 1]);
        }
        setSize(375, 375);
        render();
    }

    private void read() throws IOException, SQLException {
        InputStream is = getClass().getResourceAsStream("FisherIris.csv");
        FisherIrisReader fisherIrisReader = new FisherIrisReader(is);
        int nObs = 150;
        species = new int[nObs];
        sepalLength = new double[nObs];
        sepalWidth = new double[nObs];
        petalLength = new double[nObs];
        petalWidth = new double[nObs];
        for (int k = 0; fisherIrisReader.next(); k++) {
            species[k] = fisherIrisReader.getInt("Species");
            sepalLength[k] = fisherIrisReader.getDouble("Sepal Length");
            sepalWidth[k] = fisherIrisReader.getDouble("Sepal Width");
            petalLength[k] = fisherIrisReader.getDouble("Petal Length");
            petalWidth[k] = fisherIrisReader.getDouble("Petal Width");
        }
    }

    static private class FisherIrisReader extends FlatFile {

        public FisherIrisReader(InputStream is) throws IOException {
            super(new BufferedReader(new InputStreamReader(is)));
            String line = readLine();
            StringTokenizer st = new StringTokenizer(line, ",");
            for (int j = 0; st.hasMoreTokens(); j++) {
                setColumnName(j + 1, st.nextToken().trim());
                setColumnClass(j, Double.class);
            }
        }
    }

    public static void main(String args[]) throws IOException, SQLException {
        new DataEx2().setVisible(true);
    }
}

Output

Link to Java source.