forked from yii2mod/yii2-c3-chart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChart.php
61 lines (54 loc) · 1.33 KB
/
Chart.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
58
59
60
61
<?php
namespace yii2mod\c3\chart;
use Yii;
use yii\bootstrap\Widget;
use yii\helpers\Html;
use yii\helpers\Json;
/**
* Yii wrapper for D3-based reusable chart library `c3`
* ~~~
* echo \app\widgets\chart\Chart::widget([
* 'clientOptions' => [
* 'data' => [
* 'columns' => [
* ['data2', 130, 300, 200, 300, 250, 450]
* ]
* ]
* ]
* ])
* ~~~
*/
class Chart extends Widget
{
/**
* Executes the widget.
* @return string the result of widget execution to be outputted.
*/
public function run()
{
echo Html::tag('div', '', ['id' => $this->options['id']]);
$this->registerAssets();
parent::run();
}
/**
* Register assets
*/
protected function registerAssets()
{
$id = $this->options['id'];
$view = $this->getView();
ChartAsset::register($view);
$options = Json::encode($this->getClientOptions());
$view->registerJs("var {$id} = c3.generate({$options});", $view::POS_END);
}
/**
* Get client options
* @return string
*/
protected function getClientOptions()
{
$id = $this->options['id'];
$this->clientOptions['bindto'] = '#' . $id;
return $this->clientOptions;
}
}