1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class WsChart
14: {
15: 16: 17: 18:
19: protected $_labels = array();
20: 21: 22: 23:
24: protected $_datasets = array();
25: 26: 27: 28:
29: protected $_options = array();
30: 31: 32: 33:
34: protected $_attributes = array();
35: 36: 37: 38:
39: protected $_id = '';
40: 41: 42: 43:
44: protected $_chart = '';
45: 46: 47: 48:
49: protected $_width;
50: 51: 52: 53:
54: protected $_height;
55: 56: 57: 58:
59: protected $_type = 'Line';
60:
61:
62: public function __construct($width='', $height='',
63: $options=array(), $attributes=array())
64: {
65: $this->_id = uniqid('WsChart_');
66:
67:
68: if ($attributes && !is_array($attributes)) {
69: $attributes = array($attributes);
70: }
71: $this->_attributes = $attributes;
72:
73:
74: if (!empty($options)) {
75: $this->_options = $options;
76: }
77:
78: $this->_width = intval($width);
79: $this->_height = intval($height);
80: }
81:
82:
83: public function __toString()
84: {
85: $this->renderChart();
86: return $this->_chart;
87: }
88:
89:
90: 91: 92: 93:
94: public function show()
95: {
96: $this->renderChart();
97: echo $this->_chart;
98: }
99:
100:
101: 102: 103: 104: 105: 106: 107:
108: public function addLabels($label, $reset=false)
109: {
110:
111: if ($reset) {
112: $this->_labels = array();
113: }
114:
115: if (is_array($label)) {
116: $this->_labels = array_merge($this->_labels, $label);
117: } else {
118: array_push($this->_labels, $label);
119: }
120: }
121:
122:
123: 124: 125: 126: 127: 128:
129: public function addDataset($dataset, $reset=null)
130: {
131:
132: if ($reset) {
133: $this->_datasets = array();
134: }
135:
136: if (is_array($dataset)) {
137: array_push($this->_datasets, $dataset);
138: }
139: }
140:
141:
142: private function renderChart()
143: {
144:
145: $attributes = '';
146: foreach ($this->_attributes as $attribute => $value) {
147: $attributes .= ' '.$attribute.'="'.$value.'"';
148: }
149:
150:
151: $width = '';
152: if ($this->_width) {
153: $width = ' width="' . $this->_width . '"';
154: }
155:
156:
157: $height = '';
158: if ($this->_height) {
159: $height = ' height="' . $this->_height . '"';
160: }
161:
162:
163: $this->_chart = '<canvas id="'
164: .$this->_id
165: .'" style="border: 1px solid black;" '.$height.$width.$attributes
166: .'></canvas>';
167:
168:
169: $this->_chart .= '<script language="javascript">';
170:
171:
172: $data = '';
173: foreach ($this->_datasets as $dataset) {
174: $data .= '{';
175: $separator = '';
176:
177:
178: if (!array_key_exists('backgroundColor', $dataset)) {
179: $data .= 'backgroundColor: randomColor(),';
180: }
181: foreach ($dataset as $key=>$val) {
182: $data .= $separator.$key.': ';
183:
184: if (is_int( $val )) {
185: $data .= $val;
186: } elseif (is_string($val)) {
187: $data .= '"'.str_replace('"', '\"', $val).'"';
188: } elseif (is_bool($val)) {
189: $data .= $val ? 'true' : 'false';
190: } elseif (is_array($val)) {
191: $data .= json_encode($val);
192: } else {
193: $data .= $val;
194: }
195: $separator = ', ';
196: }
197: $data .= '},';
198: }
199:
200:
201:
202: $this->_chart .= 'var '.$this->_id.'_data = {';
203:
204: $this->_chart .= 'labels: '.json_encode($this->_labels).',';
205:
206: $this->_chart .= 'datasets: ['.$data.'],';
207:
208: $this->_chart .= '};';
209:
210:
211: $this->_chart .= 'var ctx_'.$this->_id
212: .' = document.getElementById("'.$this->_id.'").getContext("2d");'
213: .'var '.$this->_id.' = new Chart.'.$this->_type
214: .'(ctx_'.$this->_id.', {data: '.$this->_id.'_data});';
215:
216:
217: $this->_chart .= '</script>';
218: }
219:
220:
221: 222: 223: 224: 225: 226:
227: public function setType($type)
228: {
229: $accepted_types = array(
230: 'Line',
231: 'Bar',
232: 'Radar',
233: 'PolarArea',
234: 'Pie',
235: 'Doughnut'
236: );
237:
238: if (in_array($type, $accepted_types)) {
239: $this->_type = $type;
240: }
241: }
242: }
243: