forked from cs573-22s/a2-DataVis-5ways
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd3Code
More file actions
142 lines (113 loc) · 3.61 KB
/
Copy pathd3Code
File metadata and controls
142 lines (113 loc) · 3.61 KB
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
# https://www.educative.io/edpresso/how-to-create-a-scatter-plot-using-d3
# https://riptutorial.com/d3-js/example/18426/loading-data-from-csv-files
# https://www.tutorialsteacher.com/d3js/loading-data-from-file-in-d3js
# https://observablehq.com/@d3/color-schemes
# http://bl.ocks.org/jfreels/6816504
# https://www.d3-graph-gallery.com/graph/custom_theme.html
# https://stackoverflow.com/questions/34890353/d3-change-font-size-of-axis-labels
<script src="https://d3js.org/d3.v6.min.js"></script>
<p id="para1"> Monet Norales, Assignment 2, February 11</p>
<svg id="svg" width=1000 height=1000></svg>
<script>
//loading csv file
d3.csv('cars-sample.csv')
.then(file => {
file.forEach(
d => {
d.Weight = +d.Weight;
d.MPG = +d.MPG;
}
);
render(file);
});
//make create space
const svg = d3.select('svg')
.attr('width', 700)
.attr('height', 700);
var width = +svg.attr('width');
var height = +svg.attr('height');
const render = file => {
//variables
var x = d => d.Weight;
var y = d => d.MPG;
var cR = d => d.Weight * 0.005;
var margin = { top: 50, right: 50, bottom: 100, left: 100};
var w = width - margin.left - margin.right;
var h = height - margin.top - margin.bottom;
// graph backing
const background = svg.append('rect')
.attr('x', margin.left)
.attr('y', margin.top)
.attr('width', w)
.attr('height', h)
.attr('fill', 'black');
// individual color labels
var colors = {
'bmw': 1,
'ford': 2,
'honda': 3,
'mercedes': 4,
'toyota': 5
};
var design = ([
"#7fc97f",
"#beaed4",
"#fdc086",
"#ffff99",
"#386cb0",
"#f0027f",
"#bf5b17",
"#666666"
])
const col = d => design[colors[d.Manufacturer]];
const g = svg.append('g')
.attr('transform',
`translate(${margin.left},
${margin.top})`);
const yScale = d3.scaleLinear()
.domain([d3.min(file, y),
d3.max(file, y)])
.range([h, 0]);
const yAxis = d3.axisLeft(yScale)
.ticks(5)
.tickSize(-w)
.tickPadding(10);
const yLabel = g.append('g').call(yAxis);
yLabel.append('text')
.attr('y', -40)
.attr('x', -h / 2)
.attr('fill', 'darkgrey')
.style("font-size", 30)
.attr('transform', `rotate(-90)`)
.attr('text-anchor', 'middle')
.text('MPG');
const xScale = d3.scaleLinear()
.domain(d3.extent(file, x))
.range([0, w]);
const xAxis = d3.axisBottom(xScale)
.ticks(4)
.tickSize(-h)
.tickPadding(10);
const xLabel = g.append('g').call(xAxis)
.attr('transform',
`translate(0,${h})`);
xLabel.append('text')
.attr('y', 50)
.attr('x', w/2)
.attr('fill', 'darkgrey')
.style("font-size", 30)
.text('Weight');
var scale = d3.scaleLinear()
.domain([0, 100]) // unit: km
.range([0, 200]) // unit: pixels
g.selectAll('circle')
.data(file)
.enter()
.append('circle')
.attr('cx', d => xScale(x(d)))
.attr('cy', d => yScale(y(d)))
.attr('r', cR)
.style("fill", col)
.style("opacity", .50)
}
</script>