| 1 | /* |
| 2 | PlotKit EasyPlot |
| 3 | ================ |
| 4 | |
| 5 | User friendly wrapper around the common plotting functions. |
| 6 | |
| 7 | Copyright |
| 8 | --------- |
| 9 | Copyright 2005,2006 (c) Alastair Tse <alastair^liquidx.net> |
| 10 | For use under the BSD license. <http://www.liquidx.net/plotkit> |
| 11 | |
| 12 | */ |
| 13 | |
| 14 | try { |
| 15 | if (typeof(PlotKit.CanvasRenderer) == 'undefined') |
| 16 | { |
| 17 | throw "" |
| 18 | } |
| 19 | } |
| 20 | catch (e) { |
| 21 | throw "PlotKit.EasyPlot depends on all of PlotKit's components"; |
| 22 | } |
| 23 | |
| 24 | // -------------------------------------------------------------------- |
| 25 | // Start of EasyPlot definition |
| 26 | // -------------------------------------------------------------------- |
| 27 | |
| 28 | if (typeof(PlotKit.EasyPlot) == 'undefined') { |
| 29 | PlotKit.EasyPlot = {}; |
| 30 | } |
| 31 | |
| 32 | PlotKit.EasyPlot.NAME = "PlotKit.EasyPlot"; |
| 33 | PlotKit.EasyPlot.VERSION = PlotKit.VERSION; |
| 34 | |
| 35 | PlotKit.EasyPlot.__repr__ = function() { |
| 36 | return "[" + this.NAME + " " + this.VERSION + "]"; |
| 37 | }; |
| 38 | |
| 39 | PlotKit.EasyPlot.toString = function() { |
| 40 | return this.__repr__(); |
| 41 | } |
| 42 | |
| 43 | // -------------------------------------------------------------------- |
| 44 | // Start of EasyPlot definition |
| 45 | // -------------------------------------------------------------------- |
| 46 | |
| 47 | PlotKit.EasyPlot = function(style, options, divElem, datasources) { |
| 48 | this.layout = new Layout(style, options); |
| 49 | this.divElem = divElem; |
| 50 | this.width = parseInt(divElem.getAttribute('width')); |
| 51 | this.height = parseInt(divElem.getAttribute('height')); |
| 52 | this.deferredCount = 0; |
| 53 | |
| 54 | // make sure we have non-zero width |
| 55 | if (this.width < 1) { |
| 56 | this.width = this.divElem.width ? this.divElem.width : 300; |
| 57 | } |
| 58 | |
| 59 | if (this.height < 1) { |
| 60 | this.height = this.divElem.height ? this.divElem.height : 300; |
| 61 | } |
| 62 | |
| 63 | // load data sources |
| 64 | if (isArrayLike(datasources)) { |
| 65 | for (var i = 0; i < datasources.length; i++) { |
| 66 | if (typeof(datasources[i]) == "string") { |
| 67 | this.deferredCount++; |
| 68 | // load CSV via ajax |
| 69 | var d = MochiKit.Async.doSimpleXMLHttpRequest(datasources[i]); |
| 70 | d.addCallback(MochiKit.Base.bind(PlotKit.EasyPlot.onDataLoaded, this)); |
| 71 | } |
| 72 | else if (isArrayLike(datasources[i])) { |
| 73 | this.layout.addDataset("data-" + i, datasources[i]); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | else if (!isUndefinedOrNull(datasources)) { |
| 78 | throw "Passed datasources are not Array like"; |
| 79 | } |
| 80 | |
| 81 | // setup canvas to render |
| 82 | |
| 83 | if (CanvasRenderer.isSupported()) { |
| 84 | this.element = CANVAS({"id": this.divElem.getAttribute("id") + "-canvas", |
| 85 | "width": this.width, |
| 86 | "height": this.height}, ""); |
| 87 | this.divElem.appendChild(this.element); |
| 88 | this.renderer = new SweetCanvasRenderer(this.element, this.layout, options); |
| 89 | } |
| 90 | else if (SVGRenderer.isSupported()) { |
| 91 | this.element = SVGRenderer.SVG({"id": this.divElem.getAttribute("id") + "-svg", |
| 92 | "width": this.width, |
| 93 | "height": this.height, |
| 94 | "version": "1.1", |
| 95 | "baseProfile": "full"}, ""); |
| 96 | this.divElem.appendChild(this.element); |
| 97 | this.renderer = new SweetSVGRenderer(this.element, this.layout, options); |
| 98 | } |
| 99 | |
| 100 | if ((this.deferredCount == 0) && (PlotKit.Base.keys(this.layout.datasets).length > 0)) { |
| 101 | this.layout.evaluate(); |
| 102 | this.renderer.clear(); |
| 103 | this.renderer.render(); |
| 104 | } |
| 105 | |
| 106 | }; |
| 107 | |
| 108 | PlotKit.EasyPlot.onDataLoaded = function(request) { |
| 109 | |
| 110 | // very primitive CSV parser, should fix to make it more compliant. |
| 111 | var table = new Array(); |
| 112 | var lines = request.responseText.split('\n'); |
| 113 | for (var i = 0; i < lines.length; i++) { |
| 114 | var stripped = MochiKit.Format.strip(lines[i]); |
| 115 | if ((stripped.length > 1) && (stripped.charAt(0) != '#')) { |
| 116 | table.push(stripped.split(',')); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | this.layout.addDataset("data-ajax-" + this.deferredCount, table); |
| 121 | this.deferredCount--; |
| 122 | |
| 123 | if ((this.deferredCount == 0) && (PlotKit.Base.keys(this.layout.datasets).length > 0)) { |
| 124 | this.layout.evaluate(); |
| 125 | this.renderer.clear(); |
| 126 | this.renderer.render(); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | PlotKit.EasyPlot.prototype.reload = function() { |
| 131 | this.layout.evaluate(); |
| 132 | this.renderer.clear(); |
| 133 | this.renderer.render(); |
| 134 | }; |
| 135 | |
| 136 | // Namespace Iniitialisation |
| 137 | |
| 138 | PlotKit.EasyPlotModule = {}; |
| 139 | PlotKit.EasyPlotModule.EasyPlot = PlotKit.EasyPlot; |
| 140 | |
| 141 | PlotKit.EasyPlotModule.EXPORT = [ |
| 142 | "EasyPlot" |
| 143 | ]; |
| 144 | |
| 145 | PlotKit.EasyPlotModule.EXPORT_OK = []; |
| 146 | |
| 147 | PlotKit.EasyPlotModule.__new__ = function() { |
| 148 | var m = MochiKit.Base; |
| 149 | |
| 150 | m.nameFunctions(this); |
| 151 | |
| 152 | this.EXPORT_TAGS = { |
| 153 | ":common": this.EXPORT, |
| 154 | ":all": m.concat(this.EXPORT, this.EXPORT_OK) |
| 155 | }; |
| 156 | }; |
| 157 | |
| 158 | PlotKit.EasyPlotModule.__new__(); |
| 159 | MochiKit.Base._exportSymbols(this, PlotKit.EasyPlotModule); |
| 160 | |
| 161 | |