简体   繁体   English

图表js的多行标签

[英]Multiple line labels for chart js

I have this radar chart in chart.js which has 5 labels. 我在chart.js中有这个雷达图表,它有5个标签。 The labels are quite long so I want to show them in two lines in HTML but when I use "\\n" it doesn't make a new line! 标签很长,所以我想在HTML中用两行显示它们但是当我使用“\\ n”时它不会换行!

These are my labels: 这些是我的标签:

labels: ["COMMUNICATION \n SKILL ", "PRODUCT AND PROCESS \n KNOWLEDGE "]

As you can see I'm trying to have "communication skill" and "product and process knowledge" both in two lines but it shows them in one line! 正如你所看到的那样,我试图将“沟通技巧”和“产品与流程知识”分为两行,但它将它们显示在一行中!

What's the correct way to do it? 这样做的正确方法是什么?

UPDATE UPDATE

The labels is in script tag: 标签位于脚本标记中:

var myChart = new Chart(ctx, {
    type: 'radar',
    data: {
        labels: ["COMMUNICATION  SKILL ", "PRODUCT AND PROCESS KNOWLEDGE "],
        datasets: [{
            label: labs,
            data: dps,
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
            ],
            borderWidth: 1
        },
    options: {
        maintainAspectRatio: true,
        scale: {
            ticks:{
                beginAtZero: true,
                max: 4
            }
        }
    }
});

Just read the docs https://www.chartjs.org/docs/latest/charts/radar.html you need your dataset to have the property data and that should be an array. 只需阅读文档https://www.chartjs.org/docs/latest/charts/radar.html,您需要dataset来获取属性data ,这应该是一个数组。 The values in the array will correspond with the values in the labels by their index number. 数组中的值将与标签中的索引号对应。

data: {
    labels: ['Running', 'Swimming', 'Eating', 'Cycling'],
    datasets: [{
        data: [20, 10, 4, 2]
    }]
}

I believe what you are looking for is answered here: ChartJS New Lines '\\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2 我相信您所寻找的内容在这里得到解答: ChartJS New Lines'\\ n'在X轴标签中或使用ChartJS V2在图表或工具提示周围显示更多信息

The solution is to pass a nested array as an input to 'labels' - with each element in the nested array representing a new line of text in your label. 解决方案是将嵌套数组作为输入传递给“标签” - 嵌套数组中的每个元素代表标签中的新文本行。 So in your case the following should work: 所以在你的情况下,以下应该工作:

labels: [["COMMUNICATION","SKILL"], ["PRODUCT AND PROCESS","KNOWLEDGE"]]

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

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