-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmarletShareStack.html
More file actions
145 lines (129 loc) · 3.75 KB
/
marletShareStack.html
File metadata and controls
145 lines (129 loc) · 3.75 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
143
144
145
<!DOCTYPE html>
<html>
<body>
<style>
svg {
border: 1px solid lightgrey;
}
.caption {
margin-top: 20px;
width: 600px;
text-align: center;
}
</style>
<script src="http://d3js.org/d3.v5.min.js"></script>
<script type="text/javascript">
const maxHeight = 400;
const maxWidth = 600;
const barWidth = 20;
const data = [
{
year: 2017,
quarter: 2,
samsung: 0.229,
apple: 0.118,
huawei: 0.110,
oppo: 0.08,
xiaomi: 0.062,
others: 0.401,
},
{
year: 2017,
quarter: 3,
samsung: 0.221,
apple: 0.124,
huawei: 0.104,
oppo: 0.081,
xiaomi: 0.075,
others: 0.396,
},
{
year: 2017,
quarter: 4,
samsung: 0.189,
apple: 0.196,
huawei: 0.107,
oppo: 0.069,
xiaomi: 0.071,
others: 0.368,
},
{
year: 2018,
quarter: 1,
samsung: 0.235,
apple: 0.157,
huawei: 0.118,
oppo: 0.074,
xiaomi: 0.084,
others: 0.332,
},
]
const keys = ['apple', 'samsung', 'huawei', 'oppo', 'xiaomi', 'others'];
const getTimePoint = (d) => {
const _d = d.data ? d.data : d;
return `${_d.year}-${_d.quarter}`;
}
const stack = d3.stack().keys(keys).order(d3.stackOrderNone).offset(d3.stackOffsetNone);
const series = stack(data);
const colorArray = ['#38CCCB', '#0074D9', '#2FCC40', '#FEDC00', '#FF4036', 'lightgrey'];
function renderVerticalStack() {
const svg = d3.select('body')
.append('svg')
.attr('width', maxWidth)
.attr('height', maxHeight + 50);
const xScale = d3.scalePoint()
.domain(data.map(getTimePoint))
.range([0, maxWidth])
.padding(0.2);
const xScalePoint = d3.scalePoint()
.domain(data.map(getTimePoint))
.range([0, maxWidth])
.padding(0.2)
const stackMax = (serie) => d3.max(serie, (d) => d ? d[1] : 0)
const stackMin = (serie) => d3.min(serie, (d) => d? d[0]: 0)
const y = d3.scaleLinear()
.domain([d3.max(series, stackMax), d3.min(series, stackMin)])
.range([0, maxHeight])
const g = svg.selectAll('g')
.data(series)
.enter()
.append('g')
.attr('fill', (d, i) => colorArray[i % colorArray.length])
.selectAll('rect')
.data((d) => d)
.enter()
.append('rect')
.attr('x', (d) => {
const scaledX = xScale(getTimePoint(d));
return scaledX - barWidth / 2;
})
.attr('y', (d) => y(d[1]))
.attr('width', barWidth)
.attr('height', (d) => {
return y(d[0]) - y(d[1])
})
const axis = d3.axisBottom(xScalePoint);
svg.append('g')
.attr('transform', `translate(0, ${maxHeight})`)
.call(axis)
}
renderVerticalStack()
d3.select('body')
.append('div')
.style('width', maxWidth + 'px')
.style('display', 'flex')
.style('justify-content', 'space-around')
.selectAll('.legend')
.data(keys)
.enter()
.append('div')
.attr('class', 'legend')
.text((d) => d)
.style('color', (d, i) => colorArray[i % colorArray.length])
</script>
<div class='caption'>
Worldwide Top 5 Smartphone Shipment Company Market Share<br/>
Data Source: <a href='https://www.idc.com/promo/smartphone-market-share/vendor'>IDC</a>
</div>
</body>
</html>