nv.d3.growingDiscreteBarChart.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. nv.models.growingDiscreteBarChart = function() {
  17. "use strict";
  18. //============================================================
  19. // Public Variables with Default Settings
  20. //------------------------------------------------------------
  21. var discretebar = nv.models.growingDiscreteBar()
  22. , xAxis = nv.models.axis()
  23. , yAxis = nv.models.axis()
  24. ;
  25. var margin = {top: 15, right: 10, bottom: 50, left: 60}
  26. , width = null
  27. , height = null
  28. , color = nv.utils.getColor()
  29. , showXAxis = true
  30. , showYAxis = true
  31. , rightAlignYAxis = false
  32. , staggerLabels = false
  33. , tooltips = true
  34. , tooltip = function(key, x, y, e, graph) {
  35. return '<h3>' + x + '</h3>' +
  36. '<p>' + y + '</p>'
  37. }
  38. , x
  39. , y
  40. , noData = "No Data Available."
  41. , dispatch = d3.dispatch('tooltipShow', 'tooltipHide', 'beforeUpdate')
  42. , transitionDuration = 250
  43. , selectBars = null
  44. ;
  45. xAxis
  46. .orient('bottom')
  47. .highlightZero(false)
  48. .showMaxMin(false)
  49. .tickFormat(function(d) { return d })
  50. ;
  51. yAxis
  52. .orient((rightAlignYAxis) ? 'right' : 'left')
  53. .tickFormat(d3.format(',.1f'))
  54. ;
  55. //============================================================
  56. //============================================================
  57. // Private Variables
  58. //------------------------------------------------------------
  59. var showTooltip = function(e, offsetElement) {
  60. var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ),
  61. top = e.pos[1] + ( offsetElement.offsetTop || 0),
  62. x = xAxis.tickFormat()(discretebar.x()(e.point, e.pointIndex)),
  63. y = yAxis.tickFormat()(discretebar.y()(e.point, e.pointIndex)),
  64. content = tooltip(e.series.key, x, y, e, chart);
  65. nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
  66. };
  67. //============================================================
  68. function chart(selection) {
  69. selection.each(function(data) {
  70. var container = d3.select(this),
  71. that = this;
  72. var availableWidth = (width || parseInt(container.style('width')) || 960)
  73. - margin.left - margin.right,
  74. availableHeight = (height || parseInt(container.style('height')) || 400)
  75. - margin.top - margin.bottom;
  76. chart.update = function() {
  77. dispatch.beforeUpdate();
  78. container.transition().duration(transitionDuration).call(chart);
  79. };
  80. chart.container = this;
  81. //------------------------------------------------------------
  82. // Display No Data message if there's nothing to show.
  83. if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
  84. var noDataText = container.selectAll('.nv-noData').data([noData]);
  85. noDataText.enter().append('text')
  86. .attr('class', 'nvd3 nv-noData')
  87. .attr('dy', '-.7em')
  88. .style('text-anchor', 'middle');
  89. noDataText
  90. .attr('x', margin.left + availableWidth / 2)
  91. .attr('y', margin.top + availableHeight / 2)
  92. .text(function(d) { return d });
  93. return chart;
  94. } else {
  95. container.selectAll('.nv-noData').remove();
  96. }
  97. //------------------------------------------------------------
  98. //------------------------------------------------------------
  99. // Setup Scales
  100. x = discretebar.xScale();
  101. y = discretebar.yScale().clamp(true);
  102. //------------------------------------------------------------
  103. //------------------------------------------------------------
  104. // Setup containers and skeleton of chart
  105. var wrap = container.selectAll('g.nv-wrap.nv-discreteBarWithAxes').data([data]);
  106. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-discreteBarWithAxes').append('g');
  107. var defsEnter = gEnter.append('defs');
  108. var g = wrap.select('g');
  109. gEnter.append('g').attr('class', 'nv-x nv-axis');
  110. gEnter.append('g').attr('class', 'nv-y nv-axis')
  111. .append('g').attr('class', 'nv-zeroLine')
  112. .append('line');
  113. gEnter.append('g').attr('class', 'nv-barsWrap');
  114. g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  115. if (rightAlignYAxis) {
  116. g.select(".nv-y.nv-axis")
  117. .attr("transform", "translate(" + availableWidth + ",0)");
  118. }
  119. //------------------------------------------------------------
  120. //------------------------------------------------------------
  121. // Main Chart Component(s)
  122. discretebar
  123. .width(availableWidth)
  124. .height(availableHeight);
  125. selectBars = discretebar.selectBars;
  126. var barsWrap = g.select('.nv-barsWrap')
  127. .datum(data.filter(function(d) { return !d.disabled }))
  128. barsWrap.transition().call(discretebar);
  129. //------------------------------------------------------------
  130. defsEnter.append('clipPath')
  131. .attr('id', 'nv-x-label-clip-' + discretebar.id())
  132. .append('rect');
  133. g.select('#nv-x-label-clip-' + discretebar.id() + ' rect')
  134. .attr('width', x.rangeBand() * (staggerLabels ? 2 : 1))
  135. .attr('height', 16)
  136. .attr('x', -x.rangeBand() / (staggerLabels ? 1 : 2 ));
  137. //------------------------------------------------------------
  138. // Setup Axes
  139. if (showXAxis) {
  140. xAxis
  141. .scale(x)
  142. .ticks( availableWidth / 100 )
  143. .tickSize(-availableHeight, 0);
  144. g.select('.nv-x.nv-axis')
  145. .attr('transform', 'translate(0,' + (y.range()[0] + ((discretebar.showValues() && y.domain()[0] < 0) ? 16 : 0)) + ')');
  146. //d3.transition(g.select('.nv-x.nv-axis'))
  147. g.select('.nv-x.nv-axis').transition()
  148. .call(xAxis);
  149. var xTicks = g.select('.nv-x.nv-axis').selectAll('g');
  150. if (staggerLabels) {
  151. xTicks
  152. .selectAll('text')
  153. .attr('transform', function(d,i,j) { return 'translate(0,' + (j % 2 == 0 ? '5' : '17') + ')' })
  154. }
  155. }
  156. if (showYAxis) {
  157. yAxis
  158. .scale(y)
  159. .ticks( availableHeight / 36 )
  160. .tickSize( -availableWidth, 0);
  161. g.select('.nv-y.nv-axis').transition()
  162. .call(yAxis);
  163. }
  164. // Zero line
  165. g.select(".nv-zeroLine line")
  166. .attr("x1",0)
  167. .attr("x2",availableWidth)
  168. .attr("y1", y(0))
  169. .attr("y2", y(0))
  170. ;
  171. //------------------------------------------------------------
  172. //============================================================
  173. // Event Handling/Dispatching (in chart's scope)
  174. //------------------------------------------------------------
  175. dispatch.on('tooltipShow', function(e) {
  176. if (tooltips) showTooltip(e, that.parentNode);
  177. });
  178. //============================================================
  179. });
  180. return chart;
  181. }
  182. //============================================================
  183. // Event Handling/Dispatching (out of chart's scope)
  184. //------------------------------------------------------------
  185. discretebar.dispatch.on('elementMouseover.tooltip', function(e) {
  186. e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
  187. dispatch.tooltipShow(e);
  188. });
  189. discretebar.dispatch.on('elementMouseout.tooltip', function(e) {
  190. dispatch.tooltipHide(e);
  191. });
  192. dispatch.on('tooltipHide', function() {
  193. if (tooltips) nv.tooltip.cleanup();
  194. });
  195. //============================================================
  196. //============================================================
  197. // Expose Public Variables
  198. //------------------------------------------------------------
  199. // expose chart's sub-components
  200. chart.dispatch = dispatch;
  201. chart.discretebar = discretebar;
  202. chart.xAxis = xAxis;
  203. chart.yAxis = yAxis;
  204. d3.rebind(chart, discretebar, 'x', 'y', 'xDomain', 'yDomain', 'xRange', 'yRange', 'forceX', 'forceY', 'id', 'showValues', 'valueFormat');
  205. chart.options = nv.utils.optionsFunc.bind(chart);
  206. chart.margin = function(_) {
  207. if (!arguments.length) return margin;
  208. margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
  209. margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
  210. margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
  211. margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
  212. return chart;
  213. };
  214. chart.width = function(_) {
  215. if (!arguments.length) return width;
  216. width = _;
  217. return chart;
  218. };
  219. chart.height = function(_) {
  220. if (!arguments.length) return height;
  221. height = _;
  222. return chart;
  223. };
  224. chart.color = function(_) {
  225. if (!arguments.length) return color;
  226. color = nv.utils.getColor(_);
  227. discretebar.color(color);
  228. return chart;
  229. };
  230. chart.showXAxis = function(_) {
  231. if (!arguments.length) return showXAxis;
  232. showXAxis = _;
  233. return chart;
  234. };
  235. chart.showYAxis = function(_) {
  236. if (!arguments.length) return showYAxis;
  237. showYAxis = _;
  238. return chart;
  239. };
  240. chart.rightAlignYAxis = function(_) {
  241. if(!arguments.length) return rightAlignYAxis;
  242. rightAlignYAxis = _;
  243. yAxis.orient( (_) ? 'right' : 'left');
  244. return chart;
  245. };
  246. chart.staggerLabels = function(_) {
  247. if (!arguments.length) return staggerLabels;
  248. staggerLabels = _;
  249. return chart;
  250. };
  251. chart.tooltips = function(_) {
  252. if (!arguments.length) return tooltips;
  253. tooltips = _;
  254. return chart;
  255. };
  256. chart.tooltipContent = function(_) {
  257. if (!arguments.length) return tooltip;
  258. tooltip = _;
  259. return chart;
  260. };
  261. chart.noData = function(_) {
  262. if (!arguments.length) return noData;
  263. noData = _;
  264. return chart;
  265. };
  266. chart.transitionDuration = function(_) {
  267. if (!arguments.length) return transitionDuration;
  268. transitionDuration = _;
  269. return chart;
  270. };
  271. chart.selectBars = function(args) {
  272. if (!arguments.length) return selectBars;
  273. selectBars(args);
  274. return chart;
  275. };
  276. //============================================================
  277. return chart;
  278. }