stacked-barchart.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. import BaseChartComponent from 'yarn-ui/components/base-chart-component';
  19. import Mock from 'yarn-ui/utils/mock';
  20. export default BaseChartComponent.extend({
  21. MAX_BAR_HEIGHT: 120,
  22. MAX_BAR_WIDTH: 30,
  23. GAP: 5,
  24. filter: "",
  25. WIDTH_OF_SAMPLE: 200,
  26. bindTP: function(element) {
  27. element.on("mouseover", function() {
  28. this.tooltip
  29. .style("left", (d3.event.pageX) + "px")
  30. .style("top", (d3.event.pageY - 28) + "px");
  31. element.style("opacity", 1.0);
  32. }.bind(this))
  33. .on("mousemove", function() {
  34. // Handle pie chart case
  35. var text = element.attr("tooltiptext");
  36. this.tooltip.style("opacity", .9);
  37. this.tooltip.html(text)
  38. .style("left", (d3.event.pageX) + "px")
  39. .style("top", (d3.event.pageY - 28) + "px");
  40. }.bind(this))
  41. .on("mouseout", function() {
  42. this.tooltip.style("opacity", 0);
  43. element.style("opacity", 0.8);
  44. }.bind(this));
  45. element.on("click", function() {
  46. if (element.attr("link")) {
  47. this.tooltip.remove();
  48. document.location.href = element.attr("link");
  49. }
  50. }.bind(this));
  51. },
  52. printSamples: function(n, layout, g, colorTitles) {
  53. var yOffset = layout.margin * 3;
  54. for (var i = 0; i < n; i++) {
  55. var xOffset = layout.x2 - this.WIDTH_OF_SAMPLE - layout.margin;
  56. g.append("rect").
  57. attr("fill", this.colors[i]).
  58. attr("x", xOffset).
  59. attr("y", yOffset).
  60. attr("width", 20).
  61. attr("height", 20);
  62. g.append("text").
  63. attr("x", xOffset + 30).
  64. attr("y", yOffset + 10).
  65. text(colorTitles[i]);
  66. yOffset = yOffset + 30;
  67. }
  68. },
  69. // data:
  70. // [[{value=xx, bindText=xx}, {value=yy, bindText=yy}], [ ... ]]
  71. // __________________________________________________ ___________
  72. // bar-1 bar-2
  73. show: function (data, title, colorTitles) {
  74. var width = this.MAX_BAR_WIDTH;
  75. var height = this.MAX_BAR_HEIGHT;
  76. this.chart.g.remove();
  77. this.chart.g = this.chart.svg.append("g");
  78. var g = this.chart.g;
  79. var layout = this.getLayout();
  80. layout.margin = 50;
  81. var nBarPerRow = Math.floor((layout.x2 - layout.x1 - 3 * layout.margin -
  82. this.WIDTH_OF_SAMPLE) /
  83. (width + this.GAP));
  84. var xOffset;
  85. var yOffset = layout.margin * 2;
  86. var maxValue = 0;
  87. var maxN = 0;
  88. for (var i = 0; i < data.length; i++) {
  89. var total = 0;
  90. for (var j = 0; j < data[i].length; j++) {
  91. total += data[i][j].value;
  92. }
  93. if (total > maxValue) {
  94. maxValue = total;
  95. }
  96. if (data[i].length > maxN) {
  97. maxN = data[i].length;
  98. }
  99. }
  100. // print samples
  101. this.printSamples(maxN, layout, g, colorTitles);
  102. // print data
  103. data.sort(function(a, b) {
  104. return b[0].value - a[0].value;
  105. });
  106. for (var i = 0; i < data.length; i++) {
  107. if (i % nBarPerRow == 0) {
  108. xOffset = layout.margin;
  109. yOffset += layout.margin + height;
  110. }
  111. var leftTopY = yOffset;
  112. for (var j = 0; j < data[i].length; j++) {
  113. var dy = data[i][j].value * height / maxValue;
  114. if (dy > 0) {
  115. leftTopY = leftTopY - dy;
  116. var node = g.append("rect").
  117. attr("fill", this.colors[j]).
  118. attr("x", xOffset).
  119. attr("y", leftTopY).
  120. attr("width", width).
  121. attr("height", dy).
  122. attr("tooltiptext",
  123. (data[i][j].bindText) ? data[i][j].bindText : data[i][j].value).
  124. attr("link", data[i][j].link)
  125. .style("opacity", 0.8);
  126. this.bindTP(node);
  127. }
  128. }
  129. if (data[i].length == 1) {
  130. g.append("text")
  131. .text(data[i][0].value)
  132. .attr("y", leftTopY - 10)
  133. .attr("x", xOffset + width / 2)
  134. .attr("class", "heatmap-cell")
  135. .style("fill", "black");
  136. }
  137. xOffset += width + this.GAP;
  138. }
  139. layout.y2 = yOffset + layout.margin;
  140. this.adjustMaxHeight(layout.y2);
  141. this.renderTitleAndBG(g, title, layout, false);
  142. },
  143. draw: function(data, title, textWidth) {
  144. this.initChart(true);
  145. //Mock.initMockNodesData(this);
  146. // mock data
  147. var arr = [];
  148. for (var i = 0; i < 5; i++) {
  149. var subArr = [];
  150. for (var j = 0; j < Math.random() * 4 + 1; j++) {
  151. subArr.push({
  152. value : Math.abs(Math.random())
  153. });
  154. }
  155. arr.push(subArr);
  156. }
  157. this.show(
  158. arr, this.get("title"));
  159. },
  160. didInsertElement: function () {
  161. this.draw();
  162. },
  163. actions: {
  164. applyFilter: function(event) {
  165. this.filter = event.srcElement.value;
  166. this.didInsertElement();
  167. }
  168. }
  169. })