|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace backend\widgets\chart\chartjs; |
|
4
|
|
|
|
|
5
|
|
|
use backend\widgets\chart\chartjs\assets\ChartJsAsset; |
|
6
|
|
|
use yii\helpers\ArrayHelper; |
|
7
|
|
|
use yii\helpers\Html; |
|
8
|
|
|
use yii\helpers\Json; |
|
9
|
|
|
use yii\base\Widget; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Chart |
|
13
|
|
|
* |
|
14
|
|
|
* @package backend\widgets\chart\chartjs |
|
15
|
|
|
*/ |
|
16
|
|
|
class Chart extends Widget |
|
17
|
|
|
{ |
|
18
|
|
|
const TYPE_LINE = 'line'; |
|
19
|
|
|
const TYPE_BAR = 'bar'; |
|
20
|
|
|
const TYPE_RADAR = 'radar'; |
|
21
|
|
|
const TYPE_PIE = 'pie'; |
|
22
|
|
|
const TYPE_DOUGHNUT = 'doughnut'; |
|
23
|
|
|
const TYPE_POLAR_AREA = 'polarArea'; |
|
24
|
|
|
const TYPE_BUBBLE = 'bubble'; |
|
25
|
|
|
const TYPE_SCATTER = 'scatter'; |
|
26
|
|
|
|
|
27
|
|
|
/** @var bool */ |
|
28
|
|
|
public $status = true; |
|
29
|
|
|
/** @var array */ |
|
30
|
|
|
public $containerOptions = []; |
|
31
|
|
|
/** @var array */ |
|
32
|
|
|
public $clientOptions = []; |
|
33
|
|
|
/** @var array */ |
|
34
|
|
|
public $clientData = []; |
|
35
|
|
|
/** @var string */ |
|
36
|
|
|
public $type; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritDoc |
|
40
|
|
|
*/ |
|
41
|
|
|
public function init() |
|
42
|
|
|
{ |
|
43
|
|
|
parent::init(); |
|
44
|
|
|
if ($this->status === true) { |
|
45
|
|
|
$this->registerAsset(); |
|
46
|
|
|
} |
|
47
|
|
|
$this->id = $this->id ?: $this->getId(); |
|
48
|
|
|
$this->type = $this->type ?: self::TYPE_LINE; |
|
49
|
|
|
ArrayHelper::setValue($this->containerOptions, 'id', $this->id); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Run |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
public function run() |
|
58
|
|
|
{ |
|
59
|
|
|
return Html::tag('canvas', '', $this->containerOptions); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Client Options |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getClientOptions() |
|
68
|
|
|
{ |
|
69
|
|
|
$clientOptions = []; |
|
70
|
|
|
return ArrayHelper::merge($clientOptions, $this->clientOptions); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Client Data |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getClientData() |
|
79
|
|
|
{ |
|
80
|
|
|
$clientData = []; |
|
81
|
|
|
return ArrayHelper::merge($clientData, $this->clientData); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Register Asset |
|
86
|
|
|
*/ |
|
87
|
|
|
public function registerAsset() |
|
88
|
|
|
{ |
|
89
|
|
|
$view = $this->getView(); |
|
90
|
|
|
ChartJsAsset::register($view); |
|
91
|
|
|
$clientData = Json::encode($this->getClientData()); |
|
92
|
|
|
$clientOptions = Json::encode($this->getClientOptions()); |
|
93
|
|
|
$type = $this->type; |
|
94
|
|
|
$script = " |
|
95
|
|
|
let ctx_{$this->id} = document.getElementById('{$this->id}').getContext('2d'); |
|
96
|
|
|
let jsChart_{$this->id} = new Chart(ctx_{$this->id}, { |
|
97
|
|
|
type: '{$type}', |
|
98
|
|
|
data: {$clientData}, |
|
99
|
|
|
options: {$clientOptions} |
|
100
|
|
|
}); |
|
101
|
|
|
"; |
|
102
|
|
|
$view->registerJs($script); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|