PlotKit.QuickStart.html
17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" >
<head>
<title>PlotKit Quick Start | liquidx</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="http://media.liquidx.net/css/x_general.css" media="screen" rel="Stylesheet" type="text/css" />
<link href="http://media.liquidx.net/css/x_header.css" media="screen" rel="Stylesheet" type="text/css" />
<link href="http://media.liquidx.net/css/x_layout.css" media="screen" rel="Stylesheet" type="text/css" />
<link href="http://media.liquidx.net/css/x_blocks.css" media="screen" rel="Stylesheet" type="text/css" />
<link rel="icon" href="/favicon.png" type="image/x-png">
<link rel="shortcut icon" href="/favicon.png" type="image/x-png">
<!--[if lt IE 7.]>
<script defer type="text/javascript" src="http://media.liquidx.net/js/pngfix.js"></script>
<![endif]-->
<script type="text/javascript" src="/js/mochi/MochiKit.js"></script>
<script type="text/javascript" src="/js/plotkit/Base.js"></script>
<script type="text/javascript" src="/js/plotkit/Layout.js"></script>
<script type="text/javascript" src="/js/plotkit/Canvas.js"></script>
<script type="text/javascript" src="/js/plotkit/SweetCanvas.js"></script>
<link href="doc.css" media="screen" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
<div id="logo"><a href="http://www.liquidx.net/"><img src="http://media.liquidx.net/imgx/logo.png" width="256" height="128" alt="liquidx.net" /></a></div>
<div id="menu-hack">
<div id="menu-l"><img src="http://media.liquidx.net/imgx/menu_l.png" width="17" height="28" alt="menu cap" /></div><div id="menu-r"><img src="http://media.liquidx.net/imgx/menu_r.png" width="17" height="28" alt="menu cap" /></div>
<div id="menu-main">
<ul id="menu" class="code">
<li class="tab" id="blog"><a href="http://www.liquidx.net/" title="blog/home">blog</a></li>
<li class="tab" id="code"><a href="http://www.liquidx.net/code/" title="software i have written">software</a></li>
<li class="tab" id="dev"><a href="http://projects.liquidx.net/" title="source code for my open source projects">dev</a></li>
<li class="tab" id="photos"><a href="http://al.tse.id.au/gallery/" title="photos and videos">photos</a></li>
<li class="tab" id="research"><a href="http://al.tse.id.au/research/" title="research profile">research</a></li>
<li class="tab" id="links"><a href="http://www.liquidx.net/links/" title="my bookmarks">linkblog</a></li>
<li class="tab" id="stats"><a href="http://stats.liquidx.net/" title="stats for various parts of my website">stats</a></li>
<li class="tab" id="status"><a href="http://www.liquidx.net/status/" title="weather report for alastair">status</a></li>
<li class="tab" id="about"><a href="http://al.tse.id.au/" title="about alastair tse">aboutme</a></li>
</ul>
</div>
</div>
<div id="quickbuttons">
<span class="quickbutton"><a href="http://www.liquidx.net/albumartwidget/"><img src="http://media.liquidx.net/imgx/quick_widget.png" alt="album art widget" /></a></span>
<span class="quickbutton"><a href="http://www.liquidx.net/plotkit/"><img src="http://media.liquidx.net/imgx/quick_plotkit.png" alt="plotkit" /></a></span>
<span class="quickbutton"><a href="http://www.liquidx.net/fruity/"><img src="http://media.liquidx.net/imgx/quick_fruity.png" alt="fruity" /></a></span>
</div>
</div>
<div id="body">
<div class="page doc">
<h1> PlotKit Quick Start</h1>
<p>This is just a quick guide on how to get started with PlotKit. If you
want to delve in deeper, be sure to check out the
<a href="PlotKit.html">documentation</a>.
</p>
<h1> Canvas or SVG</h1>
<p>Before we start, you should know a little about HTML Canvas and SVG
support in the real browser world. For a more indepth coverage, please
check the <a href="SVGCanvasCompat.html">SVG/Canvas Browser Support Status</a>.
</p>
<p>Basically, Canvas and SVG have similar support across modern
browsers. Canvas is supported by Opera 9, Safari 2 and Firefox 1.5,
which probably only accounts for 10% of browsers. PlotKit also supports
a degraded Emulated Canvas mode under IE which means you can achieve
nearly 90% browser support using this technology.
</p>
<p>However, the future is in SVG where Firefox 1.5 and Opera 8 have
support it, and IE6 with the Adobe SVG Viewer (ASV) install means you
get around the same coverage as HTML Canvas.
</p>
<p>PlotKit plans to support both to maximise compatiblity. Canvas has a
simplier rendering engine, but can do the equivalent to what SVG can
apart from animation. SVG has wider support, but is more complex and
support for key features varies widely across different
implementations.
</p>
<h1> Graphing with Canvas</h1>
<p>Download the latest <a href="http://www.liquidx.net/plotkit/">PlotKit</a> and <a href="http://www.mochikit.com/">MochiKit</a> and extract it on to
your web server and make sure <code>plotkit-0.8/PlotKit</code> and
<code>mochikit/MochiKit</code> is visible.
</p>
<h2> Preparing the HTML</h2>
<p>Add the script headers for PlotKit to work.
</p>
<pre><code><script type="text/javascript" src="/mochikit/MochiKit.js"></script>
<script type="text/javascript" src="/plotkit/Base.js"></script>
<script type="text/javascript" src="/plotkit/Layout.js"></script>
<script type="text/javascript" src="/plotkit/Canvas.js"></script>
<script type="text/javascript" src="/plotkit/SweetCanvas.js"></script>
</code></pre><p>MochiKit.js is an autoloader for all the elements of MochiKit. You can
reduce the size of it by making your own packed version or just
including the script headers individually.
</p>
<p>The other four PlotKit javascript files deal with some basic
functionality, the layout engine and two renderers, Canvas and
SweetCanvas.
</p>
<p>Now we add the <code><canvas></code> tag to where we want the graph to
appear. Note PlotKit <strong>requires</strong> the <code><canvas></code> tag is enclosed
inside a <div> tags for labels to work.
</p>
<pre><code> <div><canvas id="graph" height="300" width="300"></canvas></div>
</code></pre><p>This will create a blank canvas of 300 by 300 pixels.
</p>
<h2> Javascript</h2>
<p>There are only two simple steps to draw a chart, first is the create a
layout with our data and second is to create the renderer. So lets
start off with creating the layout.
</p>
<h3> Layout and Data</h3>
<pre><code>var layout = new PlotKit.Layout("bar", {});
layout.addDataset("sqrt", [[0, 0], [1, 1], [2, 1.414], [3, 1.73], [4, 2]]);
layout.evaluate();
</code></pre><p>There, it is that simple. Lets explain what each line is doing:
</p>
<ol>
<li><p> <code>var layout = new PlotKit.Layout("bar", {});</code>
</p>
<p>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.
</p>
</li>
<li><p> <code>layout.addDataset("sqrt", [[0, 0], [1, 1]...)</code>
</p>
<p>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.
</p>
<p>Note that PlotKit does not deal with negative numbers at the moment.
</p>
</li>
<li><p> <code>layout.evaluate();</code>
</p>
<p>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.
</p>
</li>
</ol>
<h3> Renderer</h3>
<pre><code> var canvas = MochiKit.DOM.getElement("graph");
var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, {});
plotter.render();
</code></pre><ol>
<li><p> <code>var canvas = MochiKit.DOM.getElement("graph");</code>
</p>
<p>This line will get the HTML element we defined earlier.
</p>
</li>
<li><p> <code>var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, {});</code>
</p>
<p>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.
</p>
</li>
<li><p> <code>plotter.render()</code>
</p>
<p>This line will render the graph.
</p>
</li>
</ol>
<h3> Putting it altogether</h3>
<pre><code>function drawGraph() {
var layout = new PlotKit.Layout("bar", {});
layout.addDataset("sqrt", [[0, 0], [1, 1], [2, 1.414], [3, 1.73], [4, 2]]);
layout.evaluate();
var canvas = MochiKit.DOM.getElement("graph");
var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, {});
plotter.render();
}
MochiKit.DOM.addLoadEvent(drawGraph);
</code></pre><p>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.
</p>
<p>See this in an [HTML example here][example1].
</p>
<h3> Additional Options</h3>
<p>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:
</p>
<pre><code>var options = {
"IECanvasHTC": "/plotkit/iecanvas.htc",
"colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]),
"padding": {left: 0, right: 0, top: 10, bottom: 30},
"xTicks": [{v:0, label:"zero"},
{v:1, label:"one"},
{v:2, label:"two"},
{v:3, label:"three"},
{v:4, label:"four"}],
"drawYAxis": false
};
function drawGraph() {
var layout = new PlotKit.Layout("bar", options);
layout.addDataset("sqrt", [[0, 0], [1, 1], [2, 1.414], [3, 1.73], [4, 2]]);
layout.evaluate();
var canvas = MochiKit.DOM.getElement("graph");
var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
plotter.render();
}
MochiKit.DOM.addLoadEvent(drawGraph);
</code></pre><p>Here we define some additional options to affect how our graph is rendered.
</p>
<ol>
<li>
First line defines where the <code>IECanvasHTC</code> behaviour file is so that we can have IE support.
</li>
<li>
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.
</li>
<li>
Third line defines some custom labels we would like by giving the mapping from X value to label.
</li>
<li>
Fourth line tells the renderer not to draw the Y axis.
</li>
</ol>
<h1> Demonstration</h1>
<p>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.
</p>
<h3> Bar charts</h3>
<div>
<div style="float: left; padding-right: 5px;" width="300" height="300"><img src="barsample.png" width="300" height="300" alt="screenshot of graph" /></div>
<div style="float: left; padding-left: 5px;" width="300" height="300"><canvas id="bargraph" width="300" height="300"></canvas></div>
<div style="clear: both; height: 1px; width: 1px;"> </div>
</div>
<h3> Pie Charts</h3>
<div>
<div style="float: left; padding-right: 5px;" width="300" height="300"><img src="piesample.png" width="300" height="300" alt="screenshot of graph" /></div>
<div style="float: left; padding-left: 5px;" width="300" height="300"><canvas id="piegraph" width="300" height="300"></canvas></div>
<div style="clear: both; height: 1px; width: 1px;"> </div>
</div>
<h1> Author</h1>
<p>Alastair Tse - Last Updated: 17th March 2006
</p>
</div>
<script type="text/javascript">
<!--
var options = {
"IECanvasHTC": "../plotkit/iecanvas.htc",
"colorScheme": PlotKit.Base.palette(PlotKit.Base.baseColors()[0]),
"padding": {left: 10, right: 10, top: 10, bottom: 30},
"xTicks": [{v:0, label:"zero"},
{v:1, label:"one"},
{v:2, label:"two"},
{v:3, label:"three"},
{v:4, label:"four"}],
"drawYAxis": false,
"pieRadius": 0.35
};
function drawBarGraph() {
var layout = new PlotKit.Layout("bar", options);
layout.addDataset("sqrt", [[0, 0], [1, 1], [2, 1.414], [3, 1.73], [4, 2]]);
layout.evaluate();
var canvas = MochiKit.DOM.getElement("bargraph");
var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
plotter.render();
}
MochiKit.DOM.addLoadEvent(drawBarGraph);
function drawPieGraph() {
var layout = new PlotKit.Layout("pie", options);
layout.addDataset("sqrt", [[0, 0], [1, 1], [2, 1.414], [3, 1.73], [4, 2]]);
layout.evaluate();
var canvas = MochiKit.DOM.getElement("piegraph");
var plotter = new PlotKit.SweetCanvasRenderer(canvas, layout, options);
plotter.render();
}
// Damn opera 9 has a bug with javascript subclassing??
// but works in sweet.html, grr!!
if (navigator.userAgent.toLowerCase().indexOf("opera") == -1) {
MochiKit.DOM.addLoadEvent(drawPieGraph);
}
//-->
</script>
</div>
<div id="footer">
<div class="block">
<h3>Syndication Feeds:</h3>
<p>
<ul class="tiny">
<li><a href="http://www.liquidx.net/blog/feed/atom/" class="feed" title="feed for all posts on liquidx.net"><img src="http://media.liquidx.net/imgx/feed.gif" class="feed" alt="feed" />Atom Feed for the Blog Entries</a></li>
<li><a href="http://www.liquidx.net/blog/feed/rss/" class="feed" title="feed for all posts on liquidx.net"><img src="http://media.liquidx.net/imgx/feed.gif" class="feed" alt="feed" />RSS Feed for the Blog Entries</a></li>
<li><a href="http://www.liquidx.net/comments/feed/atom/" class="feed" title="feed for all posts on liquidx.net"><img src="http://media.liquidx.net/imgx/feed.gif" class="feed" alt="feed" />Feed for All Comments</a></li>
<li><a href="http://www.liquidx.net/links/feed/atom/" class="feed" title="feed for all bookmarked links"><img src="http://media.liquidx.net/imgx/feed.gif" class="feed" alt="feed" />Feed for Links</a></li>
</ul>
</p>
</div>
<div class="block">
<h3>About this site:</h3>
<p>Content on this site is licensed under <a href="http://creativecommons.org/licenses/by/2.5/">CC By Attribution</a> unless otherwise specified.
Copyright (c) 2002-2006, <a href="http://al.tse.id.au/">Alastair Tse</a>.</p>
<p>For more information, see <a href="http://al.tse.id.au/">al.tse.id.au</a>.</p>
<p><script type="text/javascript" src="http://technorati.com/embed/itwctkzez.js"></script></p>
</div>
<div class="block">
<h3>Is Made Possible By:</h3>
<p>
<dl>
<dt><a href="http://ecto.kung-foo.tv/" class="clean">ecto</a>. </dt>
<dd>Blogging client for Mac</dd>
<dt><a href="http://djangoproject.com/" class="clean">Django</a>. </dt>
<dd>Python Web Framework</dd>
<dt><a href="http://www.lighttpd.net/" class="clean">lighttpd</a>. </dt>
<dd>Really Fast Web Server</dd>
<dt><a href="http://www.saddi.com/software/flup/" class="clean">flup</a>. </dt>
<dd>FastCGI for Python</dd>
</dl>
</p>
</div>
<div class="block">
<h3>Search My Sites:</h3>
<p>
<div style='margin: 10px; text-align: center; width: 160px;'><form action='http://www.rollyo.com/search.html' ><fieldset style='margin: 0; padding: 4px 0 0 0; height: 60px; border: none; background: url(http://rollyo.com/remote/togo-bg4.png) no-repeat top left;'><div style="position: absolute; float:left; z-index:99; width: 46px; height: 50px;"><a href="http://rollyo.com"><img style="border: none;" height="50" width="46" src="http://rollyo.com/remote/x.gif"></a></div> <input type='text' size='30' style='float: left; width: 90px; margin: 2px 0 0 48px; padding: 0; font-size: 12px;' name='q' value='Search...' onclick='this.value="";' /><br /> <select name='sid' style='float: left; width: 78px; height: 15px; margin: 12px 0 0 46px; font-size: 7pt; padding: 0;'><option value='106081' selected='selected'>liquidx</option><option value='web'>Search The Web</option></select><input type='image' src='http://rollyo.com/remote/btn-togo.png' alt='Go' style='margin: 12px 0 0 3px; float: left;' /><input type='hidden' name='togo-v' value='1' /></fieldset></form></div>
</p>
</div>
<div class="clear"> </div>
</div>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>
<script type="text/javascript">
_uacct = "UA-58117-1";
urchinTracker();
</script>
</body>
</html>