nv.d3.growingDiscreteBar.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. //TODO: consider deprecating by adding necessary features to multiBar model
  17. nv.models.growingDiscreteBar = function() {
  18. "use strict";
  19. //============================================================
  20. // Public Variables with Default Settings
  21. //------------------------------------------------------------
  22. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  23. , width = 960
  24. , height = 500
  25. , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one
  26. , x = d3.scale.ordinal()
  27. , y = d3.scale.linear()
  28. , getX = function(d) { return d.x }
  29. , getY = function(d) { return d.y }
  30. , forceY = [0] // 0 is forced by default.. this makes sense for the majority of bar graphs... user can always do chart.forceY([]) to remove
  31. , color = nv.utils.defaultColor()
  32. , showValues = false
  33. , valueFormat = d3.format(',.2f')
  34. , xDomain
  35. , yDomain
  36. , xRange
  37. , yRange
  38. , dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout')
  39. , rectClass = 'discreteBar'
  40. , selectBars = null
  41. ;
  42. //============================================================
  43. //============================================================
  44. // Private Variables
  45. //------------------------------------------------------------
  46. var x0, y0;
  47. //============================================================
  48. function chart(selection) {
  49. selection.each(function(data) {
  50. var availableWidth = width - margin.left - margin.right,
  51. availableHeight = height - margin.top - margin.bottom,
  52. container = d3.select(this);
  53. //add series index to each data point for reference
  54. data.forEach(function(series, i) {
  55. series.values.forEach(function(point) {
  56. point.series = i;
  57. });
  58. });
  59. //------------------------------------------------------------
  60. // Setup Scales
  61. // remap and flatten the data for use in calculating the scales' domains
  62. var seriesData = (xDomain && yDomain) ? [] : // if we know xDomain and yDomain, no need to calculate
  63. data.map(function(d) {
  64. return d.values.map(function(d,i) {
  65. return { x: getX(d,i), y: getY(d,i), y0: d.y0 }
  66. })
  67. });
  68. x .domain(xDomain || d3.merge(seriesData).map(function(d) { return d.x }))
  69. .rangeBands(xRange || [0, availableWidth], .1);
  70. y .domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) { return d.y }).concat(forceY)));
  71. // If showValues, pad the Y axis range to account for label height
  72. if (showValues) y.range(yRange || [availableHeight - (y.domain()[0] < 0 ? 12 : 0), y.domain()[1] > 0 ? 12 : 0]);
  73. else y.range(yRange || [availableHeight, 0]);
  74. //store old scales if they exist
  75. x0 = x0 || x;
  76. y0 = y0 || y.copy().range([y(0),y(0)]);
  77. //------------------------------------------------------------
  78. //------------------------------------------------------------
  79. // Setup containers and skeleton of chart
  80. var wrap = container.selectAll('g.nv-wrap.nv-discretebar').data([data]);
  81. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-discretebar');
  82. var gEnter = wrapEnter.append('g');
  83. var g = wrap.select('g');
  84. gEnter.append('g').attr('class', 'nv-groups');
  85. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  86. //------------------------------------------------------------
  87. //TODO: by definition, the discrete bar should not have multiple groups, will modify/remove later
  88. var groups = wrap.select('.nv-groups').selectAll('.nv-group')
  89. .data(function(d) { return d }, function(d) { return d.key });
  90. groups.enter().append('g')
  91. .style('stroke-opacity', 1e-6)
  92. .style('fill-opacity', 1e-6);
  93. groups.exit()
  94. .transition()
  95. .style('stroke-opacity', 1e-6)
  96. .style('fill-opacity', 1e-6)
  97. .remove();
  98. groups
  99. .attr('class', function(d,i) { return 'nv-group nv-series-' + i })
  100. .classed('hover', function(d) { return d.hover });
  101. groups
  102. .transition()
  103. .style('stroke-opacity', 1)
  104. .style('fill-opacity', .75);
  105. var bars = groups.selectAll('g.nv-bar')
  106. .data(function(d) { return d.values });
  107. bars.exit().remove();
  108. selectBars = function(selected) {
  109. $(selected).each(function(cnt, item){
  110. bars.each(function(d, i) {
  111. if (d.x == item) {
  112. d3.select(this).classed('selected', true);
  113. }
  114. });
  115. });
  116. };
  117. var barsEnter = bars.enter().append('g')
  118. .attr('transform', function(d,i,j) {
  119. return 'translate(' + (x(getX(d,i)) + x.rangeBand() * .05 ) + ', ' + y(0) + ')'
  120. })
  121. .on('mouseover', function(d,i) { //TODO: figure out why j works above, but not here
  122. d3.select(this).classed('hover', true);
  123. dispatch.elementMouseover({
  124. value: getY(d,i),
  125. point: d,
  126. series: data[d.series],
  127. pos: [x(getX(d,i)) + (x.rangeBand() * (d.series + .5) / data.length), y(getY(d,i))], // TODO: Figure out why the value appears to be shifted
  128. pointIndex: i,
  129. seriesIndex: d.series,
  130. e: d3.event
  131. });
  132. })
  133. .on('mouseout', function(d,i) {
  134. d3.select(this).classed('hover', false);
  135. dispatch.elementMouseout({
  136. value: getY(d,i),
  137. point: d,
  138. series: data[d.series],
  139. pointIndex: i,
  140. seriesIndex: d.series,
  141. e: d3.event
  142. });
  143. })
  144. .on('click', function(d,i) {
  145. dispatch.elementClick({
  146. value: getY(d,i),
  147. point: d,
  148. series: data[d.series],
  149. pos: [x(getX(d,i)) + (x.rangeBand() * (d.series + .5) / data.length), y(getY(d,i))], // TODO: Figure out why the value appears to be shifted
  150. pointIndex: i,
  151. seriesIndex: d.series,
  152. e: d3.event
  153. });
  154. d3.event.stopPropagation();
  155. })
  156. .on('dblclick', function(d,i) {
  157. dispatch.elementDblClick({
  158. value: getY(d,i),
  159. point: d,
  160. series: data[d.series],
  161. pos: [x(getX(d,i)) + (x.rangeBand() * (d.series + .5) / data.length), y(getY(d,i))], // TODO: Figure out why the value appears to be shifted
  162. pointIndex: i,
  163. seriesIndex: d.series,
  164. e: d3.event
  165. });
  166. d3.event.stopPropagation();
  167. });
  168. barsEnter.append('rect')
  169. .attr('height', 0)
  170. .attr('width', x.rangeBand() * .9 / data.length )
  171. if (showValues) {
  172. barsEnter.append('text')
  173. .attr('text-anchor', 'middle')
  174. ;
  175. bars.select('text')
  176. .text(function(d,i) { return valueFormat(getY(d,i)) })
  177. .transition()
  178. .attr('x', x.rangeBand() * .9 / 2)
  179. .attr('y', function(d,i) { return getY(d,i) < 0 ? y(getY(d,i)) - y(0) + 12 : -4 })
  180. ;
  181. } else {
  182. bars.selectAll('text').remove();
  183. }
  184. bars
  185. .attr('class', function(d,i) { return getY(d,i) < 0 ? 'nv-bar negative' : 'nv-bar positive' })
  186. .style('fill', function(d,i) { return d.color || color(d,i) })
  187. .style('stroke', function(d,i) { return d.color || color(d,i) })
  188. .select('rect')
  189. .attr('class', rectClass)
  190. .transition()
  191. .attr('width', x.rangeBand() * .9 / data.length);
  192. bars.transition()
  193. //.delay(function(d,i) { return i * 1200 / data[0].values.length })
  194. .attr('transform', function(d,i) {
  195. var left = x(getX(d,i)) + x.rangeBand() * .05,
  196. top = getY(d,i) < 0 ?
  197. y(0) :
  198. y(0) - y(getY(d,i)) < 1 ?
  199. y(0) - 1 : //make 1 px positive bars show up above y=0
  200. y(getY(d,i));
  201. return 'translate(' + left + ', ' + top + ')'
  202. })
  203. .select('rect')
  204. .attr('height', function(d,i) {
  205. return Math.max(Math.abs(y(getY(d,i)) - y((yDomain && yDomain[0]) || 0)) || 1)
  206. });
  207. //store old scales for use in transitions on update
  208. x0 = x.copy();
  209. y0 = y.copy();
  210. });
  211. return chart;
  212. }
  213. //============================================================
  214. // Expose Public Variables
  215. //------------------------------------------------------------
  216. chart.dispatch = dispatch;
  217. chart.options = nv.utils.optionsFunc.bind(chart);
  218. chart.x = function(_) {
  219. if (!arguments.length) return getX;
  220. getX = _;
  221. return chart;
  222. };
  223. chart.y = function(_) {
  224. if (!arguments.length) return getY;
  225. getY = _;
  226. return chart;
  227. };
  228. chart.margin = function(_) {
  229. if (!arguments.length) return margin;
  230. margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
  231. margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
  232. margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
  233. margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
  234. return chart;
  235. };
  236. chart.width = function(_) {
  237. if (!arguments.length) return width;
  238. width = _;
  239. return chart;
  240. };
  241. chart.height = function(_) {
  242. if (!arguments.length) return height;
  243. height = _;
  244. return chart;
  245. };
  246. chart.xScale = function(_) {
  247. if (!arguments.length) return x;
  248. x = _;
  249. return chart;
  250. };
  251. chart.yScale = function(_) {
  252. if (!arguments.length) return y;
  253. y = _;
  254. return chart;
  255. };
  256. chart.xDomain = function(_) {
  257. if (!arguments.length) return xDomain;
  258. xDomain = _;
  259. return chart;
  260. };
  261. chart.yDomain = function(_) {
  262. if (!arguments.length) return yDomain;
  263. yDomain = _;
  264. return chart;
  265. };
  266. chart.xRange = function(_) {
  267. if (!arguments.length) return xRange;
  268. xRange = _;
  269. return chart;
  270. };
  271. chart.yRange = function(_) {
  272. if (!arguments.length) return yRange;
  273. yRange = _;
  274. return chart;
  275. };
  276. chart.forceY = function(_) {
  277. if (!arguments.length) return forceY;
  278. forceY = _;
  279. return chart;
  280. };
  281. chart.color = function(_) {
  282. if (!arguments.length) return color;
  283. color = nv.utils.getColor(_);
  284. return chart;
  285. };
  286. chart.id = function(_) {
  287. if (!arguments.length) return id;
  288. id = _;
  289. return chart;
  290. };
  291. chart.showValues = function(_) {
  292. if (!arguments.length) return showValues;
  293. showValues = _;
  294. return chart;
  295. };
  296. chart.valueFormat= function(_) {
  297. if (!arguments.length) return valueFormat;
  298. valueFormat = _;
  299. return chart;
  300. };
  301. chart.rectClass= function(_) {
  302. if (!arguments.length) return rectClass;
  303. rectClass = _;
  304. return chart;
  305. };
  306. chart.selectBars = function(args) {
  307. if (!arguments.length) return selectBars;
  308. selectBars(args);
  309. return chart;
  310. };
  311. //============================================================
  312. return chart;
  313. }