nv.d3.growingPieChart.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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.growingPieChart = function() {
  17. "use strict";
  18. //============================================================
  19. // Public Variables with Default Settings
  20. //------------------------------------------------------------
  21. var pie = nv.models.growingPie()
  22. , legend = nv.models.legend()
  23. ;
  24. var margin = {top: 30, right: 20, bottom: 20, left: 20}
  25. , width = null
  26. , height = null
  27. , showLegend = true
  28. , color = nv.utils.defaultColor()
  29. , tooltips = true
  30. , tooltip = function(key, y, e, graph) {
  31. return '<h3>' + key + '</h3>' +
  32. '<p>' + y + '</p>'
  33. }
  34. , state = {}
  35. , defaultState = null
  36. , noData = "No Data Available."
  37. , selectSlices = null
  38. , dispatch = d3.dispatch('tooltipShow', 'tooltipHide', 'stateChange', 'changeState')
  39. ;
  40. //============================================================
  41. //============================================================
  42. // Private Variables
  43. //------------------------------------------------------------
  44. var showTooltip = function(e, offsetElement) {
  45. var tooltipLabel = pie.description()(e.point) || pie.x()(e.point)
  46. var left = e.pos[0] + ( (offsetElement && offsetElement.offsetLeft) || 0 ),
  47. top = e.pos[1] + ( (offsetElement && offsetElement.offsetTop) || 0),
  48. y = pie.valueFormat()(pie.y()(e.point)),
  49. content = tooltip(tooltipLabel, y, e, chart);
  50. nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
  51. };
  52. //============================================================
  53. function chart(selection) {
  54. selection.each(function(data) {
  55. var container = d3.select(this),
  56. that = this;
  57. var availableWidth = (width || parseInt(container.style('width')) || 960)
  58. - margin.left - margin.right,
  59. availableHeight = (height || parseInt(container.style('height')) || 400)
  60. - margin.top - margin.bottom;
  61. chart.update = function() { container.transition().call(chart); };
  62. chart.container = this;
  63. //set state.disabled
  64. state.disabled = data.map(function(d) { return !!d.disabled });
  65. if (!defaultState) {
  66. var key;
  67. defaultState = {};
  68. for (key in state) {
  69. if (state[key] instanceof Array)
  70. defaultState[key] = state[key].slice(0);
  71. else
  72. defaultState[key] = state[key];
  73. }
  74. }
  75. //------------------------------------------------------------
  76. // Display No Data message if there's nothing to show.
  77. if (!data || !data.length) {
  78. var noDataText = container.selectAll('.nv-noData').data([noData]);
  79. noDataText.enter().append('text')
  80. .attr('class', 'nvd3 nv-noData')
  81. .attr('dy', '-.7em')
  82. .style('text-anchor', 'middle');
  83. noDataText
  84. .attr('x', margin.left + availableWidth / 2)
  85. .attr('y', margin.top + availableHeight / 2)
  86. .text(function(d) { return d });
  87. return chart;
  88. } else {
  89. container.selectAll('.nv-noData').remove();
  90. }
  91. //------------------------------------------------------------
  92. //------------------------------------------------------------
  93. // Setup containers and skeleton of chart
  94. var wrap = container.selectAll('g.nv-wrap.nv-pieChart').data([data]);
  95. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-pieChart').append('g');
  96. var g = wrap.select('g');
  97. gEnter.append('g').attr('class', 'nv-pieWrap');
  98. gEnter.append('g').attr('class', 'nv-legendWrap');
  99. //------------------------------------------------------------
  100. //------------------------------------------------------------
  101. // Legend
  102. if (showLegend) {
  103. legend
  104. .width( availableWidth )
  105. .key(pie.x());
  106. wrap.select('.nv-legendWrap')
  107. .datum(data)
  108. .call(legend);
  109. if ( margin.top != legend.height()) {
  110. margin.top = legend.height();
  111. availableHeight = (height || parseInt(container.style('height')) || 400)
  112. - margin.top - margin.bottom;
  113. }
  114. wrap.select('.nv-legendWrap')
  115. .attr('transform', 'translate(0,' + (-margin.top) +')');
  116. }
  117. //------------------------------------------------------------
  118. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  119. //------------------------------------------------------------
  120. // Main Chart Component(s)
  121. pie
  122. .width(availableWidth)
  123. .height(availableHeight);
  124. var pieWrap = g.select('.nv-pieWrap')
  125. .datum([data]);
  126. selectSlices = pie.selectSlices;
  127. d3.transition(pieWrap).call(pie);
  128. //------------------------------------------------------------
  129. //============================================================
  130. // Event Handling/Dispatching (in chart's scope)
  131. //------------------------------------------------------------
  132. legend.dispatch.on('stateChange', function(newState) {
  133. state = newState;
  134. dispatch.stateChange(state);
  135. chart.update();
  136. });
  137. pie.dispatch.on('elementMouseout.tooltip', function(e) {
  138. dispatch.tooltipHide(e);
  139. });
  140. // Update chart from a state object passed to event handler
  141. dispatch.on('changeState', function(e) {
  142. if (typeof e.disabled !== 'undefined') {
  143. data.forEach(function(series,i) {
  144. series.disabled = e.disabled[i];
  145. });
  146. state.disabled = e.disabled;
  147. }
  148. chart.update();
  149. });
  150. //============================================================
  151. });
  152. return chart;
  153. }
  154. //============================================================
  155. // Event Handling/Dispatching (out of chart's scope)
  156. //------------------------------------------------------------
  157. pie.dispatch.on('elementMouseover.tooltip', function(e) {
  158. e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
  159. dispatch.tooltipShow(e);
  160. });
  161. dispatch.on('tooltipShow', function(e) {
  162. if (tooltips) showTooltip(e);
  163. });
  164. dispatch.on('tooltipHide', function() {
  165. if (tooltips) nv.tooltip.cleanup();
  166. });
  167. //============================================================
  168. //============================================================
  169. // Expose Public Variables
  170. //------------------------------------------------------------
  171. // expose chart's sub-components
  172. chart.legend = legend;
  173. chart.dispatch = dispatch;
  174. chart.pie = pie;
  175. d3.rebind(chart, pie, 'valueFormat', 'values', 'x', 'y', 'description', 'id', 'showLabels', 'donutLabelsOutside', 'pieLabelsOutside', 'labelType', 'donut', 'donutRatio', 'labelThreshold');
  176. chart.options = nv.utils.optionsFunc.bind(chart);
  177. chart.margin = function(_) {
  178. if (!arguments.length) return margin;
  179. margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
  180. margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
  181. margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
  182. margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
  183. return chart;
  184. };
  185. chart.width = function(_) {
  186. if (!arguments.length) return width;
  187. width = _;
  188. return chart;
  189. };
  190. chart.height = function(_) {
  191. if (!arguments.length) return height;
  192. height = _;
  193. return chart;
  194. };
  195. chart.color = function(_) {
  196. if (!arguments.length) return color;
  197. color = nv.utils.getColor(_);
  198. legend.color(color);
  199. pie.color(color);
  200. return chart;
  201. };
  202. chart.showLegend = function(_) {
  203. if (!arguments.length) return showLegend;
  204. showLegend = _;
  205. return chart;
  206. };
  207. chart.tooltips = function(_) {
  208. if (!arguments.length) return tooltips;
  209. tooltips = _;
  210. return chart;
  211. };
  212. chart.tooltipContent = function(_) {
  213. if (!arguments.length) return tooltip;
  214. tooltip = _;
  215. return chart;
  216. };
  217. chart.state = function(_) {
  218. if (!arguments.length) return state;
  219. state = _;
  220. return chart;
  221. };
  222. chart.defaultState = function(_) {
  223. if (!arguments.length) return defaultState;
  224. defaultState = _;
  225. return chart;
  226. };
  227. chart.noData = function(_) {
  228. if (!arguments.length) return noData;
  229. noData = _;
  230. return chart;
  231. };
  232. chart.selectSlices = function(args) {
  233. if (!arguments.length) return selectSlices;
  234. selectSlices(args);
  235. return chart;
  236. };
  237. //============================================================
  238. return chart;
  239. }