javascript - Min. height for stacked bar chartjs - Stack Overflow

I've a vertically stacked bar chart in chart.js. Since the bars are being generated based on some

I've a vertically stacked bar chart in chart.js. Since the bars are being generated based on some userinput, it's possible that I get a chart that would look something like this:

I want to enforce a minimum height for each stack to ensure that even small values are displayed more clearly.

In my current approach I'm checking if the height of a stack is below a certain value and then increase it's height with getProps(). The problem here is that even though the height is correctly set, the corresponding stack isn't being updated.

{
    id: 'ExtraHeightPlugin',
    afterDatasetsDraw: (chart: Chart <ChartType>) => {
        chart.data.labels.forEach((label, index) => {
            chart.data.datasets.forEach((dataset, datasetIndex) => {
                let currentStackHeight: number = chart.getDatasetMeta(datasetIndex).data[index].getProps(['height'])['height'];
                if (currentStackHeight> 0 && currentStackHeight< 35) {
                    currentStackHeight+= 35;
                    chart.getDatasetMeta(datasetIndex).data[index].getProps(['height'])['height'] = currentStackHeight;
                }
            });
        });
    }
}

Any help would be greatly appreciated.

I've a vertically stacked bar chart in chart.js. Since the bars are being generated based on some userinput, it's possible that I get a chart that would look something like this:

I want to enforce a minimum height for each stack to ensure that even small values are displayed more clearly.

In my current approach I'm checking if the height of a stack is below a certain value and then increase it's height with getProps(). The problem here is that even though the height is correctly set, the corresponding stack isn't being updated.

{
    id: 'ExtraHeightPlugin',
    afterDatasetsDraw: (chart: Chart <ChartType>) => {
        chart.data.labels.forEach((label, index) => {
            chart.data.datasets.forEach((dataset, datasetIndex) => {
                let currentStackHeight: number = chart.getDatasetMeta(datasetIndex).data[index].getProps(['height'])['height'];
                if (currentStackHeight> 0 && currentStackHeight< 35) {
                    currentStackHeight+= 35;
                    chart.getDatasetMeta(datasetIndex).data[index].getProps(['height'])['height'] = currentStackHeight;
                }
            });
        });
    }
}

Any help would be greatly appreciated.

Share Improve this question edited Mar 3 at 14:09 evolutionxbox 4,1326 gold badges38 silver badges57 bronze badges asked Mar 3 at 13:07 TorbiTorbi 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

getProps() only retrieves properties and doesn't set them.

 const minStackHeightPlugin = {
  id: 'minStackHeightPlugin',
  afterDatasetsDraw: (chart, args, options) => {
    const minHeight = options.minHeight || 35;
    
    chart.data.labels.forEach((label, index) => {
      chart.data.datasets.forEach((dataset, datasetIndex) => {
        const meta = chart.getDatasetMeta(datasetIndex);
        if (!meta.hidden) {
          const dataItem = meta.data[index];
          const height = dataItem.height;
          
          if (height > 0 && height < minHeight) {
            // Store original height for tooltip and other calculations
            if (!dataItem._originalHeight) {
              dataItem._originalHeight = height;
            }
            
            // Calculate Y adjustment to keep the bar anchored correctly
            const yAdjustment = (minHeight - height) / 2;
            
            // Update the visual properties
            dataItem.height = minHeight;
            dataItem.y -= yAdjustment;
          }
        }
      });
    });
  },

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

相关推荐

  • javascript - Min. height for stacked bar chartjs - Stack Overflow

    I've a vertically stacked bar chart in chart.js. Since the bars are being generated based on some

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信