Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart.js - Add text/label to bubble chart elements without using tooltips?

Question

Is there any way to add labels to the individual bubbles in a Chart.js bubble chart without resorting to displaying tooltips at all times?

Background

The chart data is for visualizing our project backlog. Additional details, i.e. Project Name, about each project are in a table.

We previously used google charts, and just included the row number from the table on the bubble so you could match things up. enter image description here

With Chart.js I only get the bubbles and tooltips. enter image description here

I've reviewed the following related questions, but the solution they suggested requires having tooltips display at all times. I've got a lot more information in the tooltips and displaying them all the time would significantly clutter the chart.

  • Can individual bubbles in a chartjs bubble chart have labels?
  • How to show tooltips always on Chart.js 2
like image 300
Ralph Callaway Avatar asked Dec 21 '16 01:12

Ralph Callaway


People also ask

How do you add labels to a bubble chart?

Add data labelsClick the chart, and then click the Chart Design tab. Click Add Chart Element and select Data Labels, and then select a location for the data label option. Note: The options will differ depending on your chart type. If you want to show your data label inside a text bubble shape, click Data Callout.


1 Answers

Chart.js doesn't support this directly, but Evert Timberg was very helpful in providing an example Chart.js plugin does exactly this.

From Chart.js Data Labeling Example

// Define a plugin to provide data labels
Chart.plugins.register({
  afterDatasetsDraw: function(chartInstance, easing) {
    // To only draw at the end of animation, check for easing === 1
    var ctx = chartInstance.chart.ctx;
    chartInstance.data.datasets.forEach(function (dataset, i) {
      var meta = chartInstance.getDatasetMeta(i);
      if (!meta.hidden) {
        meta.data.forEach(function(element, index) {
          // Draw the text in black, with the specified font
          ctx.fillStyle = 'rgb(0, 0, 0)';
          var fontSize = 16;
          var fontStyle = 'normal';
          var fontFamily = 'Helvetica Neue';
          ctx.font = Chart.helpers.fontString(fontSize, fontStyle, fontFamily);
          // Just naively convert to string for now
          // <---- ADJUST TO DESIRED TEXT --->
          var dataString = dataset.data[index].toString();
          // Make sure alignment settings are correct
          ctx.textAlign = 'center';
          ctx.textBaseline = 'middle';
          var padding = 5;
          var position = element.tooltipPosition();
          ctx.fillText(dataString, position.x, position.y - (fontSize / 2) - padding);
        });
      }
    });
  }
});

For example, if i just passed in "#22" as the text to render, we get this.

enter image description here

like image 118
Ralph Callaway Avatar answered Oct 02 '22 10:10

Ralph Callaway