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

curve fitting on combo chart #1907

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import { isPlatformServer } from '@angular/common';
[activeEntries]="activeEntries"
[scaleType]="scaleType"
[curve]="curve"
[tension]="tension"
[rangeFillOpacity]="rangeFillOpacity"
[hasRange]="hasRange"
[animations]="animations"
Expand Down Expand Up @@ -236,6 +237,7 @@ export class LineChartComponent extends BaseChartComponent implements OnInit {
@Input() yScaleMin: number;
@Input() yScaleMax: number;
@Input() wrapTicks = false;
@Input() tension: number;

@Output() activate: EventEmitter<any> = new EventEmitter();
@Output() deactivate: EventEmitter<any> = new EventEmitter();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Component, Input, OnChanges, SimpleChanges, ChangeDetectionStrategy } from '@angular/core';
import { area, line } from 'd3-shape';

import { area, curveCardinal, curveCatmullRom, line } from 'd3-shape';
import { id } from '../utils/id';
import { sortLinear, sortByTime, sortByDomain } from '../utils/sort';
import { ColorHelper } from '../common/color.helper';
Expand Down Expand Up @@ -69,6 +68,7 @@ export class LineSeriesComponent implements OnChanges {
@Input() colors: ColorHelper;
@Input() scaleType: ScaleType;
@Input() curve: any;
@Input() tension: number;
@Input() activeEntries: any[];
@Input() rangeFillOpacity: number;
@Input() hasRange: boolean;
Expand Down Expand Up @@ -120,7 +120,40 @@ export class LineSeriesComponent implements OnChanges {
}

getLineGenerator(): any {
return line<any>()
if (this.curve == curveCatmullRom) {
return line<any>()
.x(d => {
const label = d.name;
let value;
if (this.scaleType === ScaleType.Time) {
value = this.xScale(label);
} else if (this.scaleType === ScaleType.Linear) {
value = this.xScale(Number(label));
} else {
value = this.xScale(label);
}
return value;
})
.y(d => this.yScale(d.value))
.curve(this.curve.alpha(this.tension));
kedij777 marked this conversation as resolved.
Show resolved Hide resolved
} else if (this.curve == curveCardinal) {
return line<any>()
.x(d => {
const label = d.name;
let value;
if (this.scaleType === ScaleType.Time) {
value = this.xScale(label);
} else if (this.scaleType === ScaleType.Linear) {
value = this.xScale(Number(label));
} else {
value = this.xScale(label);
}
return value;
})
.y(d => this.yScale(d.value))
.curve(this.curve.tension(this.tension));
kedij777 marked this conversation as resolved.
Show resolved Hide resolved
} else {
return line<any>()
.x(d => {
kedij777 marked this conversation as resolved.
Show resolved Hide resolved
const label = d.name;
let value;
Expand All @@ -135,6 +168,7 @@ export class LineSeriesComponent implements OnChanges {
})
.y(d => this.yScale(d.value))
.curve(this.curve);
}
}

getRangeGenerator(): any {
Expand Down
8 changes: 8 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@
[timeline]="timeline"
[showGridLines]="showGridLines"
[curve]="curve"
[tension]="tension"
[rangeFillOpacity]="rangeFillOpacity"
[roundDomains]="roundDomains"
[tooltipDisabled]="tooltipDisabled"
Expand Down Expand Up @@ -594,6 +595,8 @@
[yAxisLabelRight]="yAxisLabelRight"
[noBarWhenZero]="noBarWhenZero"
[wrapTicks]="wrapTicks"
[curve]="curve"
[tension]="tension"
(select)="onSelect($event)"
>
</combo-chart-component>
Expand Down Expand Up @@ -1433,6 +1436,11 @@ <h3 (click)="optsVisible = !optsVisible" style="cursor: pointer">
{{ interpolationType }}
</option>
</select>
<div *ngIf="isTensionSliderRequired(curveType)">
<label for="tensionSlider">Degree of Curvature:</label>
<input id="tensionSlider" type="range" min="0" max="1" step="0.01" [(ngModel)]="tension">
<span>{{ tension }}</span>
</div>
</div>
<div *ngIf="chart.options.includes('curveClosed')">
<label>Line Interpolation</label>
Expand Down
6 changes: 5 additions & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export class AppComponent implements OnInit {
'Step After',
'Step Before'
];
tension: number = 0.5;

closedCurveType: string = 'Linear Closed';
closedCurve: any = this.curves[this.closedCurveType];
Expand Down Expand Up @@ -537,7 +538,10 @@ export class AppComponent implements OnInit {
getInterpolationType(curveType) {
return this.curves[curveType] || this.curves['default'];
}

kedij777 marked this conversation as resolved.
Show resolved Hide resolved
isTensionSliderRequired(curveType) {
return this.curves[curveType] == this.curves['Cardinal'] ||
this.curves[curveType] == this.curves['Catmull Rom'];
kedij777 marked this conversation as resolved.
Show resolved Hide resolved
}
setColorScheme(name) {
kedij777 marked this conversation as resolved.
Show resolved Hide resolved
this.selectedColorScheme = name;
this.colorScheme = this.colorSets.find(s => s.name === name);
Expand Down
5 changes: 4 additions & 1 deletion src/app/chartTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ const chartGroups = [
'timeline',
'showGridLines',
'curve',
'tension',
'rangeFillOpacity',
'roundDomains',
'tooltipDisabled',
Expand Down Expand Up @@ -701,7 +702,9 @@ const chartGroups = [
'yAxisLabel',
'showGridLines',
'roundDomains',
'tooltipDisabled'
'tooltipDisabled',
'curve',
'tension'
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
[activeEntries]="activeEntries"
[scaleType]="scaleType"
[curve]="curve"
[tension]="tension"
[rangeFillOpacity]="rangeFillOpacity"
[animations]="animations"
/>
Expand Down
1 change: 1 addition & 0 deletions src/app/custom-charts/combo-chart/combo-chart.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
})
export class ComboChartComponent extends BaseChartComponent {
@Input() curve: any = curveLinear;
@Input() tension: number;
@Input() legend = false;
@Input() legendTitle: string = 'Legend';
@Input() legendPosition: string = 'right';
Expand Down