|
| 1 | +import { Pie } from '../../../../../config/vue-chartjs.js'; |
| 2 | +import { optionalChain } from '../../../../../utils/object.utils.js'; |
| 3 | +import { colorize, transparentize } from '../charts.utils.js'; |
| 4 | + |
| 5 | +const _defaultChartOptions = { |
| 6 | + lowerIsBetter: false, |
| 7 | +}; |
| 8 | + |
| 9 | +const _SimplePieChart = { |
| 10 | + computed: { |
| 11 | + chartData: function() { |
| 12 | + const data = this.sortedData; |
| 13 | + const backColors = data.map(x => this.normalColorize(x)); |
| 14 | + |
| 15 | + const res = { |
| 16 | + datasets: [ |
| 17 | + { |
| 18 | + data: data, |
| 19 | + backgroundColor: backColors, |
| 20 | + hoverColor: backColors.map(x => this.hoverColorize(x)), |
| 21 | + }, |
| 22 | + ], |
| 23 | + labels: this.context.labels, |
| 24 | + }; |
| 25 | + |
| 26 | + return res; |
| 27 | + }, |
| 28 | + sortedData: function() { |
| 29 | + let data = optionalChain(() => [...this.context.data], { fallbackValue: [] }).sort( |
| 30 | + (a, b) => a - b |
| 31 | + ); |
| 32 | + |
| 33 | + if (this.options.lowerIsBetter) { |
| 34 | + data = data.reverse(); |
| 35 | + } |
| 36 | + |
| 37 | + return data; |
| 38 | + }, |
| 39 | + }, |
| 40 | + extends: Pie, |
| 41 | + methods: { |
| 42 | + normalColorize: function(value) { |
| 43 | + const data = this.sortedData; |
| 44 | + const min = Math.min(...data) || 0; |
| 45 | + const max = Math.max(...data) || 100; |
| 46 | + return colorize(value, { range: [min, max], lowerIsBetter: this.options.lowerIsBetter }); |
| 47 | + }, |
| 48 | + hoverColorize: function(color) { |
| 49 | + return transparentize(color); |
| 50 | + }, |
| 51 | + refresh: function() { |
| 52 | + this.styles = { width: '100%', height: '100%', position: 'relative', ...this.styles }; |
| 53 | + this.renderChart(this.chartData, { |
| 54 | + maintainAspectRatio: false, |
| 55 | + }); |
| 56 | + }, |
| 57 | + }, |
| 58 | + mounted() { |
| 59 | + this.refresh(); |
| 60 | + }, |
| 61 | + props: { |
| 62 | + context: { |
| 63 | + type: Object, |
| 64 | + default: { |
| 65 | + data: [], |
| 66 | + labels: [], |
| 67 | + }, |
| 68 | + }, |
| 69 | + options: { |
| 70 | + type: Object, |
| 71 | + default: _defaultChartOptions, |
| 72 | + }, |
| 73 | + }, |
| 74 | + watch: { |
| 75 | + context: function() { |
| 76 | + this.refresh(); |
| 77 | + }, |
| 78 | + }, |
| 79 | +}; |
| 80 | + |
| 81 | +export const SimplePieChart = _SimplePieChart; |
| 82 | +export default _SimplePieChart; |
0 commit comments