-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChartJs.php
57 lines (49 loc) · 1.32 KB
/
ChartJs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
* Created by PhpStorm.
* User: phpNT - http://phpnt.com
* Date: 27.04.2017
* Time: 9:26
*/
namespace phpnt\chartJS;
use yii\bootstrap\Widget;
use yii\helpers\Json;
class ChartJs extends Widget
{
const TYPE_LINE = 'line';
const TYPE_BAR = 'bar';
const TYPE_RADAR = 'radar';
const TYPE_POLAR_AREA = 'polarArea';
const TYPE_PIE = 'pie';
const TYPE_DOUGHNUT = 'doughnut';
const TYPE_BUBBLE = 'bubble';
public $type;
public $data = [];
public $options = [];
public function init()
{
parent::init();
$this->type = $this->type ? $this->type : 'line';
$this->data = Json::encode($this->data);
$this->options = Json::encode($this->options);
}
public function run()
{
$this->registerScript();
echo '<canvas id="'.$this->id.'"></canvas>';
}
public function registerScript()
{
$view = $this->getView();
ChartJSAsset::register($view);
$js = <<< JS
var ctx = $("#$this->id");
var myChart = new Chart(ctx, {
type: '$this->type',
data: $this->data,
options: $this->options
});
JS;
$view->registerJs($js);
}
}