Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/site/app/webroot/js/plotkit/EasyPlot.js
blob: 760773117708e16855721454a0af66b6639fdb4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* 
    PlotKit EasyPlot
    ================

    User friendly wrapper around the common plotting functions.

    Copyright
    ---------
    Copyright 2005,2006 (c) Alastair Tse <alastair^liquidx.net>
    For use under the BSD license. <http://www.liquidx.net/plotkit>
    
*/

try {    
    if (typeof(PlotKit.CanvasRenderer) == 'undefined')
    {
        throw ""
    }
} 
catch (e) {    
    throw "PlotKit.EasyPlot depends on all of PlotKit's components";
}

// --------------------------------------------------------------------
// Start of EasyPlot definition
// --------------------------------------------------------------------

if (typeof(PlotKit.EasyPlot) == 'undefined') {
    PlotKit.EasyPlot = {};
}

PlotKit.EasyPlot.NAME = "PlotKit.EasyPlot";
PlotKit.EasyPlot.VERSION = PlotKit.VERSION;

PlotKit.EasyPlot.__repr__ = function() {
    return "[" + this.NAME + " " + this.VERSION + "]";
};

PlotKit.EasyPlot.toString = function() {
    return this.__repr__();
}

// --------------------------------------------------------------------
// Start of EasyPlot definition
// --------------------------------------------------------------------

PlotKit.EasyPlot = function(style, options, divElem, datasources) {
    this.layout = new Layout(style, options);
    this.divElem = divElem;
    this.width = parseInt(divElem.getAttribute('width'));
    this.height = parseInt(divElem.getAttribute('height'));
    this.deferredCount = 0;

    // make sure we have non-zero width
    if (this.width < 1) {
        this.width = this.divElem.width ? this.divElem.width : 300;
    }
    
    if (this.height < 1) {
        this.height = this.divElem.height ? this.divElem.height : 300;
    }
    
    // load data sources
    if (isArrayLike(datasources)) {
        for (var i = 0; i < datasources.length; i++) {
            if (typeof(datasources[i]) == "string") {
                this.deferredCount++;
                // load CSV via ajax
                var d = MochiKit.Async.doSimpleXMLHttpRequest(datasources[i]);
                d.addCallback(MochiKit.Base.bind(PlotKit.EasyPlot.onDataLoaded, this));
            }
            else if (isArrayLike(datasources[i])) {
                this.layout.addDataset("data-" + i, datasources[i]);
            }
        }
    }
    else if (!isUndefinedOrNull(datasources)) {
        throw "Passed datasources are not Array like";
    }
    
    // setup canvas to render
    
    if (CanvasRenderer.isSupported()) {
        this.element = CANVAS({"id": this.divElem.getAttribute("id") + "-canvas",
                               "width": this.width,
                               "height": this.height}, "");
        this.divElem.appendChild(this.element);
        this.renderer = new SweetCanvasRenderer(this.element, this.layout, options);
    }
    else if (SVGRenderer.isSupported()) {
        this.element = SVGRenderer.SVG({"id": this.divElem.getAttribute("id") + "-svg",
                                        "width": this.width,
                                        "height": this.height,
                                        "version": "1.1",
                                        "baseProfile": "full"}, "");
        this.divElem.appendChild(this.element);
        this.renderer = new SweetSVGRenderer(this.element, this.layout, options);
    }
    
    if ((this.deferredCount == 0) && (PlotKit.Base.keys(this.layout.datasets).length > 0)) {
        this.layout.evaluate();
        this.renderer.clear();
        this.renderer.render();    
    }
    
};

PlotKit.EasyPlot.onDataLoaded = function(request) {
    
    // very primitive CSV parser, should fix to make it more compliant.
    var table = new Array();
    var lines = request.responseText.split('\n');
    for (var i = 0; i < lines.length; i++) {
        var stripped = MochiKit.Format.strip(lines[i]);
        if ((stripped.length > 1) && (stripped.charAt(0) != '#')) {
            table.push(stripped.split(','));
        }
    }
  
    this.layout.addDataset("data-ajax-" + this.deferredCount, table);
    this.deferredCount--;
    
    if ((this.deferredCount == 0) && (PlotKit.Base.keys(this.layout.datasets).length > 0)) {
        this.layout.evaluate();
        this.renderer.clear();
        this.renderer.render();
    }
};

PlotKit.EasyPlot.prototype.reload = function() {
    this.layout.evaluate();
    this.renderer.clear();
    this.renderer.render();
};

// Namespace Iniitialisation

PlotKit.EasyPlotModule = {};
PlotKit.EasyPlotModule.EasyPlot = PlotKit.EasyPlot;

PlotKit.EasyPlotModule.EXPORT = [
    "EasyPlot"
];

PlotKit.EasyPlotModule.EXPORT_OK = [];

PlotKit.EasyPlotModule.__new__ = function() {
    var m = MochiKit.Base;
    
    m.nameFunctions(this);
    
    this.EXPORT_TAGS = {
        ":common": this.EXPORT,
        ":all": m.concat(this.EXPORT, this.EXPORT_OK)
    };
};

PlotKit.EasyPlotModule.__new__();
MochiKit.Base._exportSymbols(this, PlotKit.EasyPlotModule);