-
Notifications
You must be signed in to change notification settings - Fork 3
/
ScatterChart.php
211 lines (185 loc) · 6.87 KB
/
ScatterChart.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?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;
/**
* Scatter chart widget.
* A scatter chart is a type of mathematical diagram using Cartesian coordinates to display values for two variables for a set of data.
* The data is displayed as a collection of points,
* each having the value of one variable determining the position on the horizontal axis and the value of the other variable determining the position on the vertical axis.
* When the user hovers over the points, tooltips are displayed with more information.
*
* @author Stanislav Bannikov <[email protected]>
*/
class ScatterChart extends Widget
{
/**
* @var string unique id of chart
*/
public $id;
/**
* @var array table of data
* Counts as old or previos dataset in diff scatter chart case
* Example:
* [
* ['', 'Medicine 1', 'Medicine 2'],
* [23, null, 12], [9, null, 39], [15, null, 28],
* [37, null, 30], [21, null, 14], [12, null, 18],
* [29, null, 34], [ 8, null, 12], [38, null, 28],
* [35, null, 12], [26, null, 10], [10, null, 29],
* [11, null, 10], [27, null, 38], [39, null, 17],
* [34, null, 20], [38, null, 5], [33, null, 27],
* [23, null, 39], [12, null, 10], [ 8, 15, null],
* [39, 15, null], [27, 31, null], [30, 24, null],
* [31, 39, null], [35, 6, null], [ 5, 5, null],
* [19, 39, null], [22, 8, null], [19, 23, null],
* [27, 20, null], [11, 6, null], [34, 33, null],
* [38, 8, null], [39, 29, null], [13, 23, null],
* [13, 36, null], [39, 6, null], [14, 37, null], [13, 39, null]
* ]
*/
public $data = [];
/**
* @var array table of extra data necessary for diff scatter chart types
* Counts as new dataset in diff scatter chart case
*
* A diff chart is a chart designed to highlight the differences between two charts with comparable data.
* By making the changes between analogous values prominent, they can reveal variations between datasets.
* You create a diff chart by calling the computeDiff method with two datasets to generate a third dataset representing the diff, and then drawing that.
* Supports all scatter chart options, as it jsut combines 2 charts into 1.
*
* Example:
* [
* ['', 'Medicine 1', 'Medicine 2'],
* [22, null, 12], [7, null, 40], [14, null, 31],
* [37, null, 30], [18, null, 17], [9, null, 20],
* [26, null, 36], [5, null, 13], [36, null, 30],
* [35, null, 15], [24, null, 12], [7, null, 31],
* [10, null, 12], [24, null, 40], [37, null, 18],
* [32, null, 21], [35, null, 7], [31, null, 30],
* [21, null, 42], [12, null, 10], [10, 13, null],
* [40, 12, null], [28, 29, null], [32, 22, null],
* [31, 37, null], [38, 5, null], [6, 4, null],
* [21, 36, null], [22, 8, null], [21, 22, null],
* [28, 17, null], [12, 5, null], [37, 30, null],
* [41, 7, null], [41, 27, null], [15, 20, null],
* [14, 36, null], [42, 3, null], [14, 37, null], [15, 36, null]
* ]
*/
public $extraData = [];
/**
* @var array options
* Example:
* [
* 'fontName' => 'Verdana',
* 'height' => 450,
* 'fontSize' => 12,
* 'chartArea' => [
* 'left' => '5%',
* 'width' => '90%',
* 'height' => 400
* ],
* 'tooltip' => [
* 'textStyle' => [
* 'fontName' => 'Verdana',
* 'fontSize' => 13
* ]
* ],
* 'hAxis' => [
* 'minValue' => 0
* ],
* 'vAxis' => [
* 'gridlines' => [
* 'color' => '#e5e5e5',
* 'count' => 5
* ],
* 'minValue' => 0
* ],
* 'legend' => [
* 'position' => 'top',
* 'alignment' => 'center',
* 'textStyle' => [
* 'fontSize' => 12
* ]
* ],
* 'diff' => [
* 'oldData' => [
* 'opacity' => 0.5
* ]
* ]
* ]
*/
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(drawScatter". $uniqueInt .");
";
if (!empty($this->extraData)) {
$js .= "
function drawScatter". $uniqueInt ."() {
var oldData". $uniqueInt ." = google.visualization.arrayToDataTable(". Json::encode($this->data) .");
var newData". $uniqueInt ." = google.visualization.arrayToDataTable(". Json::encode($this->extraData) .");
var options". $uniqueInt ." = ". Json::encode($this->options) .";
var scatter". $uniqueInt ." = new google.visualization.ScatterChart($('#". $this->id ."')[0]);
var diffData". $uniqueInt ." = scatter". $uniqueInt .".computeDiff(oldData". $uniqueInt .", newData". $uniqueInt .");
scatter". $uniqueInt .".draw(diffData". $uniqueInt .", options". $uniqueInt .");
}
";
} else {
$js .= "
function drawScatter". $uniqueInt ."() {
var data". $uniqueInt ." = google.visualization.arrayToDataTable(". Json::encode($this->data) .");
var options". $uniqueInt ." = ". Json::encode($this->options) .";
var scatter". $uniqueInt ." = new google.visualization.ScatterChart($('#". $this->id ."')[0]);
scatter". $uniqueInt .".draw(data". $uniqueInt .", options". $uniqueInt .");
}
";
}
$js .= "
$(function () {
$(window).on('resize', resize);
$('.sidebar-control').on('click', resize);
function resize() {
drawScatter". $uniqueInt ."();
}
});
";
return $js;
}
}