Chartjs Tooltip Line Breaks

后端 未结 5 589
盖世英雄少女心
盖世英雄少女心 2020-12-09 15:02

Is it possible to get line breaks in chartjs tooltips?

tooltipTemplate: \"<%if (label){%><%=label%>: <%}%><%= value %>\"
相关标签:
5条回答
  • 2020-12-09 15:24

    Actually all tool-tip callbacks support multiple lines of text, and you can use label callback as usual. It renders data label as tool-tip text by default.

    Quoted from documentation:

    All functions must return either a string or an array of strings. Arrays of strings are treated as multiple lines of text.

    Example code:

    tooltips: {
        callbacks: {
          label: (tooltipItem, data) => {
            if (tooltipItem.index % 2)
              return ['Item 1', 'Item 2', 'Item 3'];
            else
              return 'Single line';
          }
        }
      }
    
    0 讨论(0)
  • 2020-12-09 15:34

    If you are using 2.0.0-beta2, you can use tooltip callback and return array of strings there.

    tooltips: {
        mode: 'single',
        callbacks: {
            afterBody: function(data) {
                var multistringText = ['first string'];
                // do some stuff
                multistringText.push('another string');
    
                return multistringText;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-09 15:35

    You can use tooltips footer callback,it will also not render coloured square for each list.

    tooltips: {
      callbacks: {
        label: function(tooltipItem, data) {
          let label = data.datasets[tooltipItem.datasetIndex].label;
          let value = data.datasets[tooltipItem.datasetIndex].data[tooltipItem.index];
          return label + ': ' + value;
        },
        footer: function(tooltipItems, data) {
          return ['new line', 'another line'];
        }
      }
    }

    0 讨论(0)
  • 2020-12-09 15:37

    This worked for me. Just simply return an array of strings as labels in tooltips.

     tooltips: {
        callbacks: {
          label: function(tooltipItem, data) {
            let label = "Line 1";
            let label2 = "Line 2";
            return [label, label2];
          }
        }
      }
    
    0 讨论(0)
  • 2020-12-09 15:40

    At this point in time, it's not possible to add line breaks to a tooltip or axis label. Right now the developers are discussion implementation options; the discussion can be found Allow wrapping in axis labels (issue on github).

    0 讨论(0)
提交回复
热议问题