英文:
chart.js v4 truncate scale/labels
问题
过去曾经有类似的问题在这里提出,但最终适用于Chart.js的旧版本。这些问题的已接受答案并没有奏效。
这个图表的代码如下:
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-js -->
new Chart(
document.getElementById("chart-01"), {
type: 'bar',
data: {
labels: ["Test loooooong 1", "Test loooooong 2", "Test loooooong 3"],
datasets: [{ data: [574.48, 140.93, 64.37]}]
},
options: {
scales: {
y: {
ticks: {
callback: function(value) {
return '$ ' + value;
}
}
},
// x : {
// ticks : {
// callback : function(value, index, ticks) { return value.split(0,8)+'...';}
// }
// }
},
plugins: {
legend: {
display: false
}
}
}
}
);
<!-- 语言: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.2.1/chart.umd.js" integrity="sha512-vCUbejtS+HcWYtDHRF2T5B0BKwVG/CLeuew5uT2AiX4SJ2Wff52+kfgONvtdATqkqQMC9Ye5K+Td0OTaz+P7cw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<canvas id="chart-01" width="500" height="600"></canvas>
<!-- 结束代码片段 -->
我尝试截断x轴上的名称,只保留8个字符,然后添加'...'。所以,如果我使用了注释的代码块:
x : {
ticks : {
callback : function(value, index, ticks) { return value.slice(0,8) + '...';}
}
}
我会得到错误消息:
未捕获的Promise错误:value.split不是一个函数
如果我只使用value而不使用split,我会得到数字,而不是标签名称。
这些是对我无效的解决方案的链接:
https://stackoverflow.com/a/39537545/3408158
https://stackoverflow.com/a/44418430/3408158
我做错了什么?
英文:
similar questions has been asked here in the past but eventually for older versions of Chart.js. The accepted answers of the questions didn't work.
I have a chart like this:
The code of this chart is this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
new Chart(
document.getElementById("chart-01"), {
type: 'bar',
data: {
labels: ["Test loooooong 1", "Test loooooong 2", "Test loooooong 3"],
datasets: [{ data: [574.48, 140.93, 64.37]}]
},
options: {
scales: {
y: {
ticks: {
callback: function(value) {
return '$ ' + value;
}
}
},
// x : {
// ticks : {
// callback : function(value, index, ticks) { return value.split(0,8)+'...';}
// }
// }
},
plugins: {
legend: {
display: false
}
}
}
}
);
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.2.1/chart.umd.js" integrity="sha512-vCUbejtS+HcWYtDHRF2T5B0BKwVG/CLeuew5uT2AiX4SJ2Wff52+kfgONvtdATqkqQMC9Ye5K+Td0OTaz+P7cw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<canvas id="chart-01" width="500" height="600"></canvas>
<!-- end snippet -->
I am trying to truncate the names on the x-axis after 8 chars and add a '...', So if I am using that commented code block:
x : {
ticks : {
callback : function(value, index, ticks) { return value.split(0,8)+'...';}
}
}
I get the error message:
Uncaught (in promise) TypeError: value.split is not a function
Also if I use only the value, without split, I get numbers back but not the label names.
This are links to solutions that did not work for me.
https://stackoverflow.com/a/39537545/3408158
https://stackoverflow.com/a/44418430/3408158
What am I doing wrong?
答案1
得分: 2
要获取/更改标签的值,您必须使用函数getLabelForValue
。
(在文档中的示例)
而split
是不正确的函数,您需要使用substring
。
(链接到 MDN 文档)
以下是所有修复后的示例代码:
<!-- 开始代码段:js 隐藏:false 控制台:false babel:false -->
<!-- 语言:lang-js -->
new Chart(document.getElementById("chart"), {
type: 'bar',
data: {
labels: ["Test looooong 1", "Test looooong 2", "Test looooong 3"],
datasets: [{ data: [574.48, 140.93, 64.37]}]
},
options: {
scales: {
y: {
ticks: { callback: value => `$ ${value}` }
},
x: {
ticks: {
callback: function(value){
let newLabel = this.getLabelForValue(value)
.substring(0,8) + '...';
return newLabel;
}
}
}
}
}
});
<!-- 语言:lang-html -->
<script src="//cdn.jsdelivr.net/npm/chart.js"></script>
<div class="chart" style="height:184px; width:350px;">
<canvas id="chart"></canvas>
</div>
<!-- 结束代码段 -->
信息: 提到的解决方案不起作用,因为据我所知,它们基于 chartjs 版本 2.0+,而自那时以来 chartjs 库发生了重大变化。
英文:
To get/change the value of the label you have to use the function getLabelForValue
.
(here an example from the documentation)
And split
is the incorrect function you would need to use substring
.
(link to the mdn documentation)
Here all the fixes entered into your example code:
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-js -->
new Chart(document.getElementById("chart"), {
type: 'bar',
data: {
labels: ["Test looooong 1", "Test looooong 2", "Test looooong 3"],
datasets: [{ data: [574.48, 140.93, 64.37]}]
},
options: {
scales: {
y: {
ticks: { callback: value => `$ ${value }` }
},
x: {
ticks: {
callback: function(value){
let newLabel = this.getLabelForValue(value)
.substring(0,8) + '...';
return newLabel;
}
}
}
}
}
});
<!-- language: lang-html -->
<script src="//cdn.jsdelivr.net/npm/chart.js"></script>
<div class="chart" style="height:184px; width:350px;">
<canvas id="chart" ></canvas>
</div>
<!-- end snippet -->
> Information: The mentioned solutions don't worked because, as far as I can tell, they are base on he chartjs Verion 2.0+, and since then the chartjs library change substantially.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论