From f89514d21fea8481dbd2b2c8c16fbbef80b7ae92 Mon Sep 17 00:00:00 2001 From: Matt Marcha Date: Fri, 20 Sep 2024 15:14:57 -1000 Subject: [PATCH] ch2.6 ~JS deserve to burn in hell~ Display a pie chart --- awesome_dashboard/static/src/dashboard.js | 3 +- awesome_dashboard/static/src/dashboard.xml | 17 ++----- .../static/src/piechart/piechart.js | 45 +++++++++++++++++++ .../static/src/piechart/piechart.xml | 11 +++++ 4 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 awesome_dashboard/static/src/piechart/piechart.js create mode 100644 awesome_dashboard/static/src/piechart/piechart.xml diff --git a/awesome_dashboard/static/src/dashboard.js b/awesome_dashboard/static/src/dashboard.js index 5d03968..6ace3f8 100644 --- a/awesome_dashboard/static/src/dashboard.js +++ b/awesome_dashboard/static/src/dashboard.js @@ -5,10 +5,11 @@ import { registry } from "@web/core/registry"; import { Layout } from "@web/search/layout"; import { useService } from "@web/core/utils/hooks" import { DashboardItem } from "./dashboard_item"; +import { Piechart } from "./piechart/piechart"; class AwesomeDashboard extends Component { static template = "awesome_dashboard.AwesomeDashboard"; - static components = { Layout, DashboardItem }; + static components = { Layout, DashboardItem, Piechart}; setup() { this.display = { diff --git a/awesome_dashboard/static/src/dashboard.xml b/awesome_dashboard/static/src/dashboard.xml index 3ac9eec..fd3586f 100644 --- a/awesome_dashboard/static/src/dashboard.xml +++ b/awesome_dashboard/static/src/dashboard.xml @@ -2,19 +2,6 @@ - - - - - Total amount of new orders this month - - - - - - - - @@ -51,6 +38,10 @@ + + Shirt orders by size + + diff --git a/awesome_dashboard/static/src/piechart/piechart.js b/awesome_dashboard/static/src/piechart/piechart.js new file mode 100644 index 0000000..b5549a6 --- /dev/null +++ b/awesome_dashboard/static/src/piechart/piechart.js @@ -0,0 +1,45 @@ +/** @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, + }, + ], + }, + }); + } + + +} \ No newline at end of file diff --git a/awesome_dashboard/static/src/piechart/piechart.xml b/awesome_dashboard/static/src/piechart/piechart.xml new file mode 100644 index 0000000..9c65b69 --- /dev/null +++ b/awesome_dashboard/static/src/piechart/piechart.xml @@ -0,0 +1,11 @@ + + + + +
+
+ +
+
+
+
\ No newline at end of file