Skip to content

Commit

Permalink
增加js variable
Browse files Browse the repository at this point in the history
  • Loading branch information
hisune committed Mar 27, 2017
1 parent 7112ea3 commit 0dcd985
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Config
public static $distType = ''; // Empty is full, other options: simple, common
public static $minify = true; // Whether or not load minify js file
public static $extraScript = array();
public static $jsVar = '';

public static function jsExpr($string)
{
Expand Down Expand Up @@ -73,7 +74,7 @@ public static function render($id, $option, $theme = null, array $attribute = ar
} else
$js = '';

$random = uniqid();
$jsVar = static::$jsVar;

if(version_compare(self::$version, '3.0.0') < 0){
$dist = self::$dist;
Expand Down Expand Up @@ -104,16 +105,16 @@ function (ec) {
$eventsHtml = '';
if($events){
foreach($events as $event => $call){
$eventsHtml .= 'chart_'. $random . '.on(\'' . $event . '\', function (params) {' . $call . '});';
$eventsHtml .= 'chart_'. $jsVar . '.on(\'' . $event . '\', function (params) {' . $call . '});';
}
}
$option = self::jsonEncode($option);
return <<<HTML
<div id="$id" $attribute></div>
$js
<script type="text/javascript">
var chart_$random = echarts.init(document.getElementById('$id'), '$theme');
chart_$random.setOption($option);$eventsHtml
var chart_$jsVar = echarts.init(document.getElementById('$id'), '$theme');
chart_$jsVar.setOption($option);$eventsHtml
</script>
HTML;
}
Expand Down
20 changes: 19 additions & 1 deletion src/ECharts.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,17 @@ class ECharts implements \ArrayAccess

public $_options = [];
public $_events = [];
protected $jsVar;

/**
* @param $dist string, dist of libraries
*/
public function __construct($dist = '')
{
if ($dist)
if ($dist){
Config::$dist = $dist;
}
$this->setJsVar();
}

public function __set($offset, $value)
Expand Down Expand Up @@ -206,6 +209,21 @@ public function on($event, $callback)
$this->_events[$event] = $callback;
}

public function setJsVar($name = null)
{
if(!$name || !is_string($name)){
Config::$jsVar = uniqid();
}else{
Config::$jsVar = $name;
}
$this->jsVar = Config::$jsVar;
}

public function getJsVar()
{
return $this->jsVar;
}

}


Expand Down

0 comments on commit 0dcd985

Please sign in to comment.