1 {% extends "basex.html" %}
3 {% block pageid %}code{% endblock %}
4 {% block title %}PlotKit Quick Start{% endblock %}
6 <script type="text/javascript" src="/js/mochi/MochiKit.js"></script>
7 <script type="text/javascript" src="/js/plotkit/Base.js"></script>
8 <script type="text/javascript" src="/js/plotkit/Layout.js"></script>
9 <script type="text/javascript" src="/js/plotkit/Canvas.js"></script>
10 <script type="text/javascript" src="/js/plotkit/SweetCanvas.js"></script>
11 <link href="doc.css" media="screen" rel="stylesheet" type="text/css" />
16 <div class="page doc">
21 This is just a quick guide on how to get started with PlotKit. If you
22 want to delve in deeper, be sure to check out the
23 [documentation][docs].
28 Before we start, you should know a little about HTML Canvas and SVG
29 support in the real browser world. For a more indepth coverage, please
30 check the [SVG/Canvas Browser Support Status][Browser].
32 Basically, Canvas and SVG have similar support across modern
33 browsers. Canvas is supported by Opera 9, Safari 2 and Firefox 1.5,
34 which probably only accounts for 10% of browsers. PlotKit also supports
35 a degraded Emulated Canvas mode under IE which means you can achieve
36 nearly 90% browser support using this technology.
38 However, the future is in SVG where Firefox 1.5 and Opera 8 have
39 support it, and IE6 with the Adobe SVG Viewer (ASV) install means you
40 get around the same coverage as HTML Canvas.
42 PlotKit plans to support both to maximise compatiblity. Canvas has a
43 simplier rendering engine, but can do the equivalent to what SVG can
44 apart from animation. SVG has wider support, but is more complex and
45 support for key features varies widely across different
51 Download the latest [PlotKit][] and [MochiKit][] and extract it on to
52 your web server and make sure ``plotkit-0.8/PlotKit`` and
53 ``mochikit/MochiKit`` is visible.
58 Add the script headers for PlotKit to work.
60 <script type="text/javascript" src="/mochikit/MochiKit.js"></script>
61 <script type="text/javascript" src="/plotkit/Base.js"></script>
62 <script type="text/javascript" src="/plotkit/Layout.js"></script>
63 <script type="text/javascript" src="/plotkit/Canvas.js"></script>
64 <script type="text/javascript" src="/plotkit/SweetCanvas.js"></script>
66 MochiKit.js is an autoloader for all the elements of MochiKit. You can
67 reduce the size of it by making your own packed version or just
68 including the script headers individually.
70 The other four PlotKit javascript files deal with some basic
71 functionality, the layout engine and two renderers, Canvas and
74 Now we add the ``<canvas>`` tag to where we want the graph to
75 appear. Note PlotKit __requires__ the ``<canvas>`` tag is enclosed
76 inside a <div> tags for labels to work.
78 <div><canvas id="graph" height="300" width="300"></canvas></div>
80 This will create a blank canvas of 300 by 300 pixels.
85 There are only two simple steps to draw a chart, first is the create a
86 layout with our data and second is to create the renderer. So lets
87 start off with creating the layout.
91 var layout = new PlotKit.Layout("bar", {});
92 layout.addDataset("sqrt", [[0, 0], [1, 1], [2, 1.414], [3, 1.73], [4, 2]]);
95 There, it is that simple. Lets explain what each line is doing:
97 1. ``var layout = new PlotKit.Layout("bar", {});``
99 We create a new layout object, and tell it that we want a bar chart in the first parameter. The second parameter allows us to pass additional options, which we will go on to later. It can be left null, or in this case just an empty array.
101 2. ``layout.addDataset("sqrt", [[0, 0], [1, 1]...)``
103 This will add a new dataset to the layout. You can add multiple datasets by specifying a different name in the first parameter for each dataset. The dataset consists of an array of (x, y) values. These must be numeric, either floating point or integers.
105 Note that PlotKit does not deal with negative numbers at the moment.
107 3. ``layout.evaluate();``
109 This will be the last command you make on the layout before passing it to the renderer. This will tell the layout to calculate the layout of the chart so the renderer can draw it. It is an expensive operation, so do not call it frequently, only unless the data or options have been changed.
113 var canvas = MochiKit.DOM.getElement("graph");
114 var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, {});
117 1. ``var canvas = MochiKit.DOM.getElement("graph");``
119 This line will get the HTML element we defined earlier.
121 2. ``var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, {});``
123 This will create the renderer to work on the object passed, and also with the data in the layout we created earlier. Again, the third parameter here is for options to relates to the look of the graph. We will show you some things you can do with this in the following section.
125 3. ``plotter.render()``
127 This line will render the graph.
129 ### Putting it altogether
131 function drawGraph() {
132 var layout = new PlotKit.Layout("bar", {});
133 layout.addDataset("sqrt", [[0, 0], [1, 1], [2, 1.414], [3, 1.73], [4, 2]]);
135 var canvas = MochiKit.DOM.getElement("graph");
136 var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, {});
139 MochiKit.DOM.addLoadEvent(drawGraph);
141 This is a sample of what you would use to plot the graph of sqare roots for 0 to 4. Make sure you plot the graph on the load event because the DOM will not be ready if when the Javascript is first loaded.
143 See this in an [HTML example here][example1].
145 ### Additional Options
147 We mentioned that both the layout and renderer may take some additional options. In order to take advantage of that, we can use a simple options dictionary to store options for both layout and the renderer, in this way:
150 "IECanvasHTC": "/plotkit/iecanvas.htc",
151 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]),
152 "padding": {left: 0, right: 0, top: 10, bottom: 30},
153 "xTicks": [{v:0, label:"zero"},
156 {v:3, label:"three"},
157 {v:4, label:"four"}],
161 function drawGraph() {
162 var layout = new PlotKit.Layout("bar", options);
163 layout.addDataset("sqrt", [[0, 0], [1, 1], [2, 1.414], [3, 1.73], [4, 2]]);
165 var canvas = MochiKit.DOM.getElement("graph");
166 var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
169 MochiKit.DOM.addLoadEvent(drawGraph);
172 Here we define some additional options to affect how our graph is rendered.
174 1. First line defines where the ``IECanvasHTC`` behaviour file is so that we can have IE support.
175 2. Second line defines a new colorScheme to use. Here we are just using another preset color scheme that creates a palette out of the 6th preset base colour.
176 3. Third line defines some custom labels we would like by giving the mapping from X value to label.
177 4. Fourth line tells the renderer not to draw the Y axis.
182 To show you that is how it works, below is the graph defined exactly how it is presented in this quick start guide. On the left is a PNG of what you should expect and on the right is what it actually renders to.
187 <div style="float: left; padding-right: 5px;" width="300" height="300"><img src="barsample.png" width="300" height="300" alt="screenshot of graph" /></div>
188 <div style="float: left; padding-left: 5px;" width="300" height="300"><canvas id="bargraph" width="300" height="300"></canvas></div>
189 <div style="clear: both; height: 1px; width: 1px;"> </div>
195 <div style="float: left; padding-right: 5px;" width="300" height="300"><img src="piesample.png" width="300" height="300" alt="screenshot of graph" /></div>
196 <div style="float: left; padding-left: 5px;" width="300" height="300"><canvas id="piegraph" width="300" height="300"></canvas></div>
197 <div style="clear: both; height: 1px; width: 1px;"> </div>
203 Alastair Tse - Last Updated: 17th March 2006
206 [Browser]: SVGCanvasCompat.html
207 [PlotKit]: http://www.liquidx.net/plotkit/
208 [MochiKit]: http://www.mochikit.com/
212 <script type="text/javascript">
216 "IECanvasHTC": "../plotkit/iecanvas.htc",
217 "colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]),
218 "padding": {left: 10, right: 10, top: 10, bottom: 30},
219 "xTicks": [{v:0, label:"zero"},
222 {v:3, label:"three"},
223 {v:4, label:"four"}],
229 function drawBarGraph() {
230 var layout = new PlotKit.Layout("bar", options);
231 layout.addDataset("sqrt", [[0, 0], [1, 1], [2, 1.414], [3, 1.73], [4, 2]]);
233 var canvas = MochiKit.DOM.getElement("bargraph");
234 var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
237 MochiKit.DOM.addLoadEvent(drawBarGraph);
239 function drawPieGraph() {
240 var layout = new PlotKit.Layout("pie", options);
241 layout.addDataset("sqrt", [[0, 0], [1, 1], [2, 1.414], [3, 1.73], [4, 2]]);
243 var canvas = MochiKit.DOM.getElement("piegraph");
244 var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
248 // Damn opera 9 has a bug with javascript subclassing??
249 // but works in sweet.html, grr!!
250 if (navigator.userAgent.toLowerCase().indexOf("opera") == -1) {
251 MochiKit.DOM.addLoadEvent(drawPieGraph);