2 // Chart design based on the recommendations of Stephen Few. Implementation
3 // based on the work of Clint Ivy, Jamie Love, and Jason Davies.
4 // http://projects.instantcognition.com/protovis/bulletchart/
5 nv.models.bulletChart = function() {
7 //============================================================
8 // Public Variables with Default Settings
9 //------------------------------------------------------------
11 var bullet = nv.models.bullet()
14 var orient = 'left' // TODO top & bottom
16 , margin = {top: 5, right: 40, bottom: 20, left: 120}
17 , ranges = function(d) { return d.ranges }
18 , markers = function(d) { return d.markers }
19 , measures = function(d) { return d.measures }
24 , tooltip = function(key, x, y, e, graph) {
25 return '<h3>' + x + '</h3>' +
28 , noData = 'No Data Available.'
29 , dispatch = d3.dispatch('tooltipShow', 'tooltipHide')
32 //============================================================
35 //============================================================
37 //------------------------------------------------------------
39 var showTooltip = function(e, offsetElement) {
40 var left = e.pos[0] + ( offsetElement.offsetLeft || 0 ) + margin.left,
41 top = e.pos[1] + ( offsetElement.offsetTop || 0) + margin.top,
42 content = tooltip(e.key, e.label, e.value, e, chart);
44 nv.tooltip.show([left, top], content, e.value < 0 ? 'e' : 'w', null, offsetElement);
47 //============================================================
50 function chart(selection) {
51 selection.each(function(d, i) {
52 var container = d3.select(this);
54 var availableWidth = (width || parseInt(container.style('width')) || 960)
55 - margin.left - margin.right,
56 availableHeight = height - margin.top - margin.bottom,
60 chart.update = function() { chart(selection) };
61 chart.container = this;
63 //------------------------------------------------------------
64 // Display No Data message if there's nothing to show.
66 if (!d || !ranges.call(this, d, i)) {
67 var noDataText = container.selectAll('.nv-noData').data([noData]);
69 noDataText.enter().append('text')
70 .attr('class', 'nvd3 nv-noData')
72 .style('text-anchor', 'middle');
75 .attr('x', margin.left + availableWidth / 2)
76 .attr('y', 18 + margin.top + availableHeight / 2)
77 .text(function(d) { return d });
81 container.selectAll('.nv-noData').remove();
84 //------------------------------------------------------------
88 var rangez = ranges.call(this, d, i).slice().sort(d3.descending),
89 markerz = markers.call(this, d, i).slice().sort(d3.descending),
90 measurez = measures.call(this, d, i).slice().sort(d3.descending);
93 //------------------------------------------------------------
94 // Setup containers and skeleton of chart
96 var wrap = container.selectAll('g.nv-wrap.nv-bulletChart').data([d]);
97 var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-bulletChart');
98 var gEnter = wrapEnter.append('g');
99 var g = wrap.select('g');
101 gEnter.append('g').attr('class', 'nv-bulletWrap');
102 gEnter.append('g').attr('class', 'nv-titles');
104 wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
106 //------------------------------------------------------------
109 // Compute the new x-scale.
110 var x1 = d3.scale.linear()
111 .domain([0, Math.max(rangez[0], markerz[0], measurez[0])]) // TODO: need to allow forceX and forceY, and xDomain, yDomain
112 .range(reverse ? [availableWidth, 0] : [0, availableWidth]);
114 // Retrieve the old x-scale, if this is an update.
115 var x0 = this.__chart__ || d3.scale.linear()
116 .domain([0, Infinity])
119 // Stash the new scale.
123 // Derive width-scales from the x-scales.
124 var w0 = bulletWidth(x0),
125 w1 = bulletWidth(x1);
127 function bulletWidth(x) {
130 return Math.abs(x(d) - x(0));
134 function bulletTranslate(x) {
136 return 'translate(' + x(d) + ',0)';
141 var w0 = function(d) { return Math.abs(x0(d) - x0(0)) }, // TODO: could optimize by precalculating x0(0) and x1(0)
142 w1 = function(d) { return Math.abs(x1(d) - x1(0)) };
145 var title = gEnter.select('.nv-titles').append('g')
146 .attr('text-anchor', 'end')
147 .attr('transform', 'translate(-6,' + (height - margin.top - margin.bottom) / 2 + ')');
149 .attr('class', 'nv-title')
150 .text(function(d) { return d.title; });
153 .attr('class', 'nv-subtitle')
155 .text(function(d) { return d.subtitle; });
160 .width(availableWidth)
161 .height(availableHeight)
163 var bulletWrap = g.select('.nv-bulletWrap');
165 d3.transition(bulletWrap).call(bullet);
169 // Compute the tick format.
170 var format = tickFormat || x1.tickFormat( availableWidth / 100 );
172 // Update the tick groups.
173 var tick = g.selectAll('g.nv-tick')
174 .data(x1.ticks( availableWidth / 50 ), function(d) {
175 return this.textContent || format(d);
178 // Initialize the ticks with the old scale, x0.
179 var tickEnter = tick.enter().append('g')
180 .attr('class', 'nv-tick')
181 .attr('transform', function(d) { return 'translate(' + x0(d) + ',0)' })
182 .style('opacity', 1e-6);
184 tickEnter.append('line')
185 .attr('y1', availableHeight)
186 .attr('y2', availableHeight * 7 / 6);
188 tickEnter.append('text')
189 .attr('text-anchor', 'middle')
191 .attr('y', availableHeight * 7 / 6)
195 // Transition the updating ticks to the new scale, x1.
196 var tickUpdate = d3.transition(tick)
197 .attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
198 .style('opacity', 1);
200 tickUpdate.select('line')
201 .attr('y1', availableHeight)
202 .attr('y2', availableHeight * 7 / 6);
204 tickUpdate.select('text')
205 .attr('y', availableHeight * 7 / 6);
207 // Transition the exiting ticks to the new scale, x1.
208 d3.transition(tick.exit())
209 .attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
210 .style('opacity', 1e-6)
214 //============================================================
215 // Event Handling/Dispatching (in chart's scope)
216 //------------------------------------------------------------
218 dispatch.on('tooltipShow', function(e) {
220 if (tooltips) showTooltip(e, that.parentNode);
223 //============================================================
233 //============================================================
234 // Event Handling/Dispatching (out of chart's scope)
235 //------------------------------------------------------------
237 bullet.dispatch.on('elementMouseover.tooltip', function(e) {
238 dispatch.tooltipShow(e);
241 bullet.dispatch.on('elementMouseout.tooltip', function(e) {
242 dispatch.tooltipHide(e);
245 dispatch.on('tooltipHide', function() {
246 if (tooltips) nv.tooltip.cleanup();
249 //============================================================
252 //============================================================
253 // Expose Public Variables
254 //------------------------------------------------------------
256 chart.dispatch = dispatch;
257 chart.bullet = bullet;
259 d3.rebind(chart, bullet, 'color');
261 chart.options = nv.utils.optionsFunc.bind(chart);
263 // left, right, top, bottom
264 chart.orient = function(x) {
265 if (!arguments.length) return orient;
267 reverse = orient == 'right' || orient == 'bottom';
271 // ranges (bad, satisfactory, good)
272 chart.ranges = function(x) {
273 if (!arguments.length) return ranges;
278 // markers (previous, goal)
279 chart.markers = function(x) {
280 if (!arguments.length) return markers;
285 // measures (actual, forecast)
286 chart.measures = function(x) {
287 if (!arguments.length) return measures;
292 chart.width = function(x) {
293 if (!arguments.length) return width;
298 chart.height = function(x) {
299 if (!arguments.length) return height;
304 chart.margin = function(_) {
305 if (!arguments.length) return margin;
306 margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
307 margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
308 margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
309 margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
313 chart.tickFormat = function(x) {
314 if (!arguments.length) return tickFormat;
319 chart.tooltips = function(_) {
320 if (!arguments.length) return tooltips;
325 chart.tooltipContent = function(_) {
326 if (!arguments.length) return tooltip;
331 chart.noData = function(_) {
332 if (!arguments.length) return noData;
337 //============================================================