data-series.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * Creates data series.
  3. * @constructor
  4. * @param {Object} params Parameters to initialize series with.
  5. * @param {Array} params.values The data set to visualize.
  6. * @param {String} params.attribute Numberic or color attribute to use for data visualization. This could be: <code>fill</code>, <code>stroke</code>, <code>fill-opacity</code>, <code>stroke-opacity</code> for markers and regions and <code>r</code> (radius) for markers only.
  7. * @param {Array} params.scale Values used to map a dimension of data to a visual representation. The first value sets visualization for minimum value from the data set and the last value sets visualization for the maximum value. There also could be intermidiate values. Default value is <code>['#C8EEFF', '#0071A4']</code>
  8. * @param {Function|String} params.normalizeFunction The function used to map input values to the provided scale. This parameter could be provided as function or one of the strings: <code>'linear'</code> or <code>'polynomial'</code>, while <code>'linear'</code> is used by default. The function provided takes value from the data set as an input and returns corresponding value from the scale.
  9. * @param {Number} params.min Minimum value of the data set. Could be calculated automatically if not provided.
  10. * @param {Number} params.min Maximum value of the data set. Could be calculated automatically if not provided.
  11. */
  12. jvm.DataSeries = function(params, elements) {
  13. var scaleConstructor;
  14. params = params || {};
  15. params.attribute = params.attribute || 'fill';
  16. this.elements = elements;
  17. this.params = params;
  18. if (params.attributes) {
  19. this.setAttributes(params.attributes);
  20. }
  21. if (jvm.$.isArray(params.scale)) {
  22. scaleConstructor = (params.attribute === 'fill' || params.attribute === 'stroke') ? jvm.ColorScale : jvm.NumericScale;
  23. this.scale = new scaleConstructor(params.scale, params.normalizeFunction, params.min, params.max);
  24. } else if (params.scale) {
  25. this.scale = new jvm.OrdinalScale(params.scale);
  26. } else {
  27. this.scale = new jvm.SimpleScale(params.scale);
  28. }
  29. this.values = params.values || {};
  30. this.setValues(this.values);
  31. };
  32. jvm.DataSeries.prototype = {
  33. setAttributes: function(key, attr){
  34. var attrs = key,
  35. code;
  36. if (typeof key == 'string') {
  37. if (this.elements[key]) {
  38. this.elements[key].setStyle(this.params.attribute, attr);
  39. }
  40. } else {
  41. for (code in attrs) {
  42. if (this.elements[code]) {
  43. this.elements[code].element.setStyle(this.params.attribute, attrs[code]);
  44. }
  45. }
  46. }
  47. },
  48. /**
  49. * Set values for the data set.
  50. * @param {Object} values Object which maps codes of regions or markers to values.
  51. */
  52. setValues: function(values) {
  53. var max = Number.MIN_VALUE,
  54. min = Number.MAX_VALUE,
  55. val,
  56. cc,
  57. attrs = {};
  58. if (!(this.scale instanceof jvm.OrdinalScale) && !(this.scale instanceof jvm.SimpleScale)) {
  59. if (!this.params.min || !this.params.max) {
  60. for (cc in values) {
  61. val = parseFloat(values[cc]);
  62. if (val > max) max = values[cc];
  63. if (val < min) min = val;
  64. }
  65. if (!this.params.min) {
  66. this.scale.setMin(min);
  67. }
  68. if (!this.params.max) {
  69. this.scale.setMax(max);
  70. }
  71. this.params.min = min;
  72. this.params.max = max;
  73. }
  74. for (cc in values) {
  75. val = parseFloat(values[cc]);
  76. if (!isNaN(val)) {
  77. attrs[cc] = this.scale.getValue(val);
  78. } else {
  79. attrs[cc] = this.elements[cc].element.style.initial[this.params.attribute];
  80. }
  81. }
  82. } else {
  83. for (cc in values) {
  84. if (values[cc]) {
  85. attrs[cc] = this.scale.getValue(values[cc]);
  86. } else {
  87. attrs[cc] = this.elements[cc].element.style.initial[this.params.attribute];
  88. }
  89. }
  90. }
  91. this.setAttributes(attrs);
  92. jvm.$.extend(this.values, values);
  93. },
  94. clear: function(){
  95. var key,
  96. attrs = {};
  97. for (key in this.values) {
  98. if (this.elements[key]) {
  99. attrs[key] = this.elements[key].element.style.initial[this.params.attribute];
  100. }
  101. }
  102. this.setAttributes(attrs);
  103. this.values = {};
  104. },
  105. /**
  106. * Set scale of the data series.
  107. * @param {Array} scale Values representing scale.
  108. */
  109. setScale: function(scale) {
  110. this.scale.setScale(scale);
  111. if (this.values) {
  112. this.setValues(this.values);
  113. }
  114. },
  115. /**
  116. * Set normalize function of the data series.
  117. * @param {Function|String} normilizeFunction.
  118. */
  119. setNormalizeFunction: function(f) {
  120. this.scale.setNormalizeFunction(f);
  121. if (this.values) {
  122. this.setValues(this.values);
  123. }
  124. }
  125. };