echarts加标签和在小区域绘制top图

1.top10图

效果如下:
在这里插入图片描述

       // 基于准备好的dom,初始化echarts实例
var toptargetchart = echarts.init(document.getElementById('toptarget'));

// app.title = '坐标轴刻度与标签对齐';

    var weatherIcons = {
        first: "../../../static/img/first.png",
        second: "../../../static/img/second.png",
        third: ".../../../static/img/third.png"
    };
        targetoption = {
            title: {},
            tooltip: {
                trigger: "axis"
            },
            legend: {
                data: []
            },
            grid: {
                x: 50, //左留白
                y: 0, //上留白
                x2: 35, //右留白
                y2: 0, //下留白
            },
            calculable: true,
            dataset: {
                dimensions: ["goods_name", "num"],
                source: [
                    { goods_name: "192.168.3.12", num: 8 },
                    { goods_name: "192.168.3.10", num: 8 },
                    { goods_name: "192.167.3.12", num: 7 },
                    { goods_name: "192.198.3.12", num: 6 },
                    { goods_name: "192.168.2.12", num: 6 },
                    { goods_name: "192.168.3.11", num: 6 },
                    { goods_name: "192.168.3.15", num: 6 },
                    { goods_name: "192.168.3.19", num: 6 },
                    { goods_name: "192.168.3.13", num: 6 },
                    { goods_name: "192.168.3.14", num: 6 },
                ]
            },
            xAxis: [
                {
                    splitLine: {
                        show: false
                    },
                    type: "value",
                    axisLine: {
                        show: false,
                        lineStyle: {
                            color: "rgba(255, 255, 255, 0.3)",
                            width: 1
                        }
                    },
                    axisTick: {
                        show: false
                    },
                    axisLabel: {
                        show: true,
                        margin: 100,
                        textStyle: {
                            align: "left",
                            color: "#00FFFF", //更改坐标轴文字颜色
                            fontSize: 10 //更改坐标轴文字大小
                        }
                    }
                }
            ],
            yAxis: [
                {
                    type: "category",
                    min:-1,
                    max:10,
                    inverse: true,
                    axisLine: {
                        lineStyle: {
                            color: "rgba(255, 255, 255, 0.2)",
                            width: 1
                        }
                    },

                    axisLabel: {
                        show: true,
                        textStyle: {
                            color: function(value, index) {
                                return index <= 3 ? "#FDAD00" : "#fff";
                            },
                            //更改坐标轴文字颜色
                            fontSize: 5 //更改坐标轴文字大小
                        },
                        formatter: function(value, index) {
                            if (index == 1) {
                                return "{first|}" + "  1 " + value;
                            } else if (index == 2) {
                                return "{second|}" + "  2 " + value;
                            } else if (index == 3) {
                                return "{third|}" + "  3 " + value;
                            } else {
                                return "      " + (index ) + " " + value;
                            }
                        },
                        rich: {
                            value: {
                                color: "#fff",
                                fontSize: 12
                            },
                            first: {
                                color: "FDAD00",
                                backgroundColor: {
                                    image: weatherIcons.first
                                }
                            },
                            second: {
                                color: "FDAD00",
                                backgroundColor: {
                                    image: weatherIcons.second
                                }
                            },
                            third: {
                                color: "FDAD00",
                                backgroundColor: {
                                    image: weatherIcons.third
                                }
                            }
                        }
                    }
                }
            ],
            series: [
                {
                    // name:'2011年',
                    type: "bar",
                    //barWidth : 5,
                    // data: [200, 210, 220, 230, 240, 250],
                    itemStyle: {
                        normal: {
                            //每根柱子颜色设置
                            color: function(params) {
                                var _this = this;
                                let colorList = [
                                    ["#E56E6E", "#EFA4A4"],
                                    ["#FEB763", "#F9CF9E"],
                                    ["#00C0DD", "#00C0DD"],
                                    ["#23C83E", "#9BF194"],
                                    ["#D4FC78", "#99E5A2"],
                                    ["#1AA291", "#1AA291"],
                                    ["#C1E3FF", "#ABC7FF"],
                                    ["#4186EC", "#3AB3FB"],
                                    ["#FDEB82", "#F78FAD"],
                                    ["#A16BFE", "#BC3D2F"],
                                ];
                                var index = params.dataIndex;
                                return {
                                    colorStops: [
                                        {
                                            offset: 0.8, //颜色的开始位置
                                            color: colorList[index][0] // 0% 处的颜色
                                        },
                                        {
                                            offset: 0, //颜色的结束位置
                                            color: colorList[index][1] // 100% 处的颜色
                                        }
                                    ]
                                };
                            }
                        }
                    }, //柱状图上显示数据
                    label: {
                        show: "true",
                        position: [180, "20%"],
                        color: "#FFF",
                        fontSize: "12"
                    }
                }
            ]
        };



        // 使用刚指定的配置项和数据显示图表。
