StoreSeries.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.charting.StoreSeries"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.charting.StoreSeries"] = true;
  8. dojo.provide("dojox.charting.StoreSeries");
  9. dojo.declare("dojox.charting.StoreSeries", null, {
  10. constructor: function(store, kwArgs, value){
  11. // summary:
  12. // Series adapter for dojo object stores (dojo.store).
  13. // store: Object:
  14. // A dojo object store.
  15. // kwArgs: Object:
  16. // A store-specific keyword parameters used for querying objects.
  17. // See dojo.store docs
  18. // value: Function|Object|String|Null:
  19. // Function, which takes an object handle, and
  20. // produces an output possibly inspecting the store's item. Or
  21. // a dictionary object, which tells what names to extract from
  22. // an object and how to map them to an output. Or a string, which
  23. // is a numeric field name to use for plotting. If undefined, null
  24. // or empty string (the default), "value" field is extracted.
  25. this.store = store;
  26. this.kwArgs = kwArgs;
  27. if(value){
  28. if(typeof value == "function"){
  29. this.value = value;
  30. }else if(typeof value == "object"){
  31. this.value = function(object){
  32. var o = {};
  33. for(var key in value){
  34. o[key] = object[value[key]];
  35. }
  36. return o;
  37. };
  38. }else{
  39. this.value = function(object){
  40. return object[value];
  41. };
  42. }
  43. }else{
  44. this.value = function(object){
  45. return object.value;
  46. };
  47. }
  48. this.data = [];
  49. this.fetch();
  50. },
  51. destroy: function(){
  52. // summary:
  53. // Clean up before GC.
  54. if(this.observeHandle){
  55. this.observeHandle.dismiss();
  56. }
  57. },
  58. setSeriesObject: function(series){
  59. // summary:
  60. // Sets a dojox.charting.Series object we will be working with.
  61. // series: dojox.charting.Series:
  62. // Our interface to the chart.
  63. this.series = series;
  64. },
  65. // store fetch loop
  66. fetch: function(){
  67. // summary:
  68. // Fetches data from the store and updates a chart.
  69. var objects = this.objects = [];
  70. var self = this;
  71. if(this.observeHandle){
  72. this.observeHandle.dismiss();
  73. }
  74. var results = this.store.query(this.kwArgs.query, this.kwArgs);
  75. dojo.when(results, function(objects){
  76. self.objects = objects;
  77. update();
  78. });
  79. if(results.observe){
  80. this.observeHandle = results.observe(update, true);
  81. }
  82. function update(){
  83. self.data = dojo.map(self.objects, function(object){
  84. return self.value(object, self.store);
  85. });
  86. self._pushDataChanges();
  87. }
  88. },
  89. _pushDataChanges: function(){
  90. if(this.series){
  91. this.series.chart.updateSeries(this.series.name, this);
  92. this.series.chart.delayedRender();
  93. }
  94. }
  95. });
  96. }