javascript - How to shorten Chart.js label - Stack Overflow

The problem I'm facing is I have 2 charts, made with Chart.js, them contains dynamics labels descr

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.

How can I configure Chart.js to shorten labels, and shows plete 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=".1.0.js"></script>
<script src=".js/2.6.0/Chart.js"></script>
  <div>
    <canvas id="myCanvas1" ></canvas>
  </div>
  <div>
    <canvas id="myCanvas2"></canvas>
  </div>
</body>
</html>

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.

How can I configure Chart.js to shorten labels, and shows plete 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./jquery-3.1.0.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/Chart.js/2.6.0/Chart.js"></script>
  <div>
    <canvas id="myCanvas1" ></canvas>
  </div>
  <div>
    <canvas id="myCanvas2"></canvas>
  </div>
</body>
</html>

Share Improve this question edited Jun 8, 2017 at 7:52 Deep 3015 10.1k5 gold badges32 silver badges54 bronze badges asked Jun 6, 2017 at 17:56 Lai32290Lai32290 8,60819 gold badges70 silver badges104 bronze badges 4
  • Possible duplicate of Truncating canvas labels in ChartJS while keeping the full label value in the tooltips – Deep 3015 Commented Jun 6, 2017 at 18:09
  • I think the question is a little bit different, maybe I can truncate the label string, but I need show them pletely when mouse hover – Lai32290 Commented Jun 6, 2017 at 18:15
  • add sample code where you are facing problem – Deep 3015 Commented Jun 6, 2017 at 18:27
  • @Deep3015 My bad, I added code sample – Lai32290 Commented Jun 6, 2017 at 18:59
Add a ment  | 

1 Answer 1

Reset to default 4

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./jquery-3.1.0.js"></script>
  <script src="https://cdnjs.cloudflare./ajax/libs/Chart.js/2.6.0/Chart.js"></script>
  <div>
    <canvas id="myCanvas1"></canvas>
  </div>
  <div>
    <canvas id="myCanvas2"></canvas>
  </div>
</body>

</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745469364a4629063.html

相关推荐

  • javascript - How to shorten Chart.js label - Stack Overflow

    The problem I'm facing is I have 2 charts, made with Chart.js, them contains dynamics labels descr

    2小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信