Добавление пользовательского текста в значения меток линейчатой ​​диаграммы с помощью Chart.js - PullRequest
0 голосов
/ 11 октября 2018

Я использую плагин Chart.js для отображения гистограммы, и я получаю вывод, как показано ниже:

enter image description here Мой вопрос о том, как добавить пользовательскийтекст после рендеринга значения в бар?Например, в январе значение показывает 56. Я хочу добавить% увеличенной / уменьшенной информации рядом с ней (т.е. 56 [115%]). Как это сделать?

Вот мой код

    window.chartHeadcount = new Chart(document.getElementById("barChartHeadcount"), {
        type: 'bar',
        data: {
            labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
            datasets: [{
                label: 'Billed',
                backgroundColor: 'rgb(0, 197, 106)',
                data: billedHeadCount
            }, {
                label: 'Unbilled',
                backgroundColor: 'rgb(255, 114, 107)',
                data: unBilledHeadCount
            }]
        },
        options: {
            title: {
                display: true,
                text: 'Community Headcount - ' + Options.Globals.Year
            },
            tooltips: {
                mode: 'index',
                intersect: false
            },
            responsive: true,
            scales: {
                xAxes: [{
                    stacked: false
                }],
                yAxes: [{
                    stacked: false
                }]
            }
        }
    });

1 Ответ

0 голосов
/ 12 октября 2018

Вы можете использовать плагин chartjs-datalabels и установить свойство форматера для установки пользовательских меток.

Создана скрипка для вашей справки -> http://jsfiddle.net/upmth2cq/1/

Надеюсь, это поможет!

new Chart(document.getElementById("barChartHeadcount"), {
  type: 'bar',
  data: {
    labels: ['Jan', 'Feb', 'Mar'],
    datasets: [{
      label: 'Billed',
      backgroundColor: 'rgb(0, 197, 106)',
      data: [56, 63, 67]
    }, {
      label: 'Unbilled',
      backgroundColor: 'rgb(255, 114, 107)',
      data: [1, 2, 3]
    }]
  },
  options: {
    title: {
      display: true,
      text: 'Community Headcount'
    },
    tooltips: {
      mode: 'index',
      intersect: false
    },
    responsive: true,
    scales: {
      xAxes: [{
        stacked: false
      }],
      yAxes: [{
        stacked: false
      }]
    },
    plugins: {
      datalabels: {
        align: 'end',
        anchor: 'end',
        backgroundColor: function(context) {
          return context.dataset.backgroundColor;
        },
        borderRadius: 4,
        color: 'white',
        formatter: function(value){
            return value + ' (100%) ';
        }
      }
    }
  }
});
...