Skip to content

Commit

Permalink
Map() implementation fix for chart labels (#7022)
Browse files Browse the repository at this point in the history
Co-authored-by: Ezra Odio <[email protected]>
Co-authored-by: Eric Radman <[email protected]>
  • Loading branch information
3 people authored Jun 18, 2024
1 parent a6c728b commit 4cb32fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
19 changes: 8 additions & 11 deletions viz-lib/src/visualizations/chart/plotly/prepareDefaultData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,20 @@ function prepareSeries(series: any, options: any, additionalOptions: any) {

const sourceData = new Map();

//we hold the labels and values in a dictionary so that we can aggregate multiple values for a single label
//once we reach the end of the data, we'll convert the dictionary to separate arrays for labels and values
const labelsValuesDict: { [key: string]: any } = {};
const labelsValuesMap = new Map();

const yErrorValues: any = [];
each(data, row => {
const x = normalizeValue(row.x, options.xAxis.type); // number/datetime/category
const y = cleanYValue(row.y, seriesYAxis === "y2" ? options.yAxis[1].type : options.yAxis[0].type); // depends on series type!
const yError = cleanNumber(row.yError); // always number
const size = cleanNumber(row.size); // always number
if (x in labelsValuesDict){
labelsValuesDict[x] += y;
if (labelsValuesMap.has(x)) {
labelsValuesMap.set(x, labelsValuesMap.get(x) + y);
} else {
labelsValuesMap.set(x, y);
}
else{
labelsValuesDict[x] = y;
}
const aggregatedY = labelsValuesDict[x];
const aggregatedY = labelsValuesMap.get(x);

sourceData.set(x, {
x,
Expand All @@ -121,8 +118,8 @@ function prepareSeries(series: any, options: any, additionalOptions: any) {
yErrorValues.push(yError);
});

const xValues = Object.keys(labelsValuesDict);
const yValues = Object.values(labelsValuesDict);
const xValues = Array.from(labelsValuesMap.keys());
const yValues = Array.from(labelsValuesMap.values());

const plotlySeries = {
visible: true,
Expand Down
20 changes: 9 additions & 11 deletions viz-lib/src/visualizations/chart/plotly/preparePieData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ function prepareSeries(series: any, options: any, additionalOptions: any) {
const xPosition = (index % cellsInRow) * cellWidth;
const yPosition = Math.floor(index / cellsInRow) * cellHeight;

//we hold the labels and values in a dictionary so that we can aggregate multiple values for a single label
//once we reach the end of the data, we'll convert the dictionary to separate arrays for labels and values
const labelsValuesDict: { [key: string]: any } = {};
const labelsValuesMap = new Map();

const sourceData = new Map();
const seriesTotal = reduce(
Expand All @@ -58,13 +56,13 @@ function prepareSeries(series: any, options: any, additionalOptions: any) {
const x = hasX ? normalizeValue(row.x, options.xAxis.type) : `Slice ${index}`;
const y = cleanNumber(row.y);

if (x in labelsValuesDict){
labelsValuesDict[x] += y;
if (labelsValuesMap.has(x)) {
labelsValuesMap.set(x, labelsValuesMap.get(x) + y);
} else {
labelsValuesMap.set(x, y);
}
else{
labelsValuesDict[x] = y;
}
const aggregatedY = labelsValuesDict[x];
const aggregatedY = labelsValuesMap.get(x);


sourceData.set(x, {
x,
Expand All @@ -77,8 +75,8 @@ function prepareSeries(series: any, options: any, additionalOptions: any) {
const markerColors = map(Array.from(sourceData.values()), data => getValueColor(data.row.x));
const textColors = map(markerColors, c => chooseTextColorForBackground(c));

const labels = Object.keys(labelsValuesDict);
const values = Object.values(labelsValuesDict);
const labels = Array.from(labelsValuesMap.keys());
const values = Array.from(labelsValuesMap.values());

return {
visible: true,
Expand Down

0 comments on commit 4cb32fc

Please sign in to comment.