1 | ============
|
---|
2 | Loading data
|
---|
3 | ============
|
---|
4 |
|
---|
5 | Open Flash Chart 2 can load data from a number of places:
|
---|
6 |
|
---|
7 | ==========
|
---|
8 | 1. The URL
|
---|
9 | ==========
|
---|
10 |
|
---|
11 | OFC will look in the url for a variable called 'ofc':
|
---|
12 |
|
---|
13 | http://example.com/open-flash-chart/index.php?ofc=data/data.txt
|
---|
14 |
|
---|
15 | OFC will try to load the file http://example.com/open-flash-chart/data/data.txt
|
---|
16 |
|
---|
17 | NOTE:
|
---|
18 | - ofc=data/data.txt is a relative path.
|
---|
19 | - The data file must contain JSON (see examples in this document)
|
---|
20 |
|
---|
21 | ==================
|
---|
22 | 2. Flash Variables
|
---|
23 | ==================
|
---|
24 |
|
---|
25 | If the URL does not contain a variable 'ofc' the next thing it will
|
---|
26 | try is look in the variables that have been passed to it for a 'data-file'
|
---|
27 | if this is found, OFC will load that URL:
|
---|
28 |
|
---|
29 | <script type="text/javascript">
|
---|
30 | var so = new SWFObject("../open-flash-chart/open-flash-chart.swf", "ofc", "250", "200", "9", "#FFFFFF");
|
---|
31 | so.addVariable("data-file", "../data-files/data-60.txt");
|
---|
32 | so.addParam("allowScriptAccess", "always" );
|
---|
33 | so.write("my_chart");
|
---|
34 | </script>
|
---|
35 |
|
---|
36 | see flash-variable.html for an example of this.
|
---|
37 |
|
---|
38 | NOTE:
|
---|
39 |
|
---|
40 | - The path of my .swf will differ from yours.
|
---|
41 | - I am using SWFObject (this is in the .zip file in the folder js)
|
---|
42 |
|
---|
43 | ==================
|
---|
44 | 3. From Javascript
|
---|
45 | ==================
|
---|
46 |
|
---|
47 | If the URL does not contain a varibale 'ofc' the next thing it will
|
---|
48 | try is calling the Javascript function open_flash_chart_data(), so
|
---|
49 | you may have:
|
---|
50 |
|
---|
51 | function open_flash_chart_data()
|
---|
52 | {
|
---|
53 | alert( 'reading data' );
|
---|
54 | return JSON.stringify(data);
|
---|
55 | }
|
---|
56 |
|
---|
57 | This function should return a valid JSON string.
|
---|
58 |
|
---|
59 | see json-test.html for an example of this.
|
---|
60 |
|
---|
61 | NOTE:
|
---|
62 |
|
---|
63 | - I am using the wonderful javascript JSON converter json2.js
|
---|
64 | this is in the .zip file in js/json/json2.js, but take a look
|
---|
65 | at www.json.org for more examples
|
---|
66 |
|
---|
67 | =============================
|
---|
68 | 3. You can push JSON into OFC
|
---|
69 | =============================
|
---|
70 |
|
---|
71 | Using Javascript you can push data into OFC via an external interface.
|
---|
72 | This is really easy. When OFC has loaded and tried 1, 2 and 3 above and
|
---|
73 | failed to find any data it will try to call the Javascript function
|
---|
74 | ofc_ready():
|
---|
75 |
|
---|
76 | function ofc_ready()
|
---|
77 | {
|
---|
78 | alert('ofc_ready');
|
---|
79 | tmp = findSWF("ofc");
|
---|
80 | x = tmp.load( JSON.stringify(ofc) );
|
---|
81 | }
|
---|
82 |
|
---|
83 | In this function you can push a JSON string into OFC using
|
---|
84 | the interface 'load()', in the above function we find the chart
|
---|
85 | then call load and pass in our JSON string.
|
---|
86 |
|
---|
87 | This is useful for AJAX pages.
|
---|
88 |
|
---|
89 | see json-test-2.html for an example of this.
|
---|
90 |
|
---|
91 | =====================
|
---|
92 | Data format: JSON
|
---|
93 | =====================
|
---|
94 |
|
---|
95 | The data must be in JSON format. The basic JSON obect is:
|
---|
96 |
|
---|
97 | {}
|
---|
98 |
|
---|
99 | a more complete example with HTML and Javascript:
|
---|
100 |
|
---|
101 | <script type="text/javascript">
|
---|
102 |
|
---|
103 | var data = {};
|
---|
104 |
|
---|
105 | </script>
|
---|
106 |
|
---|
107 |
|
---|
108 | Put all other objects inside this. For example the JSON object
|
---|
109 | with a title looks like this:
|
---|
110 |
|
---|
111 | {
|
---|
112 | "title":{
|
---|
113 | "text": "Many data lines",
|
---|
114 | "style": "{font-size: 20px; color:#0000ff; font-family: Verdana; text-align: center;}"
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | This is what you would save into a data file. Below is the same data,
|
---|
119 | but this time as part of the javascript in your web page:
|
---|
120 |
|
---|
121 | <script type="text/javascript">
|
---|
122 |
|
---|
123 | var data = {
|
---|
124 | "title":{
|
---|
125 | "text": "Many data lines",
|
---|
126 | "style": "{font-size: 20px; color:#0000ff; font-family: Verdana; text-align: center;}"
|
---|
127 | }
|
---|
128 | };
|
---|
129 |
|
---|
130 | </script>
|
---|
131 |
|
---|
132 | This all sounds rather complex, but it isn't really. This is what is going on
|
---|
133 | inside OFC when it reads a data file:
|
---|
134 |
|
---|
135 | json_string = load file from URL()
|
---|
136 | json_object = parse string( json_string )
|
---|
137 | display chart( json_object )
|
---|
138 |
|
---|
139 | The same thing happens when you pass in a JSON object from Javascript,
|
---|
140 | to create the JSON string, you call:
|
---|
141 |
|
---|
142 | JSON.stringify(ofc)
|
---|
143 |
|
---|
144 | and pass the string into OFC:
|
---|
145 |
|
---|
146 | tmp = findSWF("ofc");
|
---|
147 | x = tmp.load( JSON.stringify(ofc) );
|
---|
148 |
|
---|
149 | then inside OFC the same functions are called:
|
---|
150 |
|
---|
151 | external interface load( json_string ) {
|
---|
152 | json_object = parse string( json_string )
|
---|
153 | display chart( json_object )
|
---|
154 | }
|
---|
155 |
|
---|
156 | ==========
|
---|
157 | Tutorial 1
|
---|
158 | ==========
|
---|
159 |
|
---|
160 | So, lets go.
|
---|
161 |
|
---|
162 | Copy the .swf to the root of your web site.
|
---|
163 |
|
---|
164 | Now take a copy of 'flash-variable.html' and put this into
|
---|
165 | the root of the web site.
|
---|
166 |
|
---|
167 | Copy the Javascript files to the root of your site.
|
---|
168 |
|
---|
169 | Next take the example data file 'data.txt' and also put this
|
---|
170 | into the root.
|
---|
171 |
|
---|
172 | Edit 'flash-variable.html', find the line:
|
---|
173 |
|
---|
174 | var so = new SWFObject("../open-flash-chart/open-flash-chart.swf", "ofc", "250", "200", "9", "#FFFFFF");
|
---|
175 |
|
---|
176 | and change it to:
|
---|
177 |
|
---|
178 | var so = new SWFObject("open-flash-chart.swf", "ofc", "250", "200", "9", "#FFFFFF");
|
---|
179 |
|
---|
180 | also change all the Javascript includes so they work.
|
---|
181 |
|
---|
182 | Now browse to:
|
---|
183 |
|
---|
184 | http://example.com/flash-variable.html
|
---|
185 |
|
---|
186 | this should fail.
|
---|
187 |
|
---|
188 | http://example.com/flash-variable.html?ofc=data.txt
|
---|
189 |
|
---|
190 | this should work.
|
---|
191 |
|
---|
192 | ==========
|
---|
193 | Tutorial 2
|
---|
194 | ==========
|
---|
195 |
|
---|
196 | Try editing the data file. Take a look at the example data files.
|
---|
197 |
|
---|
198 |
|
---|
199 | ================
|
---|
200 | Title (optional)
|
---|
201 | ================
|
---|
202 | All attributes are optional.
|
---|
203 | text: string, the title
|
---|
204 | style: string, the CSS style
|
---|
205 |
|
---|
206 | {
|
---|
207 | "title":{
|
---|
208 | "text": "Many data lines",
|
---|
209 | "style": "{font-size: 20px; color:#0000ff; font-family: Verdana; text-align: center;}"
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | ===================
|
---|
214 | Y Legend (optional)
|
---|
215 | ===================
|
---|
216 | All attributes are optional.
|
---|
217 | text: string, the title for the Y axis
|
---|
218 | style: string, a CSS string
|
---|
219 |
|
---|
220 | {
|
---|
221 | "y_legend":{
|
---|
222 | "text": "Open Flash Chart",
|
---|
223 | "style": "{color: #736AFF; font-size: 12px;}"
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | ========
|
---|
228 | Elements
|
---|
229 | ========
|
---|
230 | The elements attribute is an array of generic objects.
|
---|
231 | Each object is the type of chart (line, bar, scatter etc.)
|
---|
232 |
|
---|
233 | {
|
---|
234 | "elements":[
|
---|
235 | {
|
---|
236 | "type": "bar",
|
---|
237 | "alpha": 0.5,
|
---|
238 | "colour": "#9933CC",
|
---|
239 | "text": "Page views",
|
---|
240 | "font-size": 10,
|
---|
241 | "values" : [9,6,7,9,5,7,6,9,7]
|
---|
242 | },
|
---|
243 | {
|
---|
244 | "type": "bar",
|
---|
245 | "alpha": 0.5,
|
---|
246 | "colour": "#CC9933",
|
---|
247 | "text": "Page views 2",
|
---|
248 | "font-size": 10,
|
---|
249 | "values" : [9,6,7,9,5,7,6,9,7]
|
---|
250 | }
|
---|
251 | ]
|
---|
252 | }
|
---|
253 |
|
---|
254 | =================
|
---|
255 | X Axis (optional)
|
---|
256 | =================
|
---|
257 | This object is optional, if it is not present the chart will show a default
|
---|
258 | X axis.
|
---|
259 | All attributes are optional.
|
---|
260 |
|
---|
261 | stroke : number, the width of the line
|
---|
262 | tick-height : number, the height of the ticks
|
---|
263 | colour : string, the colour of the line
|
---|
264 | offset: boolean, x axis min (usually 0) is offset, used in bar charts
|
---|
265 | grid-colour: string, colour of the grid lines
|
---|
266 | 3d: boolean, is it 3D
|
---|
267 | steps: show every n ticks
|
---|
268 | labels: array of strings, the labels of each X point
|
---|
269 |
|
---|
270 | Example:
|
---|
271 | {
|
---|
272 | "x_axis":{
|
---|
273 | "stroke":1,
|
---|
274 | "tick_height":10,
|
---|
275 | "colour":"#d000d0",
|
---|
276 | "grid_colour":"#00ff00",
|
---|
277 | "labels": ["January","February","March","April","May","June","July","August","Spetember"]
|
---|
278 | }
|
---|
279 | }
|
---|
280 |
|
---|
281 | ===============
|
---|
282 | Y Axis optional
|
---|
283 | ===============
|
---|
284 | This object is optional, if it is not present the chart will show a default
|
---|
285 | Y axis.
|
---|
286 | All attributes are optional.
|
---|
287 |
|
---|
288 | Example:
|
---|
289 | {
|
---|
290 | "y_axis":{
|
---|
291 | "stroke": 4,
|
---|
292 | "tick_length": 3,
|
---|
293 | "colour": "#d000d0",
|
---|
294 | "grid_colour": "#00ff00",
|
---|
295 | "offset": 0,
|
---|
296 | "max": 20
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | ============
|
---|
301 | Elements.bar
|
---|
302 | ============
|
---|
303 | A bar chart. Must be inside the elements array.
|
---|
304 |
|
---|
305 | type: string, must be 'bar'
|
---|
306 | alpha: number, between 0 (transparent) and 1 (opaque)
|
---|
307 | colour: string, CSS colour
|
---|
308 | text: string, the key
|
---|
309 | font-size: number, size of the key text
|
---|
310 | values: array of numbers, height of each bar
|
---|
311 |
|
---|
312 | Example:
|
---|
313 | {
|
---|
314 | "elements":[
|
---|
315 | {
|
---|
316 | "type": "bar",
|
---|
317 | "alpha": 0.5,
|
---|
318 | "colour": "#9933CC",
|
---|
319 | "text": "Page views",
|
---|
320 | "font-size": 10,
|
---|
321 | "values" : [9,6,7,9,5,7,6,9,7]
|
---|
322 | }
|
---|
323 | ]
|
---|
324 | }
|
---|
325 |
|
---|
326 | ============
|
---|
327 | Elements.pie
|
---|
328 | ============
|
---|
329 | A pie chart. Must be inside the elements array.
|
---|
330 |
|
---|
331 | type: string, must be 'pie'
|
---|
332 | start-angle: number, the angle of the first pie slice
|
---|
333 | colours: array of strings, CSS colour
|
---|
334 | alpha: number, between 0 (transparent) and 1 (opaque)
|
---|
335 | stroke: number, the outline width
|
---|
336 | animate: boolean, animate the pie chart
|
---|
337 | values: array of objects, value of each pie slice. May be a number or a slice object
|
---|
338 |
|
---|
339 | Example:
|
---|
340 | {
|
---|
341 | "elements":[
|
---|
342 | {
|
---|
343 | "type": "pie",
|
---|
344 | "start-angle": 180,
|
---|
345 | "colours": ["#d01f3c","#356aa0","#C79810","#73880A","#D15600","#6BBA70"],
|
---|
346 | "alpha": 0.6,
|
---|
347 | "stroke": 2,
|
---|
348 | "animate": 1,
|
---|
349 | "values" : [0,2,{"value":0,"text":"zero"},2,6]
|
---|
350 | }
|
---|
351 | ]
|
---|
352 | }
|
---|
353 |
|
---|
354 | =============
|
---|
355 | Elements.hbar
|
---|
356 | =============
|
---|
357 | Horizontal Bar chart
|
---|
358 |
|
---|
359 | values: array of objects. Each value must have a "right" and an optional "left" value
|
---|
360 |
|
---|
361 |
|
---|
362 | Example:
|
---|
363 | {
|
---|
364 | "elements":[
|
---|
365 | {
|
---|
366 | "type": "hbar",
|
---|
367 | "colour": "#9933CC",
|
---|
368 | "text": "Page views",
|
---|
369 | "font-size": 10,
|
---|
370 | "values" : [{"right":10},{"right":15},{"left":13,"right":17}]
|
---|
371 | }
|
---|
372 | ]
|
---|
373 | }
|
---|
374 |
|
---|
375 | =================
|
---|
376 | Elements.line_dot
|
---|
377 | =================
|
---|
378 | Line chart
|
---|
379 |
|
---|
380 | values: Array of numbers:
|
---|
381 |
|
---|
382 | Example:
|
---|
383 | {
|
---|
384 | "elements":[
|
---|
385 | {
|
---|
386 | "type": "line_dot",
|
---|
387 | "colour": "#736AFF",
|
---|
388 | "text": "Avg. wave height (cm)",
|
---|
389 | "font-size": 10,
|
---|
390 | "width": 2,
|
---|
391 | "dot-size": 4,
|
---|
392 | "values" : [1.5,1.69,1.88,2.06,2.21,2.34,null,2.35,2.23,2.08]
|
---|
393 | }
|
---|
394 | ]
|
---|
395 | }
|
---|
396 |
|
---|
397 | =================
|
---|
398 | Elements.line*
|
---|
399 | =================
|
---|
400 | Just a quick note of the 3 different line types:
|
---|
401 |
|
---|
402 | Example:
|
---|
403 | {
|
---|
404 | "title":{
|
---|
405 | "text":"Many data lines",
|
---|
406 | "style":"{font-size: 30px;}"
|
---|
407 | },
|
---|
408 |
|
---|
409 | "y_legend":{
|
---|
410 | "text":"Open Flash Chart",
|
---|
411 | "style":"{font-size: 12px; color:#736AFF;}"
|
---|
412 | },
|
---|
413 |
|
---|
414 | "elements":[
|
---|
415 | {
|
---|
416 | "type": "line",
|
---|
417 | "colour": "#9933CC",
|
---|
418 | "text": "Page views",
|
---|
419 | "width": 2,
|
---|
420 | "font-size": 10,
|
---|
421 | "dot-size": 6,
|
---|
422 | "values" : [15,18,19,14,17,18,15,18,17]
|
---|
423 | },
|
---|
424 | {
|
---|
425 | "type": "line_dot",
|
---|
426 | "colour": "#CC3399",
|
---|
427 | "width": 2,
|
---|
428 | "text": "Downloads",
|
---|
429 | "font-size": 10,
|
---|
430 | "dot-size": 5,
|
---|
431 | "values" : [10,12,14,9,12,13,10,13,12]
|
---|
432 | },
|
---|
433 | {
|
---|
434 | "type": "line_hollow",
|
---|
435 | "colour": "#80a033",
|
---|
436 | "width": 2,
|
---|
437 | "text": "Bounces",
|
---|
438 | "font-size": 10,
|
---|
439 | "dot-size": 6,
|
---|
440 | "values" : [5,7,9,7,4,6,1,2,5]
|
---|
441 | }
|
---|
442 | ],
|
---|
443 |
|
---|
444 | "y_axis":{
|
---|
445 | "max": 20
|
---|
446 | },
|
---|
447 |
|
---|
448 | "x_axis":{
|
---|
449 | "steps": 2,
|
---|
450 | "labels": ["January","February","March","April","May","June","July","August","September"]
|
---|
451 | }
|
---|
452 | }
|
---|
453 |
|
---|
454 | ========
|
---|
455 | Examples
|
---|
456 | ========
|
---|
457 |
|
---|
458 | Here is a simple JSON object that contains a horizontal bar chart:
|
---|
459 |
|
---|
460 | {
|
---|
461 | "title":{
|
---|
462 | "text":"HBar Map X values",
|
---|
463 | "style":"{font-size: 20px; font-family: Verdana; text-align: center;}"
|
---|
464 | },
|
---|
465 |
|
---|
466 | "elements":[
|
---|
467 | {
|
---|
468 | "type": "hbar",
|
---|
469 | "colour": "#9933CC",
|
---|
470 | "text": "Page views",
|
---|
471 | "font-size": 10,
|
---|
472 | "values" : [{"right":10},{"right":15},{"left":13,"right":17}]
|
---|
473 | }
|
---|
474 | ],
|
---|
475 | "x_axis":{
|
---|
476 | "min": 0,
|
---|
477 | "max": 20,
|
---|
478 | "offset": 0,
|
---|
479 | "labels": ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v"]
|
---|
480 | },
|
---|
481 | "y_axis":{
|
---|
482 | "stroke": 14,
|
---|
483 | "tick_length": 30,
|
---|
484 | "colour": "#d09090",
|
---|
485 | "grid_colour": "#00ff00",
|
---|
486 | "offset": 1,
|
---|
487 | "labels": ["slashdot.org","digg.com","reddit.com"]
|
---|
488 | }
|
---|
489 | }
|
---|