Skip to content

Allow functional accessors on y, y0, mark and split #838

@nickofthyme

Description

@nickofthyme

Describe the solution you'd like
Allow AccessorFn type for yAccessors, y0Accessors, markSizeAccessor and splitSeriesAccessors.

This is currently allowed for the xAccessor

Describe alternatives you've considered
Reformatting the data before passing it to elastic charts.

Additional context
We need to refactor the way that we track the accessors from the string of the accessor to the index order of the original accessor values.

One change is to update the split series map to use an array to match the provided splitSeriesAccessors array.

function getSplitAccessors(datum: Datum, accessors: Accessor[] = []): Map<string | number, string | number> {
const splitAccessors = new Map<string | number, string | number>();
if (typeof datum === 'object' && datum !== null) {
accessors.forEach((accessor: Accessor) => {
const value = datum[accessor as keyof typeof datum];
if (typeof value === 'string' || typeof value === 'number') {
splitAccessors.set(accessor, value);
}
});
}
return splitAccessors;
}

Secondly, we need to use the yAccessor index rather than the string itself to allow use of AccessorFn

const seriesKeys = [...splitAccessors.values(), accessor];
const seriesKey = getSeriesKey({
specId,
yAccessor: accessor,
splitAccessors,
});

which includes updating the getSeriesKey method

export function getSeriesKey({
specId,
yAccessor,
splitAccessors,
}: Pick<XYChartSeriesIdentifier, 'specId' | 'yAccessor' | 'splitAccessors'>): string {
const joinedAccessors = [...splitAccessors.entries()]
.sort(([a], [b]) => (a > b ? 1 : -1))
.map(([key, value]) => `${key}-${value}`)
.join('|');
return `spec{${specId}}yAccessor{${yAccessor}}splitAccessors{${joinedAccessors}}`;
}

⚠️ This is likely a breaking change unless we want to provide backwards compatibility by using the Accessor string when available and the index otherwise.

Related to #808

Metadata

Metadata

Assignees

No one assigned

    Labels

    :dataData/series/scales related issue:xyBar/Line/Area chart relatedbreaking changeenhancementNew feature or requestreleasedIssue released publicly

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions