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

feat(series): stack series in percentage mode #250

Merged
merged 9 commits into from
Jul 5, 2019

Conversation

markov00
Copy link
Member

@markov00 markov00 commented Jun 26, 2019

Summary

This PR will add a new props that allows stacking bars, areas and lines using the percentage
mode. For each stacked x value, we scale their value to the percentage on that bucket.

That option mode is applied by groups of series (specified by groupId) so you need to specify only one prop on one of the group series the property: stackAsPercentage={true}

<Axis 
  id={getAxisId('count')} 
  title="count" 
  position={Position.Left} 
  tickFormat={d => `${Number(d * 100).toFixed(1)}%`}/>
<AreaSeries
    id={getSpecId('dataset A')}
    xScaleType={ScaleType.Time}
    yScaleType={ScaleType.Linear}
    data={KIBANA_METRICS.metrics.kibana_os_load[1].data}
    xAccessor={0}
    yAccessors={[1]}
    stackAccessors={[0]}
    stackAsPercentage={true}
  />
<AreaSeries
    id={getSpecId('dataset B')}
    xScaleType={ScaleType.Time}
    yScaleType={ScaleType.Linear}
    data={KIBANA_METRICS.metrics.kibana_os_load[0].data}
    xAccessor={0}
    yAccessors={[1]}
    stackAccessors={[0]}
    stackAsPercentage={true}
  />

100% stacked bars:

Screenshot 2019-06-26 at 14 57 50

100% stacked areas:

Screenshot 2019-06-26 at 14 43 35

100% stacked areas with y0accessors on a single area:
Screenshot 2019-06-26 at 14 43 44

100% stacked areas with y0accessors on multiple areas:
Screenshot 2019-06-26 at 14 43 52

fix #222

Checklist

Use strikethroughs to remove checklist items you don't feel are applicable to this PR.

  • Any consumer-facing exports were added to src/index.ts (and stories only import from ../src except for test data & storybook)
  • This was checked for cross-browser compatibility, including a check against IE11
  • Proper documentation or storybook story was added for features that require explanation or tutorials
  • Unit tests were updated or added to match the most common scenarios
  • Each commit follows the convention

This commit will add a new props that allows stacking bars, areas and lines using the percentage
mode. For each stacked x value, we scale their value to the percentage on that bucket.

fix elastic#222
@markov00 markov00 added :data Data/series/scales related issue enhancement New feature or request labels Jun 26, 2019
@codecov-io
Copy link

codecov-io commented Jun 26, 2019

Codecov Report

Merging #250 into master will increase coverage by 0.21%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #250      +/-   ##
==========================================
+ Coverage   97.76%   97.97%   +0.21%     
==========================================
  Files          36       36              
  Lines        2641     2920     +279     
  Branches      590      702     +112     
==========================================
+ Hits         2582     2861     +279     
+ Misses         52       50       -2     
- Partials        7        9       +2
Impacted Files Coverage Δ
src/lib/series/specs.ts 100% <ø> (ø) ⬆️
src/state/utils.ts 96.44% <100%> (+0.2%) ⬆️
src/lib/series/series.ts 100% <100%> (ø) ⬆️
src/lib/series/domains/y_domain.ts 100% <100%> (ø) ⬆️
src/lib/series/stacked_series_utils.ts 98.21% <100%> (+2.65%) ⬆️
src/lib/series/legend.ts 100% <100%> (ø) ⬆️
src/lib/axes/canvas_text_bbox_calculator.ts 100% <0%> (ø) ⬆️
src/lib/axes/axis_utils.ts 100% <0%> (ø) ⬆️
src/lib/utils/commons.ts 100% <0%> (ø) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fc6640d...3093ec9. Read the comment docs.

Copy link
Contributor

@emmacunningham emmacunningham left a comment

Choose a reason for hiding this comment

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

Left a few inline code comments, some other things that I've noticed:

last bucket value in legend is not visible in legend when bars are clustered
image

The current hover value is visible, but not the default last value.

