-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathscale_examples.html
More file actions
189 lines (143 loc) · 4.69 KB
/
scale_examples.html
File metadata and controls
189 lines (143 loc) · 4.69 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Scales in D3</title>
<style>
body {
padding: 50px;
font-family: Verdana, sans-serif;
font-size: 12pt;
}
p {
margin-left: 20px;
color: orange;
}
</style>
</head>
<body>
<h1>Playing with Scales in D3</h1>
<div id="linearScale">
First scale, mapping values to new values:
</div>
<div id="colorScale1">
Linear Color scale:
</div>
<div id="colorScale2">
Ordinal color scale:
<p>Apple
<p>Orange
<p>Pineapple
<p>Cherry
<p>Kumquat
<p>China
<p>Russia
<p>Japan
<p>India
<p>France
<p>Syria
<p>Madagascar
<p>SUV
</div>
<div id="colorScale3">
This is another ordinal scale, with data mapped to specific values:
</div>
</body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<script type="text/javascript">
// this example is from Dashing D3:https://www.dashingd3js.com/d3js-scales
var myData = [0, 1000, 3000, 2000, 5000, 4000, 7000, 6000, 9000, 8000, 10000];
var newScaledData = [];
var minDataPoint = d3.min(myData);
var maxDataPoint = d3.max(myData);
// Notice that domain and range take ARRAYS.
var linearScale = d3.scale.linear()
.domain([minDataPoint,maxDataPoint])
.range([0,100]);
// a shortcut for the max and min operation is:
console.log(d3.extent(myData));
// this is the same as above, but using the shortcut d3.extent:
var linearScale = d3.scale.linear()
.domain(d3.extent(myData))
.range([0,100]);
// Suppose we had objects, like from a CSV?
myobjects = [
{
country : "USA",
score : "42"
},
{
country : "Belgium",
score : "24"
},
{
country : "Taiwan",
score : "5"
},
{
country : "Ireland",
score : "35"
},
{
country : "Tahiti",
score : "05"
}
];
myobjMax = d3.max(myobjects, function(d) {
return d.score;
});
console.log("max from d3.max on objects", myobjMax);
myobjextents = d3.extent(myobjects, function(d) {
return +d.score;
});
console.log("max and min array from d3.extent on objects", myobjextents);
// Let's look at the scaled data from the original number data:
console.log("the scaled value 3000 is now", linearScale(3000));
myData.forEach(function(d, i){
// we call the scale function with each element,
// and put the result onto the new array:
newScaledData.push(linearScale(d));
});
// we print the results in the div element
// the join function just joins all the strings with the separator!
d3.select("#linearScale").append("p").text(newScaledData.join(", "));
var newData = [24, 54, 65, 100, 23, 43, 2, 35, 76, 47];
// you can use hex etc values in here - i'm just using named colors now
var colorScale = d3.scale.linear().domain(d3.extent(newData)).range(['gray','red']);
// draw actual paragraphs with the colors:
d3.select("#colorScale1")
.selectAll("p")
.data(newData)
.enter()
.append("p")
.text(function (d) {
return d;
})
.style("color", function(d) {
return colorScale(d);
});
// one of many ordinal scale options for generating lists of colors in d3.
var colors = d3.scale.category10();
// this is still a function: it returns the colors by count:
console.log(colors(0));
// in this example, we select existing p's and restyle them! Notice there are more than 10 items, and the colors start repeating:
d3.select("#colorScale2")
.selectAll("p")
.style("color", function(d, i) {
return colors(i);
});
// This is a way to specific particular colors for particular values:
var color3 = d3.scale.ordinal().domain(['apples', 'blueberries', 'oranges', 'lemons']).range(['red', 'blue', 'orange', 'yellow']);
var myFruit = ['apples', 'oranges', 'lemons', 'blueberries', 'lemons', 'oranges'];
d3.select("#colorScale3")
.selectAll("p")
.data(myFruit)
.enter()
.append("p")
.text(function(d) {
return d;
})
.style("color", function (d) {
return color3(d);
});
</script>