如何缩短Chart.js标签 [英] How to shorten Chart.js label

查看:90
本文介绍了如何缩短Chart.js标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题是我有两个用Chart.js制作的图表,它们包含动态标签说明,如果某些标签说明太长,图表会变小,但我需要两个图表都包含相同的高度.

The problem I'm facing is I have 2 charts, made with Chart.js, them contains dynamics labels descriptions, if some label descriptions is too long, chart is turning smaller, but I need that both charts contain same height.

如何配置Chart.js来缩短标签,并在鼠标悬停时显示完整的标签(如HTML标题),以保持标签区域的高度?

How can I configure Chart.js to shorten labels, and shows complete label like HTML title when mouse hover, to maintain the label area height?

无论标签有多长,我都需要保持图表区域的高度.

What I need is maintain height of chart area, no matter how long the label is.

var options = {
        //maintainAspectRatio: false,
        responsive:true,
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    };

var ctx = document.getElementById("myCanvas1");
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Red", "Blue yjfid ", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: options
});


var ctx = document.getElementById("myCanvas2");
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: options
});

div{
  display: inline-block;
  width: 49%;
}

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.js"></script>
  <div>
    <canvas id="myCanvas1" ></canvas>
  </div>
  <div>
    <canvas id="myCanvas2"></canvas>
  </div>
</body>
</html>

推荐答案

此答案参考来自帖子.鼠标悬停时您可以看到显示完整标签名称的工具提示

This answer reference from post.On mouse hover you can see tooltip showing full label name

var options = {
  scales: {
    xAxes: [{
      ticks: {
        callback: function(value) {
          if (value.length > 4) {
            return value.substr(0, 4) + '...'; //truncate
          } else {
            return value
          }

        },
      }
    }],
    yAxes: [{}]
  },
  tooltips: {
    enabled: true,
    mode: 'label',
    callbacks: {
      title: function(tooltipItems, data) {
        var idx = tooltipItems[0].index;
        return 'Title:' + data.labels[idx]; //do something with title
      },
      label: function(tooltipItems, data) {
        //var idx = tooltipItems.index;
        //return data.labels[idx] + ' €';
        return tooltipItems.xLabel + ' €';
      }
    }
  },
};


var ctx = document.getElementById("myCanvas1");
var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["Red", "Blue mbkjbjkbjlkbk", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)',
        'rgba(255, 159, 64, 0.2)'
      ],
      borderColor: [
        'rgba(255,99,132,1)',
        'rgba(54, 162, 235, 1)',
        'rgba(255, 206, 86, 1)',
        'rgba(75, 192, 192, 1)',
        'rgba(153, 102, 255, 1)',
        'rgba(255, 159, 64, 1)'
      ],
      borderWidth: 1
    }]
  },
  options: options
});


var ctx = document.getElementById("myCanvas2");
var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: [
        'rgba(255, 99, 132, 0.2)',
        'rgba(54, 162, 235, 0.2)',
        'rgba(255, 206, 86, 0.2)',
        'rgba(75, 192, 192, 0.2)',
        'rgba(153, 102, 255, 0.2)',
        'rgba(255, 159, 64, 0.2)'
      ],
      borderColor: [
        'rgba(255,99,132,1)',
        'rgba(54, 162, 235, 1)',
        'rgba(255, 206, 86, 1)',
        'rgba(75, 192, 192, 1)',
        'rgba(153, 102, 255, 1)',
        'rgba(255, 159, 64, 1)'
      ],
      borderWidth: 1
    }]
  },
  options: options
});

div {
  display: inline-block;
  width: 49%;
}

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>
  <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.js"></script>
  <div>
    <canvas id="myCanvas1"></canvas>
  </div>
  <div>
    <canvas id="myCanvas2"></canvas>
  </div>
</body>

</html>

这篇关于如何缩短Chart.js标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