Show:

File: ia\charts\layers\CountLayer.js

  1. /**
  2. * The base class for count bar layers.
  3. *
  4. * @author J Clare
  5. * @class ia.CountLayer
  6. * @extends ia.BarLayer
  7. * @constructor
  8. */
  9. ia.CountLayer = function()
  10. {
  11. ia.CountLayer.baseConstructor.call(this);
  12.  
  13. this.isLegendComponent = true;
  14. };
  15. ia.extend(ia.BarLayer, ia.CountLayer);
  16.  
  17. /**
  18. * Specifies a thematic for the pie layer.
  19. *
  20. * @property thematic
  21. * @type ia.Thematic
  22. */
  23. ia.CountLayer.prototype.thematic;
  24.  
  25. /**
  26. * Indicates this is a legend component.
  27. *
  28. * @property isLegendComponent
  29. * @type Boolean
  30. * @default true
  31. */
  32. ia.CountLayer.prototype.isLegendComponent;
  33.  
  34. /**
  35. * Updates the data.
  36. *
  37. * @method update
  38. */
  39. ia.CountLayer.prototype.update = function()
  40. {
  41. // Check if the data has changed
  42. if (this.map && this.dataChanged)
  43. {
  44. // Get the data.
  45. var data = this.getData()
  46.  
  47. // Clear the items.
  48. this.itemArray = [];
  49. this.clearItems();
  50. this.minValue = Infinity;
  51. this.maxValue = -Infinity;
  52. var classes = this.thematic.getClasses();
  53. for (var i = 0; i < classes.length; i++)
  54. {
  55. var legendClass = classes[i];
  56.  
  57. // Create a new chart item.
  58. var chartItem = {};
  59. chartItem.id = legendClass.index;
  60. chartItem.legendClass = legendClass;
  61. chartItem.color = legendClass.color;
  62. chartItem.value = legendClass.items.length;
  63. chartItem.state = ia.ItemLayer.UNSELECTED;
  64. if (this.selectionIds.indexOf(chartItem.id) !== -1 ) chartItem.state = ia.ItemLayer.SELECTED;
  65. chartItem.parent = this;
  66. chartItem.layer = this;
  67. chartItem.label = legendClass.getLabel() + " : " + chartItem.value;
  68. chartItem.rect = new ia.Rectangle();
  69. chartItem.hitArea = new ia.Rectangle();
  70.  
  71. this.items[chartItem.id] = chartItem;
  72. this.itemArray.push(chartItem);
  73.  
  74. // Get the min and max bar values for the layer.
  75. this.minValue = Math.min(this.minValue, chartItem.value);
  76. this.maxValue = Math.max(this.maxValue, chartItem.value);
  77. }
  78. // Sort items if in sort direction.
  79. if (this.sortDirection !== undefined)
  80. {
  81. var dir;
  82. if (this.sortDirection === "ascending") dir = 1
  83. else dir = -1;
  84. this.itemArray.sort(function(a, b)
  85. {
  86. if (a.value < b.value) return -dir;
  87. if (a.value > b.value) return dir;
  88. return 0;
  89. });
  90. }
  91. this.dataChanged = false;
  92. }
  93. };
  94.  
  95. /**
  96. * Displays the tip for the passed item
  97. *
  98. * @method showTip
  99. * @param {Object} item The map item.
  100. * @param {ia.ItemEvent} event An <code>ia.ItemEvent</code>.
  101. */
  102. ia.CountLayer.prototype.showTip = function(item, event)
  103. {
  104. this.map.datatip.text(item.label);
  105.  
  106. // Position above the bar.
  107. var px,py;
  108. if (ia.IS_TOUCH_DEVICE)
  109. {
  110. px = event.x - (this.map.datatip.getWidth() / 2);
  111. py = event.y - (this.map.datatip.getHeight() + 30);
  112. }
  113. else
  114. {
  115. if (this.map.orientation === "vertical")
  116. {
  117. px = (item.rect.x + (item.rect.width / 2)) - (this.map.datatip.getWidth() / 2);
  118.  
  119. if (item.value < 0)
  120. py = item.rect.y + item.rect.height + 5;
  121. else
  122. py = item.rect.y - this.map.datatip.getHeight() - 5;
  123. }
  124. else
  125. {
  126. if (item.value < 0)
  127. px = item.rect.x - (this.map.datatip.getWidth() + 5);
  128. else
  129. px = (item.rect.x + item.rect.width) + 5;
  130.  
  131. py = (item.rect.y + (item.rect.height / 2)) - (this.map.datatip.getHeight() / 2);
  132. }
  133. }
  134. this.map.datatip.position(px, py);
  135. this.map.datatip.show();
  136. };