1 /*********************************************************************************
2 * SugarCRM Community Edition is a customer relationship management program developed by
3 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Affero General Public License version 3 as published by the
7 * Free Software Foundation with the addition of the following permission added
8 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
17 * You should have received a copy of the GNU Affero General Public License along with
18 * this program; if not, see http://www.gnu.org/licenses or write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25 * The interactive user interfaces in modified source and object code versions
26 * of this program must display Appropriate Legal Notices, as required under
27 * Section 5 of the GNU Affero General Public License version 3.
29 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30 * these Appropriate Legal Notices must retain the display of the "Powered by
31 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32 * technical reasons, the Appropriate Legal Notices must display the words
33 * "Powered by SugarCRM".
34 ********************************************************************************/
39 function loadSugarChart (chartId,jsonFilename,css,chartConfig) {
41 var labelType, useGradients, nativeTextSupport, animate;
43 var ua = navigator.userAgent,
44 typeOfCanvas = typeof HTMLCanvasElement,
45 nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
46 textSupport = nativeCanvasSupport
47 && (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
49 nativeTextSupport = labelType == 'Native';
50 useGradients = nativeCanvasSupport;
55 switch(chartConfig["chartType"]) {
57 var handleFailure = function(o){
59 if(o.responseText !== undefined){
63 var handleSuccess = function(o){
65 if(o.responseText !== undefined && o.responseText != "No Data"){
66 var json = eval('('+o.responseText+')');
68 var properties = $jit.util.splat(json.properties)[0];
69 var marginBottom = (chartConfig["orientation"] == 'vertical' && json.values.length > 8) ? 20*4 : 20;
71 var barChart = new $jit.BarChart({
72 //id of the visualization container
74 //whether to add animations
76 nodeCount: json.values.length,
77 renderBackground: chartConfig['imageExportType'] == "jpg" ? true: false,
78 backgroundColor: 'rgb(255,255,255)',
79 colorStop1: 'rgba(255,255,255,.8)',
80 colorStop2: 'rgba(255,255,255,0)',
85 //horizontal or vertical barcharts
86 orientation: chartConfig["orientation"],
89 text: properties['title'],
95 text: properties['subtitle'],
102 color: css["gridLineColor"]
105 barsOffset: (chartConfig["orientation"] == "vertical") ? 30 : 20,
106 //visualization offset
114 text: (chartConfig["scroll"] && SUGAR.util.isTouchScreen()) ? "Use two fingers to scroll" : "",
119 onClick: function(node) {
120 if(!node || SUGAR.util.isTouchScreen()) return;
121 if(node.link == 'undefined' || node.link == '') return;
122 window.location.href=node.link;
125 //labels offset position
128 type: useGradients? chartConfig["barType"]+':gradient' : chartConfig["barType"],
129 //whether to show the aggregation of the values
131 //whether to show the labels for the bars
135 type: labelType, //Native or HTML
137 family: css["font-family"],
144 onShow: function(tip, elem) {
145 if(elem.link != 'undefined' && elem.link != '') {
146 drillDown = (SUGAR.util.isTouchScreen()) ? "<br><a href='"+ elem.link +"'>Click to drilldown</a>" : "<br>Click to drilldown";
151 if(elem.valuelabel != 'undefined' && elem.valuelabel != undefined && elem.valuelabel != '') {
152 value = "elem.valuelabel";
154 value = "elem.value";
156 eval("tip.innerHTML = '<b>' + elem."+chartConfig["tip"]+" + '</b>: ' + "+value+" + ' - ' + elem.percentage + '%' + drillDown");
161 barChart.loadJSON(json);
165 var list = $jit.id('id-list'),
166 button = $jit.id('update'),
167 orn = $jit.id('switch-orientation');
168 //update json on click 'Update Data'
169 $jit.util.addEvent(button, 'click', function() {
170 var util = $jit.util;
171 if(util.hasClass(button, 'gray')) return;
172 util.removeClass(button, 'white');
173 util.addClass(button, 'gray');
174 barChart.updateJSON(json2);
177 //dynamically add legend to list
178 var list = $jit.id('legend'+chartId);
179 var legend = barChart.getLegend(),
180 cols = (typeof SUGAR == 'undefined' || typeof SUGAR.mySugar == 'undefined') ? 8 : 4,
181 rows = Math.ceil(legend["name"].length/cols),
182 table = "<table cellpadding='0' cellspacing='0' align='left'>";
184 for(i=0;i<rows;i++) {
186 for(td=0;td<cols;td++) {
188 table += '<td width=\'16\' valign=\'top\'>';
189 if(legend["name"][j] != undefined) {
190 table += '<div class=\'query-color\' style=\'background-color:'
191 + legend["color"][j] +'\'> </div>';
195 table += '<td class=\'label\' valign=\'top\'>';
196 if(legend["name"][j] != undefined) {
197 table += legend["name"][j];
207 list.innerHTML = table;
210 //save canvas to image for pdf consumption
211 $jit.util.saveImageTest(chartId,jsonFilename,chartConfig["imageExportType"]);
219 success:handleSuccess,
220 failure:handleFailure,
221 argument: { foo:'foo', bar:''}
224 var request = YAHOO.util.Connect.asyncRequest('GET', jsonFilename + "?r=" + new Date().getTime(), callback);
228 var handleFailure = function(o){
230 if(o.responseText !== undefined){
234 var handleSuccess = function(o){
236 if(o.responseText !== undefined && o.responseText != "No Data"){
237 var json = eval('('+o.responseText+')');
239 var properties = $jit.util.splat(json.properties)[0];
241 var lineChart = new $jit.LineChart({
242 //id of the visualization container
244 //whether to add animations
246 renderBackground: chartConfig['imageExportType'] == "jpg" ? true: false,
247 backgroundColor: 'rgb(255,255,255)',
248 colorStop1: 'rgba(255,255,255,.8)',
249 colorStop2: 'rgba(255,255,255,0)',
250 selectOnHover: false,
252 text: properties['title'],
258 text: properties['subtitle'],
265 color: css["gridLineColor"]
267 //visualization offset
276 onClick: function(node) {
277 if(!node || SUGAR.util.isTouchScreen()) return;
278 if(node.link == 'undefined' || node.link == '') return;
279 window.location.href=node.link;
282 //labels offset position
285 type: useGradients? chartConfig["lineType"]+':gradient' : chartConfig["lineType"],
286 //whether to show the aggregation of the values
288 //whether to show the labels for the bars
292 type: labelType, //Native or HTML
294 family: css["font-family"],
301 onShow: function(tip, elem) {
302 if(elem.link != 'undefined' && elem.link != '') {
303 drillDown = (SUGAR.util.isTouchScreen()) ? "<br><a href='"+ elem.link +"'>Click to drilldown</a>" : "<br>Click to drilldown";
308 if(elem.valuelabel != 'undefined' && elem.valuelabel != undefined && elem.valuelabel != '') {
309 var value = "elem.valuelabel";
311 var value = "elem.value";
315 eval("var name = elem."+chartConfig["tip"]+";");
316 var content = '<table>';
318 for(var i=0; i<name.length; i++) {
319 content += '<tr><td><b>' + name[i] + '</b>:</td><td> ' + elem.value[i] + ' - ' + elem.percentage[i] + '%' + '</td></tr>';
321 content += '</table>';
322 tip.innerHTML = content;
324 eval("tip.innerHTML = '<b>' + elem."+chartConfig["tip"]+" + '</b>: ' + "+value+" + ' - ' + elem.percentage + '%' + drillDown");
330 lineChart.loadJSON(json);
334 var list = $jit.id('id-list'),
335 button = $jit.id('update'),
336 orn = $jit.id('switch-orientation');
337 //update json on click 'Update Data'
338 $jit.util.addEvent(button, 'click', function() {
339 var util = $jit.util;
340 if(util.hasClass(button, 'gray')) return;
341 util.removeClass(button, 'white');
342 util.addClass(button, 'gray');
343 barChart.updateJSON(json2);
346 //dynamically add legend to list
347 var list = $jit.id('legend'+chartId);
348 var legend = lineChart.getLegend(),
349 cols = (typeof SUGAR == 'undefined' || typeof SUGAR.mySugar == 'undefined') ? 8 : 4,
350 rows = Math.ceil(legend["name"].length/cols),
351 table = "<table cellpadding='0' cellspacing='0' align='left'>";
353 for(i=0;i<rows;i++) {
355 for(td=0;td<cols;td++) {
357 table += '<td width=\'16\' valign=\'top\'>';
358 if(legend["name"][j] != undefined) {
359 table += '<div class=\'query-color\' style=\'background-color:'
360 + legend["color"][j] +'\'> </div>';
364 table += '<td class=\'label\' valign=\'top\'>';
365 if(legend["name"][j] != undefined) {
366 table += legend["name"][j];
376 list.innerHTML = table;
379 //save canvas to image for pdf consumption
380 $jit.util.saveImageTest(chartId,jsonFilename,chartConfig["imageExportType"]);
388 success:handleSuccess,
389 failure:handleFailure,
390 argument: { foo:'foo', bar:''}
393 var request = YAHOO.util.Connect.asyncRequest('GET', jsonFilename + "?r=" + new Date().getTime(), callback);
399 var handleFailure = function(o){
401 if(o.responseText !== undefined){
405 var handleSuccess = function(o){
407 if(o.responseText !== undefined){
408 var json = eval('('+o.responseText+')');
409 var properties = $jit.util.splat(json.properties)[0];
412 var pieChart = new $jit.PieChart({
413 //id of the visualization container
415 //whether to add animations
417 renderBackground: chartConfig['imageExportType'] == "jpg" ? true: false,
418 backgroundColor: 'rgb(255,255,255)',
419 colorStop1: 'rgba(255,255,255,.8)',
420 colorStop2: 'rgba(255,255,255,0)',
421 labelType: properties['labels'],
428 type: useGradients? chartConfig["pieType"]+':gradient' : chartConfig["pieType"],
429 //whether to show the labels for the slices
432 text: properties['title'],
438 text: properties['subtitle'],
451 onClick: function(node) {
452 if(!node || SUGAR.util.isTouchScreen()) return;
453 if(node.link == 'undefined' || node.link == '') return;
454 window.location.href=node.link;
459 type: labelType, //Native or HTML
461 family: css["font-family"],
467 onShow: function(tip, elem) {
468 if(elem.link != 'undefined' && elem.link != '') {
469 drillDown = (SUGAR.util.isTouchScreen()) ? "<br><a href='"+ elem.link +"'>Click to drilldown</a>" : "<br>Click to drilldown";
474 if(elem.valuelabel != 'undefined' && elem.valuelabel != undefined && elem.valuelabel != '') {
475 value = "elem.valuelabel";
477 value = "elem.value";
479 eval("tip.innerHTML = '<b>' + elem.label + '</b>: ' + "+ value +" + ' - ' + elem.percentage + '%' + drillDown");
484 pieChart.loadJSON(json);
486 //dynamically add legend to list
487 var list = $jit.id('legend'+chartId);
488 var legend = pieChart.getLegend(),
489 cols = (typeof SUGAR == 'undefined' || typeof SUGAR.mySugar == 'undefined') ? 8 : 4,
490 rows = Math.ceil(legend["name"].length/cols);
491 table = "<table cellpadding='0' cellspacing='0' align='left'>";
493 for(i=0;i<rows;i++) {
495 for(td=0;td<cols;td++) {
497 table += '<td width=\'16\' valign=\'top\'>';
498 if(legend["name"][j] != undefined) {
499 table += '<div class=\'query-color\' style=\'background-color:'
500 + legend["color"][j] +'\'> </div>';
504 table += '<td class=\'label\' valign=\'top\'>';
505 if(legend["name"][j] != undefined) {
506 table += legend["name"][j];
516 list.innerHTML = table;
519 //save canvas to image for pdf consumption
520 $jit.util.saveImageTest(chartId,jsonFilename,chartConfig["imageExportType"]);
526 success:handleSuccess,
527 failure:handleFailure,
528 argument: { foo:'foo', bar:''}
531 var request = YAHOO.util.Connect.asyncRequest('GET', jsonFilename + "?r=" + new Date().getTime(), callback);
538 var handleFailure = function(o){
540 if(o.responseText !== undefined){
544 var handleSuccess = function(o){
546 if(o.responseText !== undefined && o.responseText != "No Data"){
547 var json = eval('('+o.responseText+')');
549 var properties = $jit.util.splat(json.properties)[0];
552 var funnelChart = new $jit.FunnelChart({
553 //id of the visualization container
555 //whether to add animations
557 renderBackground: chartConfig['imageExportType'] == "jpg" ? true: false,
558 backgroundColor: 'rgb(255,255,255)',
559 colorStop1: 'rgba(255,255,255,.8)',
560 colorStop2: 'rgba(255,255,255,0)',
561 //orientation setting should not be changed
562 orientation: "vertical",
565 text: properties['title'],
571 text: properties['subtitle'],
578 //visualization offset
587 onClick: function(node) {
588 if(!node || SUGAR.util.isTouchScreen()) return;
589 if(node.link == 'undefined' || node.link == '') return;
590 window.location.href=node.link;
593 //labels offset position
596 type: useGradients? chartConfig["funnelType"]+':gradient' : chartConfig["funnelType"],
597 //whether to show the aggregation of the values
599 //whether to show the labels for the bars
603 type: labelType, //Native or HTML
605 family: css["font-family"],
612 onShow: function(tip, elem) {
613 if(elem.link != 'undefined' && elem.link != '') {
614 drillDown = (SUGAR.util.isTouchScreen()) ? "<br><a href='"+ elem.link +"'>Click to drilldown</a>" : "<br>Click to drilldown";
619 if(elem.valuelabel != 'undefined' && elem.valuelabel != undefined && elem.valuelabel != '') {
620 value = "elem.valuelabel";
622 value = "elem.value";
624 eval("tip.innerHTML = '<b>' + elem."+chartConfig["tip"]+" + '</b>: ' + "+value+" + ' - ' + elem.percentage + '%' + drillDown");
629 funnelChart.loadJSON(json);
633 var list = $jit.id('id-list'),
634 button = $jit.id('update'),
635 orn = $jit.id('switch-orientation');
636 //update json on click 'Update Data'
637 $jit.util.addEvent(button, 'click', function() {
638 var util = $jit.util;
639 if(util.hasClass(button, 'gray')) return;
640 util.removeClass(button, 'white');
641 util.addClass(button, 'gray');
642 barChart.updateJSON(json2);
645 //dynamically add legend to list
646 var list = $jit.id('legend'+chartId);
647 var legend = funnelChart.getLegend(),
648 cols = (typeof SUGAR == 'undefined' || typeof SUGAR.mySugar == 'undefined') ? 8 : 4,
649 rows = Math.ceil(legend["name"].length/cols);
650 table = "<table cellpadding='0' cellspacing='0' align='left'>";
652 for(i=0;i<rows;i++) {
654 for(td=0;td<cols;td++) {
656 table += '<td width=\'16\' valign=\'top\'>';
657 if(legend["name"][j] != undefined) {
658 table += '<div class=\'query-color\' style=\'background-color:'
659 + legend["color"][j] +'\'> </div>';
663 table += '<td class=\'label\' valign=\'top\'>';
664 if(legend["name"][j] != undefined) {
665 table += legend["name"][j];
675 list.innerHTML = table;
677 //save canvas to image for pdf consumption
678 $jit.util.saveImageTest(chartId,jsonFilename,chartConfig["imageExportType"]);
684 success:handleSuccess,
685 failure:handleFailure,
686 argument: { foo:'foo', bar:''}
689 var request = YAHOO.util.Connect.asyncRequest('GET', jsonFilename + "?r=" + new Date().getTime(), callback);
696 var handleFailure = function(o){
698 if(o.responseText !== undefined){
702 var handleSuccess = function(o){
704 if(o.responseText !== undefined){
705 var json = eval('('+o.responseText+')');
706 var properties = $jit.util.splat(json.properties)[0];
709 var gaugeChart = new $jit.GaugeChart({
710 //id of the visualization container
712 //whether to add animations
714 renderBackground: chartConfig['imageExportType'] == "jpg" ? true: false,
715 backgroundColor: 'rgb(255,255,255)',
716 colorStop1: 'rgba(255,255,255,.8)',
717 colorStop2: 'rgba(255,255,255,0)',
718 labelType: properties['labels'],
721 text: properties['title'],
727 text: properties['subtitle'],
735 backgroundColor: '#aaaaaa',
736 borderColor: '#999999',
737 needleColor: 'rgba(255,0,0,.8)',
739 positionFontSize: 24,
743 type: useGradients? chartConfig["gaugeType"]+':gradient' : chartConfig["gaugeType"],
744 //whether to show the labels for the slices
748 onClick: function(node) {
749 if(!node || SUGAR.util.isTouchScreen()) return;
750 if(node.link == 'undefined' || node.link == '') return;
751 window.location.href=node.link;
756 type: labelType, //Native or HTML
758 family: css["font-family"],
764 onShow: function(tip, elem) {
765 if(elem.link != 'undefined' && elem.link != '') {
766 drillDown = (SUGAR.util.isTouchScreen()) ? "<br><a href='"+ elem.link +"'>Click to drilldown</a>" : "<br>Click to drilldown";
770 if(elem.valuelabel != 'undefined' && elem.valuelabel != undefined && elem.valuelabel != '') {
771 value = "elem.valuelabel";
773 value = "elem.value";
775 eval("tip.innerHTML = '<b>' + elem.label + '</b>: ' + "+ value +" + drillDown");
780 gaugeChart.loadJSON(json);
783 var list = $jit.id('legend'+chartId);
784 var legend = gaugeChart.getLegend(),
785 cols = (typeof SUGAR == 'undefined' || typeof SUGAR.mySugar == 'undefined') ? 8 : 4,
786 rows = Math.ceil(legend["name"].length/cols);
787 table = "<table cellpadding='0' cellspacing='0' align='left'>";
789 for(i=0;i<rows;i++) {
791 for(td=0;td<cols;td++) {
793 table += '<td width=\'16\' valign=\'top\'>';
794 if(legend["name"][j] != undefined) {
795 table += '<div class=\'query-color\' style=\'background-color:'
796 + legend["color"][j] +'\'> </div>';
800 table += '<td class=\'label\' valign=\'top\'>';
801 if(legend["name"][j] != undefined) {
802 table += legend["name"][j];
812 list.innerHTML = table;
815 //save canvas to image for pdf consumption
816 $jit.util.saveImageTest(chartId,jsonFilename,chartConfig["imageExportType"]);
822 success:handleSuccess,
823 failure:handleFailure,
824 argument: { foo:'foo', bar:''}
827 var request = YAHOO.util.Connect.asyncRequest('GET', jsonFilename + "?r=" + new Date().getTime(), callback);