view yetilab/plot/plot.yeti @ 125:0f362d1de06e

Toward stacked charts
author Chris Cannam
date Sat, 20 Apr 2013 12:14:40 +0100
parents 83f127d60c58
children 80eb9b6d4fb9
line wrap: on
line source
module yetilab.plot.plot;

import org.jzy3d.plot3d.builder: Mapper;
import org.jzy3d.maths: Range, Coord3d;
import org.jzy3d.plot3d.primitives: Shape, HistogramBar, FlatLine2d, Polygon, Point;
import org.jzy3d.plot3d.primitives.axes.layout.providers: StaticTickProvider;
import org.jzy3d.plot3d.primitives.axes.layout.renderers: TickLabelMap;
import org.jzy3d.chart: Chart, ChartLauncher;
import org.jzy3d.plot3d.builder: Builder;
import org.jzy3d.plot3d.builder.concrete: OrthonormalGrid;
import org.jzy3d.colors.colormaps: ColorMapRainbow;
import org.jzy3d.colors: ColorMapper, Color;
import org.jzy3d.plot3d.rendering.canvas: Quality;
import org.jzy3d.plot3d.rendering.view.modes: ViewPositionMode;

newMatrixMapper matrix =
   (class MMapper extends Mapper
        double f(double x, double y)
            result = matrix.getAt y x;
            println "f(\(x),\(y)) -> \(result)";
            result
    end;
    new MMapper());

newMatrixLogMapper matrix =
   (class MMapper extends Mapper
        double f(double x, double y)
            ln (matrix.getAt y x)
    end;
    new MMapper());

newMapper mapFunction =
   (class FMapper extends Mapper
        double f(double x, double y)
            mapFunction x y
    end;
    new FMapper());

plotMatrix matrix =
   (mapper = newMatrixMapper matrix;
    size = matrix.size;
    xrange = new Range(0, size.columns - 1);
    yrange = new Range(0, size.rows - 1);
    grid = new OrthonormalGrid(xrange, size.columns, yrange, size.rows);
    println "Matrix size: \(size)";
    surface = Builder#buildOrthonormal(grid, mapper); //??? big?
    println "Z Bounds: \(surface#getBounds()#getZmin()) -> \(surface#getBounds()#getZmax())";
    surface#setFaceDisplayed(true);
    surface#setWireframeDisplayed(true);
    surface#setWireframeColor(Color#BLACK);
//    chart = new Chart(Quality#Fastest, "swing");
    chart = new Chart(Quality#Nicest);
    chart#getScene()#getGraph()#add(surface);
    ChartLauncher#openChart(chart);
    ());

plotBarChart keys unit values =
   (chart = new Chart(Quality#Nicest);
    var n = 0;
    scene = chart#getScene();
    ticks = new float[length keys];
    tickLabels = new TickLabelMap();
    for keys do k:
        bar = new HistogramBar();
        bar#setData(new Coord3d(n, 0, 0), values[k], 0.45, Color#random());
        bar#setWireframeDisplayed(false);
        scene#add(bar);
        ticks[n] := n;
        tickLabels#register(n, k);
        n := n + 1;
    done;
    chart#getView()#setViewPoint(new Coord3d(pi/2, 0, 0));
    axes = chart#getAxeLayout();
    axes#setXAxeLabelDisplayed(false);
    axes#setYAxeLabelDisplayed(false);
    axes#setZAxeLabelDisplayed(true);
    axes#setZAxeLabel(unit);
    axes#setXTickProvider(new StaticTickProvider(ticks));
    axes#setXTickRenderer(tickLabels);
    axes#setYTickLabelDisplayed(false);
    ChartLauncher#openChart(chart);
    ());

plotLines xkeys unit values =
   (chart = new Chart(Quality#Nicest);
    scene = chart#getScene();
    keys = keys values;
    n = length xkeys;
    var z = 0;
    for keys do k:
        v = values[k];
        x = new float[n];
        y = new float[n];
        var i = 0;
        for xkeys do xk:
            x[i] := i;
            y[i] := if xk in v then v[xk] else 0 fi;
            i := i + 1;
        done;
        line = new FlatLine2d(x, y, z);
        line#setWireframeDisplayed(true);
        line#setWireframeColor(Color#random());
        line#setWireframeWidth(2);
        line#setFaceDisplayed(false);
        scene#add(line);
        z := z + 1;
    done;
    chart#getView()#setViewPoint(new Coord3d(0, 0, 0));
    axes = chart#getAxeLayout();
    axes#setXAxeLabelDisplayed(false);
    axes#setYAxeLabelDisplayed(false);
    axes#setZAxeLabelDisplayed(true);
    axes#setZAxeLabel(unit);
    axes#setYTickLabelDisplayed(false);
    ChartLauncher#openChart(chart);
    ());

stack keys xkeys values =
   (stacked = mapIntoHash id \(mapIntoHash id \0 xkeys) keys;
    prev = mapIntoHash id \0 xkeys;
    for xkeys do xk:
        for keys do k:
            value = if xk in values[k] then values[k][xk] else 0 fi;
            height = prev[xk] + value;
            stacked[k][xk] := height;
            prev[xk] := height;
        done;
    done;
    stacked);

plotStacked keys xkeys unit values =
   (chart = new Chart(Quality#Nicest);
    scene = chart#getScene();
    stacked = stack keys xkeys values;
    var z = 0;
    for keys do k:
        heights = stacked[k];
        poly = new Polygon();
        poly#setWireframeDisplayed(true);
        poly#setWireframeColor(Color#random());
        poly#setFaceDisplayed(true);
        poly#setColor(Color#random());
        poly#add(new Point(new Coord3d(0, 0, z)));
        var x = 0;
        for xkeys do xk:
            poly#add(new Point(new Coord3d(x, heights[xk], z)));
            x := x + 1;
        done;
        poly#add(new Point(new Coord3d(x - 1, 0, z)));
        scene#add(poly);
        z := z + 1;
    done;
    chart#getView()#setViewPoint(new Coord3d(0, 0, 0));
    axes = chart#getAxeLayout();
    axes#setXAxeLabelDisplayed(false);
    axes#setYAxeLabelDisplayed(false);
    axes#setZAxeLabelDisplayed(true);
    axes#setZAxeLabel(unit);
    axes#setYTickLabelDisplayed(false);
    ChartLauncher#openChart(chart);
    ());

plotStructure structure =
    case structure of
    Grid matrix:
        plotMatrix matrix;
    //!!!
    _: failWith "Cannot plot this structure (only grids implemented so far)";
    esac;

{
    newMatrixMapper,
    newMatrixLogMapper,
    newMapper,
    plotMatrix, 
    plotBarChart,
    plotLines,
    stack,
    plotStacked,
    plotStructure,
}