update combined js
[dygraphs.git] / plotkit_v091 / doc / PlotKit.QuickStart.txt
CommitLineData
6a1aa64f
DV
1{% extends "basex.html" %}
2{% load markup %}
3{% block pageid %}code{% endblock %}
4{% block title %}PlotKit Quick Start{% endblock %}
5{% block headers %}
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" />
12{% endblock %}
13
14
15{% block content %}
16<div class="page doc">
17{% filter markdown %}
18PlotKit Quick Start
19===================
20
21This is just a quick guide on how to get started with PlotKit. If you
22want to delve in deeper, be sure to check out the
23[documentation][docs].
24
25Canvas or SVG
26=============
27
28Before we start, you should know a little about HTML Canvas and SVG
29support in the real browser world. For a more indepth coverage, please
30check the [SVG/Canvas Browser Support Status][Browser].
31
32Basically, Canvas and SVG have similar support across modern
33browsers. Canvas is supported by Opera 9, Safari 2 and Firefox 1.5,
34which probably only accounts for 10% of browsers. PlotKit also supports
35a degraded Emulated Canvas mode under IE which means you can achieve
36nearly 90% browser support using this technology.
37
38However, the future is in SVG where Firefox 1.5 and Opera 8 have
39support it, and IE6 with the Adobe SVG Viewer (ASV) install means you
40get around the same coverage as HTML Canvas.
41
42PlotKit plans to support both to maximise compatiblity. Canvas has a
43simplier rendering engine, but can do the equivalent to what SVG can
44apart from animation. SVG has wider support, but is more complex and
45support for key features varies widely across different
46implementations.
47
48Graphing with Canvas
49====================
50
51Download the latest [PlotKit][] and [MochiKit][] and extract it on to
52your web server and make sure ``plotkit-0.8/PlotKit`` and
53``mochikit/MochiKit`` is visible.
54
55Preparing the HTML
56------------------
57
58Add the script headers for PlotKit to work.
59
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>
65
66MochiKit.js is an autoloader for all the elements of MochiKit. You can
67reduce the size of it by making your own packed version or just
68including the script headers individually.
69
70The other four PlotKit javascript files deal with some basic
71functionality, the layout engine and two renderers, Canvas and
72SweetCanvas.
73
74Now we add the ``<canvas>`` tag to where we want the graph to
75appear. Note PlotKit __requires__ the ``<canvas>`` tag is enclosed
76inside a <div> tags for labels to work.
77
78 <div><canvas id="graph" height="300" width="300"></canvas></div>
79
80This will create a blank canvas of 300 by 300 pixels.
81
82Javascript
83----------
84
85There are only two simple steps to draw a chart, first is the create a
86layout with our data and second is to create the renderer. So lets
87start off with creating the layout.
88
89### Layout and Data
90
91 var layout = new PlotKit.Layout("bar", {});
92 layout.addDataset("sqrt", [[0, 0], [1, 1], [2, 1.414], [3, 1.73], [4, 2]]);
93 layout.evaluate();
94
95There, it is that simple. Lets explain what each line is doing:
96
971. ``var layout = new PlotKit.Layout("bar", {});``
98
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.
100
1012. ``layout.addDataset("sqrt", [[0, 0], [1, 1]...)``
102
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.
104
105 Note that PlotKit does not deal with negative numbers at the moment.
106
1073. ``layout.evaluate();``
108
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.
110
111### Renderer
112
113 var canvas = MochiKit.DOM.getElement("graph");
114 var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, {});
115 plotter.render();
116
1171. ``var canvas = MochiKit.DOM.getElement("graph");``
118
119 This line will get the HTML element we defined earlier.
120
1212. ``var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, {});``
122
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.
124
1253. ``plotter.render()``
126
127 This line will render the graph.
128
129### Putting it altogether
130
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]]);
134 layout.evaluate();
135 var canvas = MochiKit.DOM.getElement("graph");
136 var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, {});
137 plotter.render();
138 }
139 MochiKit.DOM.addLoadEvent(drawGraph);
140
141This 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.
142
143See this in an [HTML example here][example1].
144
145### Additional Options
146
147We 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:
148
149 var options = {
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"},
154 {v:1, label:"one"},
155 {v:2, label:"two"},
156 {v:3, label:"three"},
157 {v:4, label:"four"}],
158 "drawYAxis": false
159 };
160
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]]);
164 layout.evaluate();
165 var canvas = MochiKit.DOM.getElement("graph");
166 var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
167 plotter.render();
168 }
169 MochiKit.DOM.addLoadEvent(drawGraph);
170
171
172Here we define some additional options to affect how our graph is rendered.
173
1741. First line defines where the ``IECanvasHTC`` behaviour file is so that we can have IE support.
1752. 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.
1763. Third line defines some custom labels we would like by giving the mapping from X value to label.
1774. Fourth line tells the renderer not to draw the Y axis.
178
179Demonstration
180=============
181
182To 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.
183
184### Bar charts
185
186<div>
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;">&nbsp</div>
190</div>
191
192### Pie Charts
193
194<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;">&nbsp</div>
198</div>
199
200Author
201======
202
203Alastair Tse - Last Updated: 17th March 2006
204
205[docs]: PlotKit.html
206[Browser]: SVGCanvasCompat.html
207[PlotKit]: http://www.liquidx.net/plotkit/
208[MochiKit]: http://www.mochikit.com/
209
210{% endfilter %}
211</div>
212<script type="text/javascript">
213<!--
214
215var options = {
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"},
220 {v:1, label:"one"},
221 {v:2, label:"two"},
222 {v:3, label:"three"},
223 {v:4, label:"four"}],
224 "drawYAxis": false,
225 "pieRadius": 0.35
226};
227
228
229function 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]]);
232 layout.evaluate();
233 var canvas = MochiKit.DOM.getElement("bargraph");
234 var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
235 plotter.render();
236}
237MochiKit.DOM.addLoadEvent(drawBarGraph);
238
239function 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]]);
242 layout.evaluate();
243 var canvas = MochiKit.DOM.getElement("piegraph");
244 var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
245 plotter.render();
246}
247
248// Damn opera 9 has a bug with javascript subclassing??
249// but works in sweet.html, grr!!
250if (navigator.userAgent.toLowerCase().indexOf("opera") == -1) {
251 MochiKit.DOM.addLoadEvent(drawPieGraph);
252}
253
254//-->
255</script>
256{% endblock %}