-
Notifications
You must be signed in to change notification settings - Fork 1
/
JqPlot.php
156 lines (141 loc) · 4.4 KB
/
JqPlot.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
<?php
namespace sizeg\jqplot;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\jui\Widget;
use yii\web\View;
/**
* JqPlot widget renders charts and graphs for jQuery.
*
* For example:
*
* ```php
* echo JqPlot::widget([
* 'data' => [[1, 2],[3,5.12],[5,13.1],[7,33.6],[9,85.9],[11,219.9]]
* ]);
* ```
*
* The following example will render a bar chart:
*
* ```php
* echo sizeg\jqplot\JqPlot::widget([
* 'data' => [
* [2, 6, 7, 10],
* [7, 5, 3, 2],
* [14, 9, 3, 8],
* ],
* 'clientOptions' => [
* 'stackSeries' => true,
* 'captureRightClick' => true,
* 'seriesDefaults' => [
* 'renderer' => new \yii\web\JsExpression("$.jqplot.BarRenderer"),
* 'rendererOptions' => [
* 'highlightMouseDown' => true,
* ],
* 'pointLabels' => [
* 'show' => true,
* ],
* ],
* 'legend' => [
* 'show' => true,
* 'location' => 'e',
* 'placement' => 'outside',
* ]
* ]
* ]);
* ```
*
* @see http://www.jqplot.com/
* @author Dmitry Demin <sizemail@gmail.com>
* @since 1.0.0-a
*/
class JqPlot extends Widget
{
/**
* Pluging name
*/
const NAME = 'jqplot';
/**
* @var array graph data
*/
public $data = [];
/**
* Initializes the widget
*/
public function init()
{
parent::init();
echo Html::beginTag('div', $this->options) . "\n";
}
/**
* @inheritdoc
*/
public function run()
{
echo Html::endTag('div') . "\n";
$this->registerJqPlotWidget();
}
/**
* Registers a specific jqPlot jQuery widget asset bundle, initializes it with client options and registers related events
* @param string $id the ID of the widget. If null, it will use the `id` value of [[options]].
*/
protected function registerJqPlotWidget()
{
JqPlotAsset::register($this->getView());
$this->registerClientEvents(static::NAME, $this->options['id']);
$this->registerJqPlotClientOptions($this->options['id']);
}
/**
* Registers a specific jQuery jqPlot widget options
* @param string $id the ID of the widget
*/
protected function registerJqPlotClientOptions($id)
{
$this->registerDependenciesRecursively($this->clientOptions);
$data = Json::htmlEncode($this->data);
$options = !empty($this->clientOptions) ? Json::htmlEncode($this->clientOptions) : '{}';
$js = "jQuery('#" . $id . "')." . static::NAME . "(" . $data . ", " . $options . ")";
$this->getView()->registerJs($js, View::POS_END);
}
/**
* Find renderers and register their JS plugins
* @param array $data
*/
public function registerDependenciesRecursively($data)
{
// Looking for renderers
foreach ($data as $k => $v) {
if ($k == 'renderer' || $k == 'tickRenderer') {
$this->registerRendererJsFile($v);
} elseif (is_array($v) && isset($v['show']) && (boolean)$v['show']) {
Yii::$app->assetManager->bundles[JqPlotAsset::className()]->js[] = 'plugins/jqplot.' . $k . '.js';
} elseif (is_array($v)) {
$this->registerDependenciesRecursively($v);
}
}
}
/**
* Registers additional jqPlot JS plugins
* @param string $renderer plugin jQuery name
*/
public function registerRendererJsFile($renderer)
{
if (strpos($renderer, 'BezierCurveRenderer') !== false) {
$url = 'plugins/jqplot.BezierCurveRenderer.js';
} else {
list($jqPrefix, $jqPlot, $name) = explode('.', $renderer);
$url = 'plugins/jqplot.' . lcfirst($name) . '.js';
}
// Additional dependencies
if ($name == 'CanvasAxisLabelRenderer' || $name == 'CanvasAxisTickRenderer') {
$additionalUrl = 'plugins/jqplot.canvasTextRenderer.js';
if (!in_array($additionalUrl, Yii::$app->assetManager->bundles[JqPlotAsset::className()]->js)) {
Yii::$app->assetManager->bundles[JqPlotAsset::className()]->js[] = $additionalUrl;
}
}
if (!in_array($url, Yii::$app->assetManager->bundles[JqPlotAsset::className()]->js)) {
Yii::$app->assetManager->bundles[JqPlotAsset::className()]->js[] = $url;
}
}
}