/** * Builds the template variables from the chart. * * @since 3.0.0 * @access private * @return string Javascript code block. */ private function getTemplateVars() { $chart = $this->chart; // Workaround for no :: on member vars in php5.4 $vars = ['chartLabel' => (string) $chart->getLabel(), 'chartType' => $chart::TYPE, 'chartVer' => $chart::VERSION, 'chartClass' => $chart::VIZ_CLASS, 'chartPackage' => $chart::VIZ_PACKAGE, 'chartData' => json_encode($chart->getDataTable()), 'chartOptions' => json_encode($chart->getOptions()), 'elemId' => (string) $this->elementId, 'dataVer' => DataTable::VERSION, 'dataClass' => DataTable::VIZ_CLASS, 'formats' => '', 'events' => '']; if ($chart->getDataTable()->hasFormattedColumns()) { $vars['formats'] = $this->buildFormatters(); } if ($this->chart->hasEvents()) { $vars['events'] = $this->buildEventCallbacks(); } return $vars; }
/** * 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; }