简体   繁体   English

如何覆盖功能convertTicksToLabels chartjs

[英]How do I override the function convertTicksToLabels chartjs

I want to be able to map tick values to strings in Chart.js. 我希望能够将刻度值映射到Chart.js中的字符串。

In the documentation ( https://www.chartjs.org/docs/latest/developers/axes.html?h=converttick ) I found the function 在文档( https://www.chartjs.org/docs/latest/developers/axes.html?h=converttick )中,我找到了该功能

// Transform the ticks array of the scale instance into strings. The default implementation simply calls this.options.ticks.callback(numericalTick, index, ticks);
convertTicksToLabels: function() {} 

Is there an example of how(like in what format)/where to write this function? 是否有一个示例(如哪种格式)/在哪里编写此函数的示例?

I assume it should be written in the options...Here is my code: 我认为它应该写在选项中...这是我的代码:

    chart = new Chart(ctx, {
    type: 'bubble',
    data: dataObj,
    options: {
        scales: {
            yAxes: [{ 
                ticks:{display: false},
                gridLines:{display:false}

            }],
            xAxes: [{ 
                ticks:{display: true},
                gridLines:{display:false},
                convertTicksToLabels: function() {//what goes in here?}
            }]
          }
    }
});

So far, I have only found answers about displaying formatted lines on an axis , which does not use this function, and overriding the calculateXLabelRotation function , which does not involve mapping strings to tick values (only returning one label rotation). 到目前为止,我仅找到有关在轴上显示格式化的行的答案,该轴不使用此功能,并且覆盖不涉及将字符串映射到刻度值(仅返回一个标签旋转) calculateXLabelRotation函数

I found an different way to map values to strings without using the function. 我发现了一种无需使用函数即可将值映射到字符串的不同方法。

I used the callback key instead of the convertTicksToLabels function. 我使用了callback键而不是convertTicksToLabels函数。

Here is my code now... 现在这是我的代码...

      chart = new Chart(ctx, {
        type: 'bubble',
        data: dataObj,
        options: {
            scales: {
                yAxes: [{ 
                    ticks:{
                        display: true,
                        callback: function(value, index, values) {
                            return mapValtoString(value);
                        }
                    },
                    scaleLabel: {labelString: "Factualness"},
                    gridLines:{display:false}
                }],
                xAxes: [{ 
                    ticks:{display: false},
                    scaleLabel: {labelString: "Bias"},
                    gridLines:{display:false}
                }]
              }
        }
    });

The mapValToString() function looks like this... mapValToString()函数如下所示...

function mapValtoString(val){
    switch(val){
        case 1:
            return "LOW";
            break;
        case 2:
            return "MEDIUM";
            break;
        case 3:
            return "HIGH";
            break;  
        default:
            return "";
    }

I return the string I want associated with a value and a blank string on the values not needed. 我返回要与一个值关联的字符串,并在不需要的值上返回一个空白字符串。

This solved my problem, the graph now displays strings instead of number values. 这解决了我的问题,该图现在显示字符串而不是数字值。

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

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