Skip to content

Commit

Permalink
Adding some extra methods to compliment the fix in 2.5.6 while also
Browse files Browse the repository at this point in the history
fixing issue #58
  • Loading branch information
kevinkhill committed Jul 7, 2015
1 parent 1c234af commit 3f4e709
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
24 changes: 22 additions & 2 deletions javascript/lava.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ window.lava = (function() {
this.charts = {};
this.registeredCharts = [];

this.Chart = function() {
this.init = null;
this.redraw = null;
this.setData = null;
this.data = null;
this.chart = null;
this.options = null;
this.formats = [];
};

this.get = function (chartLabel, callback) {
if (arguments.length < 2 || typeof chartLabel !== 'string' || typeof callback !== 'function') {
throw new Error('[Lavacharts] The syntax for lava.get must be (str ChartLabel, fn Callback)');
Expand All @@ -19,9 +29,19 @@ window.lava = (function() {

this.loadData = function (chartLabel, dataTableJson, callback) {
lava.getLavachart(chartLabel, function (lavachart) {
lavachart.init(dataTableJson);
console.log("sent json", dataTableJson);
console.log("old data", lavachart.data);

return callback(lavachart.chart);
lavachart.setData(dataTableJson);
console.log("new data", lavachart.data);

lavachart.redraw();

if (typeof callback == "function") {
return callback(lavachart.chart);
} else {
return true;
}
});
};

Expand Down
35 changes: 26 additions & 9 deletions src/JavascriptFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private function buildChartJs()

//Creating new chart js object
$out .= sprintf(
'lava.charts.%s["%s"] = {chart:null,draw:null,data:null,options:null,formats:[]};',
'lava.charts.%s["%s"] = new lava.Chart;',
$this->chart->type,
$this->chart->label
).PHP_EOL.PHP_EOL;
Expand All @@ -157,7 +157,7 @@ private function buildChartJs()
).PHP_EOL.PHP_EOL;

$out .= sprintf(
'lava.charts.%s["%s"].init = function (data) {',
'lava.charts.%s["%s"].init = function() {',
$this->chart->type,
$this->chart->label
).PHP_EOL;
Expand All @@ -168,18 +168,11 @@ private function buildChartJs()
$this->chart->label
).PHP_EOL.PHP_EOL;

$out .= 'if (typeof data == "string") {'.PHP_EOL;
$out .= sprintf(
'$this.data = new google.visualization.DataTable(data, %s);',
$this->googleDataTableVer
).PHP_EOL;
$out .= '} else {'.PHP_EOL;
$out .= sprintf(
'$this.data = new google.visualization.DataTable(%s, %s);',
$this->chart->datatable->toJson(),
$this->googleDataTableVer
).PHP_EOL;
$out .= '}'.PHP_EOL.PHP_EOL;

$out .= sprintf(
'$this.options = %s;',
Expand All @@ -204,6 +197,30 @@ private function buildChartJs()

$out .= "};".PHP_EOL.PHP_EOL;


$out .= sprintf(
'lava.charts.%1$s["%2$s"].setData = function (data) {' .
' var $this = lava.charts.%1$s["%2$s"];',
$this->chart->type,
$this->chart->label
).PHP_EOL;

$out .= sprintf(
'$this.data = new google.visualization.DataTable(data, %s);',
$this->googleDataTableVer
).PHP_EOL;

$out .= "};".PHP_EOL.PHP_EOL;

$out .= sprintf(
'lava.charts.%1$s["%2$s"].redraw = function () {' .
' var $this = lava.charts.%1$s["%2$s"];' .
' $this.chart.draw($this.data, $this.options);' .
'};',
$this->chart->type,
$this->chart->label
).PHP_EOL;

$out .= sprintf(
"google.load('visualization', '%s', {'packages':['%s']});",
$this->getChartPackageData('version'),
Expand Down

0 comments on commit 3f4e709

Please sign in to comment.