如何在没有鼠标悬停的情况下始终在chartjs中显示标签?

时间:2017-09-28 04:22:04

标签: javascript charts chart.js

我正在使用Chart Js的最新版本。我需要始终在图表中显示标签(不用鼠标悬停)。可能吗?如果是,那么请帮助我使用示例代码。

谢谢。

My Current Chartjs代码:

var ctx = $("#myChart");
var label = ctx.data('clabel').split(',');
var val = ctx.data('cval').split(',');


var myChart = new Chart(ctx, {
    type: 'line',
    data: {
        labels: label,
        datasets: [{
            label: 'Daily Capital',
            data: val,
            backgroundColor: [
                'rgba(0, 153, 34, 0.5)',
            ],
            borderColor: [
                'rgba(0, 153, 34, 1);',
            ],
            borderWidth: 2
        }]
    },
    options: {
        responsive: true,
        maintainAspectRatio: false,
        legend: {
            display: false,
        },
        animation: {
            duration: 0, // general animation time
        },
        hover: {
            animationDuration: 0, 
        },
        responsiveAnimationDuration: 0, // animation duration after a resize
        elements: {
            line: {
                tension: 0, // disables bezier curves
            },
        },
        tooltips: {
            callbacks: {
                label: function(tooltipItem, data){
                    return '£' + tooltipItem.yLabel;
                },
                title: function(tooltipItem, data){
                    return '';
                },
            }
        },
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true
                }
            }]
        }
    }       
});

我希望有人可以提供帮助。提前谢谢

2 个答案:

答案 0 :(得分:4)

这可以通过添加options onAnimationCompletetooltipevents来解决。

onAnitmationComplete个函数调用showToolTip方法来显示tooltips,就像悬停事件一样。

通常tooltipevents被定义为显示tooltips,但这里需要传递一个空数组。请查看下面的小提琴示例以获得折线图。

var options = {
  tooltipTemplate: "<%= value %>",

  showTooltips: true,

  onAnimationComplete: function() {
    this.showTooltip(this.datasets[0].points, true);
  },
  tooltipEvents: []
}

注意:此方法不支持折线图和条形图中的多数据集,但支持饼图中的多数据集

var data_line = {
  labels: ["January", "February", "March", "April", "May", "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: [65, 59, 80, 81, 56, 55, 40]
  }]
};


var options = {
  tooltipTemplate: "<%= value %>",

  showTooltips: true,

  onAnimationComplete: function() {
    this.showTooltip(this.datasets[0].points, true);
  },
  tooltipEvents: []
}

var context = $('#chart3').get(0).getContext('2d');
var chart = new Chart(context).Line(data_line, options);
<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/1.0.2/Chart.min.js"></script>
<div id="chartContainer">
  <canvas id="chart3" width="500" height="500"></canvas>
</div>

答案 1 :(得分:0)

options: {
             legend: {
               display: false
             },
             hover: {
               animationDuration: 0
             },
             animation: {
               onComplete: function() {
                 const chartInstance = this.chart,
                   ctx = chartInstance.ctx;

                 ctx.font = Chart.helpers.fontString(
                   18,
                   Chart.defaults.global.defaultFontStyle,
                   Chart.defaults.global.defaultFontFamily
                 );
                 ctx.textAlign = "center";
                 ctx.textBaseline = "bottom";

                 this.data.datasets.forEach(function(dataset, i) {
                   const meta = chartInstance.controller.getDatasetMeta(i);
                   meta.data.forEach(function(bar, index) {
                     const data = dataset.data[index];
                     ctx.fillStyle = "#000";
                     ctx.fillText(data, bar._model.x, bar._model.y - 2);
                   });
                 });
               }
             },
             tooltips: {
               enabled: true
             },
             responsive: true,
             scales: {
               xAxes: [
                 {
                   display: true,
                   gridLines: {
                     drawOnChartArea: false
                   }
                 }
               ],
               yAxes: [
                 {
                   display: true,
                   gridLines: {
                     drawOnChartArea: false
                   },
                   ticks: {
                     precision: 0
                   }
                 }
               ]
             }
           }