1 nv.models.pieChart = function() {
3 //============================================================
4 // Public Variables with Default Settings
5 //------------------------------------------------------------
7 var pie = nv.models.pie()
8 , legend = nv.models.legend()
11 var margin = {top: 30, right: 20, bottom: 20, left: 20}
15 , color = nv.utils.defaultColor()
17 , tooltip = function(key, y, e, graph) {
18 return '<h3>' + key + '</h3>' +
23 , noData = "No Data Available."
24 , dispatch = d3.dispatch('tooltipShow', 'tooltipHide', 'stateChange', 'changeState')
27 //============================================================
30 //============================================================
32 //------------------------------------------------------------
34 var showTooltip = function(e, offsetElement) {
35 var tooltipLabel = pie.description()(e.point) || pie.x()(e.point)
36 var left = e.pos[0] + ( (offsetElement && offsetElement.offsetLeft) || 0 ),
37 top = e.pos[1] + ( (offsetElement && offsetElement.offsetTop) || 0),
38 y = pie.valueFormat()(pie.y()(e.point)),
39 content = tooltip(tooltipLabel, y, e, chart);
41 nv.tooltip.show([left, top], content, e.value < 0 ? 'n' : 's', null, offsetElement);
44 //============================================================
47 function chart(selection) {
48 selection.each(function(data) {
49 var container = d3.select(this),
52 var availableWidth = (width || parseInt(container.style('width')) || 960)
53 - margin.left - margin.right,
54 availableHeight = (height || parseInt(container.style('height')) || 400)
55 - margin.top - margin.bottom;
57 chart.update = function() { container.transition().call(chart); };
58 chart.container = this;
61 state.disabled = data.map(function(d) { return !!d.disabled });
67 if (state[key] instanceof Array)
68 defaultState[key] = state[key].slice(0);
70 defaultState[key] = state[key];
74 //------------------------------------------------------------
75 // Display No Data message if there's nothing to show.
77 if (!data || !data.length) {
78 var noDataText = container.selectAll('.nv-noData').data([noData]);
80 noDataText.enter().append('text')
81 .attr('class', 'nvd3 nv-noData')
83 .style('text-anchor', 'middle');
86 .attr('x', margin.left + availableWidth / 2)
87 .attr('y', margin.top + availableHeight / 2)
88 .text(function(d) { return d });
92 container.selectAll('.nv-noData').remove();
95 //------------------------------------------------------------
98 //------------------------------------------------------------
99 // Setup containers and skeleton of chart
101 var wrap = container.selectAll('g.nv-wrap.nv-pieChart').data([data]);
102 var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-pieChart').append('g');
103 var g = wrap.select('g');
105 gEnter.append('g').attr('class', 'nv-pieWrap');
106 gEnter.append('g').attr('class', 'nv-legendWrap');
108 //------------------------------------------------------------
111 //------------------------------------------------------------
116 .width( availableWidth )
119 wrap.select('.nv-legendWrap')
123 if ( margin.top != legend.height()) {
124 margin.top = legend.height();
125 availableHeight = (height || parseInt(container.style('height')) || 400)
126 - margin.top - margin.bottom;
129 wrap.select('.nv-legendWrap')
130 .attr('transform', 'translate(0,' + (-margin.top) +')');
133 //------------------------------------------------------------
136 wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
139 //------------------------------------------------------------
140 // Main Chart Component(s)
143 .width(availableWidth)
144 .height(availableHeight);
147 var pieWrap = g.select('.nv-pieWrap')
150 d3.transition(pieWrap).call(pie);
152 //------------------------------------------------------------
155 //============================================================
156 // Event Handling/Dispatching (in chart's scope)
157 //------------------------------------------------------------
159 /*legend.dispatch.on('stateChange', function(newState) {
161 dispatch.stateChange(state);
165 pie.dispatch.on('elementMouseout.tooltip', function(e) {
166 dispatch.tooltipHide(e);
169 // Update chart from a state object passed to event handler
170 dispatch.on('changeState', function(e) {
172 if (typeof e.disabled !== 'undefined') {
173 data.forEach(function(series,i) {
174 series.disabled = e.disabled[i];
177 state.disabled = e.disabled;
183 //============================================================
191 //============================================================
192 // Event Handling/Dispatching (out of chart's scope)
193 //------------------------------------------------------------
195 pie.dispatch.on('elementMouseover.tooltip', function(e) {
196 e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
197 dispatch.tooltipShow(e);
200 dispatch.on('tooltipShow', function(e) {
201 if (tooltips) showTooltip(e);
204 dispatch.on('tooltipHide', function() {
205 if (tooltips) nv.tooltip.cleanup();
208 //============================================================
211 //============================================================
212 // Expose Public Variables
213 //------------------------------------------------------------
215 // expose chart's sub-components
216 chart.legend = legend;
217 chart.dispatch = dispatch;
220 d3.rebind(chart, pie, 'valueFormat', 'values', 'x', 'y', 'description', 'id', 'showLabels', 'donutLabelsOutside', 'pieLabelsOutside', 'labelType', 'donut', 'donutRatio', 'labelThreshold');
221 chart.options = nv.utils.optionsFunc.bind(chart);
223 chart.margin = function(_) {
224 if (!arguments.length) return margin;
225 margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
226 margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
227 margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
228 margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
232 chart.width = function(_) {
233 if (!arguments.length) return width;
238 chart.height = function(_) {
239 if (!arguments.length) return height;
244 chart.color = function(_) {
245 if (!arguments.length) return color;
246 color = nv.utils.getColor(_);
252 chart.showLegend = function(_) {
253 if (!arguments.length) return showLegend;
258 chart.tooltips = function(_) {
259 if (!arguments.length) return tooltips;
264 chart.tooltipContent = function(_) {
265 if (!arguments.length) return tooltip;
270 chart.state = function(_) {
271 if (!arguments.length) return state;
276 chart.defaultState = function(_) {
277 if (!arguments.length) return defaultState;
282 chart.noData = function(_) {
283 if (!arguments.length) return noData;
288 //============================================================