chart.js v4截断刻度/标签

huangapple go评论59阅读模式
英文:

chart.js v4 truncate scale/labels

问题

过去曾经有类似的问题在这里提出,但最终适用于Chart.js的旧版本。这些问题的已接受答案并没有奏效。

我有一个如下所示的图表:
chart.js v4截断刻度/标签

这个图表的代码如下:

<!-- 开始代码片段: 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:
chart.js v4截断刻度/标签

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 -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/Chart.js/4.2.1/chart.umd.js&quot; integrity=&quot;sha512-vCUbejtS+HcWYtDHRF2T5B0BKwVG/CLeuew5uT2AiX4SJ2Wff52+kfgONvtdATqkqQMC9Ye5K+Td0OTaz+P7cw==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/script&gt;
&lt;canvas id=&quot;chart-01&quot; width=&quot;500&quot; height=&quot;600&quot;&gt;&lt;/canvas&gt;

<!-- 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)+&#39;...&#39;;}
  }
}

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(&quot;chart&quot;), {
    type: &#39;bar&#39;,
    data: { 
        labels: [&quot;Test looooong 1&quot;, &quot;Test looooong 2&quot;, &quot;Test looooong 3&quot;],
        datasets: [{ data: [574.48, 140.93, 64.37]}]
    },
    options: {
        scales: {
            y: {
                ticks: { callback: value =&gt; `$ ${value }` }
            },
            x: {
               ticks: { 
                   callback: function(value){
                       let newLabel = this.getLabelForValue(value)
                           .substring(0,8) + &#39;...&#39;;
                       return newLabel;
                   }
               }
            }
        }
    }
});

<!-- language: lang-html -->

&lt;script src=&quot;//cdn.jsdelivr.net/npm/chart.js&quot;&gt;&lt;/script&gt;
&lt;div class=&quot;chart&quot; style=&quot;height:184px; width:350px;&quot;&gt;
   &lt;canvas id=&quot;chart&quot; &gt;&lt;/canvas&gt;
&lt;/div&gt;

<!-- 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.

huangapple
  • 本文由 发表于 2023年2月16日 10:52:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75467371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定