Zingchart-将一个函数传递给工具提示

Zingchart - passing a function to the tooltip

本文关键字:函数 工具提示 一个 Zingchart-      更新时间:2023-09-26

是否可以将函数传递给Zingchart Json中的工具提示键?

到目前为止,我尝试了以下内容:

  $scope.applyTooltip = function (timestamp) {
    console.log(timestamp);
    var tooltip = "<div>";
    var data = { 
     timestamp1: {
      param1: "bla",
      param2: "foo,
     },
     ...
    }
    for(var param in data){
      console.log(param);
      tooltip += param+": "+data[param]+"<br>";
    }
    tooltop += "</div>;
    return tooltip;
  }    

$scope.graphoptions = {
   //...
   //just displaying the relevant options 
   plot: {
      "html-mode": true,
       tooltip: $scope.applyTooltip("%kt"),
   }
}

}

但函数会按原样获取字符串"%kt",而不是悬停Plot所需的X值。那么,在函数中传递X值是怎么可能的呢?

ZingChart不允许通过配置对象传入函数。相反,有一个名为"jsRule"的属性,它允许您在每个工具提示事件中传递要评估的函数的名称。
tooltip : {
  jsRule : "CustomFn.formatTooltip()"
}

在该函数中,将提供一个参数,该参数将包含有关鼠标悬停在其上的节点的信息,如valuescaletextplotindexnodeindexgraphid等。只需为工具提示返回一个对象(包括格式化的文本),ZingChart就会处理剩下的内容。下面提供了示例。

jsRule需要注意的一点是,函数名称必须全局可访问,因为ZingChart不接受内联函数。我们意识到了这个问题,并计划在未来版本中将其作为一个选项。

CustomFn = {};
  var myConfig = {
   	type: "line", 
   	tooltip : {
   	  jsRule : "CustomFn.formatTooltip()"
   	},
  	series : [
  		{
  			values : [1,3,2,3,4,5,4,3,2,1,2,3,4,5,4]
  		},
  		{
  			values : [6,7,8,7,6,7,8,9,8,7,8,7,8,9,8]
  		}
  	]
  };
  
    
  CustomFn.formatTooltip = function(p){
    var dataset = zingchart.exec('myChart', 'getdata');
    var series = dataset.graphset[p.graphindex].series;
    
    var tooltipText = "";
    for (var i=0; i < series.length; i++) {
      tooltipText += "Series " + i + " : " + series[i].values[p.nodeindex] + "";
      if (i !== series.length-1) {
        tooltipText += "'n";
      }
    }
    return {
      text : tooltipText,
      backgroundColor : "#222"
    }
  }
  zingchart.render({ 
	id : 'myChart', 
	data : myConfig, 
	height: 400, 
	width: 600 
});
<!DOCTYPE html>
<html>
	<head>
		<script src= 'https://cdn.zingchart.com/2.3.1/zingchart.min.js'></script>
	</head>
	<body>
		<div id='myChart'></div>
	</body>
</html>