Skip to content

Commit e0c0418

Browse files
committed
[IMP] awesome_dashboard: Add pie chart
1 parent d704def commit e0c0418

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

awesome_dashboard/static/src/dashboard.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import { registry } from "@web/core/registry";
55
import { Layout } from "@web/search/layout";
66
import { useService } from "@web/core/utils/hooks";
77
import { DashboardItem } from "./dashboard_item";
8+
import { PieChart } from "./pie_chart";
89

910
class AwesomeDashboard extends Component {
1011
static template = "awesome_dashboard.AwesomeDashboard";
11-
static components = { Layout, DashboardItem };
12+
static components = { Layout, DashboardItem, PieChart };
1213

1314
setup(){
1415
this.action = useService("action");

awesome_dashboard/static/src/dashboard.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
<t t-esc="statistics.total_amount"/>
3939
</div>
4040
</DashboardItem>
41+
<DashboardItem size="2">
42+
Shirt orders by size
43+
<PieChart data="statistics['orders_by_size']" label="'Shirt orders by size'"/>
44+
</DashboardItem>
4145
</div>
4246
</Layout>
4347
</t>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { loadJS } from "@web/core/assets";
2+
import { getColor } from "@web/core/colors/colors";
3+
import { Component, onWillStart, onWillUnmount, onMounted, useRef } from "@odoo/owl";
4+
5+
export class PieChart extends Component {
6+
static template = "awesome_dashboard.PieChart";
7+
static props = {
8+
label: { type: String, optional: true },
9+
data: { type: Object, optional: true },
10+
};
11+
12+
setup(){
13+
this.canvasRef = useRef("canvas");
14+
onWillStart (() => loadJS("/web/static/lib/Chart/Chart.js"));
15+
onMounted(() => {
16+
this.renderChart();
17+
});
18+
onWillUnmount(() => {
19+
this.chart.destroy();
20+
});
21+
};
22+
23+
renderChart(){
24+
const labels = Object.keys(this.props.data);
25+
const data = Object.values(this.props.data);
26+
const color = labels.map((_, index) => getColor(index));
27+
this.chart = new Chart(this.canvasRef.el, {
28+
type: "pie",
29+
data: {
30+
labels: labels,
31+
datasets: [
32+
{
33+
data: data,
34+
backgroundColor: color,
35+
},
36+
],
37+
},
38+
});
39+
}
40+
}
41+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<templates xml:space="preserve">
3+
<t t-name="awesome_dashboard.PieChart">
4+
<div t-att-class="'h-100' + props.class" t-ref="root">
5+
<div class="h-100 posiion-relative" t-ref="container">
6+
<canvas t-ref="canvas"/>
7+
</div>
8+
</div>
9+
</t>
10+
</templates>

0 commit comments

Comments
 (0)