DojoD3Histogram.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. define([
  2. "dojo/_base/declare",
  3. "dojo/_base/lang",
  4. "dojo/_base/array",
  5. "./DojoD3",
  6. "./Mapping",
  7. "dojo/text!./templates/DojoD3Histogram.css"
  8. ], function (declare, lang, arrayUtil,
  9. DojoD3, Mapping,
  10. css) {
  11. return declare([Mapping, DojoD3], {
  12. mapping: {
  13. histogram: {
  14. display: "Histogram Data",
  15. fields: {
  16. value: "Value"
  17. }
  18. }
  19. },
  20. constructor: function (mappings, target) {
  21. if (mappings)
  22. this.setFieldMappings(mappings);
  23. if (target)
  24. this.renderTo(target);
  25. },
  26. renderTo: function (_target) {
  27. _target = lang.mixin({
  28. css: css,
  29. margin: { top: 0, right: 0, bottom: 20, left: 50 }
  30. }, _target);
  31. this.inherited(arguments);
  32. this.SvgG
  33. .style("fill", "none")
  34. ;
  35. this.x = d3.scale.linear()
  36. .domain([this.loVal ? this.loVal : 0, this.hiVal ? this.hiVal : 1])
  37. .range([0, this.target.width])
  38. ;
  39. this.y = d3.scale.linear()
  40. .domain([0, 100])
  41. .range([this.target.height - (this.target.margin.top + this.target.margin.bottom), 0])
  42. ;
  43. this.xAxis = d3.svg.axis()
  44. .scale(this.x)
  45. .orient("bottom")
  46. ;
  47. this.SvgG
  48. .attr("transform", "translate(" + this.target.margin.left + "," + this.target.margin.top + ")")
  49. ;
  50. this.SvgX = this.Svg.append("g")
  51. .attr("transform", "translate(" + this.target.margin.left + "," + (this.target.height - this.target.margin.bottom) + ")")
  52. .attr("class", "x axis")
  53. .call(this.xAxis)
  54. ;
  55. this.update([]);
  56. },
  57. display: function (_data) {
  58. if (_data)
  59. this.setData(_data);
  60. var mappedData = this.getMappedData();
  61. this.loVal = null;
  62. this.hiVal = null;
  63. var rawData = [];
  64. arrayUtil.forEach(mappedData, lang.hitch(this, function (item, idx) {
  65. if (this.loVal === null || this.loVal > item.value)
  66. this.loVal = item.value;
  67. if (this.hiVal === null || this.hiVal < item.value)
  68. this.hiVal = item.value;
  69. rawData.push(item.value);
  70. }));
  71. this.SvgG.selectAll(".bar").remove();
  72. this.x.domain([this.loVal, this.hiVal]);
  73. var data = d3.layout.histogram()
  74. .bins(this.x.ticks(20))
  75. (rawData)
  76. ;
  77. this.y.domain([0, d3.max(data, function (d) { return d.y; })]);
  78. this.Svg.selectAll("g.x.axis").transition()
  79. .call(this.xAxis)
  80. ;
  81. this.update(data);
  82. },
  83. update: function (data) {
  84. if (!data.length)
  85. return;
  86. this.SvgG.selectAll("svg").remove();
  87. var barX = this.SvgG.selectAll(".bar").data(data);
  88. var bar = barX.enter().append("g")
  89. .attr("class", "bar")
  90. .attr("transform", lang.hitch(this, function (d) { return "translate(" + this.x(d.x) + "," + this.y(d.y) + ")"; }))
  91. ;
  92. bar.append("rect")
  93. .attr("x", 1)
  94. .attr("width", this.x(this.loVal + data[0].dx) - 1)
  95. .attr("height", lang.hitch(this, function (d) { return this.target.height - (this.target.margin.top + this.target.margin.bottom) - this.y(d.y); }))
  96. ;
  97. var formatCount = d3.format(",.0f");
  98. bar.append("text")
  99. .attr("class", "barText")
  100. .attr("dy", ".75em")
  101. .attr("y", 6)
  102. .attr("x", this.x(data[0].dx) / 2)
  103. .attr("text-anchor", "middle")
  104. .text(lang.hitch(this, function (d) { return formatCount(d.y); }))
  105. ;
  106. }
  107. });
  108. });