|
| 1 | +<template> |
| 2 | + <div |
| 3 | + class="section" |
| 4 | + > |
| 5 | + <h2 class="title"> |
| 6 | + {{ hoverTitle }} |
| 7 | + |
| 8 | + <small class="result"> |
| 9 | + {{ hoverLabel }} |
| 10 | + </small> |
| 11 | + </h2> |
| 12 | + |
| 13 | + <div class="container"> |
| 14 | + <canvas |
| 15 | + class="chart" |
| 16 | + ref="chart" |
| 17 | + @mouseleave="hoverIndex = null" |
| 18 | + ></canvas> |
| 19 | + </div> |
| 20 | + </div> |
| 21 | +</template> |
| 22 | + |
| 23 | +<script lang="ts"> |
| 24 | +import { computed, defineComponent } from 'vue' |
| 25 | +import Chart from 'chart.js/auto' |
| 26 | +
|
| 27 | +type DownloadsItem = { week: string, count: number } |
| 28 | +
|
| 29 | +export default defineComponent({ |
| 30 | + data() { |
| 31 | + return { |
| 32 | + downloads: [] as DownloadsItem[], |
| 33 | + hoverIndex: null as number | null, |
| 34 | + defaultLabel: 'loading...' |
| 35 | + } |
| 36 | + }, |
| 37 | +
|
| 38 | + computed: { |
| 39 | + labels(): string[] { |
| 40 | + return this.downloads.map(({ week }) => week) |
| 41 | + }, |
| 42 | +
|
| 43 | + series(): number[] { |
| 44 | + return this.downloads.map(({ count }) => count) |
| 45 | + }, |
| 46 | +
|
| 47 | + hoverDownload(): DownloadsItem | undefined { |
| 48 | + return this.downloads[this.hoverIndex] |
| 49 | + }, |
| 50 | +
|
| 51 | + hoverLabel(): string { |
| 52 | + if (this.hoverDownload === undefined) { |
| 53 | + return this.defaultLabel |
| 54 | + } |
| 55 | +
|
| 56 | + return this.formatNumber(this.hoverDownload.count) |
| 57 | + }, |
| 58 | +
|
| 59 | + hoverTitle(): string { |
| 60 | + if (this.hoverDownload === undefined) { |
| 61 | + return 'Weekly downloads:' |
| 62 | + } |
| 63 | +
|
| 64 | + const start = new Date(this.hoverDownload.week + 'Z') |
| 65 | + const end = new Date(start) |
| 66 | +
|
| 67 | + end.setDate(start.getDate() + 6) |
| 68 | +
|
| 69 | + return this.formatDate(start) + ' to ' + this.formatDate(end) + ':' |
| 70 | + } |
| 71 | + }, |
| 72 | +
|
| 73 | + methods: { |
| 74 | + formatDate(date: Date): string { |
| 75 | + return date.toISOString().slice(0, 10) |
| 76 | + }, |
| 77 | +
|
| 78 | + formatNumber(number: number): string { |
| 79 | + return number.toLocaleString('es-ES', { |
| 80 | + maximumFractionDigits: 0, |
| 81 | + useGrouping: true, |
| 82 | + }) |
| 83 | + } |
| 84 | + }, |
| 85 | +
|
| 86 | + async mounted() { |
| 87 | + const response = await fetch('https://bashunit.typeddevs.com/downloads') |
| 88 | +
|
| 89 | + this.downloads = (await response.json()).reverse() |
| 90 | +
|
| 91 | + let dataCount = 0 |
| 92 | + let totalCount = 0 |
| 93 | +
|
| 94 | + this.downloads.forEach(({ count }) => { |
| 95 | + dataCount++ |
| 96 | + totalCount += count |
| 97 | + }) |
| 98 | +
|
| 99 | + if (totalCount !== 0) { |
| 100 | + this.defaultLabel = this.formatNumber(totalCount / dataCount) |
| 101 | + } |
| 102 | +
|
| 103 | + const chart = new Chart(this.$refs.chart as HTMLCanvasElement, { |
| 104 | + type: 'line', |
| 105 | + data: { |
| 106 | + labels: this.labels, |
| 107 | + datasets: [{ |
| 108 | + label: 'Downloads', |
| 109 | + data: this.series, |
| 110 | + borderColor: '#6cbe1d', |
| 111 | + borderWidth: 3, |
| 112 | + pointRadius: 0, |
| 113 | + pointHoverRadius: 0, |
| 114 | + }] |
| 115 | + }, |
| 116 | + options: { |
| 117 | + responsive: true, |
| 118 | + maintainAspectRatio: false, |
| 119 | + plugins: { |
| 120 | + tooltip: { enabled: false }, |
| 121 | + legend: { display: false }, |
| 122 | + }, |
| 123 | + scales: { |
| 124 | + x: { display: false }, |
| 125 | + y: { display: false }, |
| 126 | + }, |
| 127 | + interaction: { |
| 128 | + mode: 'index', |
| 129 | + intersect: false, |
| 130 | + }, |
| 131 | + onHover: (_event, elements) => { |
| 132 | + if (elements.length > 0) { |
| 133 | + this.hoverIndex = elements[0].index |
| 134 | + } else { |
| 135 | + this.hoverIndex = null |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + }); |
| 140 | + } |
| 141 | +}) |
| 142 | +</script> |
| 143 | + |
| 144 | +<style scoped lang="css"> |
| 145 | +.title { |
| 146 | + border: none; |
| 147 | + padding: 0; |
| 148 | + font-variant-numeric: tabular-nums; |
| 149 | +} |
| 150 | +
|
| 151 | +.result { |
| 152 | + display: block; |
| 153 | + font-family: var(--vp-font-family-base); |
| 154 | + font-weight: normal; |
| 155 | + font-size: 32px; |
| 156 | + font-variant-numeric: tabular-nums; |
| 157 | + padding-top: 6px; |
| 158 | +} |
| 159 | +
|
| 160 | +.container { |
| 161 | + height: 200px; |
| 162 | +} |
| 163 | +
|
| 164 | +.chart { |
| 165 | + background-color: var(--vp-c-bg-elv); |
| 166 | + border-radius: 12px; |
| 167 | + padding: 24px; |
| 168 | + width: 100%; |
| 169 | + height: 200px; |
| 170 | +} |
| 171 | +</style> |
0 commit comments