官网
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>);
}