2
0

established line drawing

This commit is contained in:
2018-05-12 13:02:06 +02:00
parent b7a59c02ce
commit 034f70c982

View File

@@ -1,7 +1,9 @@
import { Point } from './../models/point.model';
import { Directive, ElementRef, Renderer, AfterViewInit, HostListener, Output, EventEmitter, OnInit } from '@angular/core'; import { Directive, ElementRef, Renderer, AfterViewInit, HostListener, Output, EventEmitter, OnInit } from '@angular/core';
import * as Selection from 'd3-selection';
import * as Shape from 'd3-shape';
import { Config } from './../models/config.model'; import { Config } from './../models/config.model';
import { select, selectAll } from 'd3-selection'; import { Point } from './../models/point.model';
@Directive({ @Directive({
selector: '[appCanvas]' selector: '[appCanvas]'
@@ -37,29 +39,28 @@ export class CanvasDirective implements OnInit {
private initSvg() { private initSvg() {
if (!this.svg) { if (!this.svg) {
this.svg = select(this.canvas).append('svg'); this.svg = Selection.select(this.canvas).append('svg');
} }
this.svg this.svg
.attr('width', this.config.width) .attr('width', this.config.width)
.attr('height', this.config.height); .attr('height', this.config.height);
} }
private updateSvg() {
this.svg
.attr('width', this.config.width)
.attr('height', this.config.height);
}
private render() { private render() {
this.resetPoints(); this.resetPoints();
this.drawPoints([ this.drawPoints([
this.config.start, this.config.start,
this.config.end this.config.end
]); ]);
this.resetLines();
this.drawLine([
this.config.start,
this.config.end
]);
} }
private resetPoints(): void { private resetPoints(): void {
selectAll('circle').remove(); Selection.selectAll('circle').remove();
} }
private drawPoints(points: Point[]) { private drawPoints(points: Point[]) {
@@ -72,6 +73,18 @@ export class CanvasDirective implements OnInit {
}); });
} }
private drawLine(points: Point[]) {
return this.svg.append('path')
.attr('d', Shape.line().x(p => p.x).y(p => p.y)(points))
.attr('stroke', 'blue')
.attr('stroke-width', 2)
.attr('fill', 'none');
}
private resetLines(): void {
Selection.selectAll('path').remove();
}
private updateConfig(): void { private updateConfig(): void {
this.config = { this.config = {
width: this.canvas.clientWidth, width: this.canvas.clientWidth,