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

fix(rendering): fix rendering values <= 0 on log scale #114

Merged
merged 3 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
87 changes: 57 additions & 30 deletions src/lib/series/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,39 +87,56 @@ export function renderPoints(
indexedGeometries: Map<any, IndexedGeometry[]>;
} {
const indexedGeometries: Map<any, IndexedGeometry[]> = new Map();
const isLogScale = yScale.type === ScaleType.Log;

const pointGeometries = dataset.map((datum) => {
const x = xScale.scale(datum.x);
const y = yScale.scale(datum.y1);
const indexedGeometry: IndexedGeometry = {
specId,
datum: datum.datum,
color,
seriesKey,
geom: {
x: x + shift,
y,
width: 10,
height: 10,
isPoint: true,
},
};
mutableIndexedGeometryMapUpsert(indexedGeometries, datum.x, indexedGeometry);
return {
x,
y,
color,
value: {
const pointGeometries = dataset.reduce(
(acc, datum) => {
const x = xScale.scale(datum.x);
let y;
let radius = 10;
const isHidden = datum.y1 === null || (isLogScale && datum.y1 <= 0);
// we fix 0 and negative values at y = 0
if (isHidden) {
y = yScale.range[0];
radius = 0;
} else {
y = yScale.scale(datum.y1);
}
const indexedGeometry: IndexedGeometry = {
specId,
datum: datum.datum,
color,
seriesKey,
},
transform: {
x: shift,
y: 0,
},
};
});
geom: {
x: x + shift,
y,
width: radius,
height: radius,
isPoint: true,
},
};
mutableIndexedGeometryMapUpsert(indexedGeometries, datum.x, indexedGeometry);
const pointGeometry: PointGeometry = {
x,
y,
color,
value: {
specId,
datum: datum.datum,
seriesKey,
},
transform: {
x: shift,
y: 0,
},
};
if (isHidden) {
return acc;
}
return [...acc, pointGeometry];
},
[] as PointGeometry[],
);
return {
pointGeometries,
indexedGeometries,
Expand Down Expand Up @@ -216,9 +233,12 @@ export function renderLine(
lineGeometry: LineGeometry;
indexedGeometries: Map<any, IndexedGeometry[]>;
} {
const isLogScale = yScale.type === ScaleType.Log;

const pathGenerator = line<DataSeriesDatum>()
.x((datum: DataSeriesDatum) => xScale.scale(datum.x))
.y((datum: DataSeriesDatum) => yScale.scale(datum.y1))
.defined((datum: DataSeriesDatum) => datum.y1 !== null && !(isLogScale && datum.y1 <= 0))
.curve(getCurveFactory(curve));
const y = 0;
const x = shift;
Expand Down Expand Up @@ -263,10 +283,17 @@ export function renderArea(
areaGeometry: AreaGeometry;
indexedGeometries: Map<any, IndexedGeometry[]>;
} {
const isLogScale = yScale.type === ScaleType.Log;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this logic is repeated in a few places, could create a helper function isLogScale(type: ScaleType) to ensure consistency.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved in 790d5f7
currently passing a Scale object instead of a type as it's done for the isContinuousScale or isOrdinalScale

const pathGenerator = area<DataSeriesDatum>()
.x((datum: DataSeriesDatum) => xScale.scale(datum.x))
.y1((datum: DataSeriesDatum) => yScale.scale(datum.y1))
.y0((datum: DataSeriesDatum) => yScale.scale(datum.y0))
.y0((datum: DataSeriesDatum) => {
if (isLogScale && datum.y0 <= 0) {
return yScale.range[0];
}
return yScale.scale(datum.y0);
})
.defined((datum: DataSeriesDatum) => datum.y1 !== null && !(isLogScale && datum.y1 <= 0))
.curve(getCurveFactory(curve));
const { lineGeometry, indexedGeometries } = renderLine(
shift,
Expand Down
Loading