diff --git a/viz-lib/src/visualizations/chart/plotly/fixtures/prepareData/pie/without-x.json b/viz-lib/src/visualizations/chart/plotly/fixtures/prepareData/pie/without-x.json index 2b09b10e15..e0fb71e5ae 100644 --- a/viz-lib/src/visualizations/chart/plotly/fixtures/prepareData/pie/without-x.json +++ b/viz-lib/src/visualizations/chart/plotly/fixtures/prepareData/pie/without-x.json @@ -28,19 +28,19 @@ "series": [ { "visible": true, - "values": [10, 60, 100, 30], - "labels": ["Slice 0", "Slice 0", "Slice 0", "Slice 0"], + "values": [200], + "labels": ["Slice 0"], "type": "pie", "hole": 0.4, "marker": { - "colors": ["#356AFF", "#E92828", "#3BD973", "#604FE9"] + "colors": ["#356AFF"] }, "hoverinfo": "text+label", "hover": [], - "text": ["15% (30)", "15% (30)", "15% (30)", "15% (30)"], + "text": ["100% (200)"], "textinfo": "percent", "textposition": "inside", - "textfont": { "color": ["#ffffff", "#ffffff", "#333333", "#ffffff"] }, + "textfont": { "color": ["#ffffff"] }, "name": "a", "direction": "counterclockwise", "domain": { "x": [0, 0.98], "y": [0, 0.9] } diff --git a/viz-lib/src/visualizations/chart/plotly/prepareDefaultData.ts b/viz-lib/src/visualizations/chart/plotly/prepareDefaultData.ts index c5102309f4..c925cebdcb 100644 --- a/viz-lib/src/visualizations/chart/plotly/prepareDefaultData.ts +++ b/viz-lib/src/visualizations/chart/plotly/prepareDefaultData.ts @@ -91,27 +91,39 @@ function prepareSeries(series: any, options: any, additionalOptions: any) { }; const sourceData = new Map(); - const xValues: any = []; - const yValues: any = []; + + //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 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, + y: aggregatedY, 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, diff --git a/viz-lib/src/visualizations/chart/plotly/preparePieData.ts b/viz-lib/src/visualizations/chart/plotly/preparePieData.ts index 80101f0729..2816e5ddb2 100644 --- a/viz-lib/src/visualizations/chart/plotly/preparePieData.ts +++ b/viz-lib/src/visualizations/chart/plotly/preparePieData.ts @@ -41,8 +41,10 @@ function prepareSeries(series: any, options: any, additionalOptions: any) { const xPosition = (index % cellsInRow) * cellWidth; const yPosition = Math.floor(index / cellsInRow) * cellHeight; - const labels: any = []; - const values: any = []; + //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 sourceData = new Map(); const seriesTotal = reduce( series.data, @@ -55,19 +57,29 @@ 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); - labels.push(x); - values.push(y); + + if (x in labelsValuesDict){ + labelsValuesDict[x] += y; + } + else{ + labelsValuesDict[x] = y; + } + const aggregatedY = labelsValuesDict[x]; + sourceData.set(x, { x, - y, - yPercent: (y / seriesTotal) * 100, + y: aggregatedY, + yPercent: (aggregatedY / seriesTotal) * 100, row, }); }); - const markerColors = map(series.data, row => getValueColor(row.x)); + 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); + return { visible: true, values,