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(last_value): compute last value for non stacked series #261

Merged
merged 2 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 0 additions & 9 deletions src/lib/series/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,13 @@ export function splitSeries(
rawDataSeries: RawDataSeries[];
colorsValues: Map<string, any[]>;
xValues: Set<any>;
splitSeriesLastValues: Map<string, any>;
} {
const { xAccessor, yAccessors, y0Accessors, splitSeriesAccessors = [] } = accessors;
const colorAccessors = accessors.colorAccessors ? accessors.colorAccessors : splitSeriesAccessors;
const isMultipleY = yAccessors && yAccessors.length > 1;
const series = new Map<string, RawDataSeries>();
const colorsValues = new Map<string, any[]>();
const xValues = new Set<any>();
const splitSeriesLastValues = new Map<string, any>();

data.forEach((datum) => {
const seriesKey = getAccessorsValues(datum, splitSeriesAccessors);
Expand All @@ -107,7 +105,6 @@ export function splitSeries(
const colorValuesKey = getColorValuesAsString(colorValues, specId);
colorsValues.set(colorValuesKey, colorValues);
const cleanedDatum = cleanDatum(datum, xAccessor, accessor, y0Accessors && y0Accessors[index]);
splitSeriesLastValues.set(colorValuesKey, cleanedDatum.y1);
xValues.add(cleanedDatum.x);
updateSeriesMap(series, [...seriesKey, accessor], cleanedDatum, specId, colorValuesKey);
}, {});
Expand All @@ -116,17 +113,14 @@ export function splitSeries(
const colorValuesKey = getColorValuesAsString(colorValues, specId);
colorsValues.set(colorValuesKey, colorValues);
const cleanedDatum = cleanDatum(datum, xAccessor, yAccessors[0], y0Accessors && y0Accessors[0]);
splitSeriesLastValues.set(colorValuesKey, cleanedDatum.y1);
xValues.add(cleanedDatum.x);
updateSeriesMap(series, [...seriesKey], cleanedDatum, specId, colorValuesKey);
}
}, {});

return {
rawDataSeries: [...series.values()],
colorsValues,
xValues,
splitSeriesLastValues,
};
}

Expand Down Expand Up @@ -318,13 +312,10 @@ export function getSplittedSeries(
splittedSeries.set(specId, currentRawDataSeries);

dataSeries.colorsValues.forEach((colorValues, key) => {
const lastValue = dataSeries.splitSeriesLastValues.get(key);

seriesColors.set(key, {
specId,
specSortIndex: spec.sortIndex,
colorValues,
lastValue,
});
});

Expand Down
49 changes: 34 additions & 15 deletions src/state/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,36 @@ export function getUpdatedCustomSeriesColors(seriesSpecs: Map<SpecId, BasicSerie
return updatedCustomSeriesColors;
}

export function getLastValues(formattedDataSeries: {
stacked: FormattedDataSeries[];
nonStacked: FormattedDataSeries[];
}): Map<string, number> {
const lastValues = new Map<string, number>();

// we need to get the latest
formattedDataSeries.stacked.forEach((ds) => {
ds.dataSeries.forEach((series) => {
if (series.data.length > 0) {
const last = series.data[series.data.length - 1];
if (last !== null && last.initialY1 !== null) {
lastValues.set(series.seriesColorKey, last.initialY1);
}
}
});
});
formattedDataSeries.nonStacked.forEach((ds) => {
ds.dataSeries.forEach((series) => {
if (series.data.length > 0) {
const last = series.data[series.data.length - 1];
if (last !== null && last.initialY1 !== null) {
lastValues.set(series.seriesColorKey, last.initialY1);
}
}
});
});
return lastValues;
}

/**
*
* @param seriesSpecs
Expand All @@ -112,29 +142,18 @@ export function computeSeriesDomains(
deselectedDataSeries?: DataSeriesColorsValues[] | null,
): SeriesDomainsAndData {
const { splittedSeries, xValues, seriesColors } = getSplittedSeries(seriesSpecs, deselectedDataSeries);
// tslint:disable-next-line:no-console
// console.log({ splittedSeries, xValues, seriesColors });

const splittedDataSeries = [...splittedSeries.values()];
const specsArray = [...seriesSpecs.values()];

const xDomain = mergeXDomain(specsArray, xValues, customXDomain);
const yDomain = mergeYDomain(splittedSeries, specsArray, domainsByGroupId);

const formattedDataSeries = getFormattedDataseries(specsArray, splittedSeries);
// tslint:disable-next-line:no-console
// console.log({ formattedDataSeries, xDomain, yDomain });\
const lastValues = new Map<string, number>();

formattedDataSeries.stacked.forEach((ds) => {
ds.dataSeries.forEach((series) => {
if (series.data.length > 0) {
const last = series.data[series.data.length - 1];
if (last !== null && last.initialY1 !== null) {
lastValues.set(series.seriesColorKey, last.initialY1);
}
}
});
});
// we need to get the last values from the formatted dataseries
// because we change the format if we are on percentage mode
const lastValues = getLastValues(formattedDataSeries);
const updatedSeriesColors = new Map<string, DataSeriesColorsValues>();
seriesColors.forEach((value, key) => {
const lastValue = lastValues.get(key);
Expand Down
5 changes: 3 additions & 2 deletions stories/bar_chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ storiesOf('Bar Chart', module)
.add('stacked as percentage', () => {
const stackedAsPercentage = boolean('stacked as percentage', true);
const clusterBars = boolean('cluster', true);

return (
<Chart className={'story-chart'}>
<Settings showLegend={true} legendPosition={Position.Right} />
Expand All @@ -486,7 +487,7 @@ storiesOf('Bar Chart', module)
id={getAxisId('left2')}
title={'Left axis'}
position={Position.Left}
tickFormat={(d) => (stackedAsPercentage && clusterBars ? `${Number(d * 100).toFixed(0)} %` : d)}
tickFormat={(d) => (stackedAsPercentage && !clusterBars ? `${Number(d * 100).toFixed(0)} %` : d)}
/>

<BarSeries
Expand All @@ -496,7 +497,7 @@ storiesOf('Bar Chart', module)
xAccessor="x"
yAccessors={['y']}
stackAccessors={clusterBars ? [] : ['x']}
stackAsPercentage={stackedAsPercentage}
stackAsPercentage={clusterBars ? false : stackedAsPercentage}
splitSeriesAccessors={['g']}
data={[
{ x: 0, y: 2, g: 'a' },
Expand Down