Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zingchart - passing a function to the tooltip

Is it possible to pass a function to the tooltip key in the Zingchart Json?

I tried the following so far:

  $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"),
   }
}

}

But the function gets the string "%kt" as it is and not the wanted X-Value of the hovered Plot. So how is it possible to pass the X-Value in the Function?

like image 606
GuyWithCookies Avatar asked May 04 '16 09:05

GuyWithCookies


1 Answers

ZingChart does not allow passing in functions through the configuration object. Instead, there is a property called "jsRule" which allows you to pass the name a function to be evaluated during each tooltip event.

tooltip : {
  jsRule : "CustomFn.formatTooltip()"
}

Inside that function, a parameter will be available that will contain information about the node you moused over such as value, scaletext, plotindex, nodeindex, graphid, and more. Simply return an object for the tooltip (including the formatted text) and ZingChart will take care of the rest. Example provided down below.

The one caveat to jsRule is that the function name has to be accessible globally since ZingChart does not accept inline functions. We are aware of this issue and are planning for this to be an option in future versions.

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>
like image 77
mike-schultz Avatar answered Nov 07 '22 13:11

mike-schultz