【问题标题】:How to add a vertical line on Chart.js when hover a point?悬停点时如何在 Chart.js 上添加垂直线?
【发布时间】:2018-04-20 21:53:53
【问题描述】:

我使用 Chart.js 创建了一个图表。它在水平轴和垂直轴上显示一些货币值。我添加了很多点,并将它们显示为圆圈。当悬停在这样的点上时,我想在该点上添加一条垂直线:

这是我的图表代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.js"></script>
<div class="container" style="width: 100%;margin-top:30px;">
    <canvas id="myChart"></canvas>
</div>
<script>
    let zzz = document.getElementById('myChart').getContext('2d');

    // Global Options
    Chart.defaults.global.defaultFontFamily = 'Arial';
    Chart.defaults.global.defaultFontSize = 16;
    Chart.defaults.global.defaultFontColor = '#000';


    let massPopChart = new Chart(zzz, {
      type:'line', 
      data:{
        labels:[
        '20/09/2017',
        '20/10/2017',
        '20/11/2017',
        '20/12/2017'
        ],
        datasets:[{
          label:'US Dollar',
          fill: false,
          lineTension: 0,
          data:[
            123,
            143,
            156,
            122
          ],
          pointBackgroundColor: '#f90',
          pointHoverBackgroundColor: '#fff',
          //backgroundColor:'green',
          backgroundColor:[
            '#2277bb',
            '#2277bb',
            '#2277bb',
            '#000000',
          ],
          borderWidth:3,
          borderColor:'#f90',
          hoverBorderWidth:3,
          hoverBorderColor:'#fff'
        }]
      },
      options:{
        title:{
          display:true,
          text:'Chart 1',
          fontSize:16
        },
        legend:{
          display:true,
          position:'top',
          labels:{
            fontColor:'#000'
          }
        },
        layout:{
          padding:{
            left:50,
            right:0,
            bottom:0,
            top:0
          }
        },
        tooltips:{
          enabled:true,
        },
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        },
        elements: {
            point: {
                radius: 5
            }
        },
        
        
        
      }
    });
  </script>

我怎样才能像上面的例子那样在我的图表中添加一条线?

【问题讨论】:

    标签: javascript charts chart.js


    【解决方案1】:

    我发现another question 描述了您想要的解决方案。我希望这会有所帮助。

    Chart.defaults.LineWithLine = Chart.defaults.line;
    Chart.controllers.LineWithLine = Chart.controllers.line.extend({
        draw: function(ease) {
            Chart.controllers.line.prototype.draw.call(this, ease);
    
            if (this.chart.tooltip._active && this.chart.tooltip._active.length) {
                var activePoint = this.chart.tooltip._active[0],
                    ctx = this.chart.ctx,
                    x = activePoint.tooltipPosition().x,
                    topY = this.chart.scales['y-axis-0'].top,
                    bottomY = this.chart.scales['y-axis-0'].bottom;
    
                // draw line
                ctx.save();
                ctx.beginPath();
                ctx.moveTo(x, topY);
                ctx.lineTo(x, bottomY);
                ctx.lineWidth = 4;
                ctx.strokeStyle = '#757575';
                ctx.stroke();
                ctx.restore();
            }
        }
    });
    
    Chart.defaults.global.defaultFontFamily = 'Arial';
    Chart.defaults.global.defaultFontSize = 16;
    Chart.defaults.global.defaultFontColor = '#000';
    
    var chart = new Chart("myChart", {
        type: 'LineWithLine',
        data: {
            labels: ['20/09/2017',
                '20/10/2017',
                '20/11/2017',
                '20/12/2017'
            ],
            datasets: [{
                label: 'US Dollar',
                fill: false,
                lineTension: 0,
                data: [123, 143, 156, 122],
                pointBackgroundColor: '#f90',
                pointHoverBackgroundColor: '#fff',
                //backgroundColor:'green',
                backgroundColor: ['#2277bb', '#2277bb', '#2277bb', '#000000', ],
                borderWidth: 3,
                borderColor: '#f90',
                hoverBorderWidth: 3,
                hoverBorderColor: '#fff'
            }]
        },
        options: {
            title: {
                display: true,
                text: 'Chart 1',
                fontSize: 16
            },
            legend: {
                display: true,
                position: 'top',
                labels: {
                    fontColor: '#000'
                }
            },
            layout: {
                padding: {
                    left: 50,
                    right: 0,
                    bottom: 0,
                    top: 0
                }
            },
            tooltips: {
                enabled: true,
                intersect: false
            },
            elements: {
                point: {
                    radius: 5
                }
            },
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: true
                    }
                }]
            },
        }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="container" style="width: 100%;margin-top:30px;">
        <canvas id="myChart"></canvas>
    </div>

    【讨论】:

    • 你是我的英雄.. 非常感谢:)
    猜你喜欢
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多