Antv/G2 自定义折线图线条样式及tootip提示信息样式

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>自定义折线图线条样式及tootip提示信息样式</title>    
    <style>
        #chartcontainer {
            margin: 50px auto;
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="chartcontainer"></div>
    <script>
        /*Fixing iframe window.innerHeight 0 issue in Safari*/ document.body
        .clientHeight
    </script>
    <script src="https://gw.alipayobjects.com/os/lib/antv/g2/4.2.8/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 = [
        { city: 'London', month: 'Jan', trate: 18.9 },
        { city: 'London', month: 'Feb', trate: 28.8 },
        { city: 'London', month: 'Mar', trate: 39.3 },
        { city: 'London', month: 'Apr', trate: 81.4 },
        { city: 'London', month: 'May', trate: 47.1 },
        { city: 'London', month: 'Jun', trate: 20.3 },
        { city: 'London', month: 'Jul', trate: 24.7 },
        { city: 'London', month: 'Aug', trate: 35.6 },
        { city: 'Berlin', month: 'Jan', trate: 12.4 },
        { city: 'Berlin', month: 'Feb', trate: 23.2 },
        { city: 'Berlin', month: 'Mar', trate: 34.5 },
        { city: 'Berlin', month: 'Apr', trate: 99.7 },
        { city: 'Berlin', month: 'May', trate: 52.6 },
        { city: 'Berlin', month: 'Jun', trate: 35.5 },
        { city: 'Berlin', month: 'Jul', trate: 37.4 },
        { city: 'Berlin', month: 'Aug', trate: 42.4 },
        ]   
        // 设置图表  
        const chart = new G2.Chart({
              container: 'chartcontainer',
            autoFit: true,//图表是否自适应容器宽高,默认为 false
              height: 500,
            padding: [100, 100, 50, 100],//图表内边距,依次为:上,右,下,左
            pixelRatio:window.devicePixelRatio,//设置设备像素比,默认取浏览器的值 window.devicePixelRatio
            renderer:"canvas",//指定渲染引擎,默认使用 canvas。可选:'canvas' | 'svg'
            // theme:"dark",//配置主题,目前 g2 默认有 dark 主题模式,如需要自定义配置,可以先通过 registerTheme 注册主题,再设置主题 key。
            visible:true,//chart 是否可见,默认为 true,设置为 false 则会隐藏。
          });
          chart.data(data);
        // 设置提示框信息样式
          chart.tooltip({
              showCrosshairs: true,
            shared: true,
            crosshairs:{
                line:{
                    style:{
                            stroke:"#AFEEEE",//辅助线颜色
                            lineWidth:1,//辅助线粗细
                        },
                    }
                },
                domStyles:{
                    'g2-tooltip':{
                        background:"rgba(00, 00, 00,0.2)",//背景RGBA形式的值
                        color:"#7B68EE",//文字颜色
                        boxShadow:"0px 0px 5px #000000",//阴影大小 阴影颜色 
                    },
                }
              });
        // 设置Y轴
          chart.axis('trate', {
            // 设置虚线样式
            grid:{
                line:{
                    type:"line",
                    style:{
                                // fill:'#ff0000',
                                stroke:"#000",//网格线颜色
                                opacity:0.3,//网格线透明度
                                lineDash:[1,3],//虚线
                            }
                        }
                    },
                      label: {
                        style:{
                            fill:"#4169E1",///Y轴文字颜色
                            fontFamily: "Microsoft YaHei",///Y轴文字字体
                            fontWeight: 400,///Y轴文字粗细
                            fontSize: 12,///Y轴文字大小
                        },
              formatter: function formatter(val) { // 格式化
                  return val + '°C';
              }
          }
      });
        // 自定义图例
        chart.legend('city',{
            clickable: true,
            position: 'top', // 设置图例的显示位置      
        })
        // 设置图表折线相关属性【折线样式】
        chart.line()
        .color('city', ["#FFA07A","#90EE90","#2611ff","#26aa99"]).shape('smooth')//曲率
        .position('month*trate') // X轴 * Y轴
        .style({ 
            lineWidth: 4 // 折线宽度
        })
        .label("trate", { // 折线上的值标签
            content: (originData) => {
                        return originData["trate"]+"°C";//设置值标签最终显示的内容
                    },
                    style: {
                        fill: "#A0522D",
                        fontFamily: "Microsoft YaHei",
                        fontWeight: 400,
                        fontSize: 16,
                        // fill: "#ffffff",
                    },
                    position:"top",//显示位置
                })       
        chart.render();
    </script>
</body>
</html>

页面效果:

相关推荐
小J听不清1 小时前
CSS 外边距(margin)全解析:取值规则 + 实战用法
前端·javascript·css·html·css3
小J听不清3 小时前
CSS 边框(border)全解析:样式 / 宽度 / 颜色 / 方向取值
前端·javascript·css·html·css3
赵锦川6 小时前
大屏比例缩放
前端·javascript·html
我命由我1234510 小时前
Element Plus 2.2.27 的单选框 Radio 组件,选中一个选项后,全部选项都变为选中状态
开发语言·前端·javascript·html·ecmascript·html5·js
dzj88810 小时前
云朵字生成器-html
前端·css·html·云朵字
小J听不清11 小时前
CSS 内边距(padding)全解析:取值规则 + 表格实战
前端·javascript·css·html·css3
桐溪漂流11 小时前
Uni-app H5 环境下 ResizeObserver 监听 mp-html 动态高度
前端·uni-app·html
孙12~13 小时前
前端vue3+vite,后端SpringBoot+MySQL
前端·html·学习方法
我命由我1234513 小时前
Element Plus 问题:选择框表单校验没有触发
开发语言·前端·javascript·html·ecmascript·html5·js
小同志0013 小时前
HTML 基础
前端·javascript·html