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: repeated color in the same chart #23762

Merged
merged 10 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ describe('Dashboard edit', () => {
'[data-test-chart-name="Top 10 California Names Timeseries"] .line .nv-legend-symbol',
)
.first()
.should('have.css', 'fill', 'rgb(31, 168, 201)');
.should('have.css', 'fill', 'rgb(69, 78, 124)');
});

it('should apply same color to same labels with color scheme set', () => {
Expand All @@ -230,7 +230,7 @@ describe('Dashboard edit', () => {
'[data-test-chart-name="Top 10 California Names Timeseries"] .line .nv-legend-symbol',
)
.first()
.should('have.css', 'fill', 'rgb(234, 11, 140)');
.should('have.css', 'fill', 'rgb(51, 217, 193)');

// open 2nd main tab
openTab(0, 1);
Expand All @@ -239,7 +239,7 @@ describe('Dashboard edit', () => {
// label Anthony
cy.get('[data-test-chart-name="Trends"] .line .nv-legend-symbol')
.eq(2)
.should('have.css', 'fill', 'rgb(234, 11, 140)');
.should('have.css', 'fill', 'rgb(51, 217, 193)');
});

it('should apply same color to same labels with no color scheme set', () => {
Expand All @@ -260,7 +260,7 @@ describe('Dashboard edit', () => {
'[data-test-chart-name="Top 10 California Names Timeseries"] .line .nv-legend-symbol',
)
.first()
.should('have.css', 'fill', 'rgb(31, 168, 201)');
.should('have.css', 'fill', 'rgb(69, 78, 124)');

// open 2nd main tab
openTab(0, 1);
Expand All @@ -269,7 +269,7 @@ describe('Dashboard edit', () => {
// label Anthony
cy.get('[data-test-chart-name="Trends"] .line .nv-legend-symbol')
.eq(2)
.should('have.css', 'fill', 'rgb(31, 168, 201)');
.should('have.css', 'fill', 'rgb(69, 78, 124)');
});

it('custom label colors should take the precedence in nested tabs', () => {
Expand Down Expand Up @@ -359,17 +359,17 @@ describe('Dashboard edit', () => {
'[data-test-chart-name="Top 10 California Names Timeseries"] .line .nv-legend-symbol',
)
.first()
.should('have.css', 'fill', 'rgb(234, 11, 140)');
.should('have.css', 'fill', 'rgb(255, 0, 0)');
cy.get(
'[data-test-chart-name="Top 10 California Names Timeseries"] .line .nv-legend-symbol',
)
.eq(1)
.should('have.css', 'fill', 'rgb(108, 131, 142)');
.should('have.css', 'fill', 'rgb(157, 172, 185)');
cy.get(
'[data-test-chart-name="Top 10 California Names Timeseries"] .line .nv-legend-symbol',
)
.eq(2)
.should('have.css', 'fill', 'rgb(41, 171, 226)');
.should('have.css', 'fill', 'rgb(45, 85, 132)');
});

it('should re-apply original color after removing custom label color with no color scheme set', () => {
Expand All @@ -383,17 +383,17 @@ describe('Dashboard edit', () => {
'[data-test-chart-name="Top 10 California Names Timeseries"] .line .nv-legend-symbol',
)
.first()
.should('have.css', 'fill', 'rgb(31, 168, 201)');
.should('have.css', 'fill', 'rgb(69, 78, 124)');
cy.get(
'[data-test-chart-name="Top 10 California Names Timeseries"] .line .nv-legend-symbol',
)
.eq(1)
.should('have.css', 'fill', 'rgb(69, 78, 124)');
.should('have.css', 'fill', 'rgb(224, 67, 85)');
cy.get(
'[data-test-chart-name="Top 10 California Names Timeseries"] .line .nv-legend-symbol',
)
.eq(2)
.should('have.css', 'fill', 'rgb(90, 193, 137)');
.should('have.css', 'fill', 'rgb(168, 104, 183)');

openProperties();
cy.get('[aria-label="Select color scheme"]').should('have.value', '');
Expand Down Expand Up @@ -463,7 +463,7 @@ describe('Dashboard edit', () => {
'[data-test-chart-name="Top 10 California Names Timeseries"] .line .nv-legend-symbol',
)
.eq(1)
.should('have.css', 'fill', 'rgb(108, 131, 142)');
.should('have.css', 'fill', 'rgb(157, 172, 185)');

openExplore('Top 10 California Names Timeseries');

Expand Down Expand Up @@ -495,7 +495,7 @@ describe('Dashboard edit', () => {
'[data-test-chart-name="Top 10 California Names Timeseries"] .line .nv-legend-symbol',
)
.first()
.should('have.css', 'fill', 'rgb(234, 11, 140)');
.should('have.css', 'fill', 'rgb(51, 217, 193)');

// open 2nd main tab
openTab(0, 1);
Expand All @@ -504,7 +504,7 @@ describe('Dashboard edit', () => {
// label Anthony
cy.get('[data-test-chart-name="Trends"] .line .nv-legend-symbol')
.eq(2)
.should('have.css', 'fill', 'rgb(234, 11, 140)');
.should('have.css', 'fill', 'rgb(51, 217, 193)');

editDashboard();
openProperties();
Expand Down Expand Up @@ -569,7 +569,7 @@ describe('Dashboard edit', () => {

cy.get('[data-test-chart-name="Trends"] .line .nv-legend-symbol')
.first()
.should('have.css', 'fill', 'rgb(255, 90, 95)');
.should('have.css', 'fill', 'rgb(140, 224, 113)');

// go back to first tab
openTab(0, 0);
Expand Down Expand Up @@ -616,7 +616,7 @@ describe('Dashboard edit', () => {
'[data-test-chart-name="Top 10 California Names Timeseries"] .line .nv-legend-symbol',
)
.first()
.should('have.css', 'fill', 'rgb(234, 11, 140)');
.should('have.css', 'fill', 'rgb(51, 217, 193)');

// open another nested tab
openTab(2, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ class CategoricalColorScale extends ExtensibleFunction {
getColor(value?: string, sliceId?: number) {
const cleanedValue = stringifyAndTrim(value);
const sharedLabelColor = getSharedLabelColor();
const sharedColorMap = sharedLabelColor.getColorMap();
const sharedColor = sharedColorMap.get(cleanedValue);

// priority: parentForcedColors > forcedColors > labelColors
let color =
this.parentForcedColors?.[cleanedValue] ||
this.forcedColors?.[cleanedValue] ||
sharedLabelColor.getColorMap().get(cleanedValue);
sharedColor;

if (isFeatureEnabled(FeatureFlag.USE_ANALAGOUS_COLORS)) {
const multiple = Math.floor(
Expand All @@ -96,7 +98,22 @@ class CategoricalColorScale extends ExtensibleFunction {
const newColor = this.scale(cleanedValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is moving the scale to the next color in order

if (!color) {
color = newColor;
// make sure we don't overwrite the origin colors
if (!isFeatureEnabled(FeatureFlag.USE_ANALAGOUS_COLORS)) {
const updatedRange = [...this.originColors];
// remove the color option from shared color
sharedColorMap.forEach((value, key) => {
if (key !== cleanedValue) {
const index = updatedRange.indexOf(value);
updatedRange.splice(index, 1);
Copy link
Member

@eschutho eschutho May 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this may be removing items from a list. Can you use the filter function here instead?

}
});

this.range(updatedRange.length > 0 ? updatedRange : this.originColors);
color = this.scale(cleanedValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling this again will move to the next color in the scale again unnecessarily

}
}

sharedLabelColor.addSlice(cleanedValue, color, sliceId);

return color;
Expand Down