Debian Codegraph @

tutorial/introduction.html

raw
1 <!doctype>
2 <head>
3 <link rel="stylesheet" href="vendor/prettify/prettify.css">
4 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
5 <script src="vendor/prettify/prettify.js"></script>
6 <link rel="stylesheet" href="style.css">
7 </head>
8
9 <body>
10
11 <h1>Taking Rickshaw for a Go</h1>
12
13 <p>
14 Rickshaw is a simple framework for drawing charts of time series data
15 on a web page, built on top of Mike Bostock's delightful
16 <a href="http://mbostock.github.com/d3/">D3 library</a>. These charts
17 can be powered by static historical data sets, or living data that
18 continuously updates in real time.
19 </p>
20
21 <p>
22 Rickshaw builds on top of D3 technically, and spiritually too.
23 Rickshaw makes every effort to provide help for common problems
24 without obscuring anything underneath it. If you need to reach down
25 to D3 or the SVG layers below, go right ahead -- it's all there waiting.
26 </p>
27
28 <p>
29 Let's start with a simple but complete program that paints a chart:
30 </p>
31
32 <section class="example" id="example_01">
33 <header>
34 <a href="example_01.html" target="_blank">view</a>
35 <h3>Example 01</h3>
36 </header>
37 <iframe scrolling="no"></iframe>
38 <pre class="prettyprint"></pre>
39 </section>
40
41 <p>
42 Breaking that down, first we pull in our dependencies and create
43 a div to hold our chart. Then in our <code>script</code> we
44 call <code>Rickshaw.Graph</code>'s constructor, and pass along an
45 element reference to our chart container, some layout instructions,
46 and a series of data objects.
47 </p>
48
49 <p>
50 The <code>series</code> object has a couple of slots,
51 a <code>data</code> array of coordinate objects, and a color to draw
52 them with. Finally, we call the render() method on our just instantiated
53 graph object, which creates paints our chart on the screen.
54 </p>
55
56 <h2>Let's Try with Real Data</h2>
57
58 <p>
59 Our previous work allowed us to paint a chart of made up values with
60 minimal scaffolding. That was fun, but it doesn't tell us anything
61 interesting about data. Let's use <a href="http://2010.census.gov/2010census/data/pop_change.csv">population change data</a> from the
62 2010 U.S. Census to power our chart, and see what we find.
63 </p>
64
65 <p>
66 We'll begin by drawing a line representing the United States
67 population with a point for each decade from 1910 to 2010. We'll use
68 a short <a href="transform.pl">script</a> we've written to massage the
69 CSV data at the census.gov URL into a JavaScript data structure
70 that <code>Rickshaw.Graph</code>'s constructor can take as
71 its <code>data</code> argument.
72 </p>
73
74 <br>
75 <section class="example" id="example_02">
76 <header>
77 <a href="example_02.html" target="_blank">view</a>
78 <h3>Example 02</h3>
79 </header>
80 <iframe scrolling="no"></iframe>
81 <pre class="prettyprint linenums"></pre>
82 </section>
83
84 <h2>Time on the X-Axis</h2>
85
86 <p>
87 A trained eye can already see some points of interest there. For
88 instance, ending about a quarter way into the graph there is a short
89 period where the growth rate flattens out significantly. What
90 happened then?
91 </p>
92
93 <p>
94 First we have to answer the question of when the flattening happened.
95 Putting a label on our x axis should help. Rickshaw gives us a helper
96 for time based axes. After we modify our data transformation script
97 to use epoch seconds for the <code>x</code> values we can pass our
98 graph along to <code>Rickshaw.Graph.Axis.Time</code>'s constructor.
99 When the graph's <code>render()</code> function is later called
100 Rickshaw examines the <code>x</code> domain and determines the time
101 unit being used, and labels the graph accordingly. The styling we
102 included lines up the labels nicely across the bottom of our graph.
103 </p>
104
105 <p>
106 Our updated <a href="./transform_epoch.pl">transform_epoch.pl</a> uses epoch
107 seconds for <code>x</code>. Let's see how we do.
108 </p>
109
110 <br>
111 <section class="example" id="example_03">
112 <header>
113 <a href="example_03.html" target="_blank">view</a>
114 <h3>Example 03</h3>
115 </header>
116 <iframe scrolling="no"></iframe>
117 <pre class="prettyprint linenums"></pre>
118 </section>
119
120
121 <h2>Y-Axis Too</h2>
122
123 <p>
124 Now let's add the pieces to get a <code>y</code> axis. We need a new
125 HTML element to put the <code>y</code> axis in, as well as some
126 styling to position the axis absolutely in relation to the chart.
127 </p>
128
129 <p>
130 We pass along a reference to our graph to
131 <code>Rickshaw.Graph.Axis.Y</code>'s constructor, as well as the
132 element we want to draw the axis inside. We also ask
133 <code>Rickshaw.Fixtures.Number.formatKMBT</code> to help us format the
134 numbers on our <code>y</code> ticks in there.
135 </p>
136
137 <section class="example" id="example_04">
138 <header>
139 <a href="example_04.html" target="_blank">view</a>
140 <h3>Example 04</h3>
141 </header>
142 <iframe scrolling="no"></iframe>
143 <pre class="prettyprint linenums"></pre>
144 </section>
145 <br>
146
147 <h2>Breaking Things Down</h2>
148
149 <p>
150 The Great Depression left a mark. We should break that data down by
151 region. Some simple changes to our data transformation gives us the
152 regional data for our series. Here's <a href="transform_region.pl">transform_region.pl</a>.
153 </p>
154
155 <p>
156 Plugging that data into our series parameter leaves us wanting to
157 provide colors for each of those individual series. We'll use the
158 <code>Rickshaw.Color.Palette</code> plugin to pick our colors. Once
159 we've created our palette, calling its <code>color()</code> method
160 returns the next color.
161 </p>
162
163 <br>
164 <section class="example" id="example_05">
165 <header>
166 <a href="example_05.html" target="_blank">view</a>
167 <h3>Example 05</h3>
168 </header>
169 <iframe scrolling="no"></iframe>
170 <pre class="prettyprint linenums"></pre>
171 </section>
172
173 <h2>What Are We Looking At?</h2>
174
175 <p>
176 We need a legend! Following a familiar pattern, we add a container
177 div for the legend and style it. Then we call the constructor for the
178 <code>Rickshaw.Graph.Legend</code> plugin, which takes a reference to
179 our newly added DOM element, and a reference to the graph.
180 </p>
181
182 <br>
183 <section class="example" id="example_06">
184 <header>
185 <a href="example_06.html" target="_blank">view</a>
186 <h3>Example 06</h3>
187 </header>
188 <iframe scrolling="no"></iframe>
189 <pre class="prettyprint linenums"></pre>
190 </section>
191
192 <h2>Unstacking</h2>
193
194 <p>
195 It's clear that the South is growing quickly, but instead of painting
196 this chart as a stacked graph it would be nice to see how these growth
197 patterns line up against each other. We set the renderer in a
198 callback, and then ask the graph to update.
199 </p>
200 <p>
201 In addition to setting the default renderer for the graph, we've added
202 a little JavaScript to observe clicks between our stack/line toggle
203 whose job is to update the type of renderer we're using and render the
204 graph appropriately.
205 </p>
206
207 <br>
208 <section class="example" id="example_07">
209 <header>
210 <a href="example_07.html" target="_blank">view</a>
211 <h3>Example 07</h3>
212 </header>
213 <iframe scrolling="no"></iframe>
214 <pre class="prettyprint"></pre>
215 </section>
216
217 <h2>More Later</h2>
218
219 <p>
220 We're just getting started, but that's all for today. Next time we'll
221 get into stacked bars, and different line interpolations, and smoothing,
222 and zooming.
223 </p>
224
225 <p>
226 If you're clamoring for more, you may enjoy a poke around in the
227 <a href="../examples/">examples</a> directory.
228 </p>
229
230 <br><br>
231 <script>
232 var sections = document.querySelectorAll('section.example');
233
234 var htmlEntities = function(str) {
235 return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
236 }
237
238 Array.prototype.forEach.call(sections, function(s) {
239
240 var code = s.querySelector('pre');
241 var iframe = s.querySelector('iframe');
242 var anchor = s.querySelector('a');
243
244 var exampleURL = s.getAttribute('id') + ".html";
245
246 iframe.src = exampleURL;
247
248 $.ajax( {
249 url: exampleURL,
250 success: function(r) {
251 code.innerHTML = htmlEntities(r);
252 code.innerHTML = prettyPrintOne(code.innerHTML);
253 }
254 } );
255
256 } );
257
258 </script>
259 </body>