Skip to content

Commit

Permalink
Fix stacked area chart with uneven data (carbon-design-system#681)
Browse files Browse the repository at this point in the history
* fix: fix stacked area chart with uneven data

sort stackKeys

* style: remove console.log

* improvement: add demo for uneven data and revise code

* Update packages/core/src/model.ts

Co-authored-by: Donisius Wigie <[email protected]>

* style: add semicolons and revert code

Co-authored-by: Fei <[email protected]>
Co-authored-by: Eliad Moosavi <[email protected]>
Co-authored-by: Donisius Wigie <[email protected]>
  • Loading branch information
4 people authored and linhenry0417 committed Jul 23, 2020
1 parent 8dff0fe commit f7fd64e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/core/demo/data/area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ export const stackedAreaTimeSeriesOptions = {
curve: "curveMonotoneX"
};

export const stackedAreaTimeSeriesUnevenData = [
{ group: "Dataset 1", date: new Date(2019, 0, 1), value: 10000 },
{ group: "Dataset 1", date: new Date(2019, 0, 8), value: 10000 },
{ group: "Dataset 1", date: new Date(2019, 0, 13), value: 49213 },
{ group: "Dataset 1", date: new Date(2019, 0, 17), value: 51213 },
{ group: "Dataset 2", date: new Date(2019, 0, 5), value: 25000 },
{ group: "Dataset 2", date: new Date(2019, 0, 8), value: 60000 },
{ group: "Dataset 2", date: new Date(2019, 0, 17), value: 55213 },
{ group: "Dataset 3", date: new Date(2019, 0, 1), value: 30000 },
{ group: "Dataset 3", date: new Date(2019, 0, 5), value: 20000 },
{ group: "Dataset 3", date: new Date(2019, 0, 8), value: 40000 },
{ group: "Dataset 3", date: new Date(2019, 0, 13), value: 60213 },
{ group: "Dataset 3", date: new Date(2019, 0, 17), value: 25213 }
];

export const stackedAreaTimeSeriesUnevenDataOptions = {
title: "Stacked area (time series with uneven data)",
axes: {
left: {
stacked: true
},
bottom: {
scaleType: "time"
}
},
curve: "curveMonotoneX"
};

export const stackedAreaPercentageTimeSeriesOptions = {
title: "Stacked area (percentage)",
axes: {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/demo/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ let allDemoGroups = [
chartType: chartTypes.StackedAreaChart,
isDemoExample: true
},
{
options: areaDemos.stackedAreaTimeSeriesUnevenDataOptions,
data: areaDemos.stackedAreaTimeSeriesUnevenData,
chartType: chartTypes.StackedAreaChart,
isDemoExample: true
},
{
options: areaDemos.stackedAreaPercentageTimeSeriesOptions,
data: areaDemos.stackedAreaTimeSeriesData,
Expand Down
18 changes: 18 additions & 0 deletions packages/core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ export class ChartModel {
displayData,
(datum) => datum[domainIdentifier]
).keys();

const axisPosition = this.services.cartesianScales.domainAxisPosition;
const scaleType = options.axes[axisPosition].scaleType;

// Sort keys
if (scaleType === ScaleTypes.TIME) {
stackKeys.sort((a: any, b: any) => {
const dateA: any = new Date(a);
const dateB: any = new Date(b);
return dateA - dateB;
});
} else if (
scaleType === ScaleTypes.LOG ||
scaleType === ScaleTypes.LINEAR
) {
stackKeys.sort((a: any, b: any) => a - b);
};

const dataGroupNames = this.getDataGroupNames();

return stackKeys.map((key) => {
Expand Down

0 comments on commit f7fd64e

Please sign in to comment.