简体   繁体   English

在 ChartJs 中将长标签更改为多行(换行)标签

[英]change long labels to multiline(wrap) label in ChartJs

(↑ That is not my question's answer at all!! :( ) (↑ 这根本不是我的问题的答案!! :( )

Hi~ I'm using chartJS for drawing line graphs.嗨~我正在使用chartJS来绘制折线图。

    var areaChartData = {
      labels: ["2016-02-11_19:59:24", "2016-02-11_20:59:24", "2016-02-12_21:59:24", "2016-02-21_22:59:24", "2016-02-21_23:59:24", "2016-02-22_19:59:24", "2016-02-22_23:59:24", "2016-02-23_23:59:24", "2016-02-24_23:59:24", "2016-02-25_23:59:24"],
      datasets: [
        {
          label: "Elec",
          fillColor: "rgba(210, 214, 222, 1)",
          strokeColor: "rgba(210, 214, 222, 1)",
          pointColor: "rgba(210, 214, 222, 1)",
          pointStrokeColor: "#c1c7d1",
          pointHighlightFill: "#fff",
          pointHighlightStroke: "rgba(220,220,220,1)",
          data: [65, 59, 80, 81, 56, 55, 40, 23, 22, 21]
        },
        {
          label: "Goods",
          fillColor: "rgba(60,141,188,0.9)",
          strokeColor: "rgba(60,141,188,0.8)",
          pointColor: "#3b8bba",
          pointStrokeColor: "rgba(60,141,188,1)",
          pointHighlightFill: "#fff",
          pointHighlightStroke: "rgba(60,141,188,1)",
          data: [28, 48, 40, 19, 86, 27, 90, 23, 22, 21]
        }
      ]
    };

I can't show without angle the label in the graph because labels is too long to display.我无法在没有角度的情况下显示图表中的标签,因为标签太长而无法显示。 I want to change labels to multiline lables in axis labels我想在轴标签中将标签更改为多行标签

2016-02-11_19:59:24 2016-02-11_19:59:24

to

2016-02-11 2016-02-11
19:59:24 19:59:24

How can I change the options or Char.js script?如何更改选项或 Char.js 脚本?

http://jsfiddle.net/TZq6q/242/ http://jsfiddle.net/TZq6q/242/
http://jsfiddle.net/zruvru23/1/ http://jsfiddle.net/zruvru23/1/

According to the official sample repo you should put the single lines into another array:根据官方示例存储库,您应该将单行放入另一个数组中:

data: {
    labels: [['June', '2015'], 'July', '...', ['January', '2016'], 'February', '...'],
    // Rest of config

In this case June 2015 and January 2016 would have a new line for the year, the rest of the labels would not be wrapped.在这种情况下, June 2015January 2016 June 2015 January 2016将有一个新的生产线,其余的标签不会被包装。

The example is from a line chart, I tested it with a bar chart too, but it should work for all chart types.该示例来自折线图,我也用条形图对其进行了测试,但它应该适用于所有图表类型。

Tested with version 2.7.2 of ChartsJS使用 ChartsJS 2.7.2 版测试

将标签定义更改为:

 labels: [["2016-02-11", "19:59:24"], ["2016-02-11","20:59:24"]]

You can change the long label name to an array, chartjs will split each element into a line , this exemple worked for me :您可以将长标签名称更改为数组,chartjs 会将每个元素拆分为一行,这个示例对我有用:

labels: ['DAB', ['Park', 'Assist'], ['Lane', 'Assist'], ['Smartphone', 'Interface'], 'GPS', 'LED']

But, in this case, when you pass the mouse over the chart, the tooltip shown will seperate each line by a comma, in my case ['Park', 'Assist'] will be shown on tooltip as "Park,Assis".但是,在这种情况下,当您将鼠标移到图表上时,显示的工具提示将用逗号分隔每一行,在我的情况下,['Park', 'Assist'] 将在工具提示上显示为“Park,Assis”。 To resolve this problem you have to remove the comma by a space in tooltips callback, like this :要解决此问题,您必须在工具提示回调中通过空格删除逗号,如下所示:

tooltips: {
  enabled: true,
  callbacks: {

    title: function(tooltipItem, data) {
      return data.labels[tooltipItem.index];
    },
    label: function(tooltipItem, data) {
      let dataval = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
      let dslabels = data.labels[tooltipItem.index];

      // Remove the comma

      dslabels = dslabels.toString().replace(",", " ");

      return ' ' + dslabels + ' : ' + dataval + '%';
    }
  }
},

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

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