【问题标题】:ApexCharts - Adding a number inside the "Simple Donut Chart"ApexCharts - 在“简单圆环图”中添加一个数字
【发布时间】:2023-03-22 06:10:02
【问题描述】:

我一直在尝试在 ApexCharts 的“简单圆环图”内部添加一个数字。这是它的链接 - https://apexcharts.com/javascript-chart-demos/pie-charts/simple-donut/

要使用图表,我在项目终端中运行了命令“npm install apexcharts --save”。

这是我的 HTML 文件:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <link rel="stylesheet" href="./Chart.css">

    <script src="./Chart.js"></script>

    <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>

</head>

<div id="chart"></div>
    <script>
        const chart = new ApexCharts(document.querySelector("#chart"), options);
        chart.render();
    </script>

</html>

这是我的 CSS 文件:

#chart{
    width:400px;
    height:400px;
    margin-left: auto;
    margin-right: auto;
}

这是我在我的 JavaScript 文件中使用的:

var options = {
    series: [44, 55, 41, 60],
    labels: ["Transport", "Shopping", "Energy use", "Food"],
    chart: {
        type: 'donut',
    },
    responsive: [{
        breakpoint: 480,
        options: {
            chart: {
                width: 200
            },
            legend: {
                position: 'bottom'
            }
        }
    }]
};

这是上面提到的代码的结果:

Current Chart

这是一个展示我希望如何显示数字的模型:

Example Chart

我想知道是否可以使用 ApexCharts 在当前图表中添加一个数字,如示例图表所示。

【问题讨论】:

  • 欢迎来到 Stack Overflow!请考虑发布您的实现代码,以便其他人可以有一个起点来更好地帮助您。

标签: javascript html apexcharts


【解决方案1】:

是的,这是可能的。

let options = {
  series: [44, 55, 41, 60],
  labels: ["Transport", "Shopping", "Energy use", "Food"],
  chart: {
    type: 'donut',
  },
  plotOptions: {
    pie: {
      donut: {
        labels: {
          show: true,
          total: {
            show: true,
            label: '',
            formatter: () => 'Text you want'
          }
        }
      }
    }
  }
};

const chart = new ApexCharts(document.querySelector("#chart"), options);
chart.render();
<script src="https://cdn.jsdelivr.net/npm/apexcharts@3.18.1/dist/apexcharts.min.js"></script>
<div id="chart"></div>

您可以在甜甜圈内显示以下信息:

  • 单个甜甜圈片的值(悬停在该片上)
  • 所有甜甜圈的总和
  • 自定义文本

尝试更改total 对象属性的值以更好地理解我的意思。更详细的信息是here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多