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

feat: reference lines for vertical/horizontal bar chart and area chart #1923

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Series } from '../models/chart-data.model';
import { LegendOptions, LegendPosition } from '../common/types/legend.model';
import { ViewDimensions } from '../common/types/view-dimension.interface';
import { ScaleType } from '../common/types/scale-type.enum';
import { select } from 'd3-selection';

@Component({
selector: 'ngx-charts-area-chart',
Expand Down Expand Up @@ -75,6 +76,9 @@ import { ScaleType } from '../common/types/scale-type.enum';
[maxTickLength]="maxYAxisTickLength"
[tickFormatting]="yAxisTickFormatting"
[ticks]="yAxisTicks"
[referenceLines]="referenceLines"
[showRefLines]="showRefLines"
[showRefLabels]="showRefLabels"
[wrapTicks]="wrapTicks"
(dimensionsChanged)="updateYAxisWidth($event)"
></svg:g>
Expand Down Expand Up @@ -192,6 +196,9 @@ export class AreaChartComponent extends BaseChartComponent {
@Input() yAxisTicks: any[];
@Input() roundDomains: boolean = false;
@Input() tooltipDisabled: boolean = false;
@Input() referenceLines: any[];
@Input() showRefLines: boolean = false;
@Input() showRefLabels: boolean = false;
@Input() xScaleMin: any;
@Input() xScaleMax: any;
@Input() yScaleMin: number;
Expand Down Expand Up @@ -274,6 +281,10 @@ export class AreaChartComponent extends BaseChartComponent {

this.clipPathId = 'clip' + id().toString();
this.clipPath = `url(#${this.clipPathId})`;

const parent = select(this.chartElement.nativeElement).select('.area-chart').node() as HTMLElement;
const refLines = select(this.chartElement.nativeElement).selectAll('.ref-line').nodes() as HTMLElement[];
refLines.forEach(line => parent.appendChild(line));
}

updateTimeline(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { BaseChartComponent } from '../common/base-chart.component';
import { LegendOptions, LegendPosition } from '../common/types/legend.model';
import { ScaleType } from '../common/types/scale-type.enum';
import { ViewDimensions } from '../common/types/view-dimension.interface';
import { select } from 'd3-selection';

@Component({
selector: 'ngx-charts-bar-horizontal',
Expand Down Expand Up @@ -44,6 +45,9 @@ import { ViewDimensions } from '../common/types/view-dimension.interface';
[maxTickLength]="maxXAxisTickLength"
[tickFormatting]="xAxisTickFormatting"
[ticks]="xAxisTicks"
[referenceLines]="referenceLines"
[showRefLines]="showRefLines"
[showRefLabels]="showRefLabels"
[wrapTicks]="wrapTicks"
(dimensionsChanged)="updateXAxisHeight($event)"
></svg:g>
Expand Down Expand Up @@ -101,6 +105,9 @@ export class BarHorizontalComponent extends BaseChartComponent {
@Input() xAxisLabel: string;
@Input() yAxisLabel: string;
@Input() tooltipDisabled: boolean = false;
@Input() referenceLines: any;
@Input() showRefLines: boolean = false;
@Input() showRefLabels: boolean = false;
@Input() gradient: boolean;
@Input() showGridLines: boolean = true;
@Input() activeEntries: any[] = [];
Expand Down Expand Up @@ -175,6 +182,12 @@ export class BarHorizontalComponent extends BaseChartComponent {
this.legendOptions = this.getLegendOptions();

this.transform = `translate(${this.dims.xOffset} , ${this.margin[0]})`;

if (this.showRefLines) {
const parent = select(this.chartElement.nativeElement).select('.bar-chart').node() as HTMLElement;
const refLines = select(this.chartElement.nativeElement).selectAll('.ref-line').nodes() as HTMLElement[];
refLines.forEach(line => parent.appendChild(line));
}
}

getXScale(): any {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { DataItem } from '../models/chart-data.model';
import { LegendOptions, LegendPosition } from '../common/types/legend.model';
import { ScaleType } from '../common/types/scale-type.enum';
import { ViewDimensions } from '../common/types/view-dimension.interface';
import { select } from 'd3-selection';

@Component({
selector: 'ngx-charts-bar-vertical',
Expand Down Expand Up @@ -61,6 +62,9 @@ import { ViewDimensions } from '../common/types/view-dimension.interface';
[maxTickLength]="maxYAxisTickLength"
[tickFormatting]="yAxisTickFormatting"
[ticks]="yAxisTicks"
[referenceLines]="referenceLines"
[showRefLines]="showRefLines"
[showRefLabels]="showRefLabels"
[wrapTicks]="wrapTicks"
(dimensionsChanged)="updateYAxisWidth($event)"
></svg:g>
Expand Down Expand Up @@ -104,6 +108,9 @@ export class BarVerticalComponent extends BaseChartComponent {
@Input() yAxisLabel: string;
@Input() tooltipDisabled: boolean = false;
@Input() gradient: boolean;
@Input() referenceLines: any[];
@Input() showRefLines;
@Input() showRefLabels;
@Input() showGridLines: boolean = true;
@Input() activeEntries: any[] = [];
@Input() schemeType: ScaleType;
Expand Down Expand Up @@ -179,6 +186,12 @@ export class BarVerticalComponent extends BaseChartComponent {
this.legendOptions = this.getLegendOptions();

this.transform = `translate(${this.dims.xOffset} , ${this.margin[0] + this.dataLabelMaxHeight.negative})`;

if (this.showRefLines) {
const parent = select(this.chartElement.nativeElement).select('.bar-chart').node() as HTMLElement;
const refLines = select(this.chartElement.nativeElement).selectAll('.ref-line').nodes() as HTMLElement[];
refLines.forEach(line => parent.appendChild(line));
}
}

getXScale(): any {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { trimLabel } from '../trim-label.helper';
import { getTickLines, reduceTicks } from './ticks.helper';
import { Orientation } from '../types/orientation.enum';
import { TextAnchor } from '../types/text-anchor.enum';
import { roundedRect } from '../../common/shape.helper';

@Component({
selector: 'g[ngx-charts-x-axis-ticks]',
Expand Down Expand Up @@ -54,6 +55,30 @@ import { TextAnchor } from '../types/text-anchor.enum';
<svg:line class="gridline-path gridline-path-vertical" [attr.y1]="-gridLineHeight" y2="0" />
</svg:g>
</svg:g>

<svg:path
*ngIf="referenceLineLength > 1 && refMax && refMin && showRefLines"
class="reference-area"
[attr.d]="referenceAreaPath"
[attr.transform]="gridLineTransform()"
/>

<svg:g *ngFor="let refLine of referenceLines" class="ref-line">
<svg:g *ngIf="showRefLines" [attr.transform]="transform(refLine.value)">
<svg:line
class="refline-path gridline-path-vertical"
y1="25"
[attr.y2]="25 + gridLineHeight"
[attr.transform]="gridLineTransform()"
/>
<svg:g *ngIf="showRefLabels">
<title>{{ tickTrim(tickFormat(refLine.value)) }}</title>
<svg:text class="refline-label" transform="rotate(-90)" [attr.text-anchor]="textAnchor" [attr.x]="30">
{{ refLine.name }}
</svg:text>
</svg:g>
</svg:g>
</svg:g>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
Expand All @@ -71,6 +96,9 @@ export class XAxisTicksComponent implements OnChanges, AfterViewInit {
@Input() width: number;
@Input() rotateTicks: boolean = true;
@Input() wrapTicks = false;
@Input() referenceLines: any[];
@Input() showRefLabels: boolean = false;
@Input() showRefLines: boolean = false;

@Output() dimensionsChanged = new EventEmitter();

Expand All @@ -89,6 +117,17 @@ export class XAxisTicksComponent implements OnChanges, AfterViewInit {
height: number = 0;
approxHeight: number = 10;
maxPossibleLengthForTickIfWrapped = 16;
transform: (o: any) => string;
refMax: number;
refMin: number;
referenceLineLength: number = 0;
referenceAreaPath: string;
dx: string;
x1: number;
x2: number;
y1: number;
y2: number;
tickSpacing: number;

@ViewChild('ticksel') ticksElement: ElementRef;

Expand Down Expand Up @@ -123,7 +162,55 @@ export class XAxisTicksComponent implements OnChanges, AfterViewInit {

update(): void {
const scale = this.scale;
this.adjustedScale = this.scale.bandwidth
? function (d) {
return this.scale(d) + this.scale.bandwidth() * 0.5;
}
: this.scale;
this.ticks = this.getTicks();
const sign = this.orient === Orientation.Top || this.orient === Orientation.Right ? -1 : 1;
this.tickSpacing = Math.max(this.innerTickSize, 0) + this.tickPadding;

switch (this.orient) {
case Orientation.Bottom:
this.transform = function (tick) {
return 'translate(' + this.adjustedScale(tick) + ',0)';
};
this.textAnchor = TextAnchor.Middle;
this.x2 = this.innerTickSize * sign;
this.x1 = this.tickSpacing * sign;
this.dx = sign < 0 ? '0em' : '.71em';
break;
case Orientation.Left:
this.transform = function (tick) {
return 'translate(0,' + this.adjustedScale(tick) + ')';
};
this.textAnchor = TextAnchor.End;
console.log('ANCHOR' + this.textAnchor);
this.y2 = this.innerTickSize * -sign;
this.y1 = this.tickSpacing * -sign;
this.dx = '.32em';
break;
case Orientation.Top:
this.transform = function (tick) {
return 'translate(' + this.adjustedScale(tick) + ',0)';
};
this.textAnchor = TextAnchor.Middle;
this.y2 = this.innerTickSize * sign;
this.y1 = this.tickSpacing * sign;
this.dx = sign < 0 ? '0em' : '.71em';
break;
case Orientation.Right:
this.transform = function (tick) {
return 'translate(0,' + this.adjustedScale(tick) + ')';
};
this.textAnchor = TextAnchor.Start;
this.x2 = this.innerTickSize * -sign;
this.x1 = this.tickSpacing * -sign;
this.dx = '.32em';
break;
default:
}

if (this.tickFormatting) {
this.tickFormat = this.tickFormatting;
Expand All @@ -141,12 +228,6 @@ export class XAxisTicksComponent implements OnChanges, AfterViewInit {

const angle = this.rotateTicks ? this.getRotationAngle(this.ticks) : null;

this.adjustedScale = this.scale.bandwidth
? function (d) {
return this.scale(d) + this.scale.bandwidth() * 0.5;
}
: this.scale;

this.textTransform = '';
if (angle && angle !== 0) {
this.textTransform = `rotate(${angle})`;
Expand All @@ -159,6 +240,31 @@ export class XAxisTicksComponent implements OnChanges, AfterViewInit {
setTimeout(() => this.updateDims());
}

setReferencelines(): void {
this.refMin = this.adjustedScale(
Math.min.apply(
null,
this.referenceLines.map(item => item.value)
)
);
this.refMax = this.adjustedScale(
Math.max.apply(
null,
this.referenceLines.map(item => item.value)
)
);
this.referenceLineLength = this.referenceLines.length;

this.referenceAreaPath = roundedRect(
this.refMax,
-this.gridLineHeight + 25,
this.refMin - this.refMax,
this.gridLineHeight,
0,
[false, false, false, false]
);
}

getRotationAngle(ticks: any[]): number {
let angle = 0;
this.maxTicksLength = 0;
Expand Down Expand Up @@ -207,6 +313,10 @@ export class XAxisTicksComponent implements OnChanges, AfterViewInit {

this.approxHeight = Math.min(requiredHeight, 200);

if (this.showRefLines && this.referenceLines) {
this.setReferencelines();
}

return angle;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import { ViewDimensions } from '../types/view-dimension.interface';
[orient]="xOrient"
[showGridLines]="showGridLines"
[gridLineHeight]="dims.height"
[referenceLines]="referenceLines"
[showRefLines]="showRefLines"
[showRefLabels]="showRefLabels"
[width]="dims.width"
[tickValues]="ticks"
[wrapTicks]="wrapTicks"
Expand Down Expand Up @@ -61,6 +64,9 @@ export class XAxisComponent implements OnChanges {
@Input() ticks: any[];
@Input() xAxisTickCount: number;
@Input() xOrient: Orientation = Orientation.Bottom;
@Input() referenceLines: any[];
@Input() showRefLines: boolean;
@Input() showRefLabels: boolean;
@Input() xAxisOffset: number = 0;
@Input() wrapTicks = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,9 @@ import { TextAnchor } from '../types/text-anchor.enum';
</svg:g>
</svg:g>

<svg:g *ngFor="let refLine of referenceLines">
<svg:g *ngFor="let refLine of referenceLines" class="ref-line">
<svg:g *ngIf="showRefLines" [attr.transform]="transform(refLine.value)">
<svg:line
class="refline-path gridline-path-horizontal"
x1="0"
[attr.x2]="gridLineWidth"
[attr.transform]="gridLineTransform()"
/>
<svg:line class="refline-path gridline-path-horizontal" x1="0" [attr.x2]="gridLineWidth" />
<svg:g *ngIf="showRefLabels">
<title>{{ tickTrim(tickFormat(refLine.value)) }}</title>
<svg:text
Expand Down
9 changes: 9 additions & 0 deletions src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
[showXAxisLabel]="showXAxisLabel"
[showYAxisLabel]="showYAxisLabel"
[tooltipDisabled]="tooltipDisabled"
[referenceLines]="refLines"
[showRefLines]="showRefLines"
[showRefLabels]="showRefLabels"
[xAxisLabel]="xAxisLabel"
[yAxisLabel]="yAxisLabel"
[showGridLines]="showGridLines"
Expand Down Expand Up @@ -56,6 +59,9 @@
[showXAxisLabel]="showXAxisLabel"
[showYAxisLabel]="showYAxisLabel"
[tooltipDisabled]="tooltipDisabled"
[referenceLines]="refLines"
[showRefLines]="showRefLines"
[showRefLabels]="showRefLabels"
[xAxisLabel]="xAxisLabel"
[yAxisLabel]="yAxisLabel"
[showGridLines]="showGridLines"
Expand Down Expand Up @@ -483,6 +489,9 @@
[roundDomains]="roundDomains"
[curve]="curve"
[tooltipDisabled]="tooltipDisabled"
[referenceLines]="refLinesArea"
[showRefLines]="showRefLines"
[showRefLabels]="showRefLabels"
[trimXAxisTicks]="trimXAxisTicks"
[trimYAxisTicks]="trimYAxisTicks"
[rotateXAxisTicks]="rotateXAxisTicks"
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 @@ -259,7 +259,11 @@ export class AppComponent implements OnInit {
{ value: 37750, name: 'Average' },
{ value: 33000, name: 'Minimum' }
];

refLinesArea = [
{ value: 5062, name: 'Maximum' },
{ value: 4030, name: 'Average' },
{ value: 3000, name: 'Minimum' }
];
// data
plotData: any;

Expand Down
Loading