]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarCharts/JsChart.php
Release 6.2.0beta4
[Github/sugarcrm.git] / include / SugarCharts / JsChart.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37
38
39
40 require_once("include/SugarCharts/SugarChart.php");
41
42 class JsChart extends SugarChart {
43         protected $ss;
44         var $xmlFile;
45         var $jsonFilename;
46         var $chartId;
47         var $width;
48         var $height;
49         var $chartType;
50
51         function __construct() {
52                 parent::__construct();
53         }
54
55         function isSupported($chartType) {
56                 $charts = array(
57                         "stacked group by chart",
58                         "group by chart",
59                         "bar chart",
60                         "horizontal group by chart",
61                         "horizontal",
62                         "horizontal bar chart",
63                         "pie chart",
64                         "gauge chart",
65                         "funnel chart 3D",
66                         "line chart",
67                 );
68
69                 if(in_array($chartType,$charts)) {
70                         return true;
71                 } else {
72                         return false;
73                 }
74
75         }
76
77         function tab($str, $depth){
78                 return str_repeat("\t", $depth) . $str . "\n";
79         }
80
81         function display($name, $xmlFile, $width='320', $height='480', $resize=false) {
82
83
84                 $this->chartId = $name;
85                 $this->height = $height;
86                 $this->width = $width;
87                 $this->xmlFile = $xmlFile;
88                 $this->chartType = $this->chart_properties['type'];
89
90
91                 $style = array();
92                 $chartConfig = array();
93                 $xmlStr = $this->processXML($this->xmlFile);
94                 $json = $this->buildJson($xmlStr);
95                 $this->saveJsonFile($json);
96                 $this->ss->assign("chartId", $this->chartId);
97                 $this->ss->assign("filename", $this->jsonFilename);
98
99
100                 $dimensions = $this->getChartDimensions($xmlStr);
101                 $this->ss->assign("width", $dimensions['width']);
102                 $this->ss->assign("height", $dimensions['height']);
103                 $config = $this->getConfigProperties();
104                 $style['gridLineColor'] = str_replace("0x","#",$config->gridLines);
105                 $style['font-family'] = $config->labelFontFamily;
106                 $style['color'] = str_replace("0x","#",$config->labelFontColor);
107                 $this->ss->assign("css", $style);
108                 foreach($this->getChartConfigParams($xmlStr) as $key => $value) {
109                         $chartConfig[$key] = $value;
110                 }
111                 $chartConfig['imageExportType'] = $this->image_export_type;
112                 $this->ss->assign("config", $chartConfig);
113                 if($json == "No Data") {
114                         $this->ss->assign("error", "No Data");
115                 }
116
117                 if(!$this->isSupported($this->chartType)) {
118                         $this->ss->assign("error", "Unsupported Chart Type");
119                 }
120
121
122                 $file = "";
123
124                 return $this->ss->fetch($file);
125         }
126
127
128         function getDashletScript($id,$xmlFile="") {
129
130                 global $sugar_config, $current_user, $current_language;
131                 $this->id = $id;
132                 $this->chartId = $id;
133                 $this->xmlFile = (!$xmlFile) ? $sugar_config['tmp_dir']. $current_user->id . '_' . $this->id . '.xml' : $xmlFile;
134
135
136                 $style = array();
137                 $chartConfig = array();
138                 $this->ss->assign("chartId", $this->chartId);
139                 $this->ss->assign("filename", str_replace(".xml",".js",$this->xmlFile));
140                 $config = $this->getConfigProperties();
141                 $style['gridLineColor'] = str_replace("0x","#",$config->gridLines);
142                 $style['font-family'] = $config->labelFontFamily;
143                 $style['color'] = str_replace("0x","#",$config->labelFontColor);
144                 $this->ss->assign("css", $style);
145                 $xmlStr = $this->processXML($this->xmlFile);
146                 foreach($this->getChartConfigParams($xmlStr) as $key => $value) {
147                         $chartConfig[$key] = $value;
148                 }
149                 
150                 $chartConfig['imageExportType'] = $this->image_export_type;
151                 $this->ss->assign("config", $chartConfig);
152
153                 $file = "";
154                 return $this->ss->fetch($file);
155         }
156
157         function chartArray($chartsArray) {
158
159                 $customChartsArray = array();
160                 $style = array();
161                 $chartConfig = array();
162                 foreach($chartsArray as $id => $data) {
163                         $customChartsArray[$id] = array();
164                         $customChartsArray[$id]['chartId'] = $id;
165                         $customChartsArray[$id]['filename'] = str_replace(".xml",".js",$data['xmlFile']);
166                         $customChartsArray[$id]['width'] = $data['width'];
167                         $customChartsArray[$id]['height'] = $data['height'];
168
169                         $config = $this->getConfigProperties();
170                         $style['gridLineColor'] = str_replace("0x","#",$config->gridLines);
171                         $style['font-family'] = (string)$config->labelFontFamily;
172                         $style['color'] = str_replace("0x","#",$config->labelFontColor);
173                         $customChartsArray[$id]['css'] = $style;
174                         $xmlStr = $this->processXML($data['xmlFile']);
175                         $xml = new SimpleXMLElement($xmlStr);
176                         $params = $this->getChartConfigParams($xmlStr);
177                         $customChartsArray[$id]['supported'] = ($this->isSupported($xml->properties->type)) ? "true" : "false";
178                         foreach($params as $key => $value) {
179                                 $chartConfig[$key] = $value;
180                         }
181                         $chartConfig['imageExportType'] = $this->image_export_type;
182                         $customChartsArray[$id]['chartConfig'] = $chartConfig;
183                 }
184
185                 return $customChartsArray;
186         }
187
188         function getChartConfigParams($xmlStr) {
189
190                 $xml = new SimpleXMLElement($xmlStr);
191
192                 $chartType = $xml->properties->type;
193                 if($chartType == "pie chart") {
194                         return array ("pieType" => "basic","tip" => "name","chartType" => "pieChart");
195                 } elseif($chartType == "line chart") {
196                         return array ("lineType" => "basic","tip" => "name","chartType" => "lineChart");
197                 } elseif($chartType == "funnel chart 3D") {
198                         return array ("funnelType" => "basic","tip" => "name","chartType" => "funnelChart");
199                 } elseif($chartType == "gauge chart") {
200                         return array ("gaugeType" => "basic","tip" => "name","chartType" => "gaugeChart");
201                 } elseif($chartType == "stacked group by chart") {
202                         return array ("orientation" => "vertical","barType" => "stacked","tip" => "name","chartType" => "barChart");
203                 } elseif($chartType == "group by chart") {
204                         return array("orientation" => "vertical", "barType" => "grouped", "tip" => "title","chartType" => "barChart");
205                 } elseif($chartType == "bar chart") {
206                         return array("orientation" => "vertical", "barType" => "basic", "tip" => "label","chartType" => "barChart");
207                 } elseif ($chartType == "horizontal group by chart") {
208                         return array("orientation" => "horizontal", "barType" => "stacked", "tip" => "name","chartType" => "barChart");
209                 } elseif ($chartType == "horizontal bar chart" || "horizontal") {
210                         return array("orientation" => "horizontal","barType" => "basic","tip" => "label","chartType" => "barChart");
211                 } else {
212                         return array("orientation" => "vertical","barType" => "stacked","tip" => "name","chartType" => "barChart");
213                 }
214         }
215         function getChartDimensions($xmlStr) {
216                 if($this->getNumNodes($xmlStr) > 9 && $this->chartType != "pie chart") {
217                         if($this->chartType == "horizontal group by chart" || $this->chartType == "horizontal bar chart") {
218                                 $diff = $this->getNumNodes($xmlStr) - 9;
219                                 $height = ($diff * (.20 * $this->height)) + $this->height;
220                                 return array("width"=>$this->width, "height"=>($height));
221                         } else {
222                                 return array("width"=>($this->width * 2), "height"=>$this->height);
223                         }
224                 } else {
225                         return array("width"=>"100%", "height"=>$this->height);
226                 }
227         }
228
229         function checkData($xmlstr) {
230                 $xml = new SimpleXMLElement($xmlstr);
231                 if(sizeof($xml->data->group) > 0) {
232                         return true;
233                 } else {
234                         return false;
235                 }
236         }
237
238         function getNumNodes($xmlstr) {
239                 $xml = new SimpleXMLElement($xmlstr);
240                 return sizeof($xml->data->group);
241         }
242
243         function buildProperties($xmlstr) {
244                 $content = $this->tab("'properties': [\n",1);
245                 $properties = array();
246                 $xml = new SimpleXMLElement($xmlstr);
247                 foreach($xml->properties->children() as $property) {
248                         $properties[] = $this->tab("'".$property->getName()."':"."'".$property."'",2);
249                 }
250                 $content .= $this->tab("{\n",1);
251                 $content .= join(",\n",$properties)."\n";
252                 $content .= $this->tab("}\n",1);
253                 $content .= $this->tab("],\n",1);
254                 return $content;
255         }
256
257         function buildLabelsBarChartStacked($xmlstr) {
258                 $content = $this->tab("'label': [\n",1);
259                 $labels = array();
260                 $xml = new SimpleXMLElement($xmlstr);
261                 foreach($xml->data->group[0]->subgroups->group as $group) {
262                         $labels[] = $this->tab("'".$group->title."'",2);
263                 }
264                 $content .= join(",\n",$labels)."\n";
265                 $content .= $this->tab("],\n",1);
266                 return $content;
267         }
268
269         function buildLabelsBarChart($xmlstr) {
270                 $content = $this->tab("'label': [\n",1);
271                 $labels = array();
272                 $xml = new SimpleXMLElement($xmlstr);
273                 foreach($xml->data->group as $group) {
274                         $labels[] = $this->tab("'".$group->title."'",2);
275                 }
276                 $labelStr = join(",\n",$labels)."\n";
277                 $content .= $labelStr;
278                 $content .= $this->tab("],\n",1);
279                 return $content;
280         }
281
282         function buildDataBarChartStacked($xmlstr) {
283                 $content = $this->tab("'values': [\n",1);
284                 $data = array();
285                 $xml = new SimpleXMLElement($xmlstr);
286                 foreach($xml->data->group as $group) {
287                         $groupcontent = $this->tab("{\n",1);
288                         $groupcontent .= $this->tab("'label': '{$group->title}',\n",2);
289                         $groupcontent .= $this->tab("'gvalue': '{$group->value}',\n",2);
290                         $groupcontent .= $this->tab("'gvaluelabel': '{$group->label}',\n",2);
291                         $subgroupValues = array();
292                         $subgroupValueLabels = array();
293                         $subgroupLinks = array();
294                         foreach($group->subgroups->group as $subgroups) {
295                                 $subgroupValues[] = $this->tab(($subgroups->value == "NULL") ? 0 : $subgroups->value,3);
296                                 $subgroupValueLabels[] = $this->tab("'".$subgroups->label."'",3);
297                                 $subgroupLinks[] = $this->tab("'".$subgroups->link."'",3);
298                         }
299                         $subgroupValuesStr = join(",\n",$subgroupValues)."\n";
300                         $subgroupValueLabelsStr = join(",\n",$subgroupValueLabels)."\n";
301                         $subgroupLinksStr = join(",\n",$subgroupLinks)."\n";
302
303                         $groupcontent .= $this->tab("'values': [\n".$subgroupValuesStr,2);
304                         $groupcontent .= $this->tab("],\n",2);
305                         $groupcontent .= $this->tab("'valuelabels': [\n".$subgroupValueLabelsStr,2);
306                         $groupcontent .= $this->tab("],\n",2);
307                         $groupcontent .= $this->tab("'links': [\n".$subgroupLinksStr,2);
308                         $groupcontent .= $this->tab("]\n",2);
309                         $groupcontent .= $this->tab("}",1);
310                         $data[] = $groupcontent;
311                 }
312                 $content .= join(",\n",$data)."\n";
313                 $content .= $this->tab("]",1);
314                 return $content;
315         }
316
317         function buildDataBarChartGrouped($xmlstr) {
318                 $content = $this->tab("'values': [\n",1);
319                 $data = array();
320                 $xml = new SimpleXMLElement($xmlstr);
321                 foreach($xml->data->group as $group) {
322                         $groupcontent = $this->tab("{\n",1);
323                         $groupcontent .= $this->tab("'label': '{$group->title}',\n",2);
324                         $groupcontent .= $this->tab("'gvalue': '{$group->value}',\n",2);
325                         $groupcontent .= $this->tab("'gvaluelabel': '{$group->label}',\n",2);
326                         $subgroupValues = array();
327                         $subgroupValueLabels = array();
328                         $subgroupLinks = array();
329                         $subgroupTitles = array();
330                         foreach($group->subgroups->group as $subgroups) {
331                                 $subgroupValues[] = $this->tab(($subgroups->value == "NULL") ? 0 : $subgroups->value,3);
332                                 $subgroupValueLabels[] = $this->tab("'".$subgroups->label."'",3);
333                                 $subgroupLinks[] = $this->tab("'".$subgroups->link."'",3);
334                                 $subgroupTitles[] = $this->tab("'".$subgroups->title."'",3);
335                         }
336                         $subgroupValuesStr = join(",\n",$subgroupValues)."\n";
337                         $subgroupValueLabelsStr = join(",\n",$subgroupValueLabels)."\n";
338                         $subgroupLinksStr = join(",\n",$subgroupLinks)."\n";
339                         $subgroupTitlesStr = join(",\n",$subgroupTitles)."\n";
340
341                         $groupcontent .= $this->tab("'values': [\n".$subgroupValuesStr,2);
342                         $groupcontent .= $this->tab("],\n",2);
343                         $groupcontent .= $this->tab("'valuelabels': [\n".$subgroupValueLabelsStr,2);
344                         $groupcontent .= $this->tab("],\n",2);
345                         $groupcontent .= $this->tab("'links': [\n".$subgroupLinksStr,2);
346                         $groupcontent .= $this->tab("],\n",2);
347                         $groupcontent .= $this->tab("'titles': [\n".$subgroupTitlesStr,2);
348                         $groupcontent .= $this->tab("]\n",2);
349                         $groupcontent .= $this->tab("}",1);
350                         $data[] = $groupcontent;
351                 }
352                 $content .= join(",\n",$data)."\n";
353                 $content .= $this->tab("]",1);
354                 return $content;
355         }
356
357         function buildDataBarChart($xmlstr) {
358                 $content = $this->tab("'values': [\n",1);
359                 $data = array();
360                 $xml = new SimpleXMLElement($xmlstr);
361                 $groupcontent = "";
362                 $groupcontentArr = array();
363
364                 foreach($xml->data->group as $group) {
365                 $groupcontent = $this->tab("{\n",1);
366                 $groupcontent .= $this->tab("'label': [\n",2);
367                 $groupcontent .= $this->tab("'{$group->title}'\n",3);
368                 $groupcontent .= $this->tab("],\n",2);
369                 $groupcontent .= $this->tab("'values': [\n",2);
370                 $groupcontent .= $this->tab(($group->value == "NULL") ? 0 : $group->value."\n",3);
371                 $groupcontent .= $this->tab("],\n",2);
372                 if($group->label) {
373                         $groupcontent .= $this->tab("'valuelabels': [\n",2);
374                         $groupcontent .= $this->tab("'{$group->label}'\n",3);
375                         $groupcontent .= $this->tab("],\n",2);
376                 }
377                 $groupcontent .= $this->tab("'links': [\n",2);
378                 $groupcontent .= $this->tab("'{$group->link}'\n",3);
379                 $groupcontent .= $this->tab("]\n",2);
380                 $groupcontent .= $this->tab("}",1);
381                 $groupcontentArr[] = $groupcontent;
382                 }
383                 $content .= join(",\n",$groupcontentArr)."\n";
384                 $content .= $this->tab("]",1);
385                 return $content;
386         }
387
388           function buildLabelsPieChart($xmlstr) {
389                 $content = $this->tab("'label': [\n",1);
390                 $labels = array();
391                 $xml = new SimpleXMLElement($xmlstr);
392                 foreach($xml->data->group as $group) {
393                         $labels[] = $this->tab("'".$group->title."'",2);
394                 }
395                 $labelStr = join(",\n",$labels)."\n";
396                 $content .= $labelStr;
397                 $content .= $this->tab("],\n",1);
398                 return $content;
399         }
400
401
402         function buildDataPieChart($xmlstr) {
403                 $content = $this->tab("'values': [\n",1);
404                 $data = array();
405                 $xml = new SimpleXMLElement($xmlstr);
406                 $groupcontent = "";
407                 $groupcontentArr = array();
408
409                 foreach($xml->data->group as $group) {
410                 $groupcontent = $this->tab("{\n",1);
411                 $groupcontent .= $this->tab("'label': [\n",2);
412                 $groupcontent .= $this->tab("'{$group->title}'\n",3);
413                 $groupcontent .= $this->tab("],\n",2);
414                 $groupcontent .= $this->tab("'values': [\n",2);
415                 $groupcontent .= $this->tab("{$group->value}\n",3);
416                 $groupcontent .= $this->tab("],\n",2);
417                 $groupcontent .= $this->tab("'valuelabels': [\n",2);
418                 $groupcontent .= $this->tab("'{$group->label}'\n",3);
419                 $groupcontent .= $this->tab("],\n",2);
420                 $groupcontent .= $this->tab("'links': [\n",2);
421                 $groupcontent .= $this->tab("'{$group->link}'\n",3);
422                 $groupcontent .= $this->tab("]\n",2);
423                 $groupcontent .= $this->tab("}",1);
424                 $groupcontentArr[] = $groupcontent;
425                 }
426
427
428                 $content .= join(",\n",$groupcontentArr)."\n";
429                 $content .= $this->tab("\n]",1);
430                 return $content;
431         }
432
433         function buildLabelsGaugeChart($xmlstr) {
434                 $content = $this->tab("'label': [\n",1);
435                 $labels = array();
436                 $xml = new SimpleXMLElement($xmlstr);
437                 foreach($xml->data->group as $group) {
438                         $labels[] = $this->tab("'".$group->title."'",2);
439                 }
440                 $labelStr = join(",\n",$labels)."\n";
441                 $content .= $labelStr;
442                 $content .= $this->tab("],\n",1);
443                 return $content;
444         }
445
446         function buildDataGaugeChart($xmlstr) {
447                 $content = $this->tab("'values': [\n",1);
448                 $data = array();
449                 $xml = new SimpleXMLElement($xmlstr);
450                 foreach($xml->data->group as $group) {
451                         $groupcontent = $this->tab("{\n",1);
452                         $groupcontent .= $this->tab("'label': '{$group->title}',\n",2);
453                         $groupcontent .= $this->tab("'gvalue': '{$group->value}',\n",2);
454                         $finalComma = ($group->title != "GaugePosition") ? "," : "";
455                         $groupcontent .= $this->tab("'gvaluelabel': '{$group->label}'{$finalComma}\n",2);
456                         $subgroupTitles = array();
457                         $subgroupValues = array();
458                         $subgroupValueLabels = array();
459                         $subgroupLinks = array();
460
461                         if(is_object($group->subgroups->group)) {
462                                 foreach($group->subgroups->group as $subgroups) {
463                                         $subgroupTitles[] = $this->tab("'".$subgroups->title."'",3);
464                                         //$subgroupValues[] = $this->tab($subgroups->value,3);
465                                         $subgroupValues[] = $subgroups->value;
466                                         $subgroupValueLabels[] = $this->tab("'".$subgroups->label."'",3);
467                                         $subgroupLinks[] = $this->tab("'".$subgroups->link."'",3);
468                                 }
469                                 $subgroupTitlesStr = join(",\n",$subgroupTitles)."\n";
470                                 $subgroupValuesStr = join(",\n",$subgroupValues)."\n";
471                                 $subgroupValueLabelsStr = join(",\n",$subgroupValueLabels)."\n";
472                                 $subgroupLinksStr = join(",\n",$subgroupLinks)."\n";
473
474                                 //$groupcontent .= $this->tab("'labels': [\n".$subgroupTitlesStr,2);
475                                 //$groupcontent .= $this->tab("],\n",2);
476                                 $val = ((int)$subgroupValues[1] == (int)$subgroupValues[0]) ? $this->tab($subgroupValues[1],3)."\n" : $this->tab($subgroupValues[1] - $subgroupValues[0],3)."\n";
477
478                                 $groupcontent .= $this->tab("'values': [\n".$val,2);
479                                 $groupcontent .= $this->tab("],\n",2);
480                                 $groupcontent .= $this->tab("'valuelabels': [\n".$subgroupValueLabelsStr,2);
481                                 $groupcontent .= $this->tab("]\n",2);
482                                 //$groupcontent .= $this->tab("'links': [\n".$subgroupLinksStr,2);
483                                 //$groupcontent .= $this->tab("]\n",2);
484
485                         }
486
487                                 $groupcontent .= $this->tab("}",1);
488                                 $data[] = $groupcontent;
489
490                 }
491
492                 $content .= join(",\n",$data)."\n";
493
494
495                 $content .= $this->tab("]",1);
496                 return $content;
497         }
498
499
500         function getConfigProperties() {
501                 $path = SugarThemeRegistry::current()->getImageURL('sugarColors.xml',false);
502
503                 if(!file_exists($path)) {
504                         $GLOBALS['log']->debug("Cannot open file ($path)");
505                 }
506                 $xmlstr = file_get_contents($path);
507                 $xml = new SimpleXMLElement($xmlstr);
508                 return $xml->charts;
509         }
510
511         function buildChartColors() {
512
513                 $content = $this->tab("'color': [\n",1);
514                 $colorArr = array();
515                 $xml = $this->getConfigProperties();
516                 $colors = ($this->chartType == "gauge chart") ? $xml->gaugeChartElementColors->color : $xml->chartElementColors->color;
517                 foreach($colors as $color) {
518                         $colorArr[] = $this->tab("'".str_replace("0x","#",$color)."'",2);
519                 }
520                 $content .= join(",\n",$colorArr)."\n";
521                 $content .= $this->tab("],\n",1);
522
523                 return $content;
524
525         }
526
527         function buildJson($xmlstr){
528                 if($this->checkData($xmlstr)) {
529                         $content = "{\n";
530                         if ($this->chartType == "pie chart" || $this->chartType == "funnel chart 3D") {
531                                 $content .= $this->buildProperties($xmlstr);
532                                 $content .= $this->buildLabelsPieChart($xmlstr);
533                                 $content .= $this->buildChartColors();
534                                 $content .= $this->buildDataPieChart($xmlstr);
535                         }
536                         elseif ($this->chartType == "gauge chart") {
537                                 $content .= $this->buildProperties($xmlstr);
538                                 $content .= $this->buildLabelsGaugeChart($xmlstr);
539                                 $content .= $this->buildChartColors();
540                                 $content .= $this->buildDataGaugeChart($xmlstr);
541                         }
542                         elseif ($this->chartType == "horizontal bar chart" || $this->chartType == "bar chart") {
543                                 $content .= $this->buildProperties($xmlstr);
544                                 $content .= $this->buildLabelsBarChart($xmlstr);
545                                 $content .= $this->buildChartColors();
546                                 $content .= $this->buildDataBarChart($xmlstr);
547                         }
548                         elseif ($this->chartType == "group by chart") {
549                                 $content .= $this->buildProperties($xmlstr);
550                                 $content .= $this->buildLabelsBarChartStacked($xmlstr);
551                                 $content .= $this->buildChartColors();
552                                 $content .= $this->buildDataBarChartGrouped($xmlstr);
553                         } else {
554                                 $content .= $this->buildProperties($xmlstr);
555                                 $content .= $this->buildLabelsBarChartStacked($xmlstr);
556                                 $content .= $this->buildChartColors();
557                                 $content .= $this->buildDataBarChartStacked($xmlstr);
558                         }
559                         $content .= "\n}";
560                         return $content;
561                 } else {
562                         return "No Data";
563                 }
564         }
565
566         function buildHTMLLegend($xmlFile) {
567                 $xmlstr = $this->processXML($xmlFile);
568                 $xml = new SimpleXMLElement($xmlstr);
569                 $this->chartType = $xml->properties->type;
570                 $html = "<table align=\"left\" cellpadding=\"2\" cellspacing=\"2\">";
571
572                 if ($this->chartType == "group by chart" || $this->chartType == "horizontal group by chart") {
573                         $groups = $xml->data->group[0]->subgroups->group;
574                         $items = (sizeof($xml->data->group[0]->subgroups->group) <= 5) ? 5 : sizeof($xml->data->group[0]->subgroups->group);
575                 } else {
576                         $groups = $xml->data->group;
577                         $items = (sizeof($xml->data->group) <= 5) ? 5 : sizeof($xml->data->group);
578                 }
579
580                 $rows = ceil($items/5);
581                 $fullItems = $rows * 5;
582                 $remainder = ($items < $fullItems) ? $fullItems - $items : 0;
583                 $i = 0;
584                 $x = 0;
585
586
587                 $colorArr = array();
588                 $xmlColors = $this->getConfigProperties();
589                 $colors = ($this->chartType == "gauge chart") ? $xmlColors->gaugeChartElementColors->color : $xmlColors->chartElementColors->color;
590
591                 foreach($colors as $color) {
592                         $colorArr[] = str_replace("0x","#",$color);
593                 }
594
595
596                 foreach($groups as $group) {
597                         if($i == 5) {$i = 0;}
598                         $html .= ($i == 0) ? "<tr>" : "";
599                         $html .= "<td width=\"50\">";
600                         $html .= "<div style=\"background-color:".$colorArr[$x].";\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>";
601                         $html .= "</td>";
602                         $html .= "<td>";
603                         $html .= $group->title;
604                         $html .= "</td>";
605                         $html .= ($x+1 == $items) ? "<td colspan=".($remainder*2)."></td>" : "";
606                         $html .= ($i == 4) ? "</tr>" : "";
607                         $x++;
608                         $i++;
609                 }
610
611
612                 $html .= "</table>";
613                 return $html;
614         }
615
616         function saveJsonFile($jsonContents) {
617
618                 $this->jsonFilename = str_replace(".xml",".js",$this->xmlFile);
619                 //$jsonContents = mb_convert_encoding($jsonContents, 'UTF-16LE', 'UTF-8');
620
621                 // open file
622                 if (!$fh = sugar_fopen($this->jsonFilename, 'w')) {
623                         $GLOBALS['log']->debug("Cannot open file ($this->jsonFilename)");
624                         return;
625                 }
626
627                 // write the contents to the file
628                 if (fwrite($fh,$jsonContents) === FALSE) {
629                         $GLOBALS['log']->debug("Cannot write to file ($this->jsonFilename)");
630                         return false;
631                 }
632
633                 $GLOBALS['log']->debug("Success, wrote ($jsonContents) to file ($this->jsonFilename)");
634
635                 fclose($fh);
636                 return true;
637         }
638
639         function get_image_cache_file_name ($xmlFile,$ext = ".png") {
640                 $filename = str_replace("/xml/","/images/",str_replace(".xml",$ext,$xmlFile));
641
642                 return $filename;
643         }
644
645
646         function getXMLChartProperties($xmlStr) {
647                 $props = array();
648                 $xml = new SimpleXMLElement($xmlstr);
649                 foreach($xml->properties->children() as $properties) {
650                         $props[$properties->getName()] = $properties;
651                 }
652                 return $props;
653         }
654
655         function processXML($xmlFile) {
656
657                 if(!file_exists($xmlFile)) {
658                         $GLOBALS['log']->debug("Cannot open file ($xmlFile)");
659                 }
660
661                 $pattern = array();
662                 $replacement = array();
663                 $content = file_get_contents($xmlFile);
664                 $content = mb_convert_encoding($content, 'UTF-8','UTF-16LE' );
665                 $pattern[] = '/\<link\>([a-zA-Z0-9#?&%.;\[\]\/=+_-\s]+)\<\/link\>/e';
666                 $replacement[] = "'<link>'.urlencode(\"$1\").'</link>'";
667 //              $pattern[] = '/NULL/e';
668 //              $replacement[] = "";
669                 return preg_replace($pattern,$replacement, $content);
670         }
671
672
673 }
674
675 ?>