Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
fix: y axis bounds when input are nan (#22)
Browse files Browse the repository at this point in the history
* fix: avoid computing log for 0

* fix: y axis bounds when input are nan
  • Loading branch information
kristw committed Mar 18, 2019
1 parent 5560443 commit 545fd81
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
setAxisShowMaxMin,
stringifyTimeRange,
wrapTooltip,
computeYDomain,
} from './utils';
import {
annotationLayerType,
Expand Down Expand Up @@ -584,7 +585,7 @@ function nvd3Vis(element, props) {

// For log scale, only show 1, 10, 100, 1000, ...
if (yIsLogScale) {
chart.yAxis.tickFormat(d => (Math.log10(d) % 1 === 0 ? yAxisFormatter(d) : ''));
chart.yAxis.tickFormat(d => (d !== 0 && Math.log10(d) % 1 === 0 ? yAxisFormatter(d) : ''));
}

if (xLabelRotation > 0) {
Expand All @@ -593,15 +594,23 @@ function nvd3Vis(element, props) {
xTicks.selectAll('text').attr('dx', -6.5);
}

// Apply y-axis bounds
if (chart.yDomain && Array.isArray(yAxisBounds) && yAxisBounds.length === 2) {
const [min, max] = yAxisBounds;
const [trueMin, trueMax] = chart.yAxis.scale().domain();
const yMin = isDefined(min) ? min : trueMin;
const yMax = isDefined(max) ? max : trueMax;
if (yMin !== trueMin || yMax !== trueMax) {
chart.yDomain([yMin, yMax]);
chart.clipEdge(true);
const hasCustomMin = isDefined(min) && !Number.isNaN(min);
const hasCustomMax = isDefined(max) && !Number.isNaN(max);
let yMin;
let yMax;
if (hasCustomMin && hasCustomMax) {
yMin = min;
yMax = max;
} else {
const [trueMin, trueMax] = computeYDomain(data);
yMin = hasCustomMin ? min : trueMin;
yMax = hasCustomMax ? max : trueMax;
}
chart.yDomain([yMin, yMax]);
chart.clipEdge(true);
}

// align yAxis1 and yAxis2 ticks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,15 @@ export function setAxisShowMaxMin(axis, showminmax) {
axis.showMaxMin(showminmax);
}
}

export function computeYDomain(data) {
if (Array.isArray(data) && data.length > 0 && Array.isArray(data[0].values)) {
const extents = data.map(row => d3.extent(row.values, v => v.y));
const minOfMin = d3.min(extents, ([min]) => min);
const maxOfMax = d3.max(extents, ([, max]) => max);

return [minOfMin, maxOfMax];
}

return [0, 1];
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ export default [
renderStory: () => (
<div className="container">
<h2>yAxisBounds</h2>
<pre>yAxisBounds=undefined</pre>
<SuperChart
chartType="line"
chartProps={{
datasource: { verboseMap: {} },
formData: {
richTooltip: true,
showLegend: false,
vizType: 'line',
},
height: 200,
payload: { data },
width: 400,
}}
/>
<pre>yAxisBounds=[0, 60000]</pre>
<SuperChart
chartType="line"
Expand Down

0 comments on commit 545fd81

Please sign in to comment.