StoreSeries.js 2.6 KB

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