-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBarChart.php
144 lines (126 loc) · 3.54 KB
/
BarChart.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace bsadnu\googlecharts;
use Yii;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\View;
use bsadnu\googlecharts\GoogleJsApiAsset;
/**
* Bar chart widget.
* A bar graph is a chart that uses horizontal bars to show comparisons among categories.
* One axis of the chart shows the specific categories being compared, and the other axis represents a discrete value.
* Like all Google charts, column charts display tooltips when the user hovers over the data.
* By default, text labels are hidden, but can be turned on in chart settings.
*
* @author Stanislav Bannikov <bsadnu@gmail.com>
*/
class BarChart extends Widget
{
/**
* @var string unique id of chart
*/
public $id;
/**
* @var array table of data
* Example:
* [
* ['Year', 'Sales', 'Expenses'],
* ['2004', 1000, 400],
* ['2005', 1170, 460],
* ['2006', 660, 1120],
* ['2007', 1030, 540]
* ]
*/
public $data = [];
/**
* @var array options
* Example:
* [
* 'fontName' => 'Verdana',
* 'height' => 400,
* 'fontSize' => 12,
* 'chartArea' => [
* 'left' => '5%',
* 'width' => '90%',
* 'height' => 350
* ],
* 'tooltip' => [
* 'textStyle' => [
* 'fontName' => 'Verdana',
* 'fontSize' => 13
* ]
* ],
* 'vAxis' => [
* 'gridlines' => [
* 'color' => '#e5e5e5',
* 'count' => 10
* ],
* 'minValue' => 0
* ],
* 'legend' => [
* 'position' => 'top',
* 'alignment' => 'center',
* 'textStyle' => [
* 'fontSize' => 12
* ]
* ]
* ]
*/
public $options = [];
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
$view = Yii::$app->getView();
$this->registerAssets();
$view->registerJs($this->getJs(), View::POS_END);
}
/**
* Renders the widget.
*/
public function run()
{
$content = Html::tag('div', null, ['id'=> $this->id]);
return $content;
}
/**
* Registers necessary assets.
*/
public function registerAssets()
{
$view = $this->getView();
GoogleJsApiAsset::register($view);
}
/**
* Return necessary javascript.
*/
private function getJs()
{
$uniqueInt = mt_rand(1, 999999);
$js = "
google.load('visualization', '1', {packages:['corechart']});
google.setOnLoadCallback(drawBar". $uniqueInt .");
";
$js .= "
function drawBar". $uniqueInt ."() {
var data". $uniqueInt ." = google.visualization.arrayToDataTable(". Json::encode($this->data) .");
var options_bar". $uniqueInt ." = ". Json::encode($this->options) .";
var bar". $uniqueInt ." = new google.visualization.BarChart($('#". $this->id ."')[0]);
bar". $uniqueInt .".draw(data". $uniqueInt .", options_bar". $uniqueInt .");
}
";
$js .= "
$(function () {
$(window).on('resize', resize);
$('.sidebar-control').on('click', resize);
function resize() {
drawBar". $uniqueInt ."();
}
});
";
return $js;
}
}