在Chart.js中将总计添加到堆叠条形图的顶部

ubby3x7f  于 11个月前  发布在  Chart.js
关注(0)|答案(2)|浏览(161)


的数据
正如你可以看到在图像中,我已经被要求添加一些总数到顶部的堆叠条形图.我似乎不能弄清楚如何实现这一点,虽然.我的图到目前为止看起来像它需要,但没有在顶部的总数.
这就是我所拥有的。

任何帮助将不胜感激。

const dailySalesConfig = {
    type: 'bar',
    data: dailySalesData,
    options: {
        plugins: {
            datalabels: {
                color: 'white',
                formatter: function(value, context) {
                    return `$${value.replace(/\B(?=(\d{3})+(?!\d))/g, ",")}M`;
                }
            },
            legend: {
                position: 'bottom' 
            }
        },
        responsive: false,
        scales: {
            x: {
                stacked: true,
                grid: {
                    display: false
                }
            },
            y: {
                stacked: true,
                ticks: {
                    display:false
                },
                grid: {
                    display:false
                }
            }
        }
    }
};

字符串
这是我的病历选择。
JS Fiddle link to demo/example

yrefmtwq

yrefmtwq1#

你只需要使用formatter函数来实现你的目的。我已经创建了一个totalizer插件,并在formatter中使用它来计算和显示总数。此外,注意我添加了一些填充以使数据可见。你可以根据需要调整它们:

const dailySalesConfig = {
   type: 'bar',
   data: dailySalesData,
   options: {
   layout: {
        padding: {
            left: 50,
            right: 50,
            top: 50,
            bottom: 50
        }
    },
       plugins: {
           datalabels: {
               color: 'black',
               display: true,
               anchor: 'end',
               align: 'top',
               formatter: (value, ctx) => {
                  const total = ctx.chart.$totalizer.totals[ctx.dataIndex]
                  return `${total.toLocaleString('en-US', {
                    style: 'currency',
                    currency: 'USD'
                  })}`
               },
               display: function(ctx) {
                  return ctx.datasetIndex === ctx.chart.$totalizer.utmost
               }
           },
//rest of code...

字符串
该插件看起来像:

const totalizer = {
 id: 'totalizer',
 beforeUpdate: chart => {
   let totals = {}
   let utmost = 0
   chart.data.datasets.forEach((dataset, datasetIndex) => {
     if (chart.isDatasetVisible(datasetIndex)) {
       utmost = datasetIndex
       dataset.data.forEach((value, index) => {
         totals[index] = (totals[index] || 0) + value
       })
     }
   })
   chart.$totalizer = {
     totals: totals,
     utmost: utmost
   }
 }
}

2sbarzqh

2sbarzqh2#

我无法得到任何解决方案的工作,所以我只是添加了一个表,没有样式,并定位它绝对在页面上模仿数据是在每个酒吧的顶部。不是我想要的,但它的工作。
编辑:虽然上面的工作正常,我能够得到格式化程序设置正确格式化数据。它需要一个额外的数据集,您将数据设置为0,背景颜色为rgb,这是完全透明的。一旦您添加了额外的数据集,您就可以使用格式化程序添加数据。

formatter: function(value, index, array) {
    //console.log(index);
    if (index.datasetIndex < 4) {
        const percent = Math.round(value * 100 / totals[index.dataIndex]);
        return `${percent}%`;
    } else {
        const a = (totals[index.dataIndex] / 1000000).toFixed(2);
        console.log(a);
        return `$${a}M`;
    }
},

字符串
示例数据集:

, {
     label: '',
     backgroundColor: 'rgb(255, 99, 132, 0)',
     data: ['0', '0', '0', '0'],
     stack: 'Stack 0'
   }

相关问题