-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_histograma.js
150 lines (116 loc) · 3.63 KB
/
app_histograma.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
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
async function draw() {
// Data
const dataset = await d3.json('data.json')
//const xAccessor = d => d.currently.humidity
//const yAccessor = d => d.length
// Dimensions
let dimensions = {
width: 800,
height: 400,
margins: 50
};
dimensions.ctrWidth = dimensions.width - dimensions.margins * 2
dimensions.ctrHeight = dimensions.height - dimensions.margins * 2
// Draw Image
const svg = d3.select('#chart')
.append("svg")
.attr("width", dimensions.width)
.attr("height", dimensions.height)
const ctr = svg.append("g") // <g>
.attr(
"transform",
`translate(${dimensions.margins}, ${dimensions.margins})`
)
// Scales
/* const xScale = d3.scaleLinear()
.domain(d3.extent(dataset, xAccessor))
.range([0, dimensions.ctrWidth])
.nice()
const bin = d3.bin()
.domain(xScale.domain())
.value(xAccessor)
.thresholds(10)
const newDataset = bin(dataset)
const padding = 1
const yScale = d3.scaleLinear()
.domain([0, d3.max(newDataset, yAccessor)])
.range([dimensions.ctrHeight, 0])
.nice()
*/
//console.log({original: dataset, new: newDataset})
/* // Draw Bars
ctr.selectAll('rect')
.data(newDataset)
.join('rect')
.attr('width', d => d3.max([0, xScale(d.x1) - xScale(d.x0) - padding]))
.attr('height', d => dimensions.ctrHeight - yScale(yAccessor(d)))
.attr('x', d => xScale(d.x0))
.attr('y', d => yScale(yAccessor(d)))
.attr('fill', '#01c5c4') */
/* ctr.append('g')
.classed('bar-labels', true)
.selectAll('text')
.data(newDataset)
.join('text')
.attr('x', d => xScale(d.x0) + (xScale(d.x1) - xScale(d.x0))/2 )
.attr('y', d => yScale(yAccessor(d)) - 10)
.text(yAccessor) */
/* // Draw Axis
const xAxis = d3.axisBottom(xScale)
const xAxisGroup = ctr.append('g')
.style('transform', `translateY(${dimensions.ctrHeight}px)`)
xAxisGroup.call(xAxis) */
const labelsGroup = ctr.append('g')
.classed('bar-labels', true)
const xAxisGroup = ctr.append('g')
.style('transform', `translateY(${dimensions.ctrHeight}px)`)
function histogram(metric){
const xAccessor = d => d.currently[metric]
const yAccessor = d => d.length
// Scales
const xScale = d3.scaleLinear()
.domain(d3.extent(dataset, xAccessor))
.range([0, dimensions.ctrWidth])
.nice()
const bin = d3.bin()
.domain(xScale.domain())
.value(xAccessor)
.thresholds(10)
const newDataset = bin(dataset)
const padding = 1
const yScale = d3.scaleLinear()
.domain([0, d3.max(newDataset, yAccessor)])
.range([dimensions.ctrHeight, 0])
.nice()
// Draw Bars
ctr.selectAll('rect')
.data(newDataset)
.join('rect')
.attr('width', d => d3.max([0, xScale(d.x1) - xScale(d.x0) - padding]))
.attr('height', d => dimensions.ctrHeight - yScale(yAccessor(d)))
.attr('x', d => xScale(d.x0))
.attr('y', d => yScale(yAccessor(d)))
.attr('fill', '#01c5c4')
/* ctr.append('g')
.classed('bar-labels', true) */
labelsGroup.selectAll('text')
.data(newDataset)
.join('text')
.attr('x', d => xScale(d.x0) + (xScale(d.x1) - xScale(d.x0))/2 )
.attr('y', d => yScale(yAccessor(d)) - 10)
.text(yAccessor)
// Draw Axis
const xAxis = d3.axisBottom(xScale)
/* const xAxisGroup = ctr.append('g')
.style('transform', `translateY(${dimensions.ctrHeight}px)`) */
xAxisGroup.call(xAxis)
}
d3.select('#metric')
.on('change', function(e){
e.preventDefault()
//console.log(this)
histogram(this.value)
})
histogram('humidity')
}
draw()