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(xy): stacked polarity #1502

Merged
merged 29 commits into from
Dec 9, 2021
Merged

Conversation

nickofthyme
Copy link
Collaborator

@nickofthyme nickofthyme commented Nov 23, 2021

Summary

Resolves issue with stacking a mix of negative and positive bars and areas for any StackMode. Key changes include:

  • Disallowing stacked band charts (i.e. using stackAccessors with y0Accessors). Now shows console.warn when used and ignores y0Accessor for rendering but still adds to initialY0 value. See fix(xy): stacked polarity #1502 (comment).
  • Blocks showing banded legend and tooltip values for banded stacks, see case above.
  • Prevents percentage domain constraining when no stackAccessors are specified, see fix(xy): stacked polarity #1502 (comment)

No offset

Kapture 2021-12-08 at 11 32 16

Note: The original order of the series is maintained above and below 0.

Wiggle offset

Kapture 2021-12-08 at 11 35 41

Silhouette offset

Kapture 2021-12-08 at 11 36 37

Percentage offset

Kapture 2021-12-08 at 11 37 43

Note: The order is change for mixed values to displace negative values below positive values.

Details

Stacked offsets was implemented to be grouped by series. This creates issues when attempting to sort values differently based on their polarity for a given x value grouping. Thus this logic has been changed to group and stack by x values as demonstrated in this example, which allows for using d3.stackOffsetDiverging.

Additionally, the logic had an intermediate step to group by y0 or y1 values that is no longer needed. This is handled by using the y0 series as a placeholder during stacking and ignoring it when structuring the final geometry data. Though not advisable to stack when using y0Accessors.

Issues

fix #1280

