简体   繁体   English

根据x轴值设置flot中工具提示的标签

[英]Setting label for tooltip in flot based on x-axis value

I have a line graph using flot. 我有一个使用flot的折线图。 I want the tooltip to show the x-axis and y-axis values. 我希望工具提示显示x轴和y轴值。

I am trying: 我在尝试:

content: "Orders <b>%y</b> for <span>%x</span>",

But this shows "Orders 100 for 0" for the first point, "Orders 100 for 1" for the second and so on. 但是第一点显示“订单100为0”,第二点显示“订单100为1”,依此类推。

If I use: 如果我使用:

content: "Orders <b>%y</b> for <span>"+chartData.axis[0][1]+"</span>",

Then this correctly shows the x-axis value for the first point. 然后,这会正确显示第一个点的x轴值。

But this doesn't work: 但这不起作用:

content: "Orders <b>%y</b> for <span>"+chartData.axis[%x][1]+"</span>",

It gives me: 它给了我:

Uncaught SyntaxError: Unexpected token % 

How can I reference the value of %x within chartData.axis ? 如何在chartData.axis中引用%x的值?

Here you would be better served using the function callback of the content property instead of the string formatting (and I'm guessing that's where you are going and asked your next question ). 在这里,您可以使用content属性的function回调而不是字符串格式来更好地服务(我猜这就是您要去的地方并询问您的下一个问题 )。

 tooltip: true,
   tooltipOpts: {
       content: function(label, xval, yval, flotItem){
           return "Orders <b>"+yval+"</b> for <span>"+chartData.axis[xval][2]+"</span>"
       },
       shifts: {
         x: -30,
         y: -50
       }
  }

Fiddle example here . 这里的小提琴示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM