reworked bullet a little bit and created bulletChart

master-patched
Bob Monteverde 12 years ago
parent e07921f473
commit d57bd67074

@ -6,6 +6,7 @@ JS_FILES = \
src/models/axis.js \
src/models/historicalBar.js \
src/models/bullet.js \
src/models/bulletChart.js \
src/models/cumulativeLineChart.js \
src/models/discreteBar.js \
src/models/discreteBarChart.js \

@ -49,49 +49,21 @@ var chart = nv.models.bullet()
.attr("height", height)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.call(chart);
var title = vis.append("g")
.attr("text-anchor", "end")
.attr("transform", "translate(-6," + (height - margin.top - margin.bottom) / 2 + ")");
title.append("text")
.attr("class", "title")
.text(function(d) { return d.title; });
title.append("text")
.attr("class", "subtitle")
.attr("dy", "1em")
.text(function(d) { return d.subtitle; });
chart.duration(1000);
vis
.transition()
.duration(1000)
.call(chart);
window.transition = function() {
vis.datum(randomize).call(chart);
};
vis.datum(randomize);
chart.dispatch.on('elementMouseover', function(e) {
var offsetElement = document.getElementById("chart"),
left = e.pos[0] + offsetElement.offsetLeft + margin.left,
top = e.pos[1] + offsetElement.offsetTop + margin.top;
var content = '<h3>' + e.label + '</h3>' +
'<p>' +
e.value +
'</p>';
nv.tooltip.show([left, top], content, e.value < 0 ? 'e' : 'w');
});
chart.dispatch.on('elementMouseout', function(e) {
nv.tooltip.cleanup();
});
vis
.transition()
.duration(1000)
.call(chart);
};

@ -0,0 +1,94 @@
<!DOCTYPE html>
<meta charset="utf-8">
<link href="../src/d3.css" rel="stylesheet" type="text/css">
<style>
body {
overflow-y:scroll;
}
</style>
<body>
<br> <br> <br> <br> <br>
<div class="gallery" id="chart"></div>
<script src="../lib/d3.v2.js"></script>
<script src="../nv.d3.js"></script>
<script src="../src/models/bullet.js"></script>
<script src="../src/models/bulletChart.js"></script>
<script>
var width = 960,
height = 80,
margin = {top: 5, right: 40, bottom: 20, left: 120};
var chart = nv.models.bulletChart()
.width(width - margin.right - margin.left)
.height(height - margin.top - margin.bottom);
data = [
{"title":"Revenue","subtitle":"US$, in thousands","ranges":[150,225,300],"measures":[220],"markers":[250]}
/*
//TODO: fix the tooltip offset if multiple
,
{"title":"Profit","subtitle":"%","ranges":[20,25,30],"measures":[21],"markers":[26]},
{"title":"Order Size","subtitle":"US$, average","ranges":[350,500,600],"measures":[100],"markers":[550]},
{"title":"New Customers","subtitle":"count","ranges":[1400,2000,2500],"measures":[1000],"markers":[1000]},
{"title":"Satisfaction","subtitle":"out of 5","ranges":[3.5,4.25,5],"measures":[3.2,4.7],"markers":[4.4]}
*/
];
//TODO: to be consistent with other models, shouild be appending a g to an already made svg, not creating the svg element
var vis = d3.select("#chart").selectAll("svg")
.data(data)
.enter().append("svg")
.attr("class", "bullet nvd3")
.attr("width", width)
.attr("height", height)
vis
.transition()
.duration(1000)
.call(chart);
window.transition = function() {
vis.datum(randomize);
vis
.transition()
.duration(1000)
.call(chart);
};
function randomize(d) {
if (!d.randomizer) d.randomizer = randomizer(d);
d.ranges = d.ranges.map(d.randomizer);
d.markers = d.markers.map(d.randomizer);
d.measures = d.measures.map(d.randomizer);
return d;
}
function randomizer(d) {
var k = d3.max(d.ranges) * .2;
return function(d) {
return Math.max(0, d + k * (Math.random() - .5));
};
}
d3.select('body').on('click', window.transition);
</script>

@ -745,9 +745,9 @@ nv.models.historicalBar = function() {
// based on the work of Clint Ivy, Jamie Love, and Jason Davies.
// http://projects.instantcognition.com/protovis/bulletchart/
nv.models.bullet = function() {
var orient = "left", // TODO top & bottom
var orient = 'left', // TODO top & bottom
reverse = false,
duration = 0,
margin = {top: 0, right: 0, bottom: 0, left: 0},
ranges = function(d) { return d.ranges },
markers = function(d) { return d.markers },
measures = function(d) { return d.measures },
@ -758,17 +758,28 @@ nv.models.bullet = function() {
var dispatch = d3.dispatch('elementMouseover', 'elementMouseout');
// For each small multiple…
function bullet(g) {
function chart(g) {
g.each(function(d, i) {
var availableWidth = width - margin.left - margin.right,
availableHeight = height - margin.top - margin.bottom;
var rangez = ranges.call(this, d, i).slice().sort(d3.descending),
markerz = markers.call(this, d, i).slice().sort(d3.descending),
measurez = measures.call(this, d, i).slice().sort(d3.descending),
g = d3.select(this);
measurez = measures.call(this, d, i).slice().sort(d3.descending);
var wrap = d3.select(this).selectAll('g.wrap.bullet').data([data]);
var wrapEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 bullet');
var gEnter = wrapEnter.append('g');
var g = wrap.select('g')
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// Compute the new x-scale.
var x1 = d3.scale.linear()
.domain([0, Math.max(rangez[0], markerz[0], measurez[0])]) // TODO: need to allow forceX and forceY, and xDomain, yDomain
.range(reverse ? [width, 0] : [0, width]);
.range(reverse ? [availableWidth, 0] : [0, availableWidth]);
// Retrieve the old x-scale, if this is an update.
var x0 = this.__chart__ || d3.scale.linear()
@ -792,7 +803,7 @@ nv.models.bullet = function() {
function bulletTranslate(x) {
return function(d) {
return "translate(" + x(d) + ",0)";
return 'translate(' + x(d) + ',0)';
};
}
*/
@ -802,19 +813,19 @@ nv.models.bullet = function() {
// Update the range rects.
var range = g.selectAll("rect.range")
var range = g.selectAll('rect.range')
.data(rangez);
range.enter().append("rect")
.attr("class", function(d, i) { return "range s" + i; })
.attr("width", w0)
.attr("height", height)
.attr("x", reverse ? x0 : 0)
range.enter().append('rect')
.attr('class', function(d, i) { return 'range s' + i; })
.attr('width', w0)
.attr('height', availableHeight)
.attr('x', reverse ? x0 : 0)
.on('mouseover', function(d,i) {
dispatch.elementMouseover({
value: d,
label: (i <= 0) ? 'Maximum' : (i > 1) ? 'Minimum' : 'Mean', //TODO: make these labels a variable
pos: [x1(d), height/2]
pos: [x1(d), availableHeight/2]
})
})
.on('mouseout', function(d,i) {
@ -823,33 +834,28 @@ nv.models.bullet = function() {
label: (i <= 0) ? 'Minimum' : (i >=1) ? 'Maximum' : 'Mean', //TODO: make these labels a variable
})
})
.transition()
.duration(duration)
.attr("width", w1)
.attr("x", reverse ? x1 : 0);
range.transition()
.duration(duration)
.attr("x", reverse ? x1 : 0)
.attr("width", w1)
.attr("height", height);
d3.transition(range)
.attr('x', reverse ? x1 : 0)
.attr('width', w1)
.attr('height', availableHeight);
// Update the measure rects.
var measure = g.selectAll("rect.measure")
var measure = g.selectAll('rect.measure')
.data(measurez);
measure.enter().append("rect")
.attr("class", function(d, i) { return "measure s" + i; })
.attr("width", w0)
.attr("height", height / 3)
.attr("x", reverse ? x0 : 0)
.attr("y", height / 3)
measure.enter().append('rect')
.attr('class', function(d, i) { return 'measure s' + i; })
.attr('width', w0)
.attr('height', availableHeight / 3)
.attr('x', reverse ? x0 : 0)
.attr('y', availableHeight / 3)
.on('mouseover', function(d) {
dispatch.elementMouseover({
value: d,
label: 'Current', //TODO: make these labels a variable
pos: [x1(d), height/2]
pos: [x1(d), availableHeight/2]
})
})
.on('mouseout', function(d) {
@ -858,34 +864,29 @@ nv.models.bullet = function() {
label: 'Current' //TODO: make these labels a variable
})
})
.transition()
.duration(duration)
.attr("width", w1)
.attr("x", reverse ? x1 : 0)
measure.transition()
.duration(duration)
.attr("width", w1)
.attr("height", height / 3)
.attr("x", reverse ? x1 : 0)
.attr("y", height / 3);
d3.transition(measure)
.attr('width', w1)
.attr('height', availableHeight / 3)
.attr('x', reverse ? x1 : 0)
.attr('y', availableHeight / 3);
// Update the marker lines.
var marker = g.selectAll("path.markerTriangle")
var marker = g.selectAll('path.markerTriangle')
.data(markerz);
var h3 = height / 6;
marker.enter().append("path")
.attr("class", "markerTriangle")
.attr('transform', function(d) { return 'translate(' + x0(d) + ',' + (height / 2) + ')' })
var h3 = availableHeight / 6;
marker.enter().append('path')
.attr('class', 'markerTriangle')
.attr('transform', function(d) { return 'translate(' + x0(d) + ',' + (availableHeight / 2) + ')' })
.attr('d', 'M0,' + h3 + 'L' + h3 + ',' + (-h3) + ' ' + (-h3) + ',' + (-h3) + 'Z')
.on('mouseover', function(d,i) {
dispatch.elementMouseover({
value: d,
label: 'Previous',
pos: [x1(d), height/2]
pos: [x1(d), availableHeight/2]
})
})
.on('mouseout', function(d,i) {
@ -895,144 +896,352 @@ nv.models.bullet = function() {
})
});
marker.transition().duration(duration)
.attr('transform', function(d) { return 'translate(' + x1(d) + ',' + (height / 2) + ')' });
d3.transition(marker)
.attr('transform', function(d) { return 'translate(' + x1(d) + ',' + (availableHeight / 2) + ')' });
marker.exit().remove();
/*
marker.enter().append("line")
.attr("class", "marker")
.attr("x1", x0)
.attr("x2", x0)
.attr("y1", height / 6)
.attr("y2", height * 5 / 6)
.transition()
.duration(duration)
.attr("x1", x1)
.attr("x2", x1);
marker.transition(
.duration(duration)
.attr("x1", x1)
.attr("x2", x1)
.attr("y1", height / 6)
.attr("y2", height * 5 / 6);
*/
});
d3.timer.flush();
}
chart.dispatch = dispatch;
// left, right, top, bottom
chart.orient = function(_) {
if (!arguments.length) return orient;
orient = _;
reverse = orient == 'right' || orient == 'bottom';
return chart;
};
// ranges (bad, satisfactory, good)
chart.ranges = function(_) {
if (!arguments.length) return ranges;
ranges = _;
return chart;
};
// markers (previous, goal)
chart.markers = function(_) {
if (!arguments.length) return markers;
markers = _;
return chart;
};
// measures (actual, forecast)
chart.measures = function(_) {
if (!arguments.length) return measures;
measures = _;
return chart;
};
chart.width = function(_) {
if (!arguments.length) return width;
width = _;
return chart;
};
chart.height = function(_) {
if (!arguments.length) return height;
height = _;
return chart;
};
chart.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
return chart;
};
chart.tickFormat = function(_) {
if (!arguments.length) return tickFormat;
tickFormat = _;
return chart;
};
return chart;
};
// Chart design based on the recommendations of Stephen Few. Implementation
// based on the work of Clint Ivy, Jamie Love, and Jason Davies.
// http://projects.instantcognition.com/protovis/bulletchart/
nv.models.bulletChart = function() {
var orient = 'left', // TODO top & bottom
reverse = false,
margin = {top: 5, right: 40, bottom: 20, left: 120},
ranges = function(d) { return d.ranges },
markers = function(d) { return d.markers },
measures = function(d) { return d.measures },
width = 380,
height = 55,
tickFormat = null,
tooltips = true,
tooltip = function(key, x, y, e, graph) {
return '<h3>' + e.label + '</h3>' +
'<p>' + e.value + '</p>'
};
var dispatch = d3.dispatch('tooltipShow', 'tooltipHide'),
bullet = nv.models.bullet();
var showTooltip = function(e, offsetElement) {
var offsetElement = document.getElementById("chart"),
left = e.pos[0] + offsetElement.offsetLeft + margin.left,
top = e.pos[1] + offsetElement.offsetTop + margin.top;
var content = '<h3>' + e.label + '</h3>' +
'<p>' + e.value + '</p>';
nv.tooltip.show([left, top], content, e.value < 0 ? 'e' : 'w');
};
// For each small multiple…
function chart(g) {
g.each(function(d, i) {
var availableWidth = width - margin.left - margin.right,
availableHeight = height - margin.top - margin.bottom,
that = this;
var rangez = ranges.call(this, d, i).slice().sort(d3.descending),
markerz = markers.call(this, d, i).slice().sort(d3.descending),
measurez = measures.call(this, d, i).slice().sort(d3.descending);
var wrap = d3.select(this).selectAll('g.wrap.bulletChart').data([d]);
var wrapEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 bulletChart');
var gEnter = wrapEnter.append('g');
gEnter.append('g').attr('class', 'bulletWrap');
gEnter.append('g').attr('class', 'titles');
var g = wrap.select('g')
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// Compute the new x-scale.
var x1 = d3.scale.linear()
.domain([0, Math.max(rangez[0], markerz[0], measurez[0])]) // TODO: need to allow forceX and forceY, and xDomain, yDomain
.range(reverse ? [availableWidth, 0] : [0, availableWidth]);
// Retrieve the old x-scale, if this is an update.
var x0 = this.__chart__ || d3.scale.linear()
.domain([0, Infinity])
.range(x1.range());
// Stash the new scale.
this.__chart__ = x1;
/*
// Derive width-scales from the x-scales.
var w0 = bulletWidth(x0),
w1 = bulletWidth(x1);
function bulletWidth(x) {
var x0 = x(0);
return function(d) {
return Math.abs(x(d) - x(0));
};
}
function bulletTranslate(x) {
return function(d) {
return 'translate(' + x(d) + ',0)';
};
}
*/
var w0 = function(d) { return Math.abs(x0(d) - x0(0)) }, // TODO: could optimize by precalculating x0(0) and x1(0)
w1 = function(d) { return Math.abs(x1(d) - x1(0)) };
var title = g.select('.titles').append("g")
.attr("text-anchor", "end")
.attr("transform", "translate(-6," + (height - margin.top - margin.bottom) / 2 + ")");
title.append("text")
.attr("class", "title")
.text(function(d) { return d.title; });
title.append("text")
.attr("class", "subtitle")
.attr("dy", "1em")
.text(function(d) { return d.subtitle; });
bullet
.width(availableWidth)
.height(availableHeight)
var bulletWrap = g.select('.bulletWrap')
//.datum(data);
d3.transition(bulletWrap).call(bullet);
// Compute the tick format.
var format = tickFormat || x1.tickFormat(8);
// Update the tick groups.
var tick = g.selectAll("g.tick")
var tick = g.selectAll('g.tick')
.data(x1.ticks(8), function(d) {
return this.textContent || format(d);
});
// Initialize the ticks with the old scale, x0.
var tickEnter = tick.enter().append("g")
.attr("class", "tick")
.attr("transform", function(d) { return "translate(" + x0(d) + ",0)" })
.style("opacity", 1e-6);
var tickEnter = tick.enter().append('g')
.attr('class', 'tick')
.attr('transform', function(d) { return 'translate(' + x0(d) + ',0)' })
.style('opacity', 1e-6);
tickEnter.append("line")
.attr("y1", height)
.attr("y2", height * 7 / 6);
tickEnter.append('line')
.attr('y1', availableHeight)
.attr('y2', availableHeight * 7 / 6);
tickEnter.append("text")
.attr("text-anchor", "middle")
.attr("dy", "1em")
.attr("y", height * 7 / 6)
tickEnter.append('text')
.attr('text-anchor', 'middle')
.attr('dy', '1em')
.attr('y', availableHeight * 7 / 6)
.text(format);
// Transition the entering ticks to the new scale, x1.
tickEnter.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + x1(d) + ",0)" })
.style("opacity", 1);
d3.transition(tickEnter)
.attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
.style('opacity', 1);
// Transition the updating ticks to the new scale, x1.
var tickUpdate = tick.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + x1(d) + ",0)" })
.style("opacity", 1);
var tickUpdate = d3.transition(tick)
.attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
.style('opacity', 1);
tickUpdate.select("line")
.attr("y1", height)
.attr("y2", height * 7 / 6);
tickUpdate.select('line')
.attr('y1', availableHeight)
.attr('y2', availableHeight * 7 / 6);
tickUpdate.select("text")
.attr("y", height * 7 / 6);
tickUpdate.select('text')
.attr('y', availableHeight * 7 / 6);
// Transition the exiting ticks to the new scale, x1.
tick.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + x1(d) + ",0)" })
.style("opacity", 1e-6)
d3.transition(tick.exit())
.attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
.style('opacity', 1e-6)
.remove();
/*
bullet.dispatch.on('elementMouseover', function(e) {
var offsetElement = document.getElementById("chart"),
left = e.pos[0] + offsetElement.offsetLeft + margin.left,
top = e.pos[1] + offsetElement.offsetTop + margin.top;
var content = '<h3>' + e.label + '</h3>' +
'<p>' +
e.value +
'</p>';
nv.tooltip.show([left, top], content, e.value < 0 ? 'e' : 'w');
});
bullet.dispatch.on('elementMouseout', function(e) {
nv.tooltip.cleanup();
});
*/
bullet.dispatch.on('elementMouseover.tooltip', function(e) {
//e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
dispatch.tooltipShow(e);
});
if (tooltips) dispatch.on('tooltipShow', function(e) { showTooltip(e, that.parentNode) } ); // TODO: maybe merge with above?
bullet.dispatch.on('elementMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
});
if (tooltips) dispatch.on('tooltipHide', nv.tooltip.cleanup);
});
d3.timer.flush();
}
bullet.dispatch = dispatch;
chart.dispatch = dispatch;
chart.bullet = bullet;
// left, right, top, bottom
bullet.orient = function(x) {
chart.orient = function(x) {
if (!arguments.length) return orient;
orient = x;
reverse = orient == "right" || orient == "bottom";
return bullet;
reverse = orient == 'right' || orient == 'bottom';
return chart;
};
// ranges (bad, satisfactory, good)
bullet.ranges = function(x) {
chart.ranges = function(x) {
if (!arguments.length) return ranges;
ranges = x;
return bullet;
return chart;
};
// markers (previous, goal)
bullet.markers = function(x) {
chart.markers = function(x) {
if (!arguments.length) return markers;
markers = x;
return bullet;
return chart;
};
// measures (actual, forecast)
bullet.measures = function(x) {
chart.measures = function(x) {
if (!arguments.length) return measures;
measures = x;
return bullet;
return chart;
};
bullet.width = function(x) {
chart.width = function(x) {
if (!arguments.length) return width;
width = x;
return bullet;
return chart;
};
bullet.height = function(x) {
chart.height = function(x) {
if (!arguments.length) return height;
height = x;
return bullet;
return chart;
};
chart.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
return chart;
};
bullet.tickFormat = function(x) {
chart.tickFormat = function(x) {
if (!arguments.length) return tickFormat;
tickFormat = x;
return bullet;
return chart;
};
bullet.duration = function(x) {
if (!arguments.length) return duration;
duration = x;
return bullet;
chart.tooltips = function(_) {
if (!arguments.length) return tooltips;
tooltips = _;
return chart;
};
chart.tooltipContent = function(_) {
if (!arguments.length) return tooltip;
tooltip = _;
return chart;
};
return bullet;
return chart;
};

6
nv.d3.min.js vendored

File diff suppressed because one or more lines are too long

@ -3,9 +3,9 @@
// based on the work of Clint Ivy, Jamie Love, and Jason Davies.
// http://projects.instantcognition.com/protovis/bulletchart/
nv.models.bullet = function() {
var orient = "left", // TODO top & bottom
var orient = 'left', // TODO top & bottom
reverse = false,
duration = 0,
margin = {top: 0, right: 0, bottom: 0, left: 0},
ranges = function(d) { return d.ranges },
markers = function(d) { return d.markers },
measures = function(d) { return d.measures },
@ -16,17 +16,28 @@ nv.models.bullet = function() {
var dispatch = d3.dispatch('elementMouseover', 'elementMouseout');
// For each small multiple…
function bullet(g) {
function chart(g) {
g.each(function(d, i) {
var availableWidth = width - margin.left - margin.right,
availableHeight = height - margin.top - margin.bottom;
var rangez = ranges.call(this, d, i).slice().sort(d3.descending),
markerz = markers.call(this, d, i).slice().sort(d3.descending),
measurez = measures.call(this, d, i).slice().sort(d3.descending),
g = d3.select(this);
measurez = measures.call(this, d, i).slice().sort(d3.descending);
var wrap = d3.select(this).selectAll('g.wrap.bullet').data([data]);
var wrapEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 bullet');
var gEnter = wrapEnter.append('g');
var g = wrap.select('g')
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// Compute the new x-scale.
var x1 = d3.scale.linear()
.domain([0, Math.max(rangez[0], markerz[0], measurez[0])]) // TODO: need to allow forceX and forceY, and xDomain, yDomain
.range(reverse ? [width, 0] : [0, width]);
.range(reverse ? [availableWidth, 0] : [0, availableWidth]);
// Retrieve the old x-scale, if this is an update.
var x0 = this.__chart__ || d3.scale.linear()
@ -50,7 +61,7 @@ nv.models.bullet = function() {
function bulletTranslate(x) {
return function(d) {
return "translate(" + x(d) + ",0)";
return 'translate(' + x(d) + ',0)';
};
}
*/
@ -60,19 +71,19 @@ nv.models.bullet = function() {
// Update the range rects.
var range = g.selectAll("rect.range")
var range = g.selectAll('rect.range')
.data(rangez);
range.enter().append("rect")
.attr("class", function(d, i) { return "range s" + i; })
.attr("width", w0)
.attr("height", height)
.attr("x", reverse ? x0 : 0)
range.enter().append('rect')
.attr('class', function(d, i) { return 'range s' + i; })
.attr('width', w0)
.attr('height', availableHeight)
.attr('x', reverse ? x0 : 0)
.on('mouseover', function(d,i) {
dispatch.elementMouseover({
value: d,
label: (i <= 0) ? 'Maximum' : (i > 1) ? 'Minimum' : 'Mean', //TODO: make these labels a variable
pos: [x1(d), height/2]
pos: [x1(d), availableHeight/2]
})
})
.on('mouseout', function(d,i) {
@ -81,33 +92,28 @@ nv.models.bullet = function() {
label: (i <= 0) ? 'Minimum' : (i >=1) ? 'Maximum' : 'Mean', //TODO: make these labels a variable
})
})
.transition()
.duration(duration)
.attr("width", w1)
.attr("x", reverse ? x1 : 0);
range.transition()
.duration(duration)
.attr("x", reverse ? x1 : 0)
.attr("width", w1)
.attr("height", height);
d3.transition(range)
.attr('x', reverse ? x1 : 0)
.attr('width', w1)
.attr('height', availableHeight);
// Update the measure rects.
var measure = g.selectAll("rect.measure")
var measure = g.selectAll('rect.measure')
.data(measurez);
measure.enter().append("rect")
.attr("class", function(d, i) { return "measure s" + i; })
.attr("width", w0)
.attr("height", height / 3)
.attr("x", reverse ? x0 : 0)
.attr("y", height / 3)
measure.enter().append('rect')
.attr('class', function(d, i) { return 'measure s' + i; })
.attr('width', w0)
.attr('height', availableHeight / 3)
.attr('x', reverse ? x0 : 0)
.attr('y', availableHeight / 3)
.on('mouseover', function(d) {
dispatch.elementMouseover({
value: d,
label: 'Current', //TODO: make these labels a variable
pos: [x1(d), height/2]
pos: [x1(d), availableHeight/2]
})
})
.on('mouseout', function(d) {
@ -116,34 +122,29 @@ nv.models.bullet = function() {
label: 'Current' //TODO: make these labels a variable
})
})
.transition()
.duration(duration)
.attr("width", w1)
.attr("x", reverse ? x1 : 0)
measure.transition()
.duration(duration)
.attr("width", w1)
.attr("height", height / 3)
.attr("x", reverse ? x1 : 0)
.attr("y", height / 3);
d3.transition(measure)
.attr('width', w1)
.attr('height', availableHeight / 3)
.attr('x', reverse ? x1 : 0)
.attr('y', availableHeight / 3);
// Update the marker lines.
var marker = g.selectAll("path.markerTriangle")
var marker = g.selectAll('path.markerTriangle')
.data(markerz);
var h3 = height / 6;
marker.enter().append("path")
.attr("class", "markerTriangle")
.attr('transform', function(d) { return 'translate(' + x0(d) + ',' + (height / 2) + ')' })
var h3 = availableHeight / 6;
marker.enter().append('path')
.attr('class', 'markerTriangle')
.attr('transform', function(d) { return 'translate(' + x0(d) + ',' + (availableHeight / 2) + ')' })
.attr('d', 'M0,' + h3 + 'L' + h3 + ',' + (-h3) + ' ' + (-h3) + ',' + (-h3) + 'Z')
.on('mouseover', function(d,i) {
dispatch.elementMouseover({
value: d,
label: 'Previous',
pos: [x1(d), height/2]
pos: [x1(d), availableHeight/2]
})
})
.on('mouseout', function(d,i) {
@ -153,144 +154,73 @@ nv.models.bullet = function() {
})
});
marker.transition().duration(duration)
.attr('transform', function(d) { return 'translate(' + x1(d) + ',' + (height / 2) + ')' });
d3.transition(marker)
.attr('transform', function(d) { return 'translate(' + x1(d) + ',' + (availableHeight / 2) + ')' });
marker.exit().remove();
/*
marker.enter().append("line")
.attr("class", "marker")
.attr("x1", x0)
.attr("x2", x0)
.attr("y1", height / 6)
.attr("y2", height * 5 / 6)
.transition()
.duration(duration)
.attr("x1", x1)
.attr("x2", x1);
marker.transition(
.duration(duration)
.attr("x1", x1)
.attr("x2", x1)
.attr("y1", height / 6)
.attr("y2", height * 5 / 6);
*/
// Compute the tick format.
var format = tickFormat || x1.tickFormat(8);
// Update the tick groups.
var tick = g.selectAll("g.tick")
.data(x1.ticks(8), function(d) {
return this.textContent || format(d);
});
// Initialize the ticks with the old scale, x0.
var tickEnter = tick.enter().append("g")
.attr("class", "tick")
.attr("transform", function(d) { return "translate(" + x0(d) + ",0)" })
.style("opacity", 1e-6);
tickEnter.append("line")
.attr("y1", height)
.attr("y2", height * 7 / 6);
tickEnter.append("text")
.attr("text-anchor", "middle")
.attr("dy", "1em")
.attr("y", height * 7 / 6)
.text(format);
// Transition the entering ticks to the new scale, x1.
tickEnter.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + x1(d) + ",0)" })
.style("opacity", 1);
// Transition the updating ticks to the new scale, x1.
var tickUpdate = tick.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + x1(d) + ",0)" })
.style("opacity", 1);
tickUpdate.select("line")
.attr("y1", height)
.attr("y2", height * 7 / 6);
tickUpdate.select("text")
.attr("y", height * 7 / 6);
// Transition the exiting ticks to the new scale, x1.
tick.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + x1(d) + ",0)" })
.style("opacity", 1e-6)
.remove();
});
d3.timer.flush();
}
bullet.dispatch = dispatch;
chart.dispatch = dispatch;
// left, right, top, bottom
bullet.orient = function(x) {
chart.orient = function(_) {
if (!arguments.length) return orient;
orient = x;
reverse = orient == "right" || orient == "bottom";
return bullet;
orient = _;
reverse = orient == 'right' || orient == 'bottom';
return chart;
};
// ranges (bad, satisfactory, good)
bullet.ranges = function(x) {
chart.ranges = function(_) {
if (!arguments.length) return ranges;
ranges = x;
return bullet;
ranges = _;
return chart;
};
// markers (previous, goal)
bullet.markers = function(x) {
chart.markers = function(_) {
if (!arguments.length) return markers;
markers = x;
return bullet;
markers = _;
return chart;
};
// measures (actual, forecast)
bullet.measures = function(x) {
chart.measures = function(_) {
if (!arguments.length) return measures;
measures = x;
return bullet;
measures = _;
return chart;
};
bullet.width = function(x) {
chart.width = function(_) {
if (!arguments.length) return width;
width = x;
return bullet;
width = _;
return chart;
};
bullet.height = function(x) {
chart.height = function(_) {
if (!arguments.length) return height;
height = x;
return bullet;
height = _;
return chart;
};
bullet.tickFormat = function(x) {
if (!arguments.length) return tickFormat;
tickFormat = x;
return bullet;
chart.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
return chart;
};
bullet.duration = function(x) {
if (!arguments.length) return duration;
duration = x;
return bullet;
chart.tickFormat = function(_) {
if (!arguments.length) return tickFormat;
tickFormat = _;
return chart;
};
return bullet;
return chart;
};

@ -0,0 +1,279 @@
// Chart design based on the recommendations of Stephen Few. Implementation
// based on the work of Clint Ivy, Jamie Love, and Jason Davies.
// http://projects.instantcognition.com/protovis/bulletchart/
nv.models.bulletChart = function() {
var orient = 'left', // TODO top & bottom
reverse = false,
margin = {top: 5, right: 40, bottom: 20, left: 120},
ranges = function(d) { return d.ranges },
markers = function(d) { return d.markers },
measures = function(d) { return d.measures },
width = 380,
height = 55,
tickFormat = null,
tooltips = true,
tooltip = function(key, x, y, e, graph) {
return '<h3>' + e.label + '</h3>' +
'<p>' + e.value + '</p>'
};
var dispatch = d3.dispatch('tooltipShow', 'tooltipHide'),
bullet = nv.models.bullet();
var showTooltip = function(e, offsetElement) {
var offsetElement = document.getElementById("chart"),
left = e.pos[0] + offsetElement.offsetLeft + margin.left,
top = e.pos[1] + offsetElement.offsetTop + margin.top;
var content = '<h3>' + e.label + '</h3>' +
'<p>' + e.value + '</p>';
nv.tooltip.show([left, top], content, e.value < 0 ? 'e' : 'w');
};
// For each small multiple…
function chart(g) {
g.each(function(d, i) {
var availableWidth = width - margin.left - margin.right,
availableHeight = height - margin.top - margin.bottom,
that = this;
var rangez = ranges.call(this, d, i).slice().sort(d3.descending),
markerz = markers.call(this, d, i).slice().sort(d3.descending),
measurez = measures.call(this, d, i).slice().sort(d3.descending);
var wrap = d3.select(this).selectAll('g.wrap.bulletChart').data([d]);
var wrapEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 bulletChart');
var gEnter = wrapEnter.append('g');
gEnter.append('g').attr('class', 'bulletWrap');
gEnter.append('g').attr('class', 'titles');
var g = wrap.select('g')
wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// Compute the new x-scale.
var x1 = d3.scale.linear()
.domain([0, Math.max(rangez[0], markerz[0], measurez[0])]) // TODO: need to allow forceX and forceY, and xDomain, yDomain
.range(reverse ? [availableWidth, 0] : [0, availableWidth]);
// Retrieve the old x-scale, if this is an update.
var x0 = this.__chart__ || d3.scale.linear()
.domain([0, Infinity])
.range(x1.range());
// Stash the new scale.
this.__chart__ = x1;
/*
// Derive width-scales from the x-scales.
var w0 = bulletWidth(x0),
w1 = bulletWidth(x1);
function bulletWidth(x) {
var x0 = x(0);
return function(d) {
return Math.abs(x(d) - x(0));
};
}
function bulletTranslate(x) {
return function(d) {
return 'translate(' + x(d) + ',0)';
};
}
*/
var w0 = function(d) { return Math.abs(x0(d) - x0(0)) }, // TODO: could optimize by precalculating x0(0) and x1(0)
w1 = function(d) { return Math.abs(x1(d) - x1(0)) };
var title = g.select('.titles').append("g")
.attr("text-anchor", "end")
.attr("transform", "translate(-6," + (height - margin.top - margin.bottom) / 2 + ")");
title.append("text")
.attr("class", "title")
.text(function(d) { return d.title; });
title.append("text")
.attr("class", "subtitle")
.attr("dy", "1em")
.text(function(d) { return d.subtitle; });
bullet
.width(availableWidth)
.height(availableHeight)
var bulletWrap = g.select('.bulletWrap')
//.datum(data);
d3.transition(bulletWrap).call(bullet);
// Compute the tick format.
var format = tickFormat || x1.tickFormat(8);
// Update the tick groups.
var tick = g.selectAll('g.tick')
.data(x1.ticks(8), function(d) {
return this.textContent || format(d);
});
// Initialize the ticks with the old scale, x0.
var tickEnter = tick.enter().append('g')
.attr('class', 'tick')
.attr('transform', function(d) { return 'translate(' + x0(d) + ',0)' })
.style('opacity', 1e-6);
tickEnter.append('line')
.attr('y1', availableHeight)
.attr('y2', availableHeight * 7 / 6);
tickEnter.append('text')
.attr('text-anchor', 'middle')
.attr('dy', '1em')
.attr('y', availableHeight * 7 / 6)
.text(format);
// Transition the entering ticks to the new scale, x1.
d3.transition(tickEnter)
.attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
.style('opacity', 1);
// Transition the updating ticks to the new scale, x1.
var tickUpdate = d3.transition(tick)
.attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
.style('opacity', 1);
tickUpdate.select('line')
.attr('y1', availableHeight)
.attr('y2', availableHeight * 7 / 6);
tickUpdate.select('text')
.attr('y', availableHeight * 7 / 6);
// Transition the exiting ticks to the new scale, x1.
d3.transition(tick.exit())
.attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
.style('opacity', 1e-6)
.remove();
/*
bullet.dispatch.on('elementMouseover', function(e) {
var offsetElement = document.getElementById("chart"),
left = e.pos[0] + offsetElement.offsetLeft + margin.left,
top = e.pos[1] + offsetElement.offsetTop + margin.top;
var content = '<h3>' + e.label + '</h3>' +
'<p>' +
e.value +
'</p>';
nv.tooltip.show([left, top], content, e.value < 0 ? 'e' : 'w');
});
bullet.dispatch.on('elementMouseout', function(e) {
nv.tooltip.cleanup();
});
*/
bullet.dispatch.on('elementMouseover.tooltip', function(e) {
//e.pos = [e.pos[0] + margin.left, e.pos[1] + margin.top];
dispatch.tooltipShow(e);
});
if (tooltips) dispatch.on('tooltipShow', function(e) { showTooltip(e, that.parentNode) } ); // TODO: maybe merge with above?
bullet.dispatch.on('elementMouseout.tooltip', function(e) {
dispatch.tooltipHide(e);
});
if (tooltips) dispatch.on('tooltipHide', nv.tooltip.cleanup);
});
d3.timer.flush();
}
chart.dispatch = dispatch;
chart.bullet = bullet;
// left, right, top, bottom
chart.orient = function(x) {
if (!arguments.length) return orient;
orient = x;
reverse = orient == 'right' || orient == 'bottom';
return chart;
};
// ranges (bad, satisfactory, good)
chart.ranges = function(x) {
if (!arguments.length) return ranges;
ranges = x;
return chart;
};
// markers (previous, goal)
chart.markers = function(x) {
if (!arguments.length) return markers;
markers = x;
return chart;
};
// measures (actual, forecast)
chart.measures = function(x) {
if (!arguments.length) return measures;
measures = x;
return chart;
};
chart.width = function(x) {
if (!arguments.length) return width;
width = x;
return chart;
};
chart.height = function(x) {
if (!arguments.length) return height;
height = x;
return chart;
};
chart.margin = function(_) {
if (!arguments.length) return margin;
margin = _;
return chart;
};
chart.tickFormat = function(x) {
if (!arguments.length) return tickFormat;
tickFormat = x;
return chart;
};
chart.tooltips = function(_) {
if (!arguments.length) return tooltips;
tooltips = _;
return chart;
};
chart.tooltipContent = function(_) {
if (!arguments.length) return tooltip;
tooltip = _;
return chart;
};
return chart;
};
Loading…
Cancel
Save