generated from jtr13/EDAVtemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
interactive.Rmd
138 lines (110 loc) · 3.81 KB
/
interactive.Rmd
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
# Interactive component
<meta charset="utf-8">
<title>NYUJobs></title>
<style type="text/css">
h1 {color:black;} /* CSS styling */
p {color:blue;}
</style>
<body>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v4.js"></script>
<h1>Job salary/numbers according to category</h1>
</h3>
<!-- Add buttons -->
<button onclick="update(data1)">Lowest Salary</button>
<button onclick="update(data2)">Highest Salary</button>
<button onclick="update(data3)">Number of positions</button>
<!-- Create a div where the graph will take place -->
<div id="d3try" ></div>
<script>
// create data_set
var data1 = [ {Category: "Technology&Data", value: 72121},
{Category: "Finance", value: 68928},
{Category: "Engineering", value: 68092},
{Category: "Communications", value: 66899},
{Category: "Legal", value: 65258},
{Category: "Policy", value: 63137},
{Category: "Social Services", value: 61706},
{Category: "Public Safety", value: 56089},
];
var data2 = [
{Category: "Engineering", value: 105204.97},
{Category: "Technology&Data", value: 103689.77},
{Category: "Communications", value: 101403.88},
{Category: "Policy", value: 98124},
{Category: "Finance", value: 89347},
{Category: "Legal", value: 79498},
{Category: "Social Services", value: 79494},
{Category: "Human Resources", value: 74884},
];
var data3 = [
{Category: "Constituent Services", value: 122},
{Category: "Building Operation", value: 117},
{Category: "Legal", value: 66},
{Category: "Engineering", value: 47},
{Category: "Human Resources", value: 40},
{Category: "Communications", value: 39},
{Category: "Social Services", value: 34},
{Category: "Public Safety", value: 30},
];
// set the dimensions and margins of the graph
var margin = {top: 30, right: 30, bottom: 70, left: 60},
width = 800 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#d3try")
.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 + ")")
.append("g", id ='circ');
// Initialize the X axis
var x = d3.scaleBand()
.range([ 0, width ])
.padding(0.2);
var xAxis = svg.append("g")
.attr("transform", "translate(0," + height + ")")
// Initialize the Y axis
var y = d3.scaleLinear()
.range([ height, 0]);
var yAxis = svg.append("g")
.attr("class", "myYaxis")
// A function that create / update the plot for a given variable:
function update(data) {
// Update the X axis
x.domain(data.map(function(d) { return d.Category; }))
xAxis.call(d3.axisBottom(x))
// Update the Y axis
y.domain([0, d3.max(data, function(d) { return d.value }) ]);
yAxis.transition().duration(1000).call(d3.axisLeft(y));
// Create the u variable
var u = svg.selectAll("rect")
.data(data)
u
.enter()
.append("rect") // Add a new rect for each new elements
.merge(u) // get the already existing elements as well
.transition() // and apply changes to all of them
.duration(1000)
.attr("x", function(d) { return x(d.Category); })
.attr("y", function(d) { return y(d.value); })
.attr("width", x.bandwidth())
.attr("height", function(d) { return height - y(d.value); })
.attr("fill", "#7b6888")
var rect = svg.selectAll("rect").on("mouseover", function(d, i) {
//highlight
d3.select(this).style("fill","#BA5C25");
})
.on("mouseout", function(d) {
d3.select(this).style("fill","#7b6888");
});
// If less group in the new dataset, I delete the ones not in use anymore
u
.exit()
.remove()
}
update(data1)
</script>
</body>