google charts

本文介绍了如何在项目中使用Google Charts,相比于ECharts,Google Charts的引入更为轻量。通过实例展示了如何创建一个饼图,并详细讲解了数据配置和选项设置。

google charts

使用的时候,引入的包比echarts小

//在html中引入方式
 <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

//在项目中引用
npm install chart.js

google charts

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <title>Document</title>
    <style>
      body {
        padding: 16px;
      }

      canvas {
        border: 1px dotted red;
      }
    </style>
  </head>
  <body>
    <canvas id="chart" height="50vw" width="50vw"></canvas>
    <script>
      function getColor(number) {
        const colorList = [
          "#FFC125",
          "#FF6A6A",
          "#55A8FD",
          "#5FDAC7",
          "#FA7D7D",
          "#AEB3B8",
          "#92C789",
          "#6A6A6A",
          "#FFD700",
          "#FFC1C1",
          "#FF6A6A",
          "#E066FF",
        ];
        return colorList[number];
      }

      var mychart = document.getElementById("chart").getContext("2d");
      new Chart(mychart, {
        type: "doughnut",
        data: {
          labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul"],
          datasets: [
            {
              data: [65, 59, 20, 81, 56, 55, 40],
              borderWidth: 1,
              backgroundColor: [65, 59, 20, 81, 56, 55, 40].map((item, index) =>
                getColor(index % 10)
              ),
            },
          ],
        },
        options: {
          maintainAspectRatio: true,
          aspectRatio: 1.5,
          plugins: {
            legend: {
              position: "right",
              labels: {
                boxWidth: 12,
                font: {
                  boxWidth: 24,
                  size: 12,
                  pointStyle: "circle",
                  usePointStyle: true,
                },
              },
            },
            tooltip: {
              callbacks: {
                label: function (context) {
                  var label = `${context.label} ${context.formattedValue}%`;
                  return label;
                },
              },
              bodyFont: {
                size: 12,
              },
            },
          },
        },
      });
    </script>
  </body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值