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: render bands properly not nested bands data #635

Merged
merged 2 commits into from
Mar 30, 2021
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
45 changes: 18 additions & 27 deletions apps/chart/src/store/plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,28 @@ type UsingPlotLineBandOptions = ValueOf<
Pick<ChartOptionsMap, 'area' | 'line' | 'lineArea' | 'columnLine'>
>;

function getOverlappingRange(range: RangeDataType<number>[]) {
return range.reduce<RangeDataType<number>>(
(acc, rangeData) => {
function getOverlappingRange(ranges: PlotBand[]) {
const overlappingRanges = ranges.reduce<RangeDataType<number>>(
(acc, { range }) => {
const [accStart, accEnd] = acc;
const [start, end] = rangeData;
const [start, end] = range as RangeDataType<number>;

return [Math.min(accStart, start), Math.max(accEnd, end)];
},
[Number.MAX_SAFE_INTEGER, Number.MIN_SAFE_INTEGER]
);

return {
range: overlappingRanges,
color: ranges[0].color,
};
}

function getCategoryIndex(value: string, categories: string[]) {
return categories.findIndex((category) => category === String(value));
}

function getValidValue(value, categories: string[], isDateType = false): number {
function getValidValue(value: string | number, categories: string[], isDateType = false): number {
if (isDateType) {
return Number(new Date(value));
}
Expand All @@ -58,29 +63,15 @@ function makePlotLines(categories: string[], isDateType: boolean, plotLines: Plo
function makePlotBands(categories: string[], isDateType: boolean, plotBands: PlotBand[] = []) {
return plotBands.flatMap(({ range, mergeOverlappingRanges = false, color: bgColor, opacity }) => {
const color = rgba(bgColor, opacity);

if (isRangeValue(range[0])) {
const ranges = (range as PlotRangeType[]).map((rangeData) =>
rangeData.map((value) => getValidValue(value, categories, isDateType))
) as RangeDataType<number>[];

if (mergeOverlappingRanges) {
return {
color,
range: getOverlappingRange(ranges),
};
}

return ranges.map((rangeData) => ({
range: rangeData,
color,
}));
}

return {
const rangeArray = (isRangeValue(range[0]) ? range : [range]) as PlotRangeType[];
const ranges: PlotBand[] = rangeArray.map((rangeData) => ({
range: rangeData.map((value) =>
getValidValue(value, categories, isDateType)
) as RangeDataType<number>,
color,
range,
};
}));

return mergeOverlappingRanges ? getOverlappingRange(ranges) : ranges;
});
}

Expand Down
22 changes: 22 additions & 0 deletions apps/chart/stories/plot.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ export const visible = () => {
return el;
};

export const basic = () => {
const { el } = createChart(randomData(24), {
plot: {
bands: [
{
range: [3, 8],
color: '#cccccc',
opacity: 0.2,
},
],
lines: [
{
value: 12,
color: '#fa2828',
},
],
},
});

return el;
};

export const coordinate = () => {
const { el } = createChart(coordinateData, {
plot: {
Expand Down