-
Notifications
You must be signed in to change notification settings - Fork 4
/
sparkmeters.js
86 lines (73 loc) · 3.14 KB
/
sparkmeters.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
86
//----------------
// Todo:
// *
//-----------------------
//Width and height, meter fill and background
var w = 7;
var h = w*5;
var mfill = "silver";//"yellowgreen";
var mback = "whitesmoke";
var most = 1.0;
var mid = 0.5;
var low = 0.25;
//Data
var dataset = JSON.parse(document.getElementById('meterdata').value);
// Convert quoted JSON values to numeric values.
for (var i = 0; i < dataset.length; i++) {
if(dataset[i].level == "low"){dataset[i].level = low;}
if(dataset[i].level == "mid"){dataset[i].level = mid;}
if(dataset[i].level == "most"){dataset[i].level = most;}
}
// Functions to handle grouped item data
function keyf(d) { return d.key; }
function levelf(d) { return d.level; }
// Group of items with sparkmeters
var sparkgroup = d3.select(".sparkmetergroup")
.selectAll("span")
.data(dataset)
.enter()
.append("span")
.attr("class","sparkgroup");
// Enter item text and create SVG containers
var sparkspans = d3.selectAll(".sparkgroup")
.data(dataset)
.text(function(d,i) {if (i==0) {return d.key+"\u00A0";} else {return ", "+d.key+"\u00A0";} })
.append("svg")
.attr("width", w)
.attr("height", h);
// Draw meters filled to fill parameter
sparkspans.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("height", h)
.attr("width", w)
.attr("fill", mback);
sparkspans.append("rect")
.attr("x", 0)
.attr("y", function(d) { return h - h*d.level; })
.attr("height", function(d) { return h*d.level; })
.attr("width", w)
.attr("fill", mfill);
// sparkspans.append("text")
// .text(",");
//-----------------------------------------
// Single sparkmeters
var sfills = document.getElementsByClassName('sparkmeter');
var sparksingle = d3.selectAll(".sparkmeter")
.data(sfills)
.append("svg")
.attr("width", w)
.attr("height", h);
// Draw single meter up to specified fraction
sparksingle.append("rect")
.attr("x", 0)
.attr("y", 0)
.attr("height", h)
.attr("width", w)
.attr("fill", mback);
sparksingle.append("rect")
.attr("x", 0)
.attr("y", function(d) { return h - h*parseFloat(d.attributes.getNamedItem("value").value); })
.attr("height", function(d) { return h*parseFloat(d.attributes.getNamedItem("value").value); })
.attr("width", w)
.attr("fill", mfill);