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

Custom colors for linear-scaled charts and fix incorrect maximum gradient stop bug #1911

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
55 changes: 36 additions & 19 deletions projects/swimlane/ngx-charts/src/lib/common/color.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ export class ColorHelper {
return cs.name === scheme;
});
}
this.colorDomain = scheme.domain;

this.customColors = customColors;

if (this.customColors) {
this.colorDomain = this.customColors;
}
else {
this.colorDomain = scheme.domain;
}

this.scaleType = type;
this.domain = domain;
this.customColors = customColors;

this.scale = this.generateColorScheme(scheme, type, this.domain);
}
Expand All @@ -56,16 +64,25 @@ export class ColorHelper {
break;
case ScaleType.Linear:
{
const colorDomain = [...scheme.domain];
if (colorDomain.length === 1) {
colorDomain.push(colorDomain[0]);
this.colorDomain = colorDomain;
if (this.customColors) {
const colorDomain = [...this.customColors];
colorScale = scaleLinear()
.range(colorDomain as any)
.domain(range(0, 1, 0.9999 / (colorDomain.length - 1)));
}
else {
const colorDomain = [...scheme.domain];

if (colorDomain.length === 1) {
colorDomain.push(colorDomain[0]);
this.colorDomain = colorDomain;
}

const points = range(0, 1, 0.9999 / (colorDomain.length - 1));
colorScale = scaleLinear()
.range(colorDomain as any)
.domain(points);
}

const points = range(0, 1, 1.0 / colorDomain.length);
colorScale = scaleLinear()
.range(colorDomain as any)
.domain(points);
}
break;
default:
Expand Down Expand Up @@ -114,7 +131,7 @@ export class ColorHelper {
.domain(this.domain as number[])
.range([0, 1]);

const colorValueScale = scaleBand().domain(this.colorDomain).range([0, 1]);
const colorValueScale = scaleBand().domain(this.colorDomain.slice(1)).range([0, 1]);

const endColor = this.getColor(value);

Expand All @@ -134,24 +151,24 @@ export class ColorHelper {
opacity: 1
});

while (currentVal < endVal && i < this.colorDomain.length) {
while (i < this.colorDomain.length) {
const color = this.colorDomain[i];
const offset = colorValueScale(color);
const offset = colorValueScale(color) + colorValueScale.bandwidth();

if (offset >= endVal) {
break;
}

if (offset <= startVal) {
i++;
continue;
}

if (offset.toFixed(4) >= (endVal - colorValueScale.bandwidth()).toFixed(4)) {
break;
}

stops.push({
color,
offset,
opacity: 1
});
currentVal = offset;
i++;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export class HeatMapComponent extends BaseChartComponent {
}

setColors(): void {
this.colors = new ColorHelper(this.scheme, this.scaleType, this.valueDomain);
this.colors = new ColorHelper(this.scheme, this.scaleType, this.valueDomain, this.customColors);
}

getLegendOptions(): LegendOptions {
Expand Down
44 changes: 42 additions & 2 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
[view]="view"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="single"
[animations]="animations"
[gradient]="gradient"
Expand Down Expand Up @@ -82,6 +83,7 @@
[view]="view"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="multi"
[animations]="animations"
[gradient]="gradient"
Expand Down Expand Up @@ -120,6 +122,7 @@
[view]="view"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="multi"
[animations]="animations"
[gradient]="gradient"
Expand Down Expand Up @@ -158,6 +161,7 @@
[view]="view"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="multi"
[animations]="animations"
[gradient]="gradient"
Expand Down Expand Up @@ -194,6 +198,7 @@
[view]="view"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="multi"
[animations]="animations"
[gradient]="gradient"
Expand Down Expand Up @@ -230,6 +235,7 @@
[view]="view"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="multi"
[animations]="animations"
[gradient]="gradient"
Expand Down Expand Up @@ -264,6 +270,7 @@
[view]="view"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="multi"
[animations]="animations"
[gradient]="gradient"
Expand Down Expand Up @@ -297,6 +304,7 @@
class="chart-container"
[view]="view"
[scheme]="colorScheme"
[customColors]="customColorsNonlinear"
[results]="single"
[animations]="animations"
[legend]="showLegend"
Expand All @@ -320,6 +328,7 @@
class="chart-container"
[view]="view"
[scheme]="colorScheme"
[customColors]="customColorsNonlinear"
[results]="single"
[valueFormatting]="valueFormatting"
[animations]="animations"
Expand All @@ -336,6 +345,7 @@
class="chart-container"
[view]="view"
[scheme]="colorScheme"
[customColors]="customColorsNonlinear"
[results]="single"
[animations]="animations"
[tooltipDisabled]="tooltipDisabled"
Expand All @@ -351,6 +361,7 @@
class="chart-container"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="dateDataWithOrWithoutRange"
[animations]="animations"
[legend]="showLegend"
Expand Down Expand Up @@ -391,6 +402,7 @@
class="chart-container"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="multi"
[animations]="animations"
[legend]="showLegend"
Expand Down Expand Up @@ -440,6 +452,7 @@
[yScaleMax]="yScaleMax"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[roundDomains]="roundDomains"
[tooltipDisabled]="tooltipDisabled"
[minRadius]="minRadius"
Expand All @@ -461,6 +474,7 @@
[view]="view"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="dateData"
[animations]="animations"
[legend]="showLegend"
Expand Down Expand Up @@ -500,6 +514,7 @@
[view]="view"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="dateData"
[animations]="animations"
[legend]="showLegend"
Expand Down Expand Up @@ -538,6 +553,7 @@
[view]="view"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="dateData"
[animations]="animations"
[legend]="showLegend"
Expand Down Expand Up @@ -622,6 +638,7 @@
[maxXAxisTickLength]="maxXAxisTickLength"
[maxYAxisTickLength]="maxYAxisTickLength"
[wrapTicks]="wrapTicks"
[customColors]="customColorsLinear"
(select)="select($event)"
(activate)="activate($event)"
(deactivate)="deactivate($event)"
Expand Down Expand Up @@ -650,6 +667,7 @@
[maxXAxisTickLength]="maxXAxisTickLength"
[maxYAxisTickLength]="maxYAxisTickLength"
[wrapTicks]="wrapTicks"
[customColors]="customColorsLinear"
(select)="select($event)"
(activate)="activate($event)"
(deactivate)="deactivate($event)"
Expand All @@ -666,6 +684,7 @@
[labelFormatting]="gdpLabelFormatting"
[valueFormatting]="currencyFormatting"
[gradient]="gradient"
[customColors]="customColorsNonlinear"
(select)="select($event)"
>
</ngx-charts-tree-map>
Expand All @@ -678,6 +697,7 @@
emptyColor="#1e222e"
[results]="single"
[animations]="animations"
[customColors]="customColorsNonlinear"
(select)="select($event)"
>
</ngx-charts-number-card>
Expand Down Expand Up @@ -738,6 +758,7 @@
class="chart-container"
[view]="view"
[scheme]="colorScheme"
[customColors]="customColorsNonlinear"
cardColor="#232837"
emptyColor="#1e222e"
[results]="statusData"
Expand Down Expand Up @@ -769,6 +790,7 @@
[margin]="margin ? [marginTop, marginRight, marginBottom, marginLeft] : null"
[tooltipDisabled]="tooltipDisabled"
[showText]="showText"
[customColors]="customColorsNonlinear"
(select)="select($event)"
(activate)="activate($event)"
(deactivate)="deactivate($event)"
Expand All @@ -785,6 +807,7 @@
[value]="gaugeValue"
[previousValue]="gaugePreviousValue"
[units]="gaugeUnits"
[customColors]="customColorsNonlinear"
(select)="select($event)"
>
</ngx-charts-linear-gauge>
Expand All @@ -802,6 +825,7 @@
class="chart-container"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="plotData"
[animations]="animations"
[legend]="false"
Expand Down Expand Up @@ -835,6 +859,7 @@
class="chart-container"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[results]="plotData"
[animations]="animations"
[legend]="false"
Expand Down Expand Up @@ -886,6 +911,7 @@ <h4 style="text-align: center">
[animations]="animations"
[tooltipDisabled]="tooltipDisabled"
[gradient]="gradient"
[customColors]="customColorsNonlinear"
(select)="treemapSelect($event)"
>
</ngx-charts-tree-map>
Expand Down Expand Up @@ -922,6 +948,7 @@ <h4 style="text-align: center">
[autoScale]="autoScale"
[scheme]="colorScheme"
[schemeType]="schemeType"
[customColors]="customColors"
[roundDomains]="roundDomains"
[tooltipDisabled]="tooltipDisabled"
[minRadius]="minRadius"
Expand Down Expand Up @@ -1181,7 +1208,7 @@ <h3 (click)="colorVisible = !colorVisible" style="cursor: pointer">
<strong>Color Scheme</strong>
</h3>
<select
[hidden]="!colorVisible"
[hidden]="!colorVisible || (chart.options.includes('customColors') && useCustomColors && (schemeType === linearScaleType || chartType === 'heat-map' || chartType === 'calendar'))"
style="margin-left: 10px"
[ngModel]="selectedColorScheme"
(ngModelChange)="setColorScheme($event)"
Expand All @@ -1194,12 +1221,25 @@ <h3 (click)="colorVisible = !colorVisible" style="cursor: pointer">
[hidden]="!colorVisible"
style="margin-left: 10px"
[ngModel]="schemeType"
(ngModelChange)="schemeType = $event"
(ngModelChange)="setSchemeType($event)"
>
<option value="ordinal">Ordinal</option>
<option value="linear">Linear</option>
</select>

<div [hidden]="!colorVisible" style="margin-left: 10px">
<div *ngIf="chart.options.includes('customColors') && (schemeType === linearScaleType || chartType === 'heat-map' || chartType === 'calendar')">
<label>
<input type="checkbox" [(ngModel)]="useCustomColors" (ngModelChange)="resetCustomColor()"/>
Custom Colors
</label>
<br />
</div>
<div *ngIf="chart.options.includes('customColors') && useCustomColors && (schemeType === linearScaleType || chartType === 'heat-map' || chartType === 'calendar')">
<label>Gradient Color Stops:</label><input type="text" [(ngModel)]="gradientStops" (ngModelChange)="setCustomColor()"/>
</div>
</div>

<div [hidden]="!colorVisible || !range" style="margin-left: 10px">
<div>
<label>Range fill color opacity (0.0 - 1.0):</label><br />
Expand Down
Loading