[9617] | 1 | /** |
---|
| 2 | * jqPlot |
---|
| 3 | * Pure JavaScript plotting plugin using jQuery |
---|
| 4 | * |
---|
| 5 | * Version: 1.0.0b2_r1012 |
---|
| 6 | * |
---|
| 7 | * Copyright (c) 2009-2011 Chris Leonello |
---|
| 8 | * jqPlot is currently available for use in all personal or commercial projects |
---|
| 9 | * under both the MIT (http://www.opensource.org/licenses/mit-license.php) and GPL |
---|
| 10 | * version 2.0 (http://www.gnu.org/licenses/gpl-2.0.html) licenses. This means that you can |
---|
| 11 | * choose the license that best suits your project and use it accordingly. |
---|
| 12 | * |
---|
| 13 | * Although not required, the author would appreciate an email letting him |
---|
| 14 | * know of any substantial use of jqPlot. You can reach the author at: |
---|
| 15 | * chris at jqplot dot com or see http://www.jqplot.com/info.php . |
---|
| 16 | * |
---|
| 17 | * If you are feeling kind and generous, consider supporting the project by |
---|
| 18 | * making a donation at: http://www.jqplot.com/donate.php . |
---|
| 19 | * |
---|
| 20 | * sprintf functions contained in jqplot.sprintf.js by Ash Searle: |
---|
| 21 | * |
---|
| 22 | * version 2007.04.27 |
---|
| 23 | * author Ash Searle |
---|
| 24 | * http://hexmen.com/blog/2007/03/printf-sprintf/ |
---|
| 25 | * http://hexmen.com/js/sprintf.js |
---|
| 26 | * The author (Ash Searle) has placed this code in the public domain: |
---|
| 27 | * "This code is unrestricted: you are free to use it however you like." |
---|
| 28 | * |
---|
| 29 | */ |
---|
| 30 | (function($) { |
---|
| 31 | /** |
---|
| 32 | * Class: $.jqplot.ciParser |
---|
| 33 | * Data Renderer function which converts a custom JSON data object into jqPlot data format. |
---|
| 34 | * Set this as a callable on the jqplot dataRenderer plot option: |
---|
| 35 | * |
---|
| 36 | * > plot = $.jqplot('mychart', [data], { dataRenderer: $.jqplot.ciParser, ... }); |
---|
| 37 | * |
---|
| 38 | * Where data is an object in JSON format or a JSON encoded string conforming to the |
---|
| 39 | * City Index API spec. |
---|
| 40 | * |
---|
| 41 | * Note that calling the renderer function is handled internally by jqPlot. The |
---|
| 42 | * user does not have to call the function. The parameters described below will |
---|
| 43 | * automatically be passed to the ciParser function. |
---|
| 44 | * |
---|
| 45 | * Parameters: |
---|
| 46 | * data - JSON encoded string or object. |
---|
| 47 | * plot - reference to jqPlot Plot object. |
---|
| 48 | * |
---|
| 49 | * Returns: |
---|
| 50 | * data array in jqPlot format. |
---|
| 51 | * |
---|
| 52 | */ |
---|
| 53 | $.jqplot.ciParser = function (data, plot) { |
---|
| 54 | var ret = [], |
---|
| 55 | line, |
---|
| 56 | temp, |
---|
| 57 | i, j, k, kk; |
---|
| 58 | |
---|
| 59 | if (typeof(data) == "string") { |
---|
| 60 | data = $.jqplot.JSON.parse(data, handleStrings); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | else if (typeof(data) == "object") { |
---|
| 64 | for (k in data) { |
---|
| 65 | for (i=0; i<data[k].length; i++) { |
---|
| 66 | for (kk in data[k][i]) { |
---|
| 67 | data[k][i][kk] = handleStrings(kk, data[k][i][kk]); |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | else { |
---|
| 74 | return null; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | // function handleStrings |
---|
| 78 | // Checks any JSON encoded strings to see if they are |
---|
| 79 | // encoded dates. If so, pull out the timestamp. |
---|
| 80 | // Expects dates to be represented by js timestamps. |
---|
| 81 | |
---|
| 82 | function handleStrings(key, value) { |
---|
| 83 | var a; |
---|
| 84 | if (value != null) { |
---|
| 85 | if (value.toString().indexOf('Date') >= 0) { |
---|
| 86 | //here we will try to extract the ticks from the Date string in the "value" fields of JSON returned data |
---|
| 87 | a = /^\/Date\((-?[0-9]+)\)\/$/.exec(value); |
---|
| 88 | if (a) { |
---|
| 89 | return parseInt(a[1], 10); |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | return value; |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | for (var prop in data) { |
---|
| 97 | line = []; |
---|
| 98 | temp = data[prop]; |
---|
| 99 | switch (prop) { |
---|
| 100 | case "PriceTicks": |
---|
| 101 | for (i=0; i<temp.length; i++) { |
---|
| 102 | line.push([temp[i]['TickDate'], temp[i]['Price']]); |
---|
| 103 | } |
---|
| 104 | break; |
---|
| 105 | case "PriceBars": |
---|
| 106 | for (i=0; i<temp.length; i++) { |
---|
| 107 | line.push([temp[i]['BarDate'], temp[i]['Open'], temp[i]['High'], temp[i]['Low'], temp[i]['Close']]); |
---|
| 108 | } |
---|
| 109 | break; |
---|
| 110 | } |
---|
| 111 | ret.push(line); |
---|
| 112 | } |
---|
| 113 | return ret; |
---|
| 114 | }; |
---|
| 115 | })(jQuery); |
---|