-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (65 loc) · 2 KB
/
index.js
File metadata and controls
81 lines (65 loc) · 2 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
import {
select,
csv,
scaleLinear,
max,
scaleBand,
axisLeft,
axisBottom,
format
} from 'd3';
const titleText = 'Brand and its Mark Value';
const xAxisLabelText = 'markValue';
const svg = select('svg');
const width = +svg.attr('width');
const height = +svg.attr('height');
const render = data => {
const xValue = d => d['markValue'];
const yValue = d => d.brand;
const margin = { top: 50, right: 40, bottom: 77, left: 180 };
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;
const xScale = scaleLinear()
.domain([0, max(data, xValue)])
.range([0, innerWidth]);
const yScale = scaleBand()
.domain(data.map(yValue))
.range([0, innerHeight])
.padding(0.1);
const g = svg.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
const xAxisTickFormat = number =>
format('.3s')(number)
.replace('G', 'B');
const xAxis = axisBottom(xScale)
.tickFormat(xAxisTickFormat)
.tickSize(-innerHeight);
g.append('g')
.call(axisLeft(yScale))
.selectAll('.domain, .tick line')
.remove();
const xAxisG = g.append('g').call(xAxis)
.attr('transform', `translate(0,${innerHeight})`);
xAxisG.select('.domain').remove();
xAxisG.append('text')
.attr('class', 'axis-label')
.attr('y', 65)
.attr('x', innerWidth / 2)
.attr('fill', 'black')
.text(xAxisLabelText);
g.selectAll('rect').data(data)
.enter().append('rect')
.attr('y', d => yScale(yValue(d)))
.attr('width', d => xScale(xValue(d)))
.attr('height', yScale.bandwidth());
g.append('text')
.attr('class', 'title')
.attr('y', -10)
.text(titleText);
};
csv('data.csv').then(data => {
data.forEach(d => {
d.markValue = +d.markValue * 1000;
});
render(data);
});