简体   繁体   English

Chart.js Y轴标签,反向工具提示顺序,缩短X轴标签

[英]Chart.js Y axis label, reverse tooltip order, shorten X axis labels

I am creating a stacked bar chart using Chart.js. 我正在使用Chart.js创建堆积条形图。 However, I cannot find in the documentation how to change some things. 但是,我在文档中找不到如何更改某些内容。

  1. How to add a label on the Y axis. 如何在Y轴上添加标签。
  2. How to shorten the label on the X axis so it displays only the first 10 letters. 如何缩短X轴上的标签,使其仅显示前10个字母。
  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"
                        }
                    }]
                }
            }
        });

1. Adding a Y-axis label 1. 添加Y轴标签

in the yAxes object give it a scaleLabel object that takes in labelString ( example fiddle ) yAxes对象中给它一个scaleLabel对象,它接受labelString示例小提琴

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

2. Shortening xAxis category labels 2. 缩短xAxis类别标签

For this, you can pass a userCallback function to the xAxis ticks object that can return your desired output. 为此,您可以将userCallback函数传递给可以返回所需输出的xAxis ticks对象。 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 3. 反向工具提示顺序

The tooltips object accepts a function called itemSort that can be passed to Array.prototype.sort . tooltips对象接受一个名为itemSort的函数,该函数可以传递给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. 所以你可以像下面这样,但你可能还需要比较对象索引以及datasetIndex来获得你想要的结果。 ( example fiddle ) 例子小提琴

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

},

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

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