【问题标题】:Zingchart - passing a function to the tooltipZingchart - 将函数传递给工具提示
【发布时间】:2016-05-04 09:03:45
【问题描述】:

是否可以将函数传递给 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”,而不是悬停图的所需 X 值。那么如何在 Function 中传递 X-Value 呢?

【问题讨论】:

    标签: javascript angularjs charts tooltip zingchart


    【解决方案1】:

    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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多