-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (78 loc) · 2.82 KB
/
app.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
// Grab Data From File
async function draw()
{
// Data
const dataset = await d3.json('data.json');
const xAccessor = (d) => d.currently.humidity;
const yAccessor = (d) => d.currently.apparentTemperature;
// Dimensions
let dimensions = {
width: 800,
height: 800,
margin: {
top: 50,
bottom: 50,
left: 50,
rigth: 50
}
}
dimensions.containerWidth = dimensions.width
- dimensions.margin.left - dimensions.margin.rigth;
dimensions.containerHeight = dimensions.height
- dimensions.margin.top - dimensions.margin.bottom;
// Draw Image
const svg = d3.select('#chart')
.append('svg')
.attr('width', dimensions.width)
.attr('height', dimensions.height)
const container = svg.append('g')
.attr('transform',
`translate(${dimensions.margin.left},${dimensions.margin.top})`
)
/* container.append('circle')
.attr('r',15)
*/
// Scales
const xScale = d3.scaleLinear()
.domain(d3.extent(dataset, xAccessor))
.rangeRound([0, dimensions.containerWidth])
.clamp(true);
const yScale = d3.scaleLinear()
.domain(d3.extent(dataset, yAccessor))
.rangeRound([dimensions.containerHeight, 0])
.nice()
.clamp(true);
// Draw Circles
container.selectAll('circle')
.data(dataset)
.join('circle')
.attr('cx', d => xScale(xAccessor(d)))
.attr('cy', d => yScale(yAccessor(d)))
.attr('r', 5)
.attr('fill','red')
.attr('data-temp', yAccessor)
// Axes
const xAxis = d3.axisBottom(xScale)
.ticks(5)
.tickFormat((d) => d*100 + '%')
//.tickValues([0.4, 0.5, 0.8])
const xAxisGroup = container.append('g').call(xAxis)
.style('transform', `translateY(${dimensions.containerHeight}px)`)
.classed('axis', true)
xAxisGroup.append('text')
.attr('x', dimensions.containerWidth/2)
.attr('y', dimensions.margin.bottom - 10)
.attr('fill', 'black')
.text('Humidity')
const yAxis = d3.axisLeft(yScale)
const yAxisGroup = container.append('g').call(yAxis)
.classed('axis', true)
yAxisGroup.append('text')
.attr('x', -dimensions.containerHeight/2)
.attr('y', -dimensions.margin.left + 15 )
.attr('fill', 'black')
.html('Temperature ° F')
.style('transform', 'rotate(270deg)')
.style('text-anchor', 'middle')
}
draw();