antv-G2实战 条形图

官网

https://g2.antv.vision/zh/examples/case/bar#bar2

HTML 代码

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>测试</title>
    <style>
      body {
        width: 100vw;
        height: 100vh;
        background-color: #0b2988;
        padding: 0;
        margin: 0;
        box-sizing: border-box;
      }
    </style>
  </head>

  <body>
    <div id="container" />
    <script src="https://gw.alipayobjects.com/os/lib/antv/g2/4.1.17/dist/g2.min.js"></script>
    <script src="https://gw.alipayobjects.com/os/antv/pkg/_antv.data-set-0.11.1/dist/data-set.js"></script>
    <script>
      const data = [
        { cat: "双师认证", type: "双师型", value: 10},
        { cat: "双师认证", type: "双师素质型", value: 20},
        { cat: "工作岗位", type: "专业技术岗位", value: 30},
        { cat: "工作岗位", type: "工勤技能岗位", value: 40},
        { cat: "工作岗位", type: "管理岗位", value: 50},
        { cat: "岗位类别", type: "专任教师", value: 60},
        { cat: "岗位类别", type: "实验员", value: 70},
        { cat: "岗位类别", type: "未录入", value: 8 },
        { cat: "岗位类别", type: "行政管理后勤", value: 90},
        { cat: "岗位类别", type: "辅导员", value: 100},
        { cat: "人员类别", type: "合同制人员", value: 110},
        { cat: "人员类别", type: "在编人员", value: 120},
        { cat: "人员类别", type: "外聘教师", value: 130},
      ];
      const chart = new window.G2.Chart({
        container: "container",
        autoFit: false,
        height: 700,
        width: 550,
        padding: [20, 0, 50, 150],
      });
      chart.data(data);
      chart.axis("type", {
        tickLine: null,
        line: null,
        label: {
          style: {
            fill: "#fff",
          },
        },
      });
      chart.axis("value", {
        label: null,
        // title: {
        //   offset: 30,
        //   style: {
        //     fontWeight: 300,
        //     fill: "#ffffff",
        //   },
        // },
        grid: null,
      });
      chart.legend(false);
      chart.coordinate("rect").transpose();
      chart
        .interval()
        .position("type*value")
        .color("cat", [
          "#face1d",
          "rgba(73,146,255,1)",
          "rgba(5,192,145,1)",
          "rgba(141,72,227,1)",
        ])
        .size(26)
        .label("value", {
          style: {
            fill: "#ffffff",
          },
          offset: 10,
          content: (originData) => {
            return (originData.value + "").replace(
              /(\d)(?=(?:\d{3})+$)/g,
              "$1,"
            );
          },
        });

      chart.annotation().text({
        top: true,
        position: ["外聘教师", "min"],
        content: "人员类别",
        style: {
          fill: "#ffffff",
          fontSize: 16,
          fontWeight: "300",
          textAlign: "left",
        },
        offsetX: -110,
        offsetY: 80,
        rotate: Math.PI * -0.5,
      });
      chart.annotation().text({
        top: true,
        position: ["实验员", "min"],
        content: "岗位类别",
        style: {
          fill: "#ffffff",
          fontSize: 16,
          fontWeight: "300",
          textAlign: "left",
        },
        offsetX: -110,
        rotate: Math.PI * -0.5,
      });
      chart.annotation().text({
        top: true,
        position: ["工勤技能岗位", "min"],
        content: "工作岗位",
        style: {
          fill: "#ffffff",
          fontSize: 16,
          fontWeight: "300",
          textAlign: "left",
        },
        offsetX: -110,
        offsetY: 35,
        rotate: Math.PI * -0.5,
      });
      chart.annotation().text({
        top: true,
        position: ["双师型", "min"],
        content: "双师认证",
        style: {
          fill: "#ffffff",
          fontSize: 16,
          fontWeight: "300",
          textAlign: "left",
        },
        offsetX: -110,
        offsetY: 15,
        rotate: Math.PI * -0.5,
      });
      chart.annotation().line({
        start: ["-30%", "23%"],
        end: ["100%", "23%"],
        style: {
          stroke: "#ffffff",
          lineDash: [2, 2],
        },
      });
      chart.annotation().line({
        start: ["-30%", "62%"],
        end: ["100%", "62%"],
        style: {
          stroke: "#ffffff",
          lineDash: [2, 2],
        },
      });
      chart.annotation().line({
        start: ["-30%", "85%"],
        end: ["100%", "85%"],
        style: {
          stroke: "#ffffff",
          lineDash: [2, 2],
        },
      });
      chart.interaction("element-active");

      chart.render();
    </script>
  </body>
</html>

react 组件

