annotate yetilab/plot/plot.yeti @ 131:70eaf374a1b5

Make charts non-animated; add a list of nice colours
author Chris Cannam
date Mon, 22 Apr 2013 10:24:44 +0100
parents aef0fbc65f16
children f268b2982ed4
rev   line source
Chris@108 1 module yetilab.plot.plot;
Chris@108 2
Chris@108 3 import org.jzy3d.plot3d.builder: Mapper;
Chris@128 4 import org.jzy3d.plot3d.text.drawable: DrawableTextBillboard, DrawableTextBitmap;
Chris@119 5 import org.jzy3d.maths: Range, Coord3d;
Chris@126 6 import org.jzy3d.plot3d.primitives: Shape, HistogramBar, FlatLine2d, Polygon, Quad, Point;
Chris@119 7 import org.jzy3d.plot3d.primitives.axes.layout.providers: StaticTickProvider;
Chris@119 8 import org.jzy3d.plot3d.primitives.axes.layout.renderers: TickLabelMap;
Chris@108 9 import org.jzy3d.chart: Chart, ChartLauncher;
Chris@108 10 import org.jzy3d.plot3d.builder: Builder;
Chris@108 11 import org.jzy3d.plot3d.builder.concrete: OrthonormalGrid;
Chris@108 12 import org.jzy3d.colors.colormaps: ColorMapRainbow;
Chris@119 13 import org.jzy3d.colors: ColorMapper, Color;
Chris@108 14 import org.jzy3d.plot3d.rendering.canvas: Quality;
Chris@119 15 import org.jzy3d.plot3d.rendering.view.modes: ViewPositionMode;
Chris@108 16
Chris@131 17 chartColours = [
Chris@131 18 new Color(161, 54, 2), // red
Chris@131 19 new Color(207, 228, 148), // grey-green
Chris@131 20 new Color(82, 126, 154), // dark steel blue
Chris@131 21 new Color(251, 116, 43), // light red
Chris@131 22 new Color(126, 33, 28), // dried blood!
Chris@131 23 new Color(21, 183, 197), // light blue
Chris@131 24 new Color(188, 13, 207), // mid purple
Chris@131 25 new Color(200, 125, 234), // light purple
Chris@131 26 ];
Chris@131 27
Chris@108 28 newMatrixMapper matrix =
Chris@108 29 (class MMapper extends Mapper
Chris@108 30 double f(double x, double y)
Chris@117 31 result = matrix.getAt y x;
Chris@117 32 println "f(\(x),\(y)) -> \(result)";
Chris@117 33 result
Chris@108 34 end;
Chris@108 35 new MMapper());
Chris@108 36
Chris@108 37 newMatrixLogMapper matrix =
Chris@108 38 (class MMapper extends Mapper
Chris@108 39 double f(double x, double y)
Chris@108 40 ln (matrix.getAt y x)
Chris@108 41 end;
Chris@108 42 new MMapper());
Chris@108 43
Chris@108 44 newMapper mapFunction =
Chris@108 45 (class FMapper extends Mapper
Chris@108 46 double f(double x, double y)
Chris@108 47 mapFunction x y
Chris@108 48 end;
Chris@108 49 new FMapper());
Chris@108 50
Chris@115 51 plotMatrix matrix =
Chris@117 52 (mapper = newMatrixMapper matrix;
Chris@108 53 size = matrix.size;
Chris@119 54 xrange = new Range(0, size.columns - 1);
Chris@119 55 yrange = new Range(0, size.rows - 1);
Chris@108 56 grid = new OrthonormalGrid(xrange, size.columns, yrange, size.rows);
Chris@112 57 println "Matrix size: \(size)";
Chris@117 58 surface = Builder#buildOrthonormal(grid, mapper); //??? big?
Chris@117 59 println "Z Bounds: \(surface#getBounds()#getZmin()) -> \(surface#getBounds()#getZmax())";
Chris@121 60 surface#setFaceDisplayed(true);
Chris@108 61 surface#setWireframeDisplayed(true);
Chris@108 62 surface#setWireframeColor(Color#BLACK);
Chris@112 63 // chart = new Chart(Quality#Fastest, "swing");
Chris@112 64 chart = new Chart(Quality#Nicest);
Chris@108 65 chart#getScene()#getGraph()#add(surface);
Chris@115 66 ChartLauncher#openChart(chart);
Chris@115 67 ());
Chris@108 68
Chris@119 69 plotBarChart keys unit values =
Chris@131 70 (quality = Quality#Nicest;
Chris@131 71 quality#setAnimated(false);
Chris@131 72 chart = new Chart(quality);
Chris@119 73 var n = 0;
Chris@119 74 scene = chart#getScene();
Chris@119 75 ticks = new float[length keys];
Chris@119 76 tickLabels = new TickLabelMap();
Chris@119 77 for keys do k:
Chris@119 78 bar = new HistogramBar();
Chris@119 79 bar#setData(new Coord3d(n, 0, 0), values[k], 0.45, Color#random());
Chris@119 80 bar#setWireframeDisplayed(false);
Chris@119 81 scene#add(bar);
Chris@119 82 ticks[n] := n;
Chris@119 83 tickLabels#register(n, k);
Chris@119 84 n := n + 1;
Chris@119 85 done;
Chris@119 86 chart#getView()#setViewPoint(new Coord3d(pi/2, 0, 0));
Chris@119 87 axes = chart#getAxeLayout();
Chris@119 88 axes#setXAxeLabelDisplayed(false);
Chris@119 89 axes#setYAxeLabelDisplayed(false);
Chris@119 90 axes#setZAxeLabelDisplayed(true);
Chris@119 91 axes#setZAxeLabel(unit);
Chris@119 92 axes#setXTickProvider(new StaticTickProvider(ticks));
Chris@119 93 axes#setXTickRenderer(tickLabels);
Chris@119 94 axes#setYTickLabelDisplayed(false);
Chris@119 95 ChartLauncher#openChart(chart);
Chris@119 96 ());
Chris@119 97
Chris@124 98 plotLines xkeys unit values =
Chris@131 99 (quality = Quality#Nicest;
Chris@131 100 quality#setAnimated(false);
Chris@131 101 chart = new Chart(quality);
Chris@124 102 scene = chart#getScene();
Chris@124 103 keys = keys values;
Chris@124 104 n = length xkeys;
Chris@124 105 var z = 0;
Chris@124 106 for keys do k:
Chris@124 107 v = values[k];
Chris@124 108 x = new float[n];
Chris@124 109 y = new float[n];
Chris@124 110 var i = 0;
Chris@124 111 for xkeys do xk:
Chris@124 112 x[i] := i;
Chris@124 113 y[i] := if xk in v then v[xk] else 0 fi;
Chris@124 114 i := i + 1;
Chris@124 115 done;
Chris@124 116 line = new FlatLine2d(x, y, z);
Chris@124 117 line#setWireframeDisplayed(true);
Chris@124 118 line#setWireframeColor(Color#random());
Chris@124 119 line#setWireframeWidth(2);
Chris@124 120 line#setFaceDisplayed(false);
Chris@124 121 scene#add(line);
Chris@124 122 z := z + 1;
Chris@124 123 done;
Chris@124 124 chart#getView()#setViewPoint(new Coord3d(0, 0, 0));
Chris@124 125 axes = chart#getAxeLayout();
Chris@124 126 axes#setXAxeLabelDisplayed(false);
Chris@124 127 axes#setYAxeLabelDisplayed(false);
Chris@124 128 axes#setZAxeLabelDisplayed(true);
Chris@124 129 axes#setZAxeLabel(unit);
Chris@124 130 axes#setYTickLabelDisplayed(false);
Chris@124 131 ChartLauncher#openChart(chart);
Chris@124 132 ());
Chris@124 133
Chris@129 134 stack keys xkeys values normalised =
Chris@127 135 (stacked = mapIntoHash id \(mapIntoHash id \{ y0 = 0, y1 = 0 } xkeys) keys;
Chris@125 136 prev = mapIntoHash id \0 xkeys;
Chris@129 137 valueOf k xk = if xk in values[k] then values[k][xk] else 0 fi;
Chris@125 138 for xkeys do xk:
Chris@129 139 total = sum (map do k: valueOf k xk done keys);
Chris@125 140 for keys do k:
Chris@129 141 value =
Chris@129 142 if normalised and total > 0
Chris@129 143 then (valueOf k xk) / total
Chris@129 144 else (valueOf k xk)
Chris@129 145 fi;
Chris@125 146 height = prev[xk] + value;
Chris@127 147 stacked[k][xk] := { y0 = prev[xk], y1 = height };
Chris@125 148 prev[xk] := height;
Chris@125 149 done;
Chris@125 150 done;
Chris@125 151 stacked);
Chris@125 152
Chris@129 153 plotStacked keys xkeys unit values normalised =
Chris@128 154 (quality = Quality#Nicest;
Chris@131 155 quality#setAnimated(false);
Chris@128 156 chart = new Chart(quality);
Chris@125 157 scene = chart#getScene();
Chris@129 158 stacked = stack keys xkeys values normalised;
Chris@125 159 var z = 0;
Chris@127 160 newRect x y0 y1 z colour is number -> number -> number -> number -> ~Color -> 'a =
Chris@126 161 (poly = new Quad();
Chris@127 162 poly#add(new Point(new Coord3d(x + 0.5, z, y0)));
Chris@127 163 poly#add(new Point(new Coord3d(x + 0.5, z, y1)));
Chris@127 164 poly#add(new Point(new Coord3d(x - 0.5, z, y1)));
Chris@127 165 poly#add(new Point(new Coord3d(x - 0.5, z, y0)));
Chris@126 166 poly#setWireframeDisplayed(true);
Chris@126 167 poly#setWireframeColor(colour);
Chris@126 168 poly#setFaceDisplayed(true);
Chris@126 169 poly#setColor(colour);
Chris@126 170 poly);
Chris@130 171 var ty = 0;
Chris@125 172 for keys do k:
Chris@127 173 ranges = stacked[k];
Chris@125 174 var x = 0;
Chris@126 175 c = Color#random();
Chris@125 176 for xkeys do xk:
Chris@127 177 rect = newRect x ranges[xk].y0 ranges[xk].y1 z c;
Chris@127 178 scene#add(rect);
Chris@125 179 x := x + 1;
Chris@125 180 done;
Chris@130 181 text = new DrawableTextBitmap(k, new Coord3d(-5, z, ty), c);
Chris@128 182 scene#add(text);
Chris@126 183 z := z - 1;
Chris@130 184 ty := ty + 0.1;
Chris@125 185 done;
Chris@129 186 chart#getView()#setViewPoint(new Coord3d(-pi/2, 0, 0));
Chris@125 187 axes = chart#getAxeLayout();
Chris@125 188 axes#setXAxeLabelDisplayed(false);
Chris@125 189 axes#setYAxeLabelDisplayed(false);
Chris@125 190 axes#setZAxeLabelDisplayed(true);
Chris@125 191 axes#setZAxeLabel(unit);
Chris@125 192 axes#setYTickLabelDisplayed(false);
Chris@125 193 ChartLauncher#openChart(chart);
Chris@125 194 ());
Chris@124 195
Chris@110 196 plotStructure structure =
Chris@110 197 case structure of
Chris@110 198 Grid matrix:
Chris@115 199 plotMatrix matrix;
Chris@110 200 //!!!
Chris@110 201 _: failWith "Cannot plot this structure (only grids implemented so far)";
Chris@110 202 esac;
Chris@108 203
Chris@115 204 {
Chris@119 205 newMatrixMapper,
Chris@119 206 newMatrixLogMapper,
Chris@119 207 newMapper,
Chris@115 208 plotMatrix,
Chris@124 209 plotBarChart,
Chris@124 210 plotLines,
Chris@125 211 stack,
Chris@125 212 plotStacked,
Chris@119 213 plotStructure,
Chris@115 214 }
Chris@110 215