最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chart.js Y axis label, reverse tooltip order, shorten X axis labels - Stack Overflow

programmeradmin1浏览0评论

I am creating a stacked bar chart using Chart.js. However, I cannot find in the documentation how to change some things.

  1. How to add a label on the Y axis.
  2. How to shorten the label on the X axis so it displays only the first 10 letters.
  3. How to reverse the order in which the values are shown in the tooltip.

Are these thigs are possible to implement?

I have marked what I want to change here.

Here are what my chart options look like now:

        var ctx = $("#newchart");

        var barGraph = new Chart(ctx, {
            type: 'bar',
            data: chartdata,
            options: {
                barValueSpacing: 20,
                tooltips: {
                    enable: true,
                    mode: 'label',
                    callbacks: {
                        label: function(tooltipItem, data){
                            var datasetLabel = data.datasets[tooltipItem.datasetIndex].label || '';
                            return datasetLabel + ': ' + Number(tooltipItem.yLabel) + ' Users';
                        }
                    }
                },
                responsive: true,
                segmentShowStroke: true,
                scales: {
                    xAxes: [{
                        display: false,
                        stacked: true,
                        ticks:{
                            stepSize : 10,
                        },
                        gridLines: {
                            lineWidth: 0,
                            color: "#9E9E9E"
                        }
                    }],
                    yAxes: [{
                        stacked: true,
                        ticks: {
                            min: 0,
                            stepSize: 10,
                        },
                        gridLines: {
                            lineWidth: 0,
                            color: "#9E9E9E"
                        }
                    }]
                }
            }
        });

I am creating a stacked bar chart using Chart.js. However, I cannot find in the documentation how to change some things.

  1. How to add a label on the Y axis.
  2. How to shorten the label on the X axis so it displays only the first 10 letters.
  3. How to reverse the order in which the values are shown in the tooltip.

Are these thigs are possible to implement?

I have marked what I want to change here.

Here are what my chart options look like now:

        var ctx = $("#newchart");

        var barGraph = new Chart(ctx, {
            type: 'bar',
            data: chartdata,
            options: {
                barValueSpacing: 20,
                tooltips: {
                    enable: true,
                    mode: 'label',
                    callbacks: {
                        label: function(tooltipItem, data){
                            var datasetLabel = data.datasets[tooltipItem.datasetIndex].label || '';
                            return datasetLabel + ': ' + Number(tooltipItem.yLabel) + ' Users';
                        }
                    }
                },
                responsive: true,
                segmentShowStroke: true,
                scales: {
                    xAxes: [{
                        display: false,
                        stacked: true,
                        ticks:{
                            stepSize : 10,
                        },
                        gridLines: {
                            lineWidth: 0,
                            color: "#9E9E9E"
                        }
                    }],
                    yAxes: [{
                        stacked: true,
                        ticks: {
                            min: 0,
                            stepSize: 10,
                        },
                        gridLines: {
                            lineWidth: 0,
                            color: "#9E9E9E"
                        }
                    }]
                }
            }
        });
Share Improve this question edited Apr 20, 2017 at 9:04 Quince 15k6 gold badges62 silver badges70 bronze badges asked Apr 18, 2017 at 14:19 Dimitar ArabadzhiyskiDimitar Arabadzhiyski 2821 gold badge7 silver badges22 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 23

1. Adding a Y-axis label

in the yAxes object give it a scaleLabel object that takes in labelString (example fiddle)

yAxes: [{
    scaleLabel: {
      labelString: 'Value'
    }
 }]

2. Shortening xAxis category labels

For this, you can pass a userCallback function to the xAxis ticks object that can return your desired output. The function will take in the original label in its first parameter so you can just return a substring at your desired length, example fiddle

 xAxes: [{
    ticks: {
      userCallback: function(label, index, labels) {
        if(typeof label === "string")
        {
         return label.substring(0,1)
        }
        return label;

      },
    }
  }],

3. reverse tooltip order

The tooltips object accepts a function called itemSort that can be passed to Array.prototype.sort.

So you could so something like below, but you may also need to compare the objects index as well as the datasetIndex to get your desired result. (example fiddle)

tooltips: {
  mode: 'label',
  itemSort: function(a, b) {
    return b.datasetIndex - a.datasetIndex
  },

},

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论