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 chart labels using Map() implementation #7022

Merged
merged 2 commits into from
Jun 18, 2024
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
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
Loading