import React, {useContext, useEffect, useState} from 'react';
export default function G2Charts(props) {
  const {
    style,
    componentIndex,
    isCallBack,
    callBackFunction,
  } = props;
  const [ready, setReady] = useState(false);
  useEffect(() => {
    if (!window.G2 || !window.G2.Charts) {
      let sc = document.createElement('script');
      sc.src = `/BigDataVisualization/g2/g2.min.js`;
      document.querySelector('body').appendChild(sc);
      sc.onload = function () {
        let sc2 = document.createElement('script');
        sc2.src = `/BigDataVisualization/g2/data-set.js`;
        document.querySelector('body').appendChild(sc2);
        sc2.onload = function () {
          console.log(window.G2);
          setReady(true);
        }
      };
    } else {
      setReady(true);
    }
  }, []);

  useEffect(() => {
    if (ready) {
        const data = [
        { cat: "双师认证", type: "双师型", value: 1},
        { cat: "双师认证", type: "双师素质型", value: 2},
        { cat: "工作岗位", type: "专业技术岗位", value: 3},
        { cat: "工作岗位", type: "工勤技能岗位", value: 4},
        { cat: "工作岗位", type: "管理岗位", value: 5},
        { cat: "岗位类别", type: "专任教师", value: 6},
        { cat: "岗位类别", type: "实验员", value: 7 },
        { cat: "岗位类别", type: "未录入", value: 8},
        { cat: "岗位类别", type: "行政管理后勤", value: 9},
        { cat: "岗位类别", type: "辅导员", value: 10},
        { cat: "人员类别", type: "合同制人员", value: 11},
        { cat: "人员类别", type: "在编人员", value: 12},
        { cat: "人员类别", type: "外聘教师", value: 13},
      ];
      const chart = new window.G2.Chart({
        container: "container",
        autoFit: false,
        height: 700,
        width: 550,
        padding: [20, 0, 50, 150],
      });
      chart.data(data);
      chart.axis("type", {
        tickLine: null,
        line: null,
        label: {
          style: {
            fill: "#fff",
          },
        },
      });
      chart.axis("value", {
        label: null,
        // title: {
        //   offset: 30,
        //   style: {
        //     fontWeight: 300,
        //     fill: "#ffffff",
        //   },
        // },
        grid: null,
      });
      chart.legend(false);
      chart.coordinate("rect").transpose();
      chart
        .interval()
        .position("type*value")
        .color("cat", [
          "#face1d",
          "rgba(73,146,255,1)",
          "rgba(5,192,145,1)",
          "rgba(141,72,227,1)",
        ])
        .size(26)
        .label("value", {
          style: {
            fill: "#ffffff",
          },
          offset: 10,
          content: (originData) => {
            return (originData.value + "").replace(
              /(\d)(?=(?:\d{3})+$)/g,
              "$1,"
            );
          },
        });

      chart.annotation().text({
        top: true,
        position: ["外聘教师", "min"],
        content: "人员类别",
        style: {
          fill: "#ffffff",
          fontSize: 16,
          fontWeight: "300",
          textAlign: "left",
        },
        offsetX: -110,
        offsetY: 80,
        rotate: Math.PI * -0.5,
      });
      chart.annotation().text({
        top: true,
        position: ["实验员", "min"],
        content: "岗位类别",
        style: {
          fill: "#ffffff",
          fontSize: 16,
          fontWeight: "300",
          textAlign: "left",
        },
        offsetX: -110,
        rotate: Math.PI * -0.5,
      });
      chart.annotation().text({
        top: true,
        position: ["工勤技能岗位", "min"],
        content: "工作岗位",
        style: {
          fill: "#ffffff",
          fontSize: 16,
          fontWeight: "300",
          textAlign: "left",
        },
        offsetX: -110,
        offsetY: 35,
        rotate: Math.PI * -0.5,
      });
      chart.annotation().text({
        top: true,
        position: ["双师型", "min"],
        content: "双师认证",
        style: {
          fill: "#ffffff",
          fontSize: 16,
          fontWeight: "300",
          textAlign: "left",
        },
        offsetX: -110,
        offsetY: 15,
        rotate: Math.PI * -0.5,
      });
      chart.annotation().line({
        start: ["-30%", "23%"],
        end: ["100%", "23%"],
        style: {
          stroke: "#ffffff",
          lineDash: [2, 2],
        },
      });
      chart.annotation().line({
        start: ["-30%", "62%"],
        end: ["100%", "62%"],
        style: {
          stroke: "#ffffff",
          lineDash: [2, 2],
        },
      });
      chart.annotation().line({
        start: ["-30%", "85%"],
        end: ["100%", "85%"],
        style: {
          stroke: "#ffffff",
          lineDash: [2, 2],
        },
      });
      chart.interaction("element-active");

      chart.render();
    }
  }, [ready]);

  return (<div id={componentIndex} style={style}>

  </div>);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值