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>' + e.label + '</h3>' +
26 '<p>' + e.value + '</p>'
28 , noData = "No Data Available."
29 , dispatch = d3.dispatch('tooltipShow', 'tooltipHide')
32 //============================================================
35 //============================================================
37 //------------------------------------------------------------
39 var showTooltip = function(e, parentElement) {
40 var offsetElement = parentElement.parentNode.parentNode,
41 left = e.pos[0] + offsetElement.offsetLeft + margin.left,
42 top = e.pos[1] + offsetElement.offsetTop + margin.top;
44 var content = '<h3>' + e.label + '</h3>' +
45 '<p>' + e.value + '</p>';
47 nv.tooltip.show([left, top], content, e.value < 0 ? 'e' : 'w', null, offsetElement.parentNode);
50 //============================================================
53 function chart(selection) {
54 selection.each(function(d, i) {
55 var container = d3.select(this);
57 var availableWidth = (width || parseInt(container.style('width')) || 960)
58 - margin.left - margin.right,
59 availableHeight = height - margin.top - margin.bottom,
63 chart.update = function() { chart(selection) };
64 chart.container = this;
66 //------------------------------------------------------------
67 // Display No Data message if there's nothing to show.
70 // Disabled until I figure out a better way to check for no data with the bullet chart
71 if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
72 var noDataText = container.selectAll('.nv-noData').data([noData]);
74 noDataText.enter().append('text')
75 .attr('class', 'nvd3 nv-noData')
77 .style('text-anchor', 'middle');
80 .attr('x', margin.left + availableWidth / 2)
81 .attr('y', margin.top + availableHeight / 2)
82 .text(function(d) { return d });
86 container.selectAll('.nv-noData').remove();
90 //------------------------------------------------------------
94 var rangez = ranges.call(this, d, i).slice().sort(d3.descending),
95 markerz = markers.call(this, d, i).slice().sort(d3.descending),
96 measurez = measures.call(this, d, i).slice().sort(d3.descending);
99 //------------------------------------------------------------
100 // Setup containers and skeleton of chart
102 var wrap = container.selectAll('g.nv-wrap.nv-bulletChart').data([d]);
103 var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-bulletChart');
104 var gEnter = wrapEnter.append('g');
105 var g = wrap.select('g');
107 gEnter.append('g').attr('class', 'nv-bulletWrap');
108 gEnter.append('g').attr('class', 'nv-titles');
110 wrap.attr('transform', 'translate(' + margin.left + ',' + ( margin.top + i*height )+ ')');
112 //------------------------------------------------------------
115 // Compute the new x-scale.
116 var MaxX = Math.max(rangez[0] ? rangez[0]:0 , markerz[0] ? markerz[0] : 0 , measurez[0] ? measurez[0] : 0)
117 var x1 = d3.scale.linear()
118 .domain([0, MaxX]).nice() // TODO: need to allow forceX and forceY, and xDomain, yDomain
119 .range(reverse ? [availableWidth, 0] : [0, availableWidth]);
121 // Retrieve the old x-scale, if this is an update.
122 var x0 = this.__chart__ || d3.scale.linear()
123 .domain([0, Infinity])
126 // Stash the new scale.
130 // Derive width-scales from the x-scales.
131 var w0 = bulletWidth(x0),
132 w1 = bulletWidth(x1);
134 function bulletWidth(x) {
137 return Math.abs(x(d) - x(0));
141 function bulletTranslate(x) {
143 return 'translate(' + x(d) + ',0)';
148 var w0 = function(d) { return Math.abs(x0(d) - x0(0)) }, // TODO: could optimize by precalculating x0(0) and x1(0)
149 w1 = function(d) { return Math.abs(x1(d) - x1(0)) };
152 var title = gEnter.select('.nv-titles').append("g")
153 .attr("text-anchor", "end")
154 .attr("transform", "translate(-6," + (height - margin.top - margin.bottom) / 2 + ")");
156 .attr("class", "nv-title")
157 .text(function(d) { return d.title; });
160 .attr("class", "nv-subtitle")
162 .text(function(d) { return d.subtitle; });
167 .width(availableWidth)
168 .height(availableHeight)
170 var bulletWrap = g.select('.nv-bulletWrap');
172 d3.transition(bulletWrap).call(bullet);
176 // Compute the tick format.
177 var format = tickFormat || x1.tickFormat(8);
179 // Update the tick groups.
180 var tick = g.selectAll('g.nv-tick')
181 .data(x1.ticks(8), function(d) {
182 return this.textContent || format(d);
185 // Initialize the ticks with the old scale, x0.
186 var tickEnter = tick.enter().append('g')
187 .attr('class', 'nv-tick')
188 .attr('transform', function(d) { return 'translate(' + x0(d) + ',0)' })
189 .style('opacity', 1e-6);
191 tickEnter.append('line')
192 .attr('y1', availableHeight)
193 .attr('y2', availableHeight * 7 / 6);
195 tickEnter.append('text')
196 .attr('text-anchor', 'middle')
198 .attr('y', availableHeight * 7 / 6)
201 // Transition the entering ticks to the new scale, x1.
202 d3.transition(tickEnter)
203 .attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
204 .style('opacity', 1);
206 // Transition the updating ticks to the new scale, x1.
207 var tickUpdate = d3.transition(tick)
208 .attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
209 .style('opacity', 1);
211 tickUpdate.select('line')
212 .attr('y1', availableHeight)
213 .attr('y2', availableHeight * 7 / 6);
215 tickUpdate.select('text')
216 .attr('y', availableHeight * 7 / 6);
218 // Transition the exiting ticks to the new scale, x1.
219 d3.transition(tick.exit())
220 .attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
221 .style('opacity', 1e-6)
225 //============================================================
226 // Event Handling/Dispatching (in chart's scope)
227 //------------------------------------------------------------
229 dispatch.on('tooltipShow', function(e) {
230 if (tooltips) showTooltip(e, that.parentNode);
233 //============================================================
243 //============================================================
244 // Event Handling/Dispatching (out of chart's scope)
245 //------------------------------------------------------------
247 bullet.dispatch.on('elementMouseover.tooltip', function(e) {
248 dispatch.tooltipShow(e);
251 bullet.dispatch.on('elementMouseout.tooltip', function(e) {
252 dispatch.tooltipHide(e);
255 dispatch.on('tooltipHide', function() {
256 if (tooltips) nv.tooltip.cleanup();
259 //============================================================
262 //============================================================
263 // Expose Public Variables
264 //------------------------------------------------------------
266 chart.dispatch = dispatch;
267 chart.bullet = bullet;
269 // left, right, top, bottom
270 chart.orient = function(x) {
271 if (!arguments.length) return orient;
273 reverse = orient == 'right' || orient == 'bottom';
277 // ranges (bad, satisfactory, good)
278 chart.ranges = function(x) {
279 if (!arguments.length) return ranges;
284 // markers (previous, goal)
285 chart.markers = function(x) {
286 if (!arguments.length) return markers;
291 // measures (actual, forecast)
292 chart.measures = function(x) {
293 if (!arguments.length) return measures;
298 chart.width = function(x) {
299 if (!arguments.length) return width;
304 chart.height = function(x) {
305 if (!arguments.length) return height;
310 chart.margin = function(_) {
311 if (!arguments.length) return margin;
312 margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
313 margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
314 margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
315 margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
319 chart.tickFormat = function(x) {
320 if (!arguments.length) return tickFormat;
325 chart.tooltips = function(_) {
326 if (!arguments.length) return tooltips;
331 chart.tooltipContent = function(_) {
332 if (!arguments.length) return tooltip;
337 chart.noData = function(_) {
338 if (!arguments.length) return noData;
343 //============================================================