2
0

switched from 2d to svg

This commit is contained in:
2018-05-10 22:39:22 +02:00
parent e4352d9579
commit 8b01fc6872
2 changed files with 38 additions and 4 deletions

View File

@@ -1,6 +1,5 @@
import { Component, OnInit, ChangeDetectorRef } from '@angular/core'; import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { Config } from './models/config.model'; import { Config } from './models/config.model';
import * as d3 from 'd3';
@Component({ @Component({
selector: 'app-root', selector: 'app-root',

View File

@@ -1,18 +1,26 @@
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 { Config } from './../models/config.model'; import { Config } from './../models/config.model';
import * as d3 from 'd3';
@Directive({ @Directive({
selector: '[appCanvas]' selector: '[appCanvas]'
}) })
export class CanvasDirective implements OnInit { export class CanvasDirective implements OnInit {
private canvas: any; private canvas: any;
private context: any; private svg: any;
public config: Config; public config: Config;
@Output() emitConfig: EventEmitter<Config>; @Output() emitConfig: EventEmitter<Config>;
@HostListener('window:resize', ['$event']) @HostListener('window:resize', ['$event'])
private onResize(event) { private onResize(event) {
this.updateConfig(); this.updateConfig();
if (this.svg) {
this.updateSvg();
} else {
this.initSvg();
}
} }
constructor( constructor(
@@ -20,12 +28,39 @@ export class CanvasDirective implements OnInit {
private renderer: Renderer private renderer: Renderer
) { ) {
this.canvas = el.nativeElement; this.canvas = el.nativeElement;
this.context = this.canvas.getContext('2d');
this.emitConfig = new EventEmitter(); this.emitConfig = new EventEmitter();
} }
ngOnInit() { ngOnInit() {
this.updateConfig(); this.updateConfig();
this.initSvg();
this.render();
}
private initSvg() {
this.svg = d3.select(this.canvas).append('svg')
.attr('width', this.config.width)
.attr('height', this.config.height);
}
private updateSvg() {
this.svg
.attr('width', this.config.width)
.attr('height', this.config.height);
}
private render() {
this.drawLine([
this.config.start,
this.config.end
]);
}
private drawLine(points: Point[]) {
// d3.line()
// .x(function(d) { return x(d.date); })
// .y(function(d) { return y(d.value); })
// .curve(d3.curveCatmullRom.alpha(0.5));
} }
private updateConfig(): void { private updateConfig(): void {
@@ -41,7 +76,7 @@ export class CanvasDirective implements OnInit {
y: this.canvas.clientHeight y: this.canvas.clientHeight
}, },
}; };
// Emit Config to parent Component // Emit Canvas Config to parent Component
this.emitConfig.next(this.config); this.emitConfig.next(this.config);
} }
} }