toptargetchart.setOption(targetoption);

说明:1.barwidth可以调节柱体宽度,如果不加则自动调节,如果加了过后就按照值显示
2.开始绘制的时候,左边1到10不能完全显示,加了min:-1, max:10,就可以全部显示了。
2.圆形加标签(标签包含后面名字对应的数字)
在这里插入图片描述
说明:通常的legend只包含名字,没有名字对应的数字。

legend: {
            orient: 'vertical',
            x: '0',
            y: '0',
            // 设置图例形状
            icon: 'circle',
            itemWidth: 10,  // 设置宽度
            itemHeight: 10, // 设置高度
            textStyle: {
                color: "#fff",
                fontSize: 10,
            },
            formatter: function (params) {
                var legendIndex = 0;
                data.forEach(function (v, i) {
                    if (v.name == params) {
                        legendIndex = i;
                    }
                });
                return params + " " + data[legendIndex].value;
            }
        },

注意⚠️:data一定要在最前面调用,在legend之前,不然没有用。

其中如果后端传过来的数据小数点后面有很多很多位数,只需要保留2位,可以修改为parseFloat(data[legendIndex].value).toFixed(2)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ECharts3 是一款非常强大的 JavaScript 可视化库,可以用于绘制各种类型的图表。其中,Graph 图表是 ECharts3 中的一种类型,可以用于绘制拓扑图。 Java 与 ECharts3 的集成可以通过 java-echarts3 库来实现,该库提供了 Java 与 ECharts3 的交互接口。下面是一个简单的示例代码,演示了如何使用 java-echarts3 绘制一个简单的拓扑图: ```java import com.timeyang.jkes.core.support.ECharts3; import com.timeyang.jkes.core.support.ECharts3.Graph; import com.timeyang.jkes.core.support.ECharts3.Graph.CategoryData; import com.timeyang.jkes.core.support.ECharts3.Graph.GraphData; import com.timeyang.jkes.core.support.ECharts3.Graph.Link; import com.timeyang.jkes.core.support.ECharts3.Graph.Node; import com.timeyang.jkes.core.support.ECharts3.Graph.Option; import java.util.ArrayList; import java.util.List; public class TopologyChart { public static void main(String[] args) { // 创建一个 ECharts3 实例 ECharts3 echarts = new ECharts3(); // 创建一个 Graph 实例 Graph graph = new Graph(); // 设置 Graph 的标题和布局方式 graph.setTitle("Topology Chart"); graph.setLayout("force"); // 创建节点数据 List<Node> nodes = new ArrayList<>(); nodes.add(new Node("node1", "Node 1", "circle", 50)); nodes.add(new Node("node2", "Node 2", "circle", 50)); nodes.add(new Node("node3", "Node 3", "circle", 50)); nodes.add(new Node("node4", "Node 4", "circle", 50)); nodes.add(new Node("node5", "Node 5", "circle", 50)); // 创建分类数据 List<CategoryData> categories = new ArrayList<>(); categories.add(new CategoryData("Category 1")); categories.add(new CategoryData("Category 2")); // 创建边数据 List<Link> links = new ArrayList<>(); links.add(new Link("node1", "node2", 1)); links.add(new Link("node2", "node3", 1)); links.add(new Link("node3", "node4", 1)); links.add(new Link("node4", "node5", 1)); links.add(new Link("node5", "node1", 1)); // 创建 Graph 数据 GraphData data = new GraphData(nodes, categories, links); // 创建 Graph 配置项 Option option = new Option(); option.setSeries(data); // 将 Graph 配置项设置给 ECharts3 实例 echarts.setOption(option); // 输出 ECharts3 实例的 HTML 代码 System.out.println(echarts.toHtml()); } } ``` 运行上述代码,将输出一个包含拓扑图的 HTML 页面。你可以使用任何 Web 浏览器打开该页面,查看绘制的拓扑图。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值