|
| 1 | +/** @odoo-module **/ |
| 2 | + |
| 3 | +import {Component, onMounted, onWillStart, useRef} from "@odoo/owl"; |
| 4 | +import {loadBundle} from "@web/core/assets"; |
| 5 | + |
| 6 | +export class ChartComponent extends Component { |
| 7 | + setup() { |
| 8 | + onMounted(() => this.renderChart()); |
| 9 | + this.canvasRef = useRef("canvas"); |
| 10 | + |
| 11 | + this.chartTitle = ""; |
| 12 | + const chartTypesWithTitle = ["pie", "doughnut"]; |
| 13 | + if (chartTypesWithTitle.includes(this.props.chart_type)) { |
| 14 | + this.chartTitle = this.props.data_label; |
| 15 | + } |
| 16 | + |
| 17 | + onWillStart(async () => { |
| 18 | + return Promise.all([ |
| 19 | + // Load external JavaScript and CSS libraries. |
| 20 | + loadBundle({ |
| 21 | + jsLibs: [ |
| 22 | + // "/awesome_dashboard/static/lib/chart-js.4.4.4/chart.umd.min.js", |
| 23 | + "https://cdn.jsdelivr.net/npm/chart.js@4.4.4/dist/chart.umd.min.js", |
| 24 | + ], |
| 25 | + }), |
| 26 | + ]); |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + renderChart() { |
| 31 | + const ctx = this.canvasRef.el.getContext("2d"); |
| 32 | + |
| 33 | + new Chart(ctx, { |
| 34 | + type: this.props.chart_type, |
| 35 | + data: { |
| 36 | + labels: this.props.labels, |
| 37 | + datasets: [ |
| 38 | + { |
| 39 | + label: this.props.data_label, |
| 40 | + data: this.props.data, |
| 41 | + backgroundColor: this.props.backgroundColor, |
| 42 | + hoverOffset: 2, |
| 43 | + }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + options: {...this.props.options}, |
| 47 | + }); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +ChartComponent.template = "spp_dashboard_base.ChartComponentTemplate"; |
| 52 | +ChartComponent.props = { |
| 53 | + chart_type: {type: String, optional: true}, |
| 54 | + labels: {type: Array, optional: true}, |
| 55 | + data_label: {type: String, optional: true}, |
| 56 | + data: {type: Array, optional: true}, |
| 57 | + backgroundColor: {type: Array, optional: true}, |
| 58 | + options: {type: Object, optional: true}, |
| 59 | + size: {type: String, optional: true}, |
| 60 | +}; |
| 61 | +ChartComponent.defaultProps = { |
| 62 | + chart_type: "pie", |
| 63 | + labels: ["Red", "Blue", "Yellow"], |
| 64 | + data_label: "Number of Colors", |
| 65 | + data: [300, 50, 150], |
| 66 | + backgroundColor: ["rgb(255, 99, 132)", "rgb(54, 162, 235)", "rgb(255, 205, 86)"], |
| 67 | + options: { |
| 68 | + maintainAspectRatio: true, |
| 69 | + aspectRatio: 2, |
| 70 | + }, |
| 71 | + size: "col-md-6", |
| 72 | +}; |
0 commit comments