-
Notifications
You must be signed in to change notification settings - Fork 121
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: blind sorting option for vislib #813
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,76 +111,138 @@ export function getSeriesIndex(series: SeriesIdentifier[], target: SeriesIdentif | |
* `y` values and `mark` values are casted to number or null. | ||
* @internal | ||
*/ | ||
export function splitSeriesDataByAccessors({ | ||
id: specId, | ||
data, | ||
xAccessor, | ||
yAccessors, | ||
y0Accessors, | ||
markSizeAccessor, | ||
splitSeriesAccessors = [], | ||
}: Pick< | ||
BasicSeriesSpec, | ||
'id' | 'data' | 'xAccessor' | 'yAccessors' | 'y0Accessors' | 'splitSeriesAccessors' | 'markSizeAccessor' | ||
>): { | ||
export function splitSeriesDataByAccessors( | ||
{ | ||
id: specId, | ||
data, | ||
xAccessor, | ||
yAccessors, | ||
y0Accessors, | ||
markSizeAccessor, | ||
splitSeriesAccessors = [], | ||
}: Pick< | ||
BasicSeriesSpec, | ||
'id' | 'data' | 'xAccessor' | 'yAccessors' | 'y0Accessors' | 'splitSeriesAccessors' | 'markSizeAccessor' | ||
>, | ||
enableVislibSeriesSort = false, | ||
): { | ||
dataSeries: Map<SeriesKey, DataSeries>; | ||
xValues: Array<string | number>; | ||
} { | ||
const dataSeries = new Map<SeriesKey, DataSeries>(); | ||
const xValues: Array<string | number> = []; | ||
const nonNumericValues: any[] = []; | ||
|
||
data.forEach((datum) => { | ||
const splitAccessors = getSplitAccessors(datum, splitSeriesAccessors); | ||
// if splitSeriesAccessors are defined we should have at least one split value to include datum | ||
if (splitSeriesAccessors.length > 0 && splitAccessors.size < 1) { | ||
return; | ||
} | ||
|
||
// skip if the datum is not an object or null | ||
if (typeof datum !== 'object' || datum === null) { | ||
return null; | ||
} | ||
if (enableVislibSeriesSort) { | ||
/* | ||
* This logic is mostly duplicated from below but is a temporary fix before | ||
* https://github.com/elastic/elastic-charts/issues/795 is completed to allow sorting | ||
* The difference from below is that it loops through all the yAsccessors before the data. | ||
*/ | ||
yAccessors.forEach((accessor, index) => { | ||
data.forEach((datum) => { | ||
const splitAccessors = getSplitAccessors(datum, splitSeriesAccessors); | ||
// if splitSeriesAccessors are defined we should have at least one split value to include datum | ||
if (splitSeriesAccessors.length > 0 && splitAccessors.size < 1) { | ||
return; | ||
} | ||
|
||
const x = getAccessorValue(datum, xAccessor); | ||
// skip if the datum is not an object or null | ||
if (typeof datum !== 'object' || datum === null) { | ||
return; | ||
} | ||
|
||
// skip if the x value is not a string or a number | ||
if (typeof x !== 'string' && typeof x !== 'number') { | ||
return null; | ||
} | ||
const x = getAccessorValue(datum, xAccessor); | ||
|
||
xValues.push(x); | ||
// skip if the x value is not a string or a number | ||
if (typeof x !== 'string' && typeof x !== 'number') { | ||
return; | ||
} | ||
|
||
yAccessors.forEach((accessor, index) => { | ||
const cleanedDatum = extractYandMarkFromDatum( | ||
datum, | ||
accessor, | ||
nonNumericValues, | ||
y0Accessors && y0Accessors[index], | ||
markSizeAccessor, | ||
); | ||
const seriesKeys = [...splitAccessors.values(), accessor]; | ||
const seriesKey = getSeriesKey({ | ||
specId, | ||
yAccessor: accessor, | ||
splitAccessors, | ||
}); | ||
const newDatum = { x, ...cleanedDatum }; | ||
const series = dataSeries.get(seriesKey); | ||
if (series) { | ||
series.data.push(newDatum); | ||
} else { | ||
dataSeries.set(seriesKey, { | ||
xValues.push(x); | ||
|
||
const cleanedDatum = extractYandMarkFromDatum( | ||
datum, | ||
accessor, | ||
nonNumericValues, | ||
y0Accessors && y0Accessors[index], | ||
markSizeAccessor, | ||
); | ||
const seriesKeys = [...splitAccessors.values(), accessor]; | ||
const seriesKey = getSeriesKey({ | ||
specId, | ||
yAccessor: accessor, | ||
splitAccessors, | ||
data: [newDatum], | ||
key: seriesKey, | ||
seriesKeys, | ||
}); | ||
const newDatum = { x, ...cleanedDatum }; | ||
const series = dataSeries.get(seriesKey); | ||
if (series) { | ||
series.data.push(newDatum); | ||
} else { | ||
dataSeries.set(seriesKey, { | ||
specId, | ||
yAccessor: accessor, | ||
splitAccessors, | ||
data: [newDatum], | ||
key: seriesKey, | ||
seriesKeys, | ||
}); | ||
} | ||
}); | ||
}); | ||
} else { | ||
data.forEach((datum) => { | ||
const splitAccessors = getSplitAccessors(datum, splitSeriesAccessors); | ||
// if splitSeriesAccessors are defined we should have at least one split value to include datum | ||
if (splitSeriesAccessors.length > 0 && splitAccessors.size < 1) { | ||
return; | ||
} | ||
|
||
// skip if the datum is not an object or null | ||
if (typeof datum !== 'object' || datum === null) { | ||
return; | ||
} | ||
|
||
const x = getAccessorValue(datum, xAccessor); | ||
|
||
// skip if the x value is not a string or a number | ||
if (typeof x !== 'string' && typeof x !== 'number') { | ||
return; | ||
} | ||
|
||
xValues.push(x); | ||
|
||
yAccessors.forEach((accessor, index) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The DRY approach would also favor code review as now it's a bit hard to contrast how the added logic deviates from the preexisting version |
||
const cleanedDatum = extractYandMarkFromDatum( | ||
datum, | ||
accessor, | ||
nonNumericValues, | ||
y0Accessors && y0Accessors[index], | ||
markSizeAccessor, | ||
); | ||
const seriesKeys = [...splitAccessors.values(), accessor]; | ||
const seriesKey = getSeriesKey({ | ||
specId, | ||
yAccessor: accessor, | ||
splitAccessors, | ||
}); | ||
const newDatum = { x, ...cleanedDatum }; | ||
const series = dataSeries.get(seriesKey); | ||
if (series) { | ||
series.data.push(newDatum); | ||
} else { | ||
dataSeries.set(seriesKey, { | ||
specId, | ||
yAccessor: accessor, | ||
splitAccessors, | ||
data: [newDatum], | ||
key: seriesKey, | ||
seriesKeys, | ||
}); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
if (nonNumericValues.length > 0) { | ||
Logger.warn( | ||
|
@@ -350,11 +412,13 @@ function getDataSeriesBySpecGroup( | |
* | ||
* @param seriesSpecs the map for all the series spec | ||
* @param deselectedDataSeries the array of deselected/hidden data series | ||
* @param enableVislibSeriesSort is optional; if not specified in <Settings />, | ||
* @internal | ||
*/ | ||
export function getDataSeriesBySpecId( | ||
seriesSpecs: BasicSeriesSpec[], | ||
deselectedDataSeries: SeriesIdentifier[] = [], | ||
enableVislibSeriesSort?: boolean, | ||
): { | ||
dataSeriesBySpecId: Map<SpecId, DataSeries[]>; | ||
seriesCollection: Map<SeriesKey, SeriesCollectionValue>; | ||
|
@@ -377,7 +441,7 @@ export function getDataSeriesBySpecId( | |
isOrdinalScale = true; | ||
} | ||
|
||
const { dataSeries, xValues } = splitSeriesDataByAccessors(spec); | ||
const { dataSeries, xValues } = splitSeriesDataByAccessors(spec, enableVislibSeriesSort); | ||
|
||
// filter deleselected dataseries | ||
let filteredDataSeries: DataSeries[] = [...dataSeries.values()]; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a level of code duplication in the branch, it'd be OK to distinguish between the cases in a more fine grained way