/
text-element.ts
60 lines (48 loc) · 1.61 KB
/
text-element.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import * as d3 from 'd3-selection';
@Component({
selector: 'page-text-element',
templateUrl: 'text-element.html',
})
export class TextElementPage {
constructor(public navCtrl: NavController, public navParams: NavParams) {
}
ionViewDidLoad() {
this.createSVG();
}
createSVG(): void {
// Data Set
const circleData = [
{ cx: 20, cy: 20, radius: 20, color : 'green' },
{ cx: 70, cy: 70, radius: 20, color : 'purple' }];
// SVG Viewport
const svgContainer = d3.select('#svg-text').append('svg')
.attr('width', 200)
.attr('height', 200);
// Add circles to the svgContainer
const circles = svgContainer.selectAll('circle')
.data(circleData)
.enter()
.append('circle');
// Add the circle attributes
circles
.attr('cx', (d) => d.cx)
.attr('cy', (d) => d.cy)
.attr('r', (d) => d.radius)
.style('fill', (d) => d.color);
// Add the SVG Text Element to the svgContainer
const text = svgContainer.selectAll('text')
.data(circleData)
.enter()
.append('text');
// Add the text attributes
text
.attr('x', (d) => d.cx)
.attr('y', (d) => d.cy)
.text((d) => '( ' + d.cx + ', ' + d.cy + ' )')
.attr('font-family', 'sans-serif')
.attr('font-size', '20px')
.attr('fill', 'red');
}
}