-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path双曲线.html
157 lines (137 loc) · 3.82 KB
/
双曲线.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>d3</title>
<style type="text/css">
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis--y path {
display: none;
}
.line path {
fill: none;
stroke-width: 1.5px;
stroke-linejoin: round;
}
.line text {
font: bold 12px sans-serif;
text-shadow: 1px 1px 0 #fff, -1px -1px 0 #fff, 1px -1px 0 #fff, -1px 1px 0 #fff;
}
</style>
</head>
<body>
<script src="http://cdn.bootcss.com/underscore.js/1.8.3/underscore-min.js"></script>
<script src="js/d3.js"></script>
<section id="chartArea"></section>
<script>
var data = [{
name: "HSL Rainbow",
labelOffset: 400,
value: function(t) {
return d3.hsl(t, 1, 0.5);
}
}, {
name: "Cubehelix Rainbow",
labelOffset: 0,
value: function(t) {
return d3.hsl(t, 1, 0.6);
}
}].map(function(color) {
console.log('color', color)
return color.deltas = d3.range(0, 360, 3).map(function(x) {
return {
input: x,
delta: delta(color.value(x - 10), color.value(x + 10))
};
}), color;
});
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 30
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scaleLinear()
.domain([0, 360])
.range([0, width]);
var y = d3.scaleLinear()
.domain([0, 50])
.range([height, 0]);
var z = d3.scaleOrdinal(d3.schemeCategory10);
var line = d3.line()
.curve(d3.curveBasis)
.x(function(d) {
return x(d.input);
})
.y(function(d) {
return y(d.delta);
});
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + y(0) + ")")
.call(d3.axisBottom(x));
svg.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y))
.selectAll(".tick:last-of-type")
.append("text")
.attr("class", "axis-title")
.attr("x", 3)
.attr("dy", ".32em")
.text("Color Difference at ±10° (CIE76)");
var g = svg.selectAll(".line")
.data(data)
.enter().append("g")
.attr("class", "line");
g.append("path")
.attr("d", function(d) {
return line(d.deltas);
})
.attr("id", function(d, i) {
return "path-" + i;
})
.style("stroke", function(d, i) {
return z(i);
});
g.append("text")
.attr("x", function(d) {
return d.labelOffset;
})
.attr("dy", -5)
.style("fill", function(d, i) {
return d3.lab(z(i)).darker();
})
.append("textPath")
.attr("class", "textpath")
.attr("xlink:href", function(d, i) {
return "#path-" + i;
})
.text(function(d) {
return d.name;
});
// CIE76 per https://en.wikipedia.org/wiki/Color_difference#CIE76
// Not as good as CIEDE2000 but a lot easier to implement.
function delta(a, b) {
var dl = (a = d3.lab(a)).l - (b = d3.lab(b)).l,
da = a.a - b.a,
db = a.b - b.b;
return Math.sqrt(dl * dl + da * da + db * db);
}
</script>
</body>
</html>