Skip to content

Commit 8568856

Browse files
committed
feat(chart): Add bar chart for downloads per year chart
As it is working better for the default 1 year preselection then the line chart.
1 parent 26e2eec commit 8568856

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

src/components/BarChart.vue

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<script>
2+
import { HorizontalBar } from 'vue-chartjs'
3+
4+
export default {
5+
extends: HorizontalBar,
6+
props: {
7+
chartData: {
8+
type: Array | Object,
9+
required: false
10+
},
11+
chartLabels: {
12+
type: Array,
13+
required: true
14+
}
15+
},
16+
data () {
17+
return {
18+
gradient: null,
19+
options: {
20+
showScale: true,
21+
scales: {
22+
yAxes: [{
23+
ticks: {
24+
beginAtZero: false
25+
26+
},
27+
gridLines: {
28+
display: true,
29+
color: '#EEF0F4',
30+
borderDash: [5, 15]
31+
},
32+
categoryPercentage: 1,
33+
barPercentage: 0.4
34+
}],
35+
xAxes: [ {
36+
ticks: {
37+
callback: (value, index, values) => {
38+
return this.formatNumber(value)
39+
}
40+
},
41+
gridLines: {
42+
display: true,
43+
color: '#EEF0F4',
44+
borderDash: [5, 15]
45+
}
46+
}]
47+
},
48+
tooltips: {
49+
backgroundColor: '#4F5565',
50+
titleFontStyle: 'normal',
51+
titleFontSize: 18,
52+
bodyFontFamily: "'Proxima Nova', sans-serif",
53+
cornerRadius: 3,
54+
bodyFontColor: '#20C4C8',
55+
bodyFontSize: 14,
56+
xPadding: 14,
57+
yPadding: 14,
58+
displayColors: false,
59+
mode: 'index',
60+
intersect: false,
61+
callbacks: {
62+
title: tooltipItem => {
63+
return `🗓 ${tooltipItem[0].xLabel}`
64+
},
65+
label: (tooltipItem, data) => {
66+
let dataset = data.datasets[tooltipItem.datasetIndex]
67+
let currentValue = dataset.data[tooltipItem.index]
68+
return `📦 ${currentValue.toLocaleString()}`
69+
}
70+
}
71+
},
72+
legend: {
73+
display: false
74+
},
75+
responsive: true,
76+
maintainAspectRatio: false
77+
}
78+
}
79+
},
80+
mounted () {
81+
this.gradient = this.$refs.canvas
82+
.getContext('2d')
83+
.createLinearGradient(0, 0, 0, 450)
84+
85+
this.gradient.addColorStop(0, 'rgba(52, 217, 221, 0.6)')
86+
this.gradient.addColorStop(0.5, 'rgba(52, 217, 221, 0.25)')
87+
this.gradient.addColorStop(1, 'rgba(52, 217, 221, 0)')
88+
89+
this.renderChart({
90+
labels: this.chartLabels,
91+
datasets: [
92+
{
93+
label: 'downloads',
94+
borderColor: '#249EBF',
95+
pointBackgroundColor: 'rgba(0,0,0,0)',
96+
pointBorderColor: 'rgba(0,0,0,0)',
97+
pointHoverBorderColor: '#249EBF',
98+
pointHoverBackgroundColor: '#fff',
99+
pointHoverRadius: 4,
100+
pointHitRadius: 10,
101+
pointHoverBorderWidth: 1,
102+
borderWidth: 1,
103+
backgroundColor: this.gradient,
104+
hoverBackgroundColor: this.gradient,
105+
data: this.chartData
106+
}
107+
]
108+
}, this.options)
109+
},
110+
methods: {
111+
formatNumber (num) {
112+
let numString = Math.round(num).toString()
113+
let numberFormatMapping = [[6, 'm'], [3, 'k']]
114+
for (let [numberOfDigits, replacement] of numberFormatMapping) {
115+
if (numString.length > numberOfDigits) {
116+
let decimal = ''
117+
if (numString[numString.length - numberOfDigits] !== '0') {
118+
decimal = '.' + numString[numString.length - numberOfDigits]
119+
}
120+
numString = numString.substr(0, numString.length - numberOfDigits) + decimal + replacement
121+
break
122+
}
123+
}
124+
return numString
125+
}
126+
}
127+
}
128+
</script>

src/pages/Start.vue

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
<hr>
7777
</div>
7878
<div class="Chart__content">
79-
<line-chart v-if="loaded" :chart-data="downloadsYear" :chart-labels="labelsYear"></line-chart>
79+
<bar-chart v-if="loaded" :chart-data="downloadsYear" :chart-labels="labelsYear"></bar-chart>
8080
</div>
8181
</div>
8282
</div>
@@ -88,6 +88,7 @@
8888
import Datepicker from 'vuejs-datepicker'
8989
9090
import LineChart from '@/components/LineChart'
91+
import BarChart from '@/components/BarChart'
9192
import PackageInfo from '@/components/PackageInfo'
9293
9394
import {
@@ -103,6 +104,7 @@
103104
export default {
104105
components: {
105106
LineChart,
107+
BarChart,
106108
PackageInfo,
107109
Datepicker
108110
},

0 commit comments

Comments
 (0)