multiple series
We should be careful to document this for the consuming users so that they understand that for stackAsPercentage to work correctly, the need to define stackAccessors for each series or else the chart will look quite strange. Below is what the chart looks like if you enable stackAsPercentage on one series but only define stackAccessors for one series:

image

Can we also add a story for the area series?

Also, should stackAsPercentage be unavailable as a prop on a line series? It seems to me that without the context that is provided by the area series, this prop with a line series could present very misleading data to the point that we shouldn't expose it as a possibility for consumer users because it would lead to a very bad practice.

stories/bar_chart.tsx Outdated Show resolved Hide resolved
} else if (customDomain && isUpperBound(customDomain)) {
if (computedDomainMin > customDomain.max) {
throw new Error(`custom yDomain for ${groupId} is invalid, computed min is greater than custom max`);
let domain: number[];
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we use the Domain type here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes and no. On the Y Axis we, currently can handle only numeric values, so the domain here is number[], or better: it should be [number, number]. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

ahhh yes, of course. Yes I agree [number, number]; maybe in the future we can consider how to tighten the Domain type so that we can between distinguish between types of domains so it is clearer.

src/lib/series/stacked_series_utils.ts Outdated Show resolved Hide resolved
src/state/utils.ts Outdated Show resolved Hide resolved
src/lib/series/series.ts Outdated Show resolved Hide resolved
/**
* Stack each series in percentage for each point.
*/
stackAsPercentage?: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

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

It is possible to define a series with empty/undefined stackAccessors and stackAsPercentage=true. While functionally it ends up rendering ok, it seems we may want to consider how to constrain these "impossible states" in a way that makes it easy for the user to understand what options are available to them given certain other prop configurations.

I don't think this needs to be addressed in this PR because we also have a few other "impossible states" that can be configured currently, but wanted to start the discussion in case that is something we want to explore. This is a talk about how to achieve such a semantics in Elm, but certainly not unique to Elm and something that we could do using Typescript/React.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, that should be addressed here: #85

Copy link
Collaborator

@nickofthyme nickofthyme left a comment

Choose a reason for hiding this comment

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

LGTM, verified locally.

@markov00
Copy link
Member Author

markov00 commented Jul 4, 2019

@emmacunningham this is addressed on 3093ec9:

Also, should stackAsPercentage be unavailable as a prop on a line series? It seems to me that without the context that is provided by the area series, this prop with a line series could present very misleading data to the point that we shouldn't expose it as a possibility for consumer users because it would lead to a very bad practice.

Copy link
Contributor

@emmacunningham emmacunningham left a comment

Choose a reason for hiding this comment

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

Changes LGTM!

@markov00 markov00 merged this pull request into elastic:master Jul 5, 2019
markov00 added a commit to markov00/elastic-charts that referenced this pull request Jul 5, 2019
Add the `stackAsPercentage` prop to allow stacking bars and areas computing the percentage of each data point on each bucket/x value.

fix elastic#222
markov00 pushed a commit that referenced this pull request Jul 5, 2019
# [7.2.0](v7.1.0...v7.2.0) (2019-07-05)

### Bug Fixes

* **ticks:** fill in additional ticks for histogram ([#251](#251)) ([af92736](af92736))

### Features

* **series:** stack series in percentage mode ([#250](#250)) ([1bfb430](1bfb430)), closes [#222](#222)
@markov00 markov00 deleted the stack_percent branch November 25, 2020 11:44
AMoo-Miki pushed a commit to AMoo-Miki/OpenSearch-Dashboards that referenced this pull request Feb 10, 2022
# [7.2.0](elastic/elastic-charts@v7.1.0...v7.2.0) (2019-07-05)

### Bug Fixes

* **ticks:** fill in additional ticks for histogram ([opensearch-project#251](elastic/elastic-charts#251)) ([8045531](elastic/elastic-charts@8045531))

### Features

* **series:** stack series in percentage mode ([opensearch-project#250](elastic/elastic-charts#250)) ([892a826](elastic/elastic-charts@892a826)), closes [opensearch-project#222](elastic/elastic-charts#222)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:data Data/series/scales related issue enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Percent stacked option
4 participants