Skip to content

Commit

Permalink
fixed DataLayerColorService overrides callback functions
Browse files Browse the repository at this point in the history
  • Loading branch information
semicolin committed Mar 18, 2024
1 parent 95c2080 commit 0e7391b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ describe('DataLayerColorService', () => {
service.chooseColorsFromPalette(layer);
expect(layer.annotations[0].backgroundColor).toEqual('rgba(0, 0, 0, 0.2)');
}));

it('should ignore datasets that use a callback function for borderColor', inject([DataLayerColorService], (service: DataLayerColorService) => {
const borderColor = () => '#000000';
const layer: any = {
datasets: [{ label: 'One', borderColor }],
};
service.chooseColorsFromPalette(layer);
expect(layer.datasets[0].borderColor).toBe(borderColor);
}));
it('should ignore datasets that use a callback function for pointBackgroundColor', inject([DataLayerColorService], (service: DataLayerColorService) => {
const pointBackgroundColor = () => '#000000';
const layer: any = {
datasets: [{ label: 'One', pointBackgroundColor }],
};
service.chooseColorsFromPalette(layer);
expect(layer.datasets[0].pointBackgroundColor).toBe(pointBackgroundColor);
}));
});

describe('addTransparency', () => {
Expand All @@ -93,7 +110,7 @@ describe('DataLayerColorService', () => {
});

describe('getColor', () => {
it('should get correct annotation color by calling getColor', inject([DataLayerColorService], (service: DataLayerColorService) => {
it('should get the borderColor of a dataset', inject([DataLayerColorService], (service: DataLayerColorService) => {
const dataset: any = {
label: 'Diastolic Blood Pressure',
yAxisID: 'mm[Hg]',
Expand All @@ -105,7 +122,7 @@ describe('DataLayerColorService', () => {
});

describe('setColor', () => {
it('should setColor function called', inject([DataLayerColorService], (service: DataLayerColorService) => {
it('should set the borderColor of a dataset', inject([DataLayerColorService], (service: DataLayerColorService) => {
const dataset: any = { borderColor: '#e41a1c' };
service.setColor(dataset, '#e41a1c');
expect(dataset.borderColor).toEqual('#e41a1c');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class DataLayerColorService {
/** Chooses colors for all of the datasets and annotations in the Layer by cycling through the palette */
chooseColorsFromPalette(layer: DataLayer): void {
for (let dataset of layer.datasets) {
if (!this.getColor(dataset)) {
if (!this.hasColor(dataset)) {
const colorIndex = this.getMatchingDatasetColorIndex(layer, dataset) ?? this.getNextPaletteIndex();
const palette = this.getPalette(dataset);
const color = palette[colorIndex];
Expand Down Expand Up @@ -127,6 +127,11 @@ export class DataLayerColorService {
return undefined;
}

hasColor(dataset: Dataset) {
const line = dataset as Dataset<'line'>;
return line.borderColor || line.backgroundColor || line.pointBorderColor || line.pointBackgroundColor;
}

/** build a CSS linear gradient that includes colors from all datasets in the layer */
getColorGradient(layer: DataLayer) {
const percent = (i: number) => Math.floor((100 * i) / layer.datasets.length);
Expand Down

0 comments on commit 0e7391b

Please sign in to comment.