Checklist

  • The proper chart type label has been added (e.g. :xy, :partition)
  • The proper feature labels have been added (e.g. :interactions, :axis)
  • All related issues have been linked (i.e. closes #123, fixes #123)
  • New public API exports have been added to packages/charts/src/index.ts
  • Unit tests have been added or updated to match the most common scenarios
  • The proper documentation and/or storybook story has been added or updated

@nickofthyme nickofthyme added bug Something isn't working :data Data/series/scales related issue :xy Bar/Line/Area chart related labels Nov 23, 2021
@nickofthyme nickofthyme changed the title fix: stacked polarity fix(xy): stacked polarity Dec 3, 2021
.gitignore Show resolved Hide resolved
@nickofthyme nickofthyme requested a review from markov00 December 8, 2021 22:55
@nickofthyme nickofthyme marked this pull request as ready for review December 8, 2021 22:56
@@ -40,7 +40,7 @@
"d3-collection": "^1.0.7",
"d3-interpolate": "^1.4.0",
"d3-scale": "^1.0.7",
"d3-shape": "^1.3.4",
"d3-shape": "^2.0.0",
Copy link
Member

Choose a reason for hiding this comment

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

what was the issue on upgrading to the latest version?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The jest tests were throwing an error similar to https://stackoverflow.com/questions/49263429/jest-gives-an-error-syntaxerror-unexpected-token-export. I tried to fix it but couldn't see any difference between the 2 so I gave up for now.

@@ -111,7 +112,7 @@ export function computeLegend(

dataSeries.forEach((series) => {
const { specId, yAccessor } = series;
const banded = isDataSeriesBanded(series);
const banded = isDataSeriesBanded(series) && !isStackedSpec(series.spec, false);
Copy link
Member

Choose a reason for hiding this comment

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

since isDataSeriesBanded is only used here and it name is really specific we can probably include the non-stacked check within that function so we have a clear and specific definition of what banded mean

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@@ -47,6 +47,7 @@ export function renderArea(
areaGeometry: AreaGeometry;
indexedGeometryMap: IndexedGeometryMap;
} {
const isBandChart = hasY0Accessors && !isStacked;
Copy link
Member

Choose a reason for hiding this comment

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

can we reuse isDataSeriesBanded here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@@ -130,6 +130,13 @@ export function splitSeriesDataByAccessors(
const dataSeries = new Map<SeriesKey, DataSeries>();
const xValues: Array<string | number> = [];
const nonNumericValues: any[] = [];
const hasStackedBands = isStacked && Boolean(y0Accessors?.length);
Copy link
Member

Choose a reason for hiding this comment

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

reuse isDataSeriesBanded

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Comment on lines +63 to +64
/>
</Chart>
Copy link
Member

Choose a reason for hiding this comment

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

If you add a zero baseline it will look better in some cases:

<LineAnnotation
        id="zeroBaseline"
        domainType={AnnotationDomainType.YDomain}
        dataValues={[{ dataValue: 0 }]}
        style={{ line: { ...baseTheme.axes.axisLine, opacity: 1 } }}
        zIndex={-1}
      />

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Comment on lines +21 to +36
const polarity = select('data polarity', ['Mixed', 'Positive', 'Negative'], 'Mixed');
const customDomain = boolean('custom domain', false);
const stackMode =
select<StackMode | undefined>(
'stackMode',
{
None: undefined,
Silhouette: StackMode.Silhouette,
Wiggle: StackMode.Wiggle,
Percentage: StackMode.Percentage,
},
undefined,
) ?? undefined;
const [Series] = getXYSeriesKnob('SeriesType', SeriesType.Bar, undefined, {
ignore: [SeriesType.Bubble, SeriesType.Line],
});
Copy link
Member

Choose a reason for hiding this comment

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

Area chart with mixed polarity values is not a good practice. Adding a warning on the console or as a text below the example can be useful.
I'm also not sure if it is worth having such cases in VRTs

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

A warning is good. I added the VRT thinking we could fix this in the future with the split series we had discussed. If you think that is not going to happen I can remove the case for mixed polarity stacked areas.

Copy link
Collaborator Author

@nickofthyme nickofthyme Dec 9, 2021

Choose a reason for hiding this comment

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

Added the warning in f06db8a. I'll keep the vrt for mixed area for now and we can decide if/when to support that later.

@nickofthyme nickofthyme merged commit 920666a into elastic:master Dec 9, 2021
@nickofthyme nickofthyme deleted the fix-stacked-polarity branch December 9, 2021 20:55
nickofthyme pushed a commit that referenced this pull request Dec 9, 2021
# [40.2.0](v40.1.0...v40.2.0) (2021-12-09)

### Bug Fixes

* **partition:** linkLabel textColor override ([#1498](#1498)) ([3013310](3013310))
* **waffle:** use descend sortPredicate by default ([#1510](#1510)) ([763e2e3](763e2e3))
* **xy:** stacked polarity ([#1502](#1502)) ([920666a](920666a)), closes [#1280](#1280)

### Features

* **xy:** expose style for interpolation fit functions ([#1505](#1505)) ([3071457](3071457))
nickofthyme added a commit to nickofthyme/elastic-charts that referenced this pull request Dec 9, 2021
Resolves issue with stacking a mix of negative and positive bars and areas for any `StackMode`. Key changes include:

- Disallowing stacked band charts (i.e. using `stackAccessors` with `y0Accessors`). Now shows `console.warn` when used and ignores `y0Accessor` for rendering but still adds to `initialY0` value.
- Blocks showing banded legend and tooltip values for banded stacks, see case above.
- Prevents percentage domain constraining when no `stackAccessors` are specified.

fix elastic#1280
# Conflicts:
#	packages/charts/src/chart_types/xy_chart/rendering/area.ts
nickofthyme added a commit that referenced this pull request Dec 9, 2021
Resolves issue with stacking a mix of negative and positive bars and areas for any `StackMode`. Key changes include:

- Disallowing stacked band charts (i.e. using `stackAccessors` with `y0Accessors`). Now shows `console.warn` when used and ignores `y0Accessor` for rendering but still adds to `initialY0` value.
- Blocks showing banded legend and tooltip values for banded stacks, see case above.
- Prevents percentage domain constraining when no `stackAccessors` are specified.

fix #1280
nickofthyme pushed a commit that referenced this pull request Dec 9, 2021
## [40.1.1](v40.1.0...v40.1.1) (2021-12-09)

### Bug Fixes

* **partition:** linkLabel textColor override ([#1498](#1498)) ([#1523](#1523)) ([5324c76](5324c76))
* **xy:** stacked polarity ([#1502](#1502)) ([#1522](#1522)) ([bfb853f](bfb853f)), closes [#1280](#1280)
nickofthyme added a commit to nickofthyme/elastic-charts that referenced this pull request Dec 10, 2021
Resolves issue with stacking a mix of negative and positive bars and areas for any `StackMode`. Key changes include:

- Disallowing stacked band charts (i.e. using `stackAccessors` with `y0Accessors`). Now shows `console.warn` when used and ignores `y0Accessor` for rendering but still adds to `initialY0` value.
- Blocks showing banded legend and tooltip values for banded stacks, see case above.
- Prevents percentage domain constraining when no `stackAccessors` are specified.

fix elastic#1280
# Conflicts:
#	integration/tests/__image_snapshots__/axis-stories-test-ts-axis-stories-should-switch-to-a-30-minute-raster-1-snap.png
#	packages/charts/src/chart_types/xy_chart/rendering/area.ts
#	packages/charts/src/chart_types/xy_chart/utils/stacked_series_utils.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working :data Data/series/scales related issue :xy Bar/Line/Area chart related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stacked bar charts do not render correctly with Positive / Negative Values
2 participants