Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raw request to support negative values in bullet charts #1891

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/bulletChart.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<link href="../build/nv.d3.css" rel="stylesheet" type="text/css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.2/d3.min.js" charset="utf-8"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.2/d3.js" charset="utf-8"></script>
<script src="../build/nv.d3.js"></script>

<style>
Expand Down Expand Up @@ -44,15 +44,15 @@
.height(height - margin.top - margin.bottom);

data = [
{"title":"Revenue","subtitle":"US$, in thousands","ranges":[150,225,300],"measures":[220],"markers":[250],"markerLines":[270]},
{"title":"Order Size","subtitle":"US$, average","ranges":[350,500,600],"measures":[100],"markers":[550], "markerLines":[530]},
{"title":"Revenue","subtitle":"US$, in thousands","ranges":[-1150,225,300],"measures":[-220],"markers":[-250],"markerLines":[270]},
{"title":"Order Size","subtitle":"US$, average","ranges":[-1350,500,600],"measures":[100],"markers":[-350,550], "markerLines":[530]},
{"title":"Satisfaction","subtitle":"out of 5","ranges":[3.5,4.25,5],"measures":[3.2,4.7],"markers":[4.4], "markerLines":[3.8]}
];

dataWithLabels = [{
"title":"Revenue",
"subtitle":"US$, in thousands",
"ranges":[150,225,300],
"ranges":[110,150,230],
"measures":[220],
"markers":[250, 100],
"markerLines":[240, 120],
Expand Down Expand Up @@ -98,7 +98,7 @@
function randomizer(d) {
var k = d3.max(d.ranges) * .2;
return function(d) {
return Math.max(0, d + k * (Math.random() - .5));
return Math.max(-1000, d + k * (Math.random() - .5));
};
}

Expand Down
24 changes: 12 additions & 12 deletions src/models/bullet.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ nv.models.bullet = function() {
, markerLabels = function(d) { return d.markerLabels ? d.markerLabels : [] }
, markerLineLabels = function(d) { return d.markerLineLabels ? d.markerLineLabels : [] }
, measureLabels = function(d) { return d.measureLabels ? d.measureLabels : [] }
, forceX = [0] // List of numbers to Force into the X scale (ie. 0, or a max / min, etc.)
, forceX = [] // List of numbers to Force into the X scale (ie. 0, or a max / min, etc.)
, width = 380
, height = 30
, container = null
Expand Down Expand Up @@ -74,7 +74,7 @@ nv.models.bullet = function() {
// Setup Scales
// Compute the new x-scale.
var x1 = d3.scale.linear()
.domain( d3.extent(d3.merge([forceX, rangez])) )
.domain( d3.extent(d3.merge([forceX, rangez, markerz, measurez])) )
.range(reverse ? [availableWidth, 0] : [0, availableWidth]);

// Retrieve the old x-scale, if this is an update.
Expand All @@ -88,7 +88,7 @@ nv.models.bullet = function() {
var rangeMin = d3.min(rangez), //rangez[2]
rangeMax = d3.max(rangez), //rangez[0]
rangeAvg = rangez[1];

var min = d3.min([].concat(rangez, markerz, measurez));
// Setup containers and skeleton of chart
var wrap = container.selectAll('g.nv-wrap.nv-bullet').data([d]);
var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-bullet');
Expand All @@ -105,12 +105,12 @@ nv.models.bullet = function() {

gEnter.append('rect').attr('class', 'nv-measure');

wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');

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 xp0 = function(d) { return d < 0 ? x0(d) : x0(0) },
xp1 = function(d) { return d < 0 ? x1(d) : x1(0) };
var w0 = function(d) { return x0(d) }, // TODO: could optimize by precalculating x0(0) and x1(0)
w1 = function(d) { return x1(d) };
var xp0 = function(d) { return 0 },
xp1 = function(d) { return 0 };

for(var i=0,il=rangez.length; i<il; i++){
var range = rangez[i];
Expand Down Expand Up @@ -150,9 +150,7 @@ nv.models.bullet = function() {
})
.transition()
.duration(duration)
.attr('width', measurez < 0 ?
x1(0) - x1(measurez[0])
: x1(measurez[0]) - x1(0))
.attr('width', w1(measurez[0]))
.attr('x', xp1(measurez));

var h3 = availableHeight / 6;
Expand Down Expand Up @@ -195,7 +193,9 @@ nv.models.bullet = function() {
.data(markerData)
.transition()
.duration(duration)
.attr('transform', function(d) { return 'translate(' + x1(d.value) + ',' + (availableHeight / 2) + ')' });
.attr('transform', function(d) {
return 'translate(' + (x1(d.value) || x1(min)) + ',' + (availableHeight / 2) + ')'
});

var markerLinesData = markerLinez.map( function(marker, index) {
return {value: marker, label: markerLineLabelz[index]}
Expand Down
7 changes: 3 additions & 4 deletions src/models/bulletChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ nv.models.bulletChart = function() {

// Compute the new x-scale.
var x1 = d3.scale.linear()
.domain([0, Math.max(rangez[0], (markerz[0] || 0), measurez[0])]) // TODO: need to allow forceX and forceY, and xDomain, yDomain
.domain([d3.min([].concat(rangez, markerz, measurez)), d3.max([].concat(rangez, markerz, measurez))]) // 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.
Expand All @@ -78,8 +78,8 @@ nv.models.bulletChart = function() {
// Stash the new scale.
this.__chart__ = x1;

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 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 = gEnter.select('.nv-titles').append('g')
.attr('text-anchor', 'end')
Expand Down Expand Up @@ -112,7 +112,6 @@ nv.models.bulletChart = function() {
// Initialize the ticks with the old scale, x0.
var tickEnter = tick.enter().append('g')
.attr('class', 'nv-tick')
.attr('transform', function(d) { return 'translate(' + x0(d) + ',0)' })
.style('opacity', 1e-6);

tickEnter.append('line')
Expand Down