Your IP : 18.219.40.177


Current Path : /usr/local/mgr5/skins/orion/src/
Upload File :
Current File : //usr/local/mgr5/skins/orion/src/App.AnyChart.js

//anychart module
App.Anychart = function() {
  'use strict';
  var init = function() {
      EventMgr.bind('loadChart', loadChart);
      // EventMgr.bind('appendReport', loadCharts);
      EventMgr.bind('loadCharts', loadCharts);
      EventMgr.bind('loadChartPrepare', loadChartPrepare);
      EventMgr.bind('runQueue', runQueue);
    },

    notEnoughData = '<table class="notenoughdata"><tr><td>' + pageInfo.notenough + '</td></tr></table>',

    ParseXML = function(xml) {
      var xmlDoc = 0, parser;
      try {
        xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
        xmlDoc.async = 'false';
        xmlDoc.loadXML(xml);
      } catch (e) {
        try {
          parser = new DOMParser();
          xmlDoc = parser.parseFromString(xml, 'text/xml');
        } catch (exp) {
        }
      }
      return xmlDoc;
    },

    loadCharts = function(e, data) {
      var bands = data.bands.bands,
        tabId = data.tabId,
        len = bands.length,
        id = null,
        gid = null,
        i = 0,
        j;
      for (i; i < len; i++) {
        id = tabId + '-' + i;
        gid = id + bands[i].id;
        if (bands[i].diagram !== undefined) {
          var lenD = bands[i].diagram.length;
          for (j = 0; j < lenD; j++) {
            if (typeof AnyChart !== 'undefined') {
              EventMgr.trigger('loadChart', {
                  diagram: bands[i].diagram[j], gid: gid + j, tabId: tabId});
            } else {
              EventMgr.trigger('loadChartPrepare', {
                  diagram: bands[i].diagram[j], gid: gid + j, tabId: tabId});
            }
          }
        }
      }
    },

    queueCharts = [],

    loadChartPrepare = function(e, data) {
      queueCharts.push(data);
      EventMgr.trigger('loadACHTML5');
      data = null;
    },

    runQueue = function() {
      var len = queueCharts.length;
      while (len--) {
        EventMgr.trigger('loadChart', queueCharts[len].data);
      }
      queueCharts = [];
    },
    /* jslint evil:true */
    isIe =!!top.execScript,

    loadChart = function(e, data) {
      if (typeof AnyChart !== undefined &&
           typeof anychart !== undefined &&
            App.Global.anychartXML !== null) {
        var diagram = data.diagram,
          gid = data.gid,
          tabId = data.tabId;
        if (App.Dom.byId(gid) === null) {return;}
        if (diagram.type === 'histogram' && !isIe) {
          AnyChart.renderingType = anychart.RenderingType.FLASH_ONLY;
        } else if (AnyChart.renderingType !== anychart.RenderingType.SVG_PREFERRED) {
          AnyChart.renderingType = anychart.RenderingType.SVG_PREFERRED;
        }
        var chart = new AnyChart(pageInfo.theme + 'anychart.swf');
        chart.width = '100%';
        chart.height = '200';
        chart.wMode = 'transparent';

        var chartTemplate = { line: 'isp-line', histogram: 'isp-bar', pie: 'isp-pie' },
          ctemplate = chartTemplate[diagram.type],
          xmldoc = new ParseXML(App.Global.anychartXML),
          datanames = diagram.dataname,
          labels = diagram.labels;
        // One point on line Graph
        if (diagram.type === 'line' &&
             diagram['data-' + diagram.data[0]].length === 1) {
          var wrapper = App.Dom.byId(gid);
          if (wrapper !== null) {
            wrapper.innerHTML = notEnoughData;
            wrapper = null;
          }
          return;
        }
        // Short line template
        if (diagram.type === 'line' && diagram['data-' + diagram.data[0]].length < 32) {
          ctemplate = 'isp-shortline';
        }
        /* jslint camelcase:false*/
        var msgother = 'Other';
        try { if (msg_other) { msgother = msg_other; } } catch (ex) {}
        /* jslint camelcase:true*/
        // XML
        var eroot = xmldoc.documentElement,
          echart = eroot.getElementsByTagName('chart')[0];
        echart.setAttribute('template', ctemplate);
        // Add series
        var seriescount = 0,
          othersum = 0,
          edata = eroot.getElementsByTagName('data')[0],
          i,
          seriessum,
          eseries,
          colname,
          pointsnum,
          lenDiagram,
          j,
          py,
          epoint;
        for (i = 0; i < diagram.data.length; i++) {
          colname = diagram.data[i];
          if (!datanames.colname) { continue; }
          seriessum = 0;
          eseries = xmldoc.createElement('series');
          eseries.setAttribute('name', datanames.colname);
          // Add points
          pointsnum = 0;
          lenDiagram = diagram['data-' + colname].length;
          for (j = 0; j < lenDiagram; j++) {
            if (diagram.type === 'histogram' && j > 29) { break; }
            py = parseFloat(diagram['data-' + colname][j]);
            if (isNaN(py)) {
              py = 0;
            }
            seriessum += py;
            // If Pie than show only first 5
            if (diagram.type === 'pie') {
              if (pointsnum < 5) {
                if (py === 0) { continue; }
                epoint = xmldoc.createElement('point');
                epoint.setAttribute('name', labels[j]);
                epoint.setAttribute('y', py);
                eseries.appendChild(epoint);
              } else {
                othersum += py;
                if (j === lenDiagram - 1) {
                  epoint = xmldoc.createElement('point');
                  epoint.setAttribute('name', msgother);
                  epoint.setAttribute('y', othersum);
                  eseries.appendChild(epoint);
                }
              }
              pointsnum++;
              // Line and Bar
            } else {
              epoint = xmldoc.createElement('point');
              epoint.setAttribute('name', labels[j]);
              epoint.setAttribute('y', py);
              eseries.appendChild(epoint);
            }
          }
          // if ( seriessum > 0 ) {
          edata.appendChild(eseries);
          seriescount++;
          // }
        }
        // If no series added then hide chart
        if (seriescount === 0) { return; }

        // Convert XMLdocument to String and set data to diagram
        var xmlstr = (xmldoc.xml) ? xmldoc.xml : (new XMLSerializer()).serializeToString(xmldoc);
        chart.setData(xmlstr);
        chart.write(gid);
        EventMgr.trigger('addedChart', {chart: chart, tabId: tabId});
        chart = null;
        tabId = null;
        xmlstr = null;
      }
    };
  return {
    init: init
  };
}();