/** * Builds the Javascript code block * * Build the script block for the chart. If there are any events defined, * they will be automatically be attached to the chart and * pulled from the callbacks folder. * * @access private * * @return string Javascript code block. */ private function buildControlWrapperJs() { $out = $this->jsO . PHP_EOL; /* * If the object does not exist for a given chart type, initialise it. * This will prevent overriding keys when multiple charts of the same * type are being rendered on the same page. */ $out .= sprintf('if ( typeof lava.controls.%1$s == "undefined" ) { lava.controls.%1$s = {}; }', $this->control->type) . PHP_EOL . PHP_EOL; $out .= sprintf("google.load('visualization', '%s', {'packages':['%s']});", $this->getChartPackageData('version'), $this->getChartPackageData('type')) . PHP_EOL; //Creating new chart js object $out .= sprintf('lava.controls.%s["%s"] = new lava.Control;', $this->control->type, $this->control->label) . PHP_EOL . PHP_EOL; //Checking if output div exists $out .= sprintf('if (!document.getElementById("%1$s"))' . '{console.error("[Lavacharts] No matching element was found with ID \\"%1$s\\"");}', $this->control->getElementId()) . PHP_EOL . PHP_EOL; $out .= sprintf('lava.controls.%s["%s"].init = function() {', $this->control->type, $this->control->label) . PHP_EOL; $out .= sprintf('var $this = lava.controls.%s["%s"];', $this->control->type, $this->control->label) . PHP_EOL . PHP_EOL; $out .= sprintf('$this.options = %s;', $this->control->optionsToJson()) . PHP_EOL . PHP_EOL; $out .= sprintf('$this.controlType = "%s";', $this->control->type) . PHP_EOL . PHP_EOL; $out .= sprintf('$this.control = new google.visualization.ControlWrapper( %s );', $this->control->getControlWrapperJson()) . PHP_EOL . PHP_EOL; $out .= "};" . PHP_EOL . PHP_EOL; $out .= sprintf('google.setOnLoadCallback(lava.controls.%s["%s"].init );', $this->control->type, $this->control->label) . PHP_EOL; $out .= sprintf('lava.registerControl("%s", "%s");', $this->control->type, $this->control->label) . PHP_EOL . PHP_EOL; $out .= $this->jsC . PHP_EOL; return $out; }
/** * Builds the Javascript code block * * Build the script block for the chart. If there are any events defined, * they will be automatically be attached to the chart and * pulled from the callbacks folder. * * @access private * * @return string Javascript code block. */ private function buildChartJs() { $out = $this->jsO . PHP_EOL; /* * If the object does not exist for a given chart type, initialise it. * This will prevent overriding keys when multiple charts of the same * type are being rendered on the same page. */ $out .= sprintf('if ( typeof lava.charts.%1$s == "undefined" ) { lava.charts.%1$s = {}; }', $this->chart->type) . PHP_EOL . PHP_EOL; //Creating new chart js object $out .= sprintf('lava.charts.%s["%s"] = {chart:null,draw:null,data:null,options:null,formats:[]};', $this->chart->type, $this->chart->label) . PHP_EOL . PHP_EOL; //Checking if output div exists $out .= sprintf('if (!document.getElementById("%1$s"))' . '{console.error("[Lavacharts] No matching element was found with ID \\"%1$s\\"");}', $this->elementId) . PHP_EOL . PHP_EOL; $out .= sprintf('lava.charts.%s["%s"].draw = function() {', $this->chart->type, $this->chart->label) . PHP_EOL; $out .= sprintf('var $this = lava.charts.%s["%s"];', $this->chart->type, $this->chart->label) . PHP_EOL . PHP_EOL; $out .= sprintf('$this.data = new google.visualization.DataTable(%s, %s);', $this->chart->datatable->toJson(), $this->googleDataTableVer) . PHP_EOL . PHP_EOL; $out .= sprintf('$this.options = %s;', $this->chart->optionsToJson()) . PHP_EOL . PHP_EOL; $out .= sprintf('$this.chart = new google.visualization.%s(document.getElementById("%s"));', $this->getChartPackageData('jsObj'), $this->elementId) . PHP_EOL . PHP_EOL; if ($this->chart->datatable->hasFormats()) { $out .= $this->buildFormatters(); } if ($this->chart->hasEvents()) { $out .= $this->buildEventCallbacks(); } $out .= '$this.chart.draw($this.data, $this.options);' . PHP_EOL; $out .= "};" . PHP_EOL . PHP_EOL; $out .= sprintf("google.load('visualization', '%s', {'packages':['%s']});", $this->getChartPackageData('version'), $this->getChartPackageData('type')) . PHP_EOL; $out .= sprintf('google.setOnLoadCallback(lava.charts.%s["%s"].draw);', $this->chart->type, $this->chart->label) . PHP_EOL; $out .= sprintf('lava.register("%s", "%s");', $this->chart->type, $this->chart->label) . PHP_EOL; if (self::DEBUG) { $out .= 'console.debug(lava);'; } $out .= $this->jsC . PHP_EOL; return $out; }