|
| 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> |
0 commit comments