Search code examples
javascripthtmlflot

how to set tooltip text color in flot chart?


I am using flot chart with multi series data.i want to change the tooltip text color in Flot Chart. I am using this code:

$("#placeholder").bind("plothover", function (event, pos, item) {
 if (item) {  
     $("#tooltip").remove();
     var hoverSeries = item.series;
     var x = item.datapoint[0],
         y = item.datapoint[1];
     var strTip = x + " / " + y + " for " + item.series.label;

     var allSeries = plot.getData();
     $.each(allSeries, function(i,s){
         if (s == hoverSeries) return;   
         $.each(s.data, function(j,p){
             if (p[0] == x){
                 strTip += "</br>" + p[0] + " / " + p[1] + " for " + s.label;
             }
         });             
     });
     showTooltip(item.pageX, item.pageY, strTip);
  }
}); 

any one for color taking in tooltip?


Solution

  • Flot chart supports HTML tags in ToolTip

    You can easily use html tag:<span>

    Flot chart ToolTip Easily support HTML tags.

    With <span> tag you can use s.clor with style. Like this js.

    $("#placeholder").bind("plothover", function (event, pos, item) {
     if (item) {  
         $("#tooltip").remove();
         var hoverSeries = item.series;
         var x = item.datapoint[0],
             y = item.datapoint[1];
         var strTip = "<span style=\"color:" + item.series.color + ";\""+x + " / " + y + " for " + item.series.label + "</span>";
    
         var allSeries = plot.getData();
         $.each(allSeries, function(i,s){
             if (s == hoverSeries) return;   
             $.each(s.data, function(j,p){
                 if (p[0] == x){
                     strTip += "</br><span style=\"color:" + s.color + ";\"" + p[0] + " / " + p[1] + " for " + s.label + "</span>";
                 }
             });             
         });
         showTooltip(item.pageX, item.pageY, strTip);
      }
    }); 
    

    Here clearly, I have used item.series.color and s.color in strTip string of ToolTip