1
- import { loadJS } from "@web/core/assets" ;
2
- import { getColor } from "@web/core/colors/colors" ;
3
- import { Component , onWillStart , useRef , onMounted , onWillUnmount } from "@odoo/owl" ;
1
+ import { loadJS } from "@web/core/assets" ;
2
+ import { getColor } from "@web/core/colors/colors" ;
3
+ import { Component , onWillStart , useRef , onMounted , onWillUnmount } from "@odoo/owl" ;
4
+ import { useService } from "@web/core/utils/hooks" ;
4
5
5
- export class PieChart extends Component {
6
- static template = "awesome_dashboard.PieChart" ;
7
- static props = {
8
- label : String ,
9
- data : Object ,
10
- } ;
6
+ export class PieChart extends Component {
7
+ static template = "awesome_dashboard.PieChart" ;
8
+ static props = {
9
+ label : String ,
10
+ data : Object ,
11
+ } ;
11
12
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
- if ( this . chart ) {
20
- this . chart . destroy ( ) ;
21
- }
22
- } ) ;
23
- }
13
+ setup ( ) {
14
+ this . canvasRef = useRef ( "canvas" ) ;
15
+ this . actionService = useService ( "action" ) ;
16
+ onWillStart ( ( ) => loadJS ( "/web/static/lib/Chart/Chart.js" ) ) ;
17
+ onMounted ( ( ) => {
18
+ this . renderChart ( ) ;
19
+ } ) ;
20
+ onWillUnmount ( ( ) => {
21
+ if ( this . chart ) {
22
+ this . chart . destroy ( ) ;
23
+ }
24
+ } ) ;
25
+ }
24
26
25
- renderChart ( ) {
26
- const labels = Object . keys ( this . props . data ) ;
27
- const data = Object . values ( this . props . data ) ;
28
- const color = labels . map ( ( _ , index ) => getColor ( index ) ) ;
29
- this . chart = new Chart ( this . canvasRef . el , {
30
- type : "pie" ,
31
- data : {
32
- labels : labels ,
33
- datasets : [
34
- {
35
- label : this . props . label ,
36
- data : data ,
37
- backgroundColor : color ,
27
+ renderChart ( ) {
28
+ const labels = Object . keys ( this . props . data ) ;
29
+ const data = Object . values ( this . props . data ) ;
30
+ const color = labels . map ( ( _ , index ) => getColor ( index ) ) ;
31
+ this . chart = new Chart ( this . canvasRef . el , {
32
+ type : "pie" ,
33
+ data : {
34
+ labels : labels ,
35
+ datasets : [
36
+ {
37
+ label : this . props . label ,
38
+ data : data ,
39
+ backgroundColor : color ,
40
+ } ,
41
+ ] ,
42
+ } ,
43
+ options : {
44
+ onClick : ( event , elements ) => {
45
+ if ( elements . length > 0 ) {
46
+ const index = elements [ 0 ] . index ;
47
+ const size = labels [ index ] ;
48
+ this . openOrdersBySize ( size ) ;
49
+ }
38
50
} ,
39
- ] ,
40
- } ,
41
- } ) ;
42
- }
43
- }
51
+ } ,
52
+ } ) ;
53
+ }
54
+
55
+ openOrdersBySize ( size ) {
56
+ this . actionService . doAction ( {
57
+ type : "ir.actions.act_window" ,
58
+ name : `Orders - Size ${ size } ` ,
59
+ res_model : "sale.order" , //just to demonstrate
60
+ views : [ [ false , "list" ] ] ,
61
+ domain : [ [ "id" , "=" , - 1 ] ] , // always empty (demo only)
62
+ } ) ;
63
+ }
64
+ }
0 commit comments