|
| 1 | +<script> |
| 2 | +import Chart from 'chart.js' |
| 3 | +import { customTooltips } from '@coreui/coreui-chartjs' |
| 4 | +
|
| 5 | +export default { |
| 6 | + name: '', |
| 7 | + type: '', |
| 8 | + props: { |
| 9 | + datasets: Array, |
| 10 | + labels: [Array, String], |
| 11 | + options: Object, |
| 12 | + plugins: Array |
| 13 | + }, |
| 14 | + months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], |
| 15 | + data() { |
| 16 | + return { |
| 17 | + chart: undefined |
| 18 | + } |
| 19 | + }, |
| 20 | + computed: { |
| 21 | + safeId() { |
| 22 | + // as long as this._uid() works there is no need to generate the key |
| 23 | + const key = () => Math.random().toString(36).replace('0.', '') |
| 24 | + return '__safe_id__' + this._uid || key() |
| 25 | + }, |
| 26 | + computedDatasets() { |
| 27 | + return this.datasets |
| 28 | + }, |
| 29 | + computedLabels() { |
| 30 | + if (this.labels && typeof this.labels !== 'string') { |
| 31 | + return this.labels |
| 32 | + } |
| 33 | + const emptyLabels = Array(this.datasets[0].data.length).fill('') |
| 34 | +
|
| 35 | + if (this.labels === 'indexes') { |
| 36 | + return emptyLabels.map((u, i) => i + 1) |
| 37 | + } else if (this.labels === 'months') { |
| 38 | + return emptyLabels.map((u, i) => this.$options.months[i % 12]) |
| 39 | + } |
| 40 | + return emptyLabels |
| 41 | + }, |
| 42 | + computedData() { |
| 43 | + return { |
| 44 | + datasets: this.computedDatasets, |
| 45 | + labels: this.computedLabels |
| 46 | + } |
| 47 | + }, |
| 48 | + customTooltips() { |
| 49 | + if (!this.options || !this.options.tooltips) { |
| 50 | + return { |
| 51 | + tooltips: { |
| 52 | + enabled: false, |
| 53 | + custom: customTooltips, |
| 54 | + intersect: true, |
| 55 | + mode: 'index', |
| 56 | + position: 'nearest', |
| 57 | + callbacks: { |
| 58 | + labelColor(tooltipItem, chart) { |
| 59 | + function getValue(prop) { |
| 60 | + return typeof prop === 'object' ? prop[tooltipItem.index] : prop |
| 61 | + } |
| 62 | + const dataset = chart.data.datasets[tooltipItem.datasetIndex] |
| 63 | + //tooltipLabelColor is coreUI custom prop used only here |
| 64 | + const backgroundColor = getValue( |
| 65 | + dataset.tooltipLabelColor || |
| 66 | + dataset.pointHoverBackgroundColor || |
| 67 | + dataset.borderColor || |
| 68 | + dataset.backgroundColor |
| 69 | + ) |
| 70 | + return { |
| 71 | + backgroundColor |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + }, |
| 79 | + computedOptions() { |
| 80 | + return Object.assign({}, this.options, this.customTooltips || {}) |
| 81 | + }, |
| 82 | + chartConfig() { |
| 83 | + return { |
| 84 | + type: this.$options.type, |
| 85 | + data: this.computedData, |
| 86 | + options: this.computedOptions || this.options, |
| 87 | + plugins: this.plugins |
| 88 | + } |
| 89 | + } |
| 90 | + }, |
| 91 | + watch: { |
| 92 | + chartConfig() { |
| 93 | + this.updateChart() |
| 94 | + } |
| 95 | + }, |
| 96 | + mounted() { |
| 97 | + this.renderChart() |
| 98 | + }, |
| 99 | + methods: { |
| 100 | + renderChart() { |
| 101 | + this.destroyChart() |
| 102 | + this.chart = new Chart( |
| 103 | + this.$refs.canvas.getContext('2d'), |
| 104 | + this.chartConfig |
| 105 | + ) |
| 106 | + }, |
| 107 | + updateChart() { |
| 108 | + Object.assign(this.chart, this.chartConfig) |
| 109 | + this.chart.update() |
| 110 | + }, |
| 111 | + destroyChart() { |
| 112 | + if (this.chart) { |
| 113 | + this.chart.destroy() |
| 114 | + } |
| 115 | + } |
| 116 | + }, |
| 117 | + beforeDestroy() { |
| 118 | + this.destroyChart() |
| 119 | + }, |
| 120 | + render(h) { |
| 121 | + return h( |
| 122 | + 'div', |
| 123 | + [ |
| 124 | + h( |
| 125 | + 'canvas', { |
| 126 | + attrs: { |
| 127 | + id: this.safeId |
| 128 | + }, |
| 129 | + ref: 'canvas' |
| 130 | + } |
| 131 | + ) |
| 132 | + ] |
| 133 | + ) |
| 134 | + } |
| 135 | +} |
| 136 | +</script> |
| 137 | + |
| 138 | +<style> |
| 139 | + @import "~@coreui/coreui-chartjs/dist/css/coreui-chartjs.css"; |
| 140 | +</style> |
0 commit comments