[483] | 1 | define(["dojo/_base/array", "dojo/_base/declare", "dojo/_base/Deferred"], |
---|
| 2 | function(arr, declare, Deferred){ |
---|
| 3 | |
---|
| 4 | return declare("dojox.charting.StoreSeries", null, { |
---|
| 5 | constructor: function(store, kwArgs, value){ |
---|
| 6 | // summary: |
---|
| 7 | // Series adapter for dojo object stores (dojo.store). |
---|
| 8 | // store: Object |
---|
| 9 | // A dojo object store. |
---|
| 10 | // kwArgs: Object |
---|
| 11 | // A store-specific keyword parameters used for querying objects. |
---|
| 12 | // See dojo.store docs |
---|
| 13 | // value: Function|Object|String |
---|
| 14 | // Function, which takes an object handle, and |
---|
| 15 | // produces an output possibly inspecting the store's item. Or |
---|
| 16 | // a dictionary object, which tells what names to extract from |
---|
| 17 | // an object and how to map them to an output. Or a string, which |
---|
| 18 | // is a numeric field name to use for plotting. If undefined, null |
---|
| 19 | // or empty string (the default), "value" field is extracted. |
---|
| 20 | this.store = store; |
---|
| 21 | this.kwArgs = kwArgs; |
---|
| 22 | |
---|
| 23 | if(value){ |
---|
| 24 | if(typeof value == "function"){ |
---|
| 25 | this.value = value; |
---|
| 26 | }else if(typeof value == "object"){ |
---|
| 27 | this.value = function(object){ |
---|
| 28 | var o = {}; |
---|
| 29 | for(var key in value){ |
---|
| 30 | o[key] = object[value[key]]; |
---|
| 31 | } |
---|
| 32 | return o; |
---|
| 33 | }; |
---|
| 34 | }else{ |
---|
| 35 | this.value = function(object){ |
---|
| 36 | return object[value]; |
---|
| 37 | }; |
---|
| 38 | } |
---|
| 39 | }else{ |
---|
| 40 | this.value = function(object){ |
---|
| 41 | return object.value; |
---|
| 42 | }; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | this.data = []; |
---|
| 46 | |
---|
| 47 | this._initialRendering = false; |
---|
| 48 | this.fetch(); |
---|
| 49 | }, |
---|
| 50 | |
---|
| 51 | destroy: function(){ |
---|
| 52 | // summary: |
---|
| 53 | // Clean up before GC. |
---|
| 54 | if(this.observeHandle){ |
---|
| 55 | this.observeHandle.remove(); |
---|
| 56 | } |
---|
| 57 | }, |
---|
| 58 | |
---|
| 59 | setSeriesObject: function(series){ |
---|
| 60 | // summary: |
---|
| 61 | // Sets a dojox.charting.Series object we will be working with. |
---|
| 62 | // series: dojox/charting/Series |
---|
| 63 | // Our interface to the chart. |
---|
| 64 | this.series = series; |
---|
| 65 | }, |
---|
| 66 | |
---|
| 67 | // store fetch loop |
---|
| 68 | |
---|
| 69 | fetch: function(){ |
---|
| 70 | // summary: |
---|
| 71 | // Fetches data from the store and updates a chart. |
---|
| 72 | var self = this; |
---|
| 73 | if(this.observeHandle){ |
---|
| 74 | this.observeHandle.remove(); |
---|
| 75 | } |
---|
| 76 | var results = this.store.query(this.kwArgs.query, this.kwArgs); |
---|
| 77 | Deferred.when(results, function(objects){ |
---|
| 78 | self.objects = objects; |
---|
| 79 | update(); |
---|
| 80 | }); |
---|
| 81 | if(results.observe){ |
---|
| 82 | this.observeHandle = results.observe(update, true); |
---|
| 83 | } |
---|
| 84 | function update(){ |
---|
| 85 | self.data = arr.map(self.objects, function(object){ |
---|
| 86 | return self.value(object, self.store); |
---|
| 87 | }); |
---|
| 88 | self._pushDataChanges(); |
---|
| 89 | } |
---|
| 90 | }, |
---|
| 91 | |
---|
| 92 | _pushDataChanges: function(){ |
---|
| 93 | if(this.series){ |
---|
| 94 | this.series.chart.updateSeries(this.series.name, this, this._initialRendering); |
---|
| 95 | this._initialRendering = false; |
---|
| 96 | this.series.chart.delayedRender(); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | }); |
---|
| 101 | }); |
---|