Chart.js-饼图计算传奇后单击后可见值的总和

发布于 2025-02-05 18:56:50 字数 975 浏览 2 评论 0原文

我使用Chart.js显示饼图及其传说。 当用户单击传奇标签以禁用\隐藏时,我想重新计算饼图中剩余的所有可见 exction的总和。

我已经覆盖了默认的传奇点击处理程序,并且正在尝试这样做 - 我不确定这是否是这样做的地方,但是这样做似乎是合乎逻辑的。

const defaultLegendClickHandler = Chart.defaults.plugins.legend.onClick;
const pieDoughnutLegendClickHandler = Chart.controllers.doughnut.overrides.plugins.legend.onClick;

const newLegendClickHandler = function (e, legendItem, legend) {
    const index = legendItem.datasetIndex;
    const type = legend.chart.config.type;

    if (type === 'pie' || type === 'doughnut') {
        pieDoughnutLegendClickHandler(e, legendItem, legend)
    } else {
        defaultLegendClickHandler(e, legendItem, legend);
    }

    let ci = legend.chart;
    //Iterate through visible values of a data set and sum them...????
               
};

Im using Chart.js to display a pie chart and its legend.
When a user clicks on a legend label to disable\hide it I want to recalculate the sum total for all visible sections remaining in the pie chart.

I have overridden the default Legend Click Handler and am trying to do this in there - Im not sure if this is the place to do it but it seems logical to do so.

const defaultLegendClickHandler = Chart.defaults.plugins.legend.onClick;
const pieDoughnutLegendClickHandler = Chart.controllers.doughnut.overrides.plugins.legend.onClick;

const newLegendClickHandler = function (e, legendItem, legend) {
    const index = legendItem.datasetIndex;
    const type = legend.chart.config.type;

    if (type === 'pie' || type === 'doughnut') {
        pieDoughnutLegendClickHandler(e, legendItem, legend)
    } else {
        defaultLegendClickHandler(e, legendItem, legend);
    }

    let ci = legend.chart;
    //Iterate through visible values of a data set and sum them...????
               
};

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

贩梦商人 2025-02-12 18:56:50

您可以将数据交叉,检查是否隐藏,如果不将其添加到总计:

const defaultLegendClickHandler = Chart.defaults.plugins.legend.onClick;
const pieDoughnutLegendClickHandler = Chart.controllers.doughnut.overrides.plugins.legend.onClick;

const newLegendClickHandler = function(e, legendItem, legend) {
  const index = legendItem.datasetIndex;
  const type = legend.chart.config.type;

  if (type === 'pie' || type === 'doughnut') {
    pieDoughnutLegendClickHandler(e, legendItem, legend)
  } else {
    defaultLegendClickHandler(e, legendItem, legend);
  }

  let ci = legend.chart;
  const sum = ci.data.datasets.reduce((acc, curr) => {
    curr.data.forEach((d, i) => {
      if (ci.getDataVisibility(i)) {
        acc += d;
      }
    });

    return acc
  }, 0)
  console.log(sum)
  // Do whatever you want with the total.
};

const options = {
  type: 'doughnut',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        backgroundColor: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        backgroundColor: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
      }
    ]
  },
  options: {
    plugins: {
      legend: {
        onClick: newLegendClickHandler
      }
    }
  }
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.js"></script>
</body>

You can interate throguh the data, check if it is hidden and if not add it to the total:

const defaultLegendClickHandler = Chart.defaults.plugins.legend.onClick;
const pieDoughnutLegendClickHandler = Chart.controllers.doughnut.overrides.plugins.legend.onClick;

const newLegendClickHandler = function(e, legendItem, legend) {
  const index = legendItem.datasetIndex;
  const type = legend.chart.config.type;

  if (type === 'pie' || type === 'doughnut') {
    pieDoughnutLegendClickHandler(e, legendItem, legend)
  } else {
    defaultLegendClickHandler(e, legendItem, legend);
  }

  let ci = legend.chart;
  const sum = ci.data.datasets.reduce((acc, curr) => {
    curr.data.forEach((d, i) => {
      if (ci.getDataVisibility(i)) {
        acc += d;
      }
    });

    return acc
  }, 0)
  console.log(sum)
  // Do whatever you want with the total.
};

const options = {
  type: 'doughnut',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        backgroundColor: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        backgroundColor: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"]
      }
    ]
  },
  options: {
    plugins: {
      legend: {
        onClick: newLegendClickHandler
      }
    }
  }
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.js"></script>
</body>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文