查看疫情数据(如何把jquery的值赋值到echars里面)

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <style>
      div {
        height: 500px;
        width: 100%;
      }
    </style>
  </head>
  <body>
    <div id="container"></div>
    <button id="jr">今日新增数据</button>
    <button id="lj">累计新增数据</button>
    <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <script type="text/javascript">
      $("#jr").click(function () {
        // alert("Text: " + $("#test").text());
        $.ajax({
          url: "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5",
          //   url: "kuaidi.json",
          type: "get",
          //   crossDomain: true,
          // data: {
          //   apiKey: apiKey,
          //   expressNumber: expressNumber,
          //   expressType: expressType,
          // },
          dataType: "jsonp",
          //   dataType: "json",
          success: function (res) {
            // console.log(res.data);
            // 转换为json字符串
            // var jsondx = JSON.stringify(res.data);
            // console.log(jsondx);
            // console.log(typeof jsondx);
            console.log(res);
            console.log(res.data);
            console.log(typeof res);
            yqsj = res.data;
            // var obj = yqsj.parseJSON(); //由JSON字符串转换为JSON对象
            var obj = JSON.parse(yqsj);
            console.log(obj);
            var areaTree = obj.areaTree[0].children;
            console.log(obj.areaTree[0].children);
            var shengfen = [];
            var nums = [];
            areaTree.forEach(function (element) {
              console.log(element.name);
              shengfen.push(element.name);
              //   nums.push(element.total.confirm);
              nums.push(element.today.confirm);
            });
            console.log(shengfen);

            var dom = document.getElementById("container");
            var myChart = echarts.init(dom);
            var app = {};

            var option;

            option = {
              xAxis: {
                type: "category",
                data: shengfen,
                axisLabel: {
                  interval: 0,
                },
              },
              yAxis: {
                type: "value",
              },
              series: [
                {
                  data: nums,
                  type: "bar",
                  showBackground: true,
                  backgroundStyle: {
                    color: "rgba(180, 180, 180, 0.2)",
                  },
                  itemStyle: {
                    //上方显示数值
                    normal: {
                      label: {
                        show: true, //开启显示
                        position: "top", //在上方显示
                        textStyle: {
                          //数值样式
                          color: "black",
                          fontSize: 16,
                        },
                      },
                    },
                  },
                },
              ],
            };

            if (option && typeof option === "object") {
              myChart.setOption(option);
            }
          },
        });
      });
      $("#lj").click(function () {
        // alert("Text: " + $("#test").text());
        $.ajax({
          url: "https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5",
          //   url: "kuaidi.json",
          type: "get",
          //   crossDomain: true,
          // data: {
          //   apiKey: apiKey,
          //   expressNumber: expressNumber,
          //   expressType: expressType,
          // },
          dataType: "jsonp",
          //   dataType: "json",
          success: function (res) {
            // console.log(res.data);
            // 转换为json字符串
            // var jsondx = JSON.stringify(res.data);
            // console.log(jsondx);
            // console.log(typeof jsondx);
            console.log(res);
            console.log(res.data);
            console.log(typeof res);
            yqsj = res.data;
            // var obj = yqsj.parseJSON(); //由JSON字符串转换为JSON对象
            var obj = JSON.parse(yqsj);
            console.log(obj);
            var areaTree = obj.areaTree[0].children;
            console.log(obj.areaTree[0].children);
            var shengfen = [];
            var nums = [];
            areaTree.forEach(function (element) {
              console.log(element.name);
              shengfen.push(element.name);
              nums.push(element.total.confirm);
              //   nums.push(element.today.confirm);
            });
            console.log(shengfen);

            var dom = document.getElementById("container");
            var myChart = echarts.init(dom);
            var app = {};

            var option;

            option = {
              xAxis: {
                type: "category",
                data: shengfen,
                axisLabel: {
                  interval: 0,
                },
              },
              yAxis: {
                type: "value",
              },
              series: [
                {
                  data: nums,
                  type: "bar",
                  showBackground: true,
                  backgroundStyle: {
                    color: "rgba(180, 180, 180, 0.2)",
                  },
                  itemStyle: {
                    //上方显示数值
                    normal: {
                      label: {
                        show: true, //开启显示
                        position: "top", //在上方显示
                        textStyle: {
                          //数值样式
                          color: "black",
                          fontSize: 16,
                        },
                      },
                    },
                  },
                },
              ],
            };

            if (option && typeof option === "object") {
              myChart.setOption(option);
            }
          },
        });
      });
    </script>
  </body>
</html>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈开发crud师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值