forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(area_charts): correctly represent baseline with negative data poi…
…nts (opensearch-project#896) This commit fixes the rendering issue when using negative values in area charts. The correct approach is to draw the area with a zero baseline (or dummy 1 baseline for log y scale).
- Loading branch information
Showing
34 changed files
with
1,400 additions
and
948 deletions.
There are no files selected for viewing
Binary file modified
BIN
-7.02 KB
(82%)
...ts-for-all-stories-area-chart-with-log-y-axis-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+45.5 KB
...stories-area-chart-with-negative-and-positive-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+45.5 KB
...stories-area-chart-with-negative-and-positive-visually-looks-correct-2-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+30.4 KB
...for-all-stories-area-chart-with-negative-band-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+33.3 KB
...r-all-stories-area-chart-with-negative-values-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+31.2 KB
...ries-stories-negative-log-areas-snows-negative-values-with-log-scale-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+24.6 KB
...gative-log-areas-snows-only-negative-values-when-hiding-positive-one-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+33.5 KB
...-negative-log-areas-snows-only-positive-domain-mixed-polarity-domain-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+24.9 KB
...gative-log-areas-snows-only-positive-values-when-hiding-negative-one-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
packages/osd-charts/src/chart_types/xy_chart/rendering/area.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { area } from 'd3-shape'; | ||
|
||
import { Scale } from '../../../scales'; | ||
import { Color } from '../../../utils/commons'; | ||
import { CurveType, getCurveFactory } from '../../../utils/curves'; | ||
import { Dimensions } from '../../../utils/dimensions'; | ||
import { AreaGeometry } from '../../../utils/geometry'; | ||
import { AreaSeriesStyle } from '../../../utils/themes/theme'; | ||
import { IndexedGeometryMap } from '../utils/indexed_geometry_map'; | ||
import { DataSeries, DataSeriesDatum } from '../utils/series'; | ||
import { PointStyleAccessor } from '../utils/specs'; | ||
import { renderPoints } from './points'; | ||
import { | ||
getClippedRanges, | ||
getY0ScaledValueOrThrow, | ||
getY1ScaledValueOrThrow, | ||
isYValueDefined, | ||
MarkSizeOptions, | ||
} from './utils'; | ||
|
||
/** @internal */ | ||
export function renderArea( | ||
shift: number, | ||
dataSeries: DataSeries, | ||
xScale: Scale, | ||
yScale: Scale, | ||
panel: Dimensions, | ||
color: Color, | ||
curve: CurveType, | ||
hasY0Accessors: boolean, | ||
xScaleOffset: number, | ||
seriesStyle: AreaSeriesStyle, | ||
markSizeOptions: MarkSizeOptions, | ||
isStacked = false, | ||
pointStyleAccessor?: PointStyleAccessor, | ||
hasFit?: boolean, | ||
): { | ||
areaGeometry: AreaGeometry; | ||
indexedGeometryMap: IndexedGeometryMap; | ||
} { | ||
const y1Fn = getY1ScaledValueOrThrow(yScale); | ||
const y0Fn = getY0ScaledValueOrThrow(yScale); | ||
const definedFn = isYValueDefined(yScale, xScale); | ||
const pathGenerator = area<DataSeriesDatum>() | ||
.x(({ x }) => xScale.scaleOrThrow(x) - xScaleOffset) | ||
.y1(y1Fn) | ||
.y0(y0Fn) | ||
.defined((datum) => { | ||
return definedFn(datum) && (hasY0Accessors ? definedFn(datum, 'y0') : true); | ||
}) | ||
.curve(getCurveFactory(curve)); | ||
|
||
const clippedRanges = getClippedRanges(dataSeries.data, xScale, xScaleOffset); | ||
|
||
let y1Line: string | null; | ||
|
||
try { | ||
y1Line = pathGenerator.lineY1()(dataSeries.data); | ||
} catch { | ||
// When values are not scalable | ||
y1Line = null; | ||
} | ||
|
||
const lines: string[] = []; | ||
if (y1Line) { | ||
lines.push(y1Line); | ||
} | ||
if (hasY0Accessors) { | ||
let y0Line: string | null; | ||
|
||
try { | ||
y0Line = pathGenerator.lineY0()(dataSeries.data); | ||
} catch { | ||
// When values are not scalable | ||
y0Line = null; | ||
} | ||
if (y0Line) { | ||
lines.push(y0Line); | ||
} | ||
} | ||
|
||
const { pointGeometries, indexedGeometryMap } = renderPoints( | ||
shift - xScaleOffset, | ||
dataSeries, | ||
xScale, | ||
yScale, | ||
panel, | ||
color, | ||
seriesStyle.line, | ||
hasY0Accessors, | ||
markSizeOptions, | ||
pointStyleAccessor, | ||
false, | ||
); | ||
|
||
let areaPath: string; | ||
|
||
try { | ||
areaPath = pathGenerator(dataSeries.data) || ''; | ||
} catch { | ||
// When values are not scalable | ||
areaPath = ''; | ||
} | ||
|
||
const areaGeometry: AreaGeometry = { | ||
area: areaPath, | ||
lines, | ||
points: pointGeometries, | ||
color, | ||
transform: { | ||
y: 0, | ||
x: shift, | ||
}, | ||
seriesIdentifier: { | ||
key: dataSeries.key, | ||
specId: dataSeries.specId, | ||
yAccessor: dataSeries.yAccessor, | ||
splitAccessors: dataSeries.splitAccessors, | ||
seriesKeys: dataSeries.seriesKeys, | ||
smHorizontalAccessorValue: dataSeries.smHorizontalAccessorValue, | ||
smVerticalAccessorValue: dataSeries.smVerticalAccessorValue, | ||
}, | ||
seriesAreaStyle: seriesStyle.area, | ||
seriesAreaLineStyle: seriesStyle.line, | ||
seriesPointStyle: seriesStyle.point, | ||
isStacked, | ||
clippedRanges, | ||
hideClippedRanges: !hasFit, | ||
}; | ||
return { | ||
areaGeometry, | ||
indexedGeometryMap, | ||
}; | ||
} |
Oops, something went wrong.