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>

页面效果:

相关推荐
夏幻灵9 分钟前
HTML5里最常用的十大标签
前端·html·html5
程序员猫哥_42 分钟前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
杨超越luckly1 小时前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强
会编程的土豆20 小时前
新手前端小细节
前端·css·html·项目
周航宇JoeZhou20 小时前
JB2-7-HTML
java·前端·容器·html·h5·标签·表单
代码小库20 小时前
【课程作业必备】Web开发技术HTML静态网站模板 - 校园动漫社团主题完整源码
前端·html
云计算DevOps-韩老师20 小时前
HTML 中的行级元素(inline)、块级元素(block)、行内块元素(inline-block)
html
珹洺20 小时前
Bootstrap-HTML(二)深入探索容器,网格系统和排版
前端·css·bootstrap·html·dubbo
BillKu20 小时前
VS Code HTML CSS Support 插件详解
前端·css·html
HetFrame1 天前
一种纯前端实现 Markdown 内容即时分享的思路
html·react·链接·markdown·工具