source: code/Website/open-flash-chart/json.help.txt@ 7849

Last change on this file since 7849 was 7849, checked in by dennisw, 15 years ago
File size: 11.0 KB
RevLine 
[7849]1============
2Loading data
3============
4
5Open Flash Chart 2 can load data from a number of places:
6
7==========
81. The URL
9==========
10
11OFC 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
15OFC will try to load the file http://example.com/open-flash-chart/data/data.txt
16
17NOTE:
18- ofc=data/data.txt is a relative path.
19- The data file must contain JSON (see examples in this document)
20
21==================
222. Flash Variables
23==================
24
25If the URL does not contain a variable 'ofc' the next thing it will
26try is look in the variables that have been passed to it for a 'data-file'
27if 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
36see flash-variable.html for an example of this.
37
38NOTE:
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==================
443. From Javascript
45==================
46
47If the URL does not contain a varibale 'ofc' the next thing it will
48try is calling the Javascript function open_flash_chart_data(), so
49you may have:
50
51function open_flash_chart_data()
52{
53 alert( 'reading data' );
54 return JSON.stringify(data);
55}
56
57This function should return a valid JSON string.
58
59see json-test.html for an example of this.
60
61NOTE:
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=============================
683. You can push JSON into OFC
69=============================
70
71Using Javascript you can push data into OFC via an external interface.
72This is really easy. When OFC has loaded and tried 1, 2 and 3 above and
73failed to find any data it will try to call the Javascript function
74ofc_ready():
75
76function ofc_ready()
77{
78 alert('ofc_ready');
79 tmp = findSWF("ofc");
80 x = tmp.load( JSON.stringify(ofc) );
81}
82
83In this function you can push a JSON string into OFC using
84the interface 'load()', in the above function we find the chart
85then call load and pass in our JSON string.
86
87This is useful for AJAX pages.
88
89see json-test-2.html for an example of this.
90
91=====================
92 Data format: JSON
93=====================
94
95The data must be in JSON format. The basic JSON obect is:
96
97{}
98
99a more complete example with HTML and Javascript:
100
101&lt;script type="text/javascript"&gt;
102
103var data = {};
104
105&lt;/script&gt;
106
107
108Put all other objects inside this. For example the JSON object
109with 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
118This is what you would save into a data file. Below is the same data,
119but this time as part of the javascript in your web page:
120
121<script type="text/javascript">
122
123var 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
132This all sounds rather complex, but it isn't really. This is what is going on
133inside 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
139The same thing happens when you pass in a JSON object from Javascript,
140to create the JSON string, you call:
141
142 JSON.stringify(ofc)
143
144and pass the string into OFC:
145
146 tmp = findSWF("ofc");
147 x = tmp.load( JSON.stringify(ofc) );
148
149then 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==========
157Tutorial 1
158==========
159
160So, lets go.
161
162Copy the .swf to the root of your web site.
163
164Now take a copy of 'flash-variable.html' and put this into
165the root of the web site.
166
167Copy the Javascript files to the root of your site.
168
169Next take the example data file 'data.txt' and also put this
170into the root.
171
172Edit '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
176and change it to:
177
178 var so = new SWFObject("open-flash-chart.swf", "ofc", "250", "200", "9", "#FFFFFF");
179
180also change all the Javascript includes so they work.
181
182Now browse to:
183
184 http://example.com/flash-variable.html
185
186this should fail.
187
188 http://example.com/flash-variable.html?ofc=data.txt
189
190this should work.
191
192==========
193Tutorial 2
194==========
195
196Try editing the data file. Take a look at the example data files.
197
198
199================
200Title (optional)
201================
202All attributes are optional.
203text: string, the title
204style: 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===================
214Y Legend (optional)
215===================
216All attributes are optional.
217text: string, the title for the Y axis
218style: 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========
228Elements
229========
230The elements attribute is an array of generic objects.
231Each 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=================
255X Axis (optional)
256=================
257This object is optional, if it is not present the chart will show a default
258X axis.
259All attributes are optional.
260
261stroke : number, the width of the line
262tick-height : number, the height of the ticks
263colour : string, the colour of the line
264offset: boolean, x axis min (usually 0) is offset, used in bar charts
265grid-colour: string, colour of the grid lines
2663d: boolean, is it 3D
267steps: show every n ticks
268labels: array of strings, the labels of each X point
269
270Example:
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===============
282Y Axis optional
283===============
284This object is optional, if it is not present the chart will show a default
285Y axis.
286All attributes are optional.
287
288Example:
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============
301Elements.bar
302============
303A bar chart. Must be inside the elements array.
304
305type: string, must be 'bar'
306alpha: number, between 0 (transparent) and 1 (opaque)
307colour: string, CSS colour
308text: string, the key
309font-size: number, size of the key text
310values: array of numbers, height of each bar
311
312Example:
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============
327Elements.pie
328============
329A pie chart. Must be inside the elements array.
330
331type: string, must be 'pie'
332start-angle: number, the angle of the first pie slice
333colours: array of strings, CSS colour
334alpha: number, between 0 (transparent) and 1 (opaque)
335stroke: number, the outline width
336animate: boolean, animate the pie chart
337values: array of objects, value of each pie slice. May be a number or a slice object
338
339Example:
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=============
355Elements.hbar
356=============
357Horizontal Bar chart
358
359values: array of objects. Each value must have a "right" and an optional "left" value
360
361
362Example:
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=================
376Elements.line_dot
377=================
378Line chart
379
380values: Array of numbers:
381
382Example:
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=================
398Elements.line*
399=================
400Just a quick note of the 3 different line types:
401
402Example:
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========
455Examples
456========
457
458Here 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}
Note: See TracBrowser for help on using the repository browser.