Skip to content

Commit

Permalink
fixed rescaleY behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhard-42 committed Apr 8, 2017
1 parent fca4c2d commit f337e08
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions src/models/cumulativeLineChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ nv.models.cumulativeLineChart = function() {
var dx = d3.scale.linear()
, index = {i: 0, x: 0}
, renderWatch = nv.utils.renderWatch(dispatch, duration)
, currentYDomain
;

var stateGetter = function(data) {
Expand Down Expand Up @@ -165,37 +166,25 @@ nv.models.cumulativeLineChart = function() {
x = lines.xScale();
y = lines.yScale();

if (!rescaleY) {
var seriesDomains = data
.filter(function(series) { return !series.disabled })
.map(function(series,i) {
var initialDomain = d3.extent(series.values, lines.y());

//account for series being disabled when losing 95% or more
if (initialDomain[0] < -.95) initialDomain[0] = -.95;

return [
(initialDomain[0] - initialDomain[1]) / (1 + initialDomain[1]),
(initialDomain[1] - initialDomain[0]) / (1 + initialDomain[0])
];
});

var completeDomain = [
d3.min(seriesDomains, function(d) { return d[0] }),
d3.max(seriesDomains, function(d) { return d[1] })
];

lines.yDomain(completeDomain);
} else {
lines.yDomain(null);
}

dx.domain([0, data[0].values.length - 1]) //Assumes all series have same length
.range([0, availableWidth])
.clamp(true);

var data = indexify(index.i, data);

// initialize the starting yDomain for the not-rescale case after indexify (to have calculated point.display)
if (typeof(currentYDomain) === "undefined") {
currentYDomain = getCurrentYDomain(data);
}

if (!rescaleY) {
lines.yDomain(currentYDomain);
lines.clipEdge(true);
} else {
lines.yDomain(null);
}

// Setup containers and skeleton of chart
var interactivePointerEvents = (useInteractiveGuideline) ? "none" : "all";
var wrap = container.selectAll('g.nv-wrap.nv-cumulativeLine').data([data]);
Expand Down Expand Up @@ -258,7 +247,7 @@ nv.models.cumulativeLineChart = function() {
.attr("transform", "translate(" + availableWidth + ",0)");
}

// Show error if series goes below 100%
// Show error if index point value is 0 (division by zero avoided)
var tempDisabled = data.filter(function(d) { return d.tempDisabled });

wrap.select('.tempDisabled').remove(); //clean-up and prevent duplicates
Expand Down Expand Up @@ -428,8 +417,10 @@ nv.models.cumulativeLineChart = function() {
controls.dispatch.on('legendClick', function(d,i) {
d.disabled = !d.disabled;
rescaleY = !d.disabled;

state.rescaleY = rescaleY;
if (!rescaleY) {
currentYDomain = getCurrentYDomain(data); // rescale is turned off, so set the currentYDomain
}
dispatch.stateChange(state);
chart.update();
});
Expand All @@ -448,7 +439,7 @@ nv.models.cumulativeLineChart = function() {
data
.filter(function(series, i) {
series.seriesIndex = i;
return !series.disabled;
return !(series.disabled || series.tempDisabled);
})
.forEach(function(series,i) {
pointIndex = nv.interactiveBisect(series.values, e.pointXValue, chart.x());
Expand Down Expand Up @@ -580,6 +571,19 @@ nv.models.cumulativeLineChart = function() {
})
}

function getCurrentYDomain(data) {
var seriesDomains = data
.filter(function(series) { return !(series.disabled || series.tempDisabled)})
.map(function(series,i) {
return d3.extent(series.values, function (d) { return d.display.y });
});

return [
d3.min(seriesDomains, function(d) { return d[0] }),
d3.max(seriesDomains, function(d) { return d[1] })
];
}

//============================================================
// Expose Public Variables
//------------------------------------------------------------
Expand All @@ -601,7 +605,6 @@ nv.models.cumulativeLineChart = function() {
// simple options, just get/set the necessary values
width: {get: function(){return width;}, set: function(_){width=_;}},
height: {get: function(){return height;}, set: function(_){height=_;}},
rescaleY: {get: function(){return rescaleY;}, set: function(_){rescaleY=_;}},
showControls: {get: function(){return showControls;}, set: function(_){showControls=_;}},
showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
average: {get: function(){return average;}, set: function(_){average=_;}},
Expand All @@ -612,6 +615,10 @@ nv.models.cumulativeLineChart = function() {
noErrorCheck: {get: function(){return noErrorCheck;}, set: function(_){noErrorCheck=_;}},

// options that require extra logic in the setter
rescaleY: {get: function(){return rescaleY;}, set: function(_){
rescaleY = _;
chart.state.rescaleY = _; // also update state
}},
margin: {get: function(){return margin;}, set: function(_){
if (_.top !== undefined) {
margin.top = _.top;
Expand Down

0 comments on commit f337e08

Please sign in to comment.