-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOrgChart.php
58 lines (51 loc) · 1.79 KB
/
OrgChart.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
<?php
namespace kongoon\orgchart;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\View;
class OrgChart extends Widget
{
public $htmlOptions = [];
public $setupOptions = [];
public $data = [];
public function run()
{
if(isset($this->htmlOptions['id'])){
$this->id = $this->htmlOptions['id'];
}else{
$this->id = $this->htmlOptions['id'] = $this->getId();
}
echo Html::tag('div', '', $this->htmlOptions);
if(is_string($this->data)){
$this->data = Json::decode($this->data);
}
$this->registerAsset();
parent::run();
}
protected function registerAsset()
{
$jsData = Json::encode($this->data);
$setupOptions = Json::encode($this->setupOptions);
$js = "
google.charts.load('current', {packages:[\"orgchart\"]});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('string', 'Manager');
data.addColumn('string', 'ToolTip');
// For each orgchart box, provide the name, manager, and tooltip to show.
data.addRows(
".$jsData."
);
// Create the chart.
var chart = new google.visualization.OrgChart(document.getElementById('".$this->id."'));
chart.draw(data, {allowHtml:true});
}
";
$this->view->registerJsFile('https://www.gstatic.com/charts/loader.js', ['depends' => [\yii\web\JqueryAsset::className()]]);
$this->view->registerJs($js, View::POS_READY);
}
}