-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstarplot.js
More file actions
executable file
·162 lines (137 loc) · 3.53 KB
/
starplot.js
File metadata and controls
executable file
·162 lines (137 loc) · 3.53 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var d3 = require('d3')
function Starplot (data) {
var self = this
this.data = []
this.label = data.label
this.selector = data.selector
this.width = 220
this.height = 220
this.centerX = -110
this.centerY = -110
this.id = data.id
this.clicked = false
if (data.selector === '.small-plots') {
this.centerY = -100
}
this.svgContainer = null
this.dataSetCount = 0
this.draw(data.selector, data.id)
if (data.selector !== '.small-plots') {
this.addAxisScale()
this.drawAxes(data.data)
}
this.addDataSet(data.data, true)
this.addLabel(this.label)
}
Starplot.prototype.draw = function (e, id) {
this.svgContainer = d3.select(e).append('div')
.attr('class', 'star-plot')
.attr('id', 'stop-' + id)
.append('svg')
.attr('viewBox', this.centerX + ' ' + this.centerY + ' ' + this.width + ' ' + this.height)
return Starplot
}
Starplot.prototype.addLabel = function (label) {
if (label !== '') {
this.svgContainer.append('text')
.attr('x', 0)
.attr('y', 95)
.attr('text-anchor', 'middle')
.text(function () {
return label
})
}
}
Starplot.prototype.drawAxes = function (data) {
var i = 0
var icons = [
'assets/Fahrten_Icon.svg',
'assets/Durchschnitt_Icon.svg',
'assets/Linie_Icon.svg',
'assets/Verbuende_Icon.svg',
'assets/Typen_Icon.svg'
]
var axes = this.svgContainer.insert('g', 'path').attr('class', 'axes').selectAll('line')
.data(data)
.enter().append('line')
.attr('x1', 0)
.attr('y1', 0)
.attr('x2', 90)
.attr('y2', 0)
.attr('stroke-linecap', 'round')
.attr('transform', function (d) {
var deg = -90 + (360 / (data.length)) * i
i++
return ('rotate(' + deg + ')')
})
.attr('class', function (d) {
return 'line-' + data.indexOf(d)
})
var iconCoords = this.dataConvert([100, 100, 100, 100, 100])
d3.select('.axes').selectAll('image')
.data(icons)
.enter().append('image')
.attr('xlink:href', function (d) {
return d
})
.attr('x', function (d, i) {
return iconCoords[i].x - 10
})
.attr('y', function (d, i) {
return iconCoords[i].y - 10
})
.attr('width', '20px')
.attr('height', '20px')
return Starplot
}
Starplot.prototype.addAxisScale = function () {
var data = this.data[1]
for (var i = 0; i < 10; i++) {
this.addDataSet([10 * i, 10 * i, 10 * i, 10 * i, 10 * i], false)
}
}
Starplot.prototype.dataConvert = function (d) {
var results = []
var data = d
for (var i = 0; i < data.length; i++) {
var coords = {}
coords.x = 0 - data[i] * Math.cos((90 + (360 / data.length) * i) / 180 * Math.PI)
coords.y = 0 - data[i] * Math.sin((90 + (360 / data.length) * i) / 180 * Math.PI)
results[i] = coords
}
return results
}
Starplot.prototype.addDataSet = function (d, push) {
if (push === undefined) {
this.dataSetCount++
push = true
}
if (push) {
this.data.push(d)
}
var count = this.dataSetCount
var lineData = this.dataConvert(d)
var lineFunction = d3.svg.line()
.x(function (d) {
return d.x
})
.y(function (d) {
return d.y
})
.interpolate('linear')
this.svgContainer.append('path')
.attr('class', function () {
if (push) {
return 'data-set-' + count
} else {
return 'scale'
}
})
.attr('d', lineFunction(lineData) + 'Z')
return Starplot
}
Starplot.prototype.removeDataSet = function (dataSet) {
d3.select('.data-set-' + dataSet).remove()
return Starplot
}
module.exports = Starplot