mirror of
https://github.com/SARL-PACIFIC-ERP/odoo-sh-test.git
synced 2025-06-25 09:32:22 +00:00
ch2.6 ~JS deserve to burn in hell~ Display a pie chart
This commit is contained in:
parent
100ce00a11
commit
f89514d21f
|
@ -5,10 +5,11 @@ import { registry } from "@web/core/registry";
|
||||||
import { Layout } from "@web/search/layout";
|
import { Layout } from "@web/search/layout";
|
||||||
import { useService } from "@web/core/utils/hooks"
|
import { useService } from "@web/core/utils/hooks"
|
||||||
import { DashboardItem } from "./dashboard_item";
|
import { DashboardItem } from "./dashboard_item";
|
||||||
|
import { Piechart } from "./piechart/piechart";
|
||||||
|
|
||||||
class AwesomeDashboard extends Component {
|
class AwesomeDashboard extends Component {
|
||||||
static template = "awesome_dashboard.AwesomeDashboard";
|
static template = "awesome_dashboard.AwesomeDashboard";
|
||||||
static components = { Layout, DashboardItem };
|
static components = { Layout, DashboardItem, Piechart};
|
||||||
|
|
||||||
setup() {
|
setup() {
|
||||||
this.display = {
|
this.display = {
|
||||||
|
|
|
@ -2,19 +2,6 @@
|
||||||
<templates xml:space="preserve">
|
<templates xml:space="preserve">
|
||||||
|
|
||||||
<t t-name="awesome_dashboard.AwesomeDashboard">
|
<t t-name="awesome_dashboard.AwesomeDashboard">
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Total amount of new orders this month
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<Layout display="display" className="'o_dashboard h-100'">
|
<Layout display="display" className="'o_dashboard h-100'">
|
||||||
<t t-set-slot="layout-buttons">
|
<t t-set-slot="layout-buttons">
|
||||||
<button class="btn btn-primary" t-on-click="openCustomers">Customers</button>
|
<button class="btn btn-primary" t-on-click="openCustomers">Customers</button>
|
||||||
|
@ -51,6 +38,10 @@
|
||||||
<t t-out="stats.average_time"/>
|
<t t-out="stats.average_time"/>
|
||||||
</div>
|
</div>
|
||||||
</DashboardItem>
|
</DashboardItem>
|
||||||
|
<DashboardItem size='4'>
|
||||||
|
Shirt orders by size
|
||||||
|
<Piechart data="stats.orders_by_size" label="'Shirt orders by size'"/>
|
||||||
|
</DashboardItem>
|
||||||
</div>
|
</div>
|
||||||
</Layout>
|
</Layout>
|
||||||
</t>
|
</t>
|
||||||
|
|
45
awesome_dashboard/static/src/piechart/piechart.js
Normal file
45
awesome_dashboard/static/src/piechart/piechart.js
Normal file
|
@ -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,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
11
awesome_dashboard/static/src/piechart/piechart.xml
Normal file
11
awesome_dashboard/static/src/piechart/piechart.xml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<templates xml:space="preserve">
|
||||||
|
|
||||||
|
<t t-name="awesome_dashboard.Piechart">
|
||||||
|
<div t-att-class="'h-100 ' + props.class" t-ref="root">
|
||||||
|
<div class="h-100 position-relative" t-ref="container">
|
||||||
|
<canvas t-ref="canvas" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</t>
|
||||||
|
</templates>
|
Loading…
Reference in a new issue