-
Notifications
You must be signed in to change notification settings - Fork 1
/
chart.js
85 lines (70 loc) · 2.51 KB
/
chart.js
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
var d3 = require('d3'),
fs = require('fs');
var chart = {
init: function(window, options) {
var body = window.document.querySelector('body'),
el = window.document.querySelector('#main'),
bgcolor, bgcolorInit, chart, τ, angle, arc, background, foreground, textLabel, textVal, clientScript;
bgColor = function() {
if (options.score >= 66) {
return "green";
}
if (options.score >= 33) {
return "orange";
}
if (options.score >= 0) {
return "red";
}
};
bgcolorInit = options.mode === "raw" ? "#ddd": bgColor;
chart = d3.select(el)
.append('svg:svg')
.attr('width', 400)
.attr('height', 400)
.append("g")
.attr("transform", "translate(200,200)");
τ = 2 * Math.PI; // http://tauday.com/tau-manifesto
angle = options.mode === "raw" ? 0 : (options.score/100) * τ;
arc = d3.svg.arc()
.innerRadius(80)
.outerRadius(100)
.startAngle(0);
background = chart.append("path")
.datum({endAngle: τ})
.style("fill", "#ddd")
.attr("d", arc);
foreground = chart.append("path")
.datum({endAngle: angle})
.style("fill", bgColor)
.attr("d", arc)
.attr("id", "foreground");
textLabel = chart
.append("g")
.attr("transform", "translate(" + 0 + "," + 35 +")")
.attr("class", "label");
textVal = textLabel
.append("text")
.text(options.score)
.style("text-anchor", "middle")
.style("letter-spacing", "-2")
.attr("id", "text")
.attr("font-family","sans-serif")
.attr("font-size", "100px")
.attr("fill", bgcolorInit)
.transition()
.duration(750);
if (options.mode === "raw") {
clientScript = "var score = " + options.score + ";";
clientScript += "var bgColor = '" + bgColor() + "';";
clientScript += fs.readFileSync("chart_script.js");
d3.select(body)
.append("script")
.attr("src","node_modules/d3/d3.min.js");
d3.select(body)
.append("script")
.html(clientScript);
}
return window.document.documentElement.outerHTML;
}
};
module.exports = chart;