如何在悬停点时在Chart.js上添加垂直线?

时间:2017-11-08 09:23:47

标签: javascript charts javascript-events chart.js

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

Screenshot

这是我的图表代码:



<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>
&#13;
&#13;
&#13;

如何在上面的示例中为我的图表添加一行?

1 个答案:

答案 0 :(得分:2)

我发现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>