json.help.txt 11 KB

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