Skip to content

Commit

Permalink
Revert "aggregate Y column values rather than displaying last Y value (
Browse files Browse the repository at this point in the history
…getredash#6908)"

This reverts commit f097603
since it broke data labels for bar charts.

getredash#6996
  • Loading branch information
eradman committed Jun 14, 2024
1 parent 17fe69f commit 395b48c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@
"series": [
{
"visible": true,
"values": [200],
"labels": ["Slice 0"],
"values": [10, 60, 100, 30],
"labels": ["Slice 0", "Slice 0", "Slice 0", "Slice 0"],
"type": "pie",
"hole": 0.4,
"marker": {
"colors": ["#356AFF"]
"colors": ["#356AFF", "#E92828", "#3BD973", "#604FE9"]
},
"hoverinfo": "text+label",
"hover": [],
"text": ["100% (200)"],
"text": ["15% (30)", "15% (30)", "15% (30)", "15% (30)"],
"textinfo": "percent",
"textposition": "inside",
"textfont": { "color": ["#ffffff"] },
"textfont": { "color": ["#ffffff", "#ffffff", "#333333", "#ffffff"] },
"name": "a",
"direction": "counterclockwise",
"domain": { "x": [0, 0.98], "y": [0, 0.9] }
Expand Down
22 changes: 5 additions & 17 deletions viz-lib/src/visualizations/chart/plotly/prepareDefaultData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,39 +91,27 @@ 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 xValues: any = [];
const yValues: any = [];
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;
}
else{
labelsValuesDict[x] = y;
}
const aggregatedY = labelsValuesDict[x];

sourceData.set(x, {
x,
y: aggregatedY,
y,
yError,
size,
yPercent: null, // will be updated later
row,
});
xValues.push(x);
yValues.push(y);
yErrorValues.push(yError);
});

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

const plotlySeries = {
visible: true,
hoverinfo: hoverInfoPattern,
Expand Down
26 changes: 7 additions & 19 deletions viz-lib/src/visualizations/chart/plotly/preparePieData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ 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 labels: any = [];
const values: any = [];
const sourceData = new Map();
const seriesTotal = reduce(
series.data,
Expand All @@ -57,29 +55,19 @@ function prepareSeries(series: any, options: any, additionalOptions: any) {
each(series.data, row => {
const x = hasX ? normalizeValue(row.x, options.xAxis.type) : `Slice ${index}`;
const y = cleanNumber(row.y);

if (x in labelsValuesDict){
labelsValuesDict[x] += y;
}
else{
labelsValuesDict[x] = y;
}
const aggregatedY = labelsValuesDict[x];

labels.push(x);
values.push(y);
sourceData.set(x, {
x,
y: aggregatedY,
yPercent: (aggregatedY / seriesTotal) * 100,
y,
yPercent: (y / seriesTotal) * 100,
row,
});
});

const markerColors = map(Array.from(sourceData.values()), data => getValueColor(data.row.x));
const markerColors = map(series.data, row => getValueColor(row.x));
const textColors = map(markerColors, c => chooseTextColorForBackground(c));

const labels = Object.keys(labelsValuesDict);
const values = Object.values(labelsValuesDict);

return {
visible: true,
values,
Expand Down

0 comments on commit 395b48c

Please sign in to comment.