score:1

Accepted answer

easiest way is to provide your data with multiple sets :

  data: {
      labels: ['total votes']
    , datasets: [{
          label: 'blue'
        , backgroundcolor: ['#2c79c5']
        , data: ['12']
    },{
          label: 'green'
        , backgroundcolor: ['#7fa830']
        , data: ['2']
    },
    ...
    ]
  }

but you can generate a custom labels using generatelabels - http://www.chartjs.org/docs/#chart-configuration-legend-configuration

or even customise the whole legend, including formatting, with legendcallback - http://www.chartjs.org/docs/#chart-configuration-common-chart-configuration

score:0

this solution uses chart.js version 3. you can pre-process your data using the plugin core api. the api offers different hooks that may be used for executing custom code.

i use the beforeinit hook to create individual datasets for each defined label/value pair. note that the data of these new datasets are defined in point format (for instance [{ x: 1, y: 12 }]):

beforeinit: chart => {
  let dataset = chart.config.data.datasets[0];
  chart.config.data.datasets = chart.config.data.labels.map((l, i) => ({
    label: l,
    data: [{ x: i + 1, y: dataset.data[i] }],
    backgroundcolor: dataset.backgroundcolor[i],
    categorypercentage: 1
  }));
  chart.config.data.labels = undefined;
}

further you need to define a second x-axis that will contain the labels.

x1: {
  offset: true,
  gridlines: {
    display: false
  }
}

the labels on x1 need to be collected and defined programmatically each time the hidden state of a dataset changes. this can be done in the beforelayout hook.

beforelayout: chart => chart.options.scales.x1.labels = chart.config.data.datasets.filter((ds, i) => !chart.getdatasetmeta(i).hidden).map(ds => ds.label)

please take a look at below runnable code and see how it works.

new chart('chart', {
  type: 'bar',
  plugins: [{
    beforeinit: chart => {      
      let dataset = chart.config.data.datasets[0];
      chart.config.data.datasets = chart.config.data.labels.map((l, i) => ({
        label: l,
        data: [{ x: i + 1, y: dataset.data[i] }],
        backgroundcolor: dataset.backgroundcolor[i],
        categorypercentage: 1
      }));
      chart.config.data.labels = undefined;
    },
    beforelayout: chart => chart.options.scales.x1.labels = chart.config.data.datasets.filter((ds, i) => !chart.getdatasetmeta(i).hidden).map(ds => ds.label)
  }],
  data: {
    labels: ['blue', 'green', 'yellow', 'red', 'purple', 'orange'],
    datasets: [{
      data: ['12', '2', '5', '0', '9', '1'],
      backgroundcolor: ['#2c79c5', '#7fa830', '#fff200', '#ed4d40', '#800080', '#ec802f']
    }]
  },
  options: {
    interaction: {
      intersect: true,
      mode: 'nearest'
    },
    plugins: {
      legend: {
        position: 'right'
      },
      tooltip: {
        callbacks: {
          title: () => undefined
        }
      }
    },   
    scales: {
      y: {
        beginatzero: true
      },
      x: {
        display: false
      },
      x1: {
        offset: true,
        gridlines: {
          display: false
        }
      }
    }
  }
});
canvas {
  max-width: 400px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/3.2.0/chart.min.js"></script>
<canvas id="chart" height="120"></canvas>

score:2

in one of the most recent releases of chart.js 2.1.x, they added back this functionality. so go get the latest release first. then insert the code below.

it is located under the options and legend. here is how you use it:

options: {
    legend: {
        position: 'right'
    }
}

Related Query

More Query from same tag