最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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;
          }
        }
      });
    });
  },
发布评论

评论列表(0)

  1. 暂无评论