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 #7002

Closed
wants to merge 18 commits into from
Closed
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
22 changes: 11 additions & 11 deletions viz-lib/src/visualizations/chart/plotly/prepareDefaultData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,29 @@ 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!
let 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;
const xIdx = xValues.indexOf(x);

if (xIdx >= 0){
y += yValues[xIdx];
yValues[xIdx] = y;
}
else{
labelsValuesDict[x] = y;
xValues.push(x);
yValues.push(y);
}
const aggregatedY = labelsValuesDict[x];

sourceData.set(x, {
x,
y: aggregatedY,
y,
yError,
size,
yPercent: null, // will be updated later
Expand All @@ -121,8 +123,6 @@ function prepareSeries(series: any, options: any, additionalOptions: any) {
yErrorValues.push(yError);
});

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

const plotlySeries = {
visible: true,
Expand Down
27 changes: 12 additions & 15 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 @@ -56,30 +54,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);
let y = cleanNumber(row.y);
const xIdx = labels.indexOf(x)

if (x in labelsValuesDict){
labelsValuesDict[x] += y;
if (xIdx >= 0){
y += values[xIdx];
values[xIdx] = y;
}
else{
labelsValuesDict[x] = y;
labels.push(x);
values.push(y);
}
const aggregatedY = labelsValuesDict[x];

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(labels, row => getValueColor(row));
const textColors = map(markerColors, c => chooseTextColorForBackground(c));

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

return {
visible: true,
values,
Expand Down
Loading