odoo-tutorials/awesome_dashboard/static/src/piechart/piechart.js

45 lines
1.2 KiB
JavaScript

/** @odoo-module **/
import { loadJS } from "@web/core/assets";
import { getColor } from "@web/core/colors/colors";
import { Component, onWillStart, useRef, onMounted, onWillUnmount } from "@odoo/owl";
export class Piechart extends Component {
static template = "awesome_dashboard.Piechart";
static props = {
label: String,
data: Object,
};
setup() {
this.canvasRef = useRef("canvas");
onWillStart(() => loadJS(["/web/static/lib/Chart/Chart.js"]));
onMounted(() => {
this.renderChart();
});
onWillUnmount(() => {
this.chart.destroy();
});
}
renderChart() {
const labels = Object.keys(this.props.data);
const data = Object.values(this.props.data);
const color = labels.map((_, index) => getColor(index));
this.chart = new Chart(this.canvasRef.el, {
type: "pie",
data: {
labels: labels,
datasets: [
{
label: this.props.label,
data: data,
backgroundColor: color,
},
],
},
});
}
}