当鼠标悬停时,为什么Chart.js的工具提示出现在错误的位置?

时间:2014-08-21 05:31:10

标签: javascript html graph tooltip chart.js

这是我的新代 Chart.js

的代码
<script>
    var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
    var lineChartData = {
        labels : ["January","February","March","April","May","June","July","January","February","March","April","May","June","July","January","February","March","April","May","June","July","January","February","March","April","May","June","July","January","February","March","April","May","June","July","January","February","March","April","May","June","July","June","July"],
        datasets : [
            {
                label: "My First dataset",
                fillColor : "rgba(220,220,220,0.2)",
                strokeColor : "rgba(220,220,220,1)",
                pointColor : "rgba(220,220,220,1)",
                pointStrokeColor : "#fff",
                pointHighlightFill : "#fff",
                pointHighlightStroke : "rgba(220,220,220,1)",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),9000,randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            },
            {
                label: "My Second dataset",
                fillColor : "rgba(151,187,205,0.2)",
                strokeColor : "rgba(151,187,205,1)",
                pointColor : "rgba(151,187,205,1)",
                pointStrokeColor : "#fff",
                pointHighlightFill : "#fff",
                pointHighlightStroke : "rgba(151,187,205,1)",
                data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),20000,randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),8977,randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
            }
        ]

    }

window.onload = function(){
    var ctx = document.getElementById("canvas").getContext("2d");
    window.myLine = new Chart(ctx).Line(lineChartData, {
        responsive: false,
        showTooltips: true,
         bezierCurve : false,
          pointDot : false,

    });
}


</script>

,结果是......

enter image description here

- &GT;红点是我的鼠标悬停,但错了!正确的位置应该是黑点

谢谢你:)

3 个答案:

答案 0 :(得分:6)

默认命中检测半径有点大。尝试设置

pointHitDetectionRadius : 1,

这为具有许多数据点的图表提供了更多定义。

更新:Fiddle

答案 1 :(得分:0)

我也遇到了类似的问题,工具提示距离我的鼠标不远。这将使工具提示锁定到鼠标的最近点:

options: {
  tooltips: {
    mode: 'nearest',
    intersect: false
  }
}

这是一个外观示例(红点是我的鼠标所在的位置):
enter image description here

答案 2 :(得分:0)

像这样使工具提示起作用

 tooltips: {
            mode: 'label',
            position: "nearest",
            label: 'mylabel',
            callbacks: {
                label: function(tooltipItem, data) {
                    return   data.datasets[tooltipItem.datasetIndex].label+ " "  + number_format2(tooltipItem.yLabel.toString(), 2, ',', '.') ; }, },
        }