javascript - Chart.js: Display Custom Tooltips, always visible on stacked bar-chart - Stack Overflow

In my MVC project, I am using chart.js (v2.8.0) to build a stacked bar-chart, with 3 data-sets.I need t

In my MVC project, I am using chart.js (v2.8.0) to build a stacked bar-chart, with 3 data-sets.

I need to display custom tooltips, that are always visible, inside the bars, for each dataset. (See the image below.) I am having difficulty understanding how to implement custom-tooltips that are always visible.

This is what my current js code for the chart is:

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: labelArray,
        datasets: [{
            label: 'Green %',
            data: greenData,
            backgroundColor: 'rgb(0,166,149)',
            borderColor: 'rgb(0,166,149)',
            borderWidth: 1,
        },
        {
            label: 'Orange %',
            data: orangeData,
            backgroundColor: 'rgb(229,117,31)',
            borderColor: 'rgb(229,117,31)',
            borderWidth: 1,
        },
        {
            label: 'Grey %',
            data: greyData,
            backgroundColor: 'rgb(179,179,179)',
            borderColor: 'rgb(179,179,179)',
            borderWidth: 1,
        }]
    },
    options: {
        scales: {
            xAxes: [{
                stacked: true,
            }],
            yAxes: [{
                stacked: true
            }]
        }
    }
});

Please, if someone can help me to get some custom tooltips always visible, in the right place in relation to the bars, then I can get it styled up appropriately.

In my MVC project, I am using chart.js (v2.8.0) to build a stacked bar-chart, with 3 data-sets.

I need to display custom tooltips, that are always visible, inside the bars, for each dataset. (See the image below.) I am having difficulty understanding how to implement custom-tooltips that are always visible.

This is what my current js code for the chart is:

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: labelArray,
        datasets: [{
            label: 'Green %',
            data: greenData,
            backgroundColor: 'rgb(0,166,149)',
            borderColor: 'rgb(0,166,149)',
            borderWidth: 1,
        },
        {
            label: 'Orange %',
            data: orangeData,
            backgroundColor: 'rgb(229,117,31)',
            borderColor: 'rgb(229,117,31)',
            borderWidth: 1,
        },
        {
            label: 'Grey %',
            data: greyData,
            backgroundColor: 'rgb(179,179,179)',
            borderColor: 'rgb(179,179,179)',
            borderWidth: 1,
        }]
    },
    options: {
        scales: {
            xAxes: [{
                stacked: true,
            }],
            yAxes: [{
                stacked: true
            }]
        }
    }
});

Please, if someone can help me to get some custom tooltips always visible, in the right place in relation to the bars, then I can get it styled up appropriately.

Share Improve this question asked Nov 5, 2019 at 15:53 Mark SeymourMark Seymour 1414 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Assuming you're referring to the percentage values in the bars, those are called labels, not tooltips (which are what appear when you mouse over an element).

Label functionality is not natively available in Chart.js but can be added via the datalabels plugin. You'll need to include the plugin via a script tag. (After the script tag where you load Chart.js!)

The bar chart sample is already quite close to your desired result, but I've merged it with your code in the snippet below to help you along.

You can reference the plugin formatting documentation to tailor the final result.

var labelArray = ["James", "Mark", "Simon"],
  greenData = [55, 82, 32],
  orangeData = [27, 10, 53],
  greyData = [18, 8, 15];

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: labelArray,
    datasets: [{
        label: 'Green %',
        data: greenData,
        backgroundColor: 'rgb(0,166,149)',
        borderColor: 'rgb(0,166,149)',
        borderWidth: 1
      },
      {
        label: 'Orange %',
        data: orangeData,
        backgroundColor: 'rgb(229,117,31)',
        borderColor: 'rgb(229,117,31)',
        borderWidth: 1,
      },
      {
        label: 'Grey %',
        data: greyData,
        backgroundColor: 'rgb(179,179,179)',
        borderColor: 'rgb(179,179,179)',
        borderWidth: 1,
      }
    ]
  },
  options: {
    scales: {
      xAxes: [{
        stacked: true,
      }],
      yAxes: [{
        stacked: true
      }]
    },
    plugins: {
      datalabels: {
        color: 'white',
        font: {
          weight: 'bold'
        },
        formatter: function(value, context) {
          return Math.round(value) + '%';
        }
      }
    }
  }
});
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/Chart.min.js"></script>
<script src="https://cdn.jsdelivr/npm/[email protected]"></script>
<canvas id="myChart"></canvas>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744697938a4588606.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信