简体   繁体   English

标签未显示在ChartJS中

[英]Label is not showing in ChartJS

I'm using ChartJS for my implementation of charts, but I notice that one of my graph's label is hidden. 我将ChartJS用于实现图表,但是我注意到图表的标签之一是隐藏的。 It is not showing its label above the bar. 它没有在条上方显示其标签。 I've added a screenshot below for the comparison of two different bar graphs. 我在下面添加了一个屏幕截图,用于比较两个不同的条形图。 The left graph shows the label even if it is on the very top but the other one is not showing. 左图显示标签,即使该标签位于最顶部,但另一标签未显示。 Please see my screenshot and code below. 请在下面查看我的屏幕截图和代码。

function createChart(context, type, bgColor, bdColor, labels, actualData, options = {}){
    new Chart(context, {
        type: type,
        data: {
            labels: labels,
            datasets: [{
                label: "Actual",
                backgroundColor: bgColor,
                borderColor: bdColor,
                data: actualData,
            }]
        },

        options: options
    });
}


function getOptions(displayLegend = true){
    return {
      events: false,
         showTooltips: false,
         legend: {
             display: displayLegend
         },
         animation: {
             duration: 0,
             onComplete: function(){
                 var ctx = this.chart.ctx;
                 ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontFamily, 'normal', Chart.defaults.global.defaultFontFamily);
                 ctx.textAlign = 'center';
                 ctx.textBaseLine = 'bottom';
                 ctx.fillStyle = '#0b7707';

                 this.data.datasets.forEach(function(dataset){
                            console.log(dataset);
                     for(var i = 0; i < dataset.data.length; i++){
                         for(var key in dataset._meta){
                             var model = dataset._meta[key].data[i]._model;
                             ctx.fillText(dataset.data[i], model.x, model.y - 13);
                         }
                     }
                 });
             }
         }
     };
 }

在此处输入图片说明

This looks like a clear case of the datalabels going out of the canvas since the bar takes height dynamically as per the data values. 这看起来像是数据标签从画布上移出的明显情况,因为条形图根据数据值动态获取高度。 You can set the max y-tick setting to solve this. 您可以设置最大y-tick设置来解决此问题。 Here is the jsfiddle -> https://jsfiddle.net/Luaf2tm4/5979/ 这是jsfiddle-> https://jsfiddle.net/Luaf2tm4/5979/

Hope it helps! 希望能帮助到你!

var canvas = document.getElementById('myChart');
var data = {
  labels: ["January", "February", "March", "April", "May", "June"],
  datasets: [{
    label: "My First dataset",
    backgroundColor: "rgba(255,99,132,0.2)",
    borderColor: "rgba(255,99,132,1)",
    borderWidth: 2,
    hoverBackgroundColor: "rgba(255,99,132,0.4)",
    hoverBorderColor: "rgba(255,99,132,1)",
    data: [500, 2000, 800, 600, 950, 890],
  }]
};

function getOptions(displayLegend = false) {
  return {
    events: false,
    showTooltips: false,
    legend: {
      display: displayLegend
    },
    scales: {
      yAxes: [{
        display: true,
        stacked: true,
        ticks: {
            stepSize: 200,
          min: 0, // minimum value
          max: 2200 // maximum value, you can either hard code if you know your datainput, else computer the value through some logic i.e taking the max value from the dataset and adding some extra value to it. 
        }
      }]
    },
    animation: {
      duration: 0,
      onComplete: function() {
        var ctx = this.chart.ctx;
        ctx.font = Chart.helpers.fontString(Chart.defaults.global.defaultFontFamily, 'normal', Chart.defaults.global.defaultFontFamily);
        ctx.textAlign = 'center';
        ctx.textBaseLine = 'bottom';
        ctx.fillStyle = '#0b7707';

        this.data.datasets.forEach(function(dataset) {
          console.log(dataset);
          for (var i = 0; i < dataset.data.length; i++) {
            for (var key in dataset._meta) {
              var model = dataset._meta[key].data[i]._model;
              ctx.fillText(dataset.data[i], model.x, model.y - 10);
            }
          }
        });
      }
    }
  };
}

var myBarChart = Chart.Bar(canvas, {
  data: data,
  options: getOptions()
});

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

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