一.echart绘制
1.安装echarts
npm i echarts
注意:我使用的是是5.4.3版本,版本不一致部分用法可能会有出入,关闭点击节点的伸缩行为以及默认节点圆点样式去除等用法看代码注释。节点之间的间距不用调,直接在代码增加子节点就行。
代码如下:
import React, { useEffect } from "react";
import * as echarts from "echarts";
export default function Sunset() {
useEffect(() => {
var myChart = echarts.init(document.getElementById("main"));
let option = {
series: [
{
name: "树图",
type: "tree",
// layout: "orthogonal",如果要调整树状显示方向此处layout和orient搭配使用可达到效果
orient: "vertical", // vertical //树状方向修改
expandAndCollapse: false, //关闭点击树节点的伸缩行为
rootLocation: { x: 100, y: "50%" }, // 根节点位置 {x: 'center',y: 10}
edgeShape: "polyline", //使用多段折线连接节点
nodePadding: 20,
symbol: "circle",
width: "1000px", //调整树状距离宽度
symbolSize: 40,
edgeStyle: {
type: "solid", // 直线类型
curveness: 0, // 弯曲程度,设置为0表示直线
},
itemStyle: {
normal: {
label: {
show: true,
position: "inside",
// distance: 30, distance和position: "inside",搭配使用可调线段据文字距离
textStyle: {
color: "#cc9999",
fontSize: 15,
fontWeight: "bolder",
},
},
lineStyle: {
color: "#000",
width: 1,
type: "broken", // 'curve'|'broken'|'solid'|'dotted'|'dashed'
},
},
emphasis: {
label: {
show: true,
},
},
},
data: [
{
name: "手机",
value: 6,
symbolSize: [90, 150],
symbol:
"image://http://www.iconpng.com/png/ecommerce-business/iphone.png",
itemStyle: {
normal: {
label: {
show: false,
},
},
},
children: [
{
name: "小米",
value: 4,
symbol:
"image://data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEABsbGxscGx4hIR4qLSgtKj04MzM4PV1CR0JHQl2NWGdYWGdYjX2Xe3N7l33gsJycsOD/2c7Z//8BGxsbGxwbHiEhHiotKC0qPTgzMzg9XUJHQkdCXY1YZ1hYZ1iNfZd7c3uXfeCwnJyw4P/ZztnCABEIAO0BBwMBIgACEQEDEQH/xAAaAAACAwEBAAAAAAAAAAAAAAAABAIDBQEG/9oACAEBAAA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PH8Z69zs+O9e54/jvXueP4717nj+O9e5o/jPTcjwX0vkI5kyNkTAxjQ1o1Af9k//EACYRAQABBAIBBAEFAAAAAAAAAAECAAMRIBAyMRIhMDNxE0BBQoH/2gAIAQIBAT8A2xrG3k802plInk+N0h1KKuXIQMyqd9lLMQCouQflh1KuXS3FWpzlcllqNub/AFaiekD5Y9SmP61xz1KjCEI4IhRFfBVwxNPgxToOIf5Vq2Rtxr0D7tKQKuOZydymnR6P4q30j+KavNT7O5w6HWrUs24VnJUsZzV33uS3OHSPgq24Ur1gcXO8tzh0PBxaxIzTV3vLc4dDxxZcTT+Hi99stzh0OGTFJVCSlXfslucOhwmRKsTzHD5Kud3c4dDk9pZKk5V3OHTP7f8A/8QAKREAAQQBBAEDAwUAAAAAAAAAAQACAxEgEDAxMiESM1EEQXEiQkNhgf/aAAgBAwEBPwDbdJR4QkYUCDwd9/Y6MY55/Smx+kUSTvP7FRxmR1INbG2gi4K73X9imuEEQP7nKRxcbc4lF0h8A0FCKjbukW//AFTuJkPwPCrzakkrhfTm4Wbv8g/Kf3d+UU8WVD7TMzkexU3uE/PlWvTZtRe23M6DB3JUgtoPwgNI+jczoMDydJAWmvsuFH0bmdG4HnSRvqj/ALaio+jczo3A86R+TXypYxaYKaMzo3IeDakbfkIcZu0bm132KObtG4Uq26Cra//Z",
symbolSize: [60, 60], //调整图片宽高
children: [
{
name: "红米",
symbolSize: 20,
value: 4,
itemStyle: {
color: "none", //通过设置none树状圆点可以去掉
},
},
{
name: "小米",
value: 4,
itemStyle: {
color: "none",
},
symbolSize: 20,
},
{
name: "笔记本",
value: 2,
itemStyle: {
color: "none",
},
symbolSize: 20,
},
],
},
{
name: "苹果",
symbol:
"image://http://www.viastreaming.com/images/apple_logo2.png",
symbolSize: [60, 60],
value: 4,
children: [
{
name: "苹果12",
symbolSize: 20,
value: 4,
itemStyle: {
color: "none", //通过设置none可将默认树状圆点可以去掉
},
},
{
name: "苹果13",
value: 4,
itemStyle: {
color: "none",
},
symbolSize: 20,
},
{
name: "苹果14",
value: 2,
itemStyle: {
color: "none",
},
symbolSize: 20,
},
],
},
{
name: "华为",
symbol:
"image://http://market.huawei.com/hwgg/logo_cn/download/logo.jpg",
symbolSize: [60, 60],
value: 2,
},
{
name: "联想",
symbol:
"image://http://www.lenovo.com.cn/HomeUpload/Home001/6d94ee9a20140714.jpg",
symbolSize: [100, 40],
value: 2,
},
],
},
],
},
],
};
myChart.setOption(option);
});
return (
);
}
效果如下:
二.SVG绘制(更多图形绘制主要考虑SVG进行使用,如果使用echarts和canvas能实现也可),更多SVG使用请参考其他文档,此处不做赘述。
Document