2020-02-10 17:31:48 +01:00
|
|
|
/* globals d3 */
|
2015-11-04 17:42:45 +01:00
|
|
|
(function() {
|
2020-02-10 17:31:48 +01:00
|
|
|
// 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/
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
d3.bullet = function() {
|
|
|
|
var orient = "right", // TODO top & bottom
|
2015-11-04 17:42:45 +01:00
|
|
|
reverse = false,
|
|
|
|
duration = 0,
|
|
|
|
ranges = bulletRanges,
|
|
|
|
markers = bulletMarkers,
|
|
|
|
measures = bulletMeasures,
|
|
|
|
width = 80,
|
|
|
|
height = 30,
|
|
|
|
tickFormat = null;
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// For each small multiple…
|
|
|
|
function bullet(g) {
|
|
|
|
g.each(function(d, i) {
|
|
|
|
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),
|
2015-11-04 17:42:45 +01:00
|
|
|
g = d3.select(this);
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Compute the new x-scale.
|
|
|
|
var x1 = d3.scale
|
|
|
|
.linear()
|
2015-11-04 17:42:45 +01:00
|
|
|
.domain([0, Math.max(rangez[0], markerz[0], measurez[0])])
|
2020-02-10 17:31:48 +01:00
|
|
|
.range(reverse ? [width, 0] : [0, width - 160]);
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Retrieve the old x-scale, if this is an update.
|
|
|
|
var x0 =
|
|
|
|
this.__chart__ ||
|
|
|
|
d3.scale
|
|
|
|
.linear()
|
|
|
|
.domain([0, Infinity])
|
|
|
|
.range(x1.range());
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Stash the new scale.
|
|
|
|
this.__chart__ = x1;
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Derive width-scales from the x-scales.
|
|
|
|
var w0 = bulletWidth(x0),
|
2015-11-04 17:42:45 +01:00
|
|
|
w1 = bulletWidth(x1);
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Update the range rects.
|
|
|
|
var range = g.selectAll("rect.range").data(rangez);
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
range
|
|
|
|
.enter()
|
|
|
|
.append("rect")
|
|
|
|
.attr("class", function(d, i) {
|
|
|
|
return "range s" + i;
|
|
|
|
})
|
2015-11-04 17:42:45 +01:00
|
|
|
.attr("width", w0)
|
|
|
|
.attr("height", height)
|
|
|
|
.attr("x", reverse ? x0 : 0)
|
2020-02-10 17:31:48 +01:00
|
|
|
.transition()
|
2015-11-04 17:42:45 +01:00
|
|
|
.duration(duration)
|
|
|
|
.attr("width", w1)
|
|
|
|
.attr("x", reverse ? x1 : 0);
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
range
|
|
|
|
.transition()
|
2015-11-04 17:42:45 +01:00
|
|
|
.duration(duration)
|
|
|
|
.attr("x", reverse ? x1 : 0)
|
|
|
|
.attr("width", w1)
|
|
|
|
.attr("height", height);
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Update the measure rects.
|
|
|
|
var measure = g.selectAll("rect.measure").data(measurez);
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
measure
|
|
|
|
.enter()
|
|
|
|
.append("rect")
|
|
|
|
.attr("class", function(d, i) {
|
|
|
|
return "measure s" + i;
|
|
|
|
})
|
2015-11-04 17:42:45 +01:00
|
|
|
.attr("width", w0)
|
|
|
|
.attr("height", height / 3)
|
|
|
|
.attr("x", reverse ? x0 : 0)
|
|
|
|
.attr("y", height / 3)
|
2020-02-10 17:31:48 +01:00
|
|
|
.transition()
|
2015-11-04 17:42:45 +01:00
|
|
|
.duration(duration)
|
|
|
|
.attr("width", w1)
|
|
|
|
.attr("x", reverse ? x1 : 0);
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
measure
|
|
|
|
.transition()
|
2015-11-04 17:42:45 +01:00
|
|
|
.duration(duration)
|
|
|
|
.attr("width", w1)
|
|
|
|
.attr("height", height / 3)
|
|
|
|
.attr("x", reverse ? x1 : 0)
|
|
|
|
.attr("y", height / 3);
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Update the marker lines.
|
|
|
|
var marker = g.selectAll("line.marker").data(markerz);
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
marker
|
|
|
|
.enter()
|
|
|
|
.append("line")
|
|
|
|
.attr("class", function(d, i) {
|
|
|
|
return "marker s" + i;
|
|
|
|
})
|
2015-11-04 17:42:45 +01:00
|
|
|
.attr("x1", x0)
|
|
|
|
.attr("x2", x0)
|
|
|
|
.attr("y1", height / 6)
|
2020-02-10 17:31:48 +01:00
|
|
|
.attr("y2", (height * 5) / 6)
|
|
|
|
.transition()
|
2015-11-04 17:42:45 +01:00
|
|
|
.duration(duration)
|
|
|
|
.attr("x1", x1)
|
|
|
|
.attr("x2", x1);
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
marker
|
|
|
|
.transition()
|
2015-11-04 17:42:45 +01:00
|
|
|
.duration(duration)
|
|
|
|
.attr("x1", x1)
|
|
|
|
.attr("x2", x1)
|
|
|
|
.attr("y1", height / 6)
|
2020-02-10 17:31:48 +01:00
|
|
|
.attr("y2", (height * 5) / 6);
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Compute the tick format.
|
|
|
|
var format = tickFormat || x1.tickFormat(8);
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Update the tick groups.
|
|
|
|
var tick = g.selectAll("g.tick").data(x1.ticks(8), function(d) {
|
|
|
|
return this.textContent || format(d);
|
|
|
|
});
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Initialize the ticks with the old scale, x0.
|
|
|
|
var tickEnter = tick
|
|
|
|
.enter()
|
|
|
|
.append("g")
|
2015-11-04 17:42:45 +01:00
|
|
|
.attr("class", "tick")
|
|
|
|
.attr("transform", bulletTranslate(x0))
|
|
|
|
.style("opacity", 1e-6);
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
tickEnter
|
|
|
|
.append("line")
|
2015-11-04 17:42:45 +01:00
|
|
|
.attr("y1", height)
|
2020-02-10 17:31:48 +01:00
|
|
|
.attr("y2", (height * 7) / 6);
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
tickEnter
|
|
|
|
.append("text")
|
2015-11-04 17:42:45 +01:00
|
|
|
.attr("text-anchor", "middle")
|
|
|
|
.attr("dy", "1em")
|
2020-02-10 17:31:48 +01:00
|
|
|
.attr("y", (height * 7) / 6)
|
2015-11-04 17:42:45 +01:00
|
|
|
.text(format);
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Transition the entering ticks to the new scale, x1.
|
|
|
|
tickEnter
|
|
|
|
.transition()
|
2015-11-04 17:42:45 +01:00
|
|
|
.duration(duration)
|
|
|
|
.attr("transform", bulletTranslate(x1))
|
|
|
|
.style("opacity", 1);
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Transition the updating ticks to the new scale, x1.
|
|
|
|
var tickUpdate = tick
|
|
|
|
.transition()
|
2015-11-04 17:42:45 +01:00
|
|
|
.duration(duration)
|
|
|
|
.attr("transform", bulletTranslate(x1))
|
|
|
|
.style("opacity", 1);
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
tickUpdate
|
|
|
|
.select("line")
|
2015-11-04 17:42:45 +01:00
|
|
|
.attr("y1", height)
|
2018-08-13 10:59:38 +02:00
|
|
|
.attr("y2", height + 7);
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
tickUpdate.select("text").attr("y", height + 7);
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
// Transition the exiting ticks to the new scale, x1.
|
|
|
|
tick
|
|
|
|
.exit()
|
|
|
|
.transition()
|
2015-11-04 17:42:45 +01:00
|
|
|
.duration(duration)
|
|
|
|
.attr("transform", bulletTranslate(x1))
|
|
|
|
.style("opacity", 1e-6)
|
|
|
|
.remove();
|
2020-02-10 17:31:48 +01:00
|
|
|
});
|
|
|
|
d3.timer.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// left, right, top, bottom
|
|
|
|
bullet.orient = function(x) {
|
|
|
|
if (!arguments.length) return orient;
|
|
|
|
orient = x;
|
|
|
|
reverse = orient == "right" || orient == "bottom";
|
|
|
|
return bullet;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ranges (bad, satisfactory, good)
|
|
|
|
bullet.ranges = function(x) {
|
|
|
|
if (!arguments.length) return ranges;
|
|
|
|
ranges = x;
|
|
|
|
return bullet;
|
|
|
|
};
|
|
|
|
|
|
|
|
// markers (previous, goal)
|
|
|
|
bullet.markers = function(x) {
|
|
|
|
if (!arguments.length) return markers;
|
|
|
|
markers = x;
|
|
|
|
return bullet;
|
|
|
|
};
|
|
|
|
|
|
|
|
// measures (actual, forecast)
|
|
|
|
bullet.measures = function(x) {
|
|
|
|
if (!arguments.length) return measures;
|
|
|
|
measures = x;
|
|
|
|
return bullet;
|
|
|
|
};
|
|
|
|
|
|
|
|
bullet.width = function(x) {
|
|
|
|
if (!arguments.length) return width;
|
|
|
|
width = x;
|
|
|
|
return bullet;
|
|
|
|
};
|
|
|
|
|
|
|
|
bullet.height = function(x) {
|
|
|
|
if (!arguments.length) return height;
|
|
|
|
height = x;
|
|
|
|
return bullet;
|
|
|
|
};
|
|
|
|
|
|
|
|
bullet.tickFormat = function(x) {
|
|
|
|
if (!arguments.length) return tickFormat;
|
|
|
|
tickFormat = x;
|
|
|
|
return bullet;
|
|
|
|
};
|
|
|
|
|
|
|
|
bullet.duration = function(x) {
|
|
|
|
if (!arguments.length) return duration;
|
|
|
|
duration = x;
|
|
|
|
return bullet;
|
|
|
|
};
|
2015-11-04 17:42:45 +01:00
|
|
|
|
|
|
|
return bullet;
|
|
|
|
};
|
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
function bulletRanges(d) {
|
|
|
|
return d.ranges;
|
|
|
|
}
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
function bulletMarkers(d) {
|
|
|
|
return d.markers;
|
|
|
|
}
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
function bulletMeasures(d) {
|
|
|
|
return d.measures;
|
|
|
|
}
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
function bulletTranslate(x) {
|
|
|
|
return function(d) {
|
|
|
|
return "translate(" + x(d) + ",0)";
|
|
|
|
};
|
|
|
|
}
|
2015-11-04 17:42:45 +01:00
|
|
|
|
2020-02-10 17:31:48 +01:00
|
|
|
function bulletWidth(x) {
|
|
|
|
var x0 = x(0);
|
|
|
|
return function(d) {
|
|
|
|
return Math.abs(x(d) - x0);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
})();
|