/
index.html
157 lines (130 loc) · 4.24 KB
/
index.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">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Toucan Toco Sparklines</title>
<meta name="author" content="David Nowinsky <david@toucantoco.com>">
<meta name="description" content="Trendy graphs">
<meta name="keywords" content="sparkline,sparklines,toucan toco,toucan,toco,open-source,d3,plugin,chart,trends">
<link href="dist/tc-sparklines.css" rel="stylesheet">
<style>
@import url(https://fonts.googleapis.com/css?family=Montserrat|Roboto+Slab);
* {
font-family: 'Montserrat', sans-serif;
text-transform: uppercase;
}
.category__label-name {
font-family: 'Roboto Slab';
text-transform: none;
}
</style>
<style>
.category {
display: block;
padding: 10px;
}
.category__label,
.category__value {
font-style: normal;
text-transform: none;
display: inline-block;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
width: 120px;
padding: 0 10px;
}
.category__value {
opacity: 0.6;
}
.category__label {
text-align: right;
}
.sparkline__tooltip {
font-size: 0.7em;
}
</style>
</head>
<body>
<h1>Sparklines example</h1>
<a href="README.md">Documentation</a>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.16/d3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
<script src="dist/tc-sparklines.js"></script>
<section class="example">
<button onClick="createOrUpdateSparklines()">Update</button>
<div id="example"></div>
<script>
function generateRandomData(seriesCount, datesCount) {
return d3.range(seriesCount).map(function (d) {
return {
name: 'Sparkline #' + d,
values: d3.range(datesCount).map(function (d) {
return {
label: d,
value: Math.random(d)
};
})
};
});
}
var sparklinesConfig = {
height: 30,
width: 200
};
// Sparklines configuration
var sparklines = d3.toucan.sparklines()
.height(sparklinesConfig.height)
.width(sparklinesConfig.width)
.dateSelector('label')
.forceLexicalOrder(false)
.valueSelector('value')
.valueFormat('.2p')
.unit('');
function createOrUpdateSparklines() {
var exampleElement = d3.selectAll('#example')
// Adding some sample parents elements
var categoriesSelection = exampleElement.selectAll('.category')
.data(generateRandomData(10, 30), function(cat) {
return cat.name;
});
var newCategories = categoriesSelection
.enter()
.append('div')
.classed('category', true);
newCategories
.append('span')
.classed('category__label', true)
categoriesSelection.select('.category__label')
.text(function (d) {
return d.name;
});
// Creating blocks that will contain sparklines
var newSparklinesSelection = newCategories
.append('svg')
.classed('sparkline', true)
.attr('height', sparklinesConfig.height)
.attr('width', sparklinesConfig.width)
// Binding data to them
var sparklinesSelection = categoriesSelection.select('.sparkline')
.datum(function (d) {
return d.values;
});
newCategories
.append('span')
.classed('category__value', true)
var valueFormatter = d3.format('.2p');
categoriesSelection.select('.category__value')
.text(function (d) {
return valueFormatter(d.values[d.values.length - 1].value);
});
// Do the magic!
sparklinesSelection
.call(sparklines);
}
createOrUpdateSparklines();
</script>
</section>
</body>
</html>