Skip to content

Commit 8750017

Browse files
committed
[IMP] awesome_dashboard: Add pie chart
1 parent 37acf5f commit 8750017

File tree

5 files changed

+83
-4
lines changed

5 files changed

+83
-4
lines changed

awesome_dashboard/static/src/dashboard.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { registry } from "@web/core/registry";
33
import { useService } from "@web/core/utils/hooks";
44
import { Layout } from "@web/search/layout";
55
import { DashboardItem } from "./dashboard_item/dashboard_item";
6+
import { PieChart } from './pie_chart/pie_chart';
67

78
class AwesomeDashboard extends Component {
89
static template = "awesome_dashboard.AwesomeDashboard";
9-
static components = { Layout, DashboardItem };
10+
static components = { Layout, DashboardItem, PieChart };
1011

1112
setup() {
1213
this.action = useService("action");

awesome_dashboard/static/src/dashboard.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Leads
1515
</button>
1616
</t>
17-
<div>
17+
<div class="d-flex">
1818
<DashboardItem>
1919
Average amount of t-shirt by order this month
2020
<t t-set-slot="value" t-esc="statistics.average_quantity"/>
@@ -28,7 +28,7 @@
2828
<t t-set-slot="value" t-esc="statistics.nb_new_orders"/>
2929
</DashboardItem>
3030
</div>
31-
<div>
31+
<div class="d-flex align-items-start">
3232
<DashboardItem>
3333
Number of cancelled orders this month
3434
<t t-set-slot="value" t-esc="statistics.nb_cancelled_orders"/>
@@ -37,6 +37,12 @@
3737
Total amount of new orders this month
3838
<t t-set-slot="value" t-esc="statistics.total_amount"/>
3939
</DashboardItem>
40+
<DashboardItem size="2">
41+
Shirt orders by size
42+
<t t-set-slot="value">
43+
<PieChart data="statistics.orders_by_size"/>
44+
</t>
45+
</DashboardItem>
4046
</div>
4147
</Layout>
4248
</t>

awesome_dashboard/static/src/dashboard_item/dashboard_item.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<templates xml:space="preserve">
33

44
<t t-name="awesome_dashboard.DashboardItem">
5-
<div class="card d-inline-block m-2" t-attf-style="width: {{props.size*18}}rem;">
5+
<div class="card d-inline-block m-4" t-attf-style="width: {{props.size*18}}rem;">
66
<div class="card-body text-center">
77
<div class="card-title">
88
<h5>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {
2+
Component,
3+
onWillStart,
4+
onWillUnmount,
5+
useRef,
6+
useEffect,
7+
} from "@odoo/owl";
8+
import { loadJS } from "@web/core/assets";
9+
10+
export class PieChart extends Component {
11+
static template = "awesome_dashboard.PieChart";
12+
static props = { data: { optional: true } }
13+
14+
setup() {
15+
this.rootRef = useRef("root");
16+
this.canvasRef = useRef("canvas");
17+
this.containerRef = useRef("container");
18+
19+
this.chart = null;
20+
this.tooltip = null;
21+
this.legendTooltip = null;
22+
23+
onWillStart(async () => {
24+
await loadJS("/web/static/lib/Chart/Chart.js")
25+
});
26+
27+
useEffect(() => {
28+
this.renderChart();
29+
});
30+
31+
onWillUnmount(this.onWillUnmount);
32+
}
33+
34+
onWillUnmount() {
35+
if (this.chart) {
36+
this.chart.destroy();
37+
}
38+
}
39+
40+
renderChart() {
41+
if (this.chart) {
42+
this.chart.destroy();
43+
}
44+
45+
const labels = Object.keys(this.props.data);
46+
const values = Object.values(this.props.data);
47+
48+
const config = {
49+
type: 'pie',
50+
data: {
51+
labels: labels,
52+
datasets: [{
53+
label: "Orders by size",
54+
data: values,
55+
}],
56+
},
57+
}
58+
59+
this.chart = new Chart(this.canvasRef.el, config);
60+
}
61+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
4+
<t t-name="awesome_dashboard.PieChart">
5+
<div t-ref="root">
6+
<canvas t-ref="canvas"/>
7+
</div>
8+
</t>
9+
10+
</templates>
11+

0 commit comments

Comments
 (0)