antv g2设置chart图例的legend为一条线与一个圆的组合

antv g2绘制带圆点的折线的时候,图例legend一般显示的是一条曲线而不带圆点,正是因为默认结构不满足需求,所以需要自定义图例容器的模板。g2一般与react配合使用。

注意:自定义模板时必须包含各个 dom 节点的 class,样式可以自定义。

chart.legend('type', {
      position: 'top-left',
      // itemGap: 20,//图例之间的距离
      offsetY: -15,
      clickable: false,
      hoverable: false,
      useHtml: true, // 针对分类类型图例,用于开启是否使用 HTML 渲染图例
      // containerTpl: '<div class="g2-legend"><div class="g2-legend-list"></div></div>',
      itemTpl: function itemTpl(value, color, checked, index) {
        // console.log(value, color, checked, index)
        let markerDom = '';
        if (value == '楼面地价(元/㎡)') {
          //正方形方块
          markerDom = '<span class="g2-legend-list-icon" style="margin-right:5px;">' +
            '<svg width="10" height="10">' +
            '<rect width="8" height="8" style="fill:' + color + ';"/>' +
            '</svg>' +
            '</span>';
        } else {
          //线条+圆点
          markerDom = '<span class="g2-legend-list-icon" style="margin-right:5px;">' +
            '<svg width="20" height="10">' +
            '<rect rx="1" ry="1" y="4" width="20" height="2" style="fill:' + color + ';" />' +
            '<circle class="indi-svg-orange" cx="10" cy="5" r="3" stroke="' + color + '" stroke-width="2" fill="' + color + '" />' +
            '</svg>' +
            '</span>';
        }

        let valueDom = '<span class="legend-item-value">' + value + '</span>';
        return '<div class="g2-legend-list-item" data-value="' + value + '">' + markerDom + valueDom + '</div>';
      }
    })

以下是使用 antv G2Plot 绘制折柱图,并带有滑块和自定义样式的代码示例: ```javascript import { Chart } from '@antv/g2'; import { Slider } from '@antv/g2-plugin-slider'; import { getInterpolation } from '@antv/g2/lib/geometry/shape/util/get-path'; // 数据源 const data = [ { year: '1991', value: 3 }, { year: '1992', value: 4 }, { year: '1993', value: 3.5 }, { year: '1994', value: 5 }, { year: '1995', value: 4.9 }, { year: '1996', value: 6 }, { year: '1997', value: 7 }, { year: '1998', value: 9 }, { year: '1999', value: 13 } ]; // 创建图表实例 const chart = new Chart({ container: 'container', autoFit: true, height: 500, }); // 设置数据源 chart.data(data); // 创建主题视图 const view = chart.createView(); // 绘制柱状图 view.interval().position('year*value'); // 绘制折线图 view.line().position('year*value').shape('smooth'); // 设置滑块样式 const slider = new Slider({ container: 'slider-container', height: 30, start: 0, end: 1, data: data.map((d) => d.year), backgroundChart: { type: 'line', options: { data, xField: 'year', yField: 'value', smooth: true, lineStyle: { lineWidth: 1, stroke: '#B4B4B4', }, point: { shape: ({ x, y }) => { const path = getInterpolation('smooth')(data); const point = { x, y }; const nextPoint = { x: point.x + 0.001, y: point.y + 0.001 }; let angle = 0; let radius = 3; for (let i = 0; i < path.length - 1; i++) { const p1 = path[i]; const p2 = path[i + 1]; if ( (p1.x <= point.x && point.x <= p2.x) || (p2.x <= point.x && point.x <= p1.x) ) { angle = ((p2.y - p1.y) / (p2.x - p1.x)) * (point.x - p1.x) + p1.y - point.y; radius = Math.sqrt(angle ** 2 + 9); break; } } return [ ['M', point.x, point.y - radius], ['a', radius, radius, 0, 1, 1, 0, 2 * radius], ['a', radius, radius, 0, 1, 1, 0, -2 * radius], ['z'], ]; }, size: 6, style: { fill: '#fff', stroke: '#B4B4B4', lineWidth: 1, }, }, }, }, foregroundChart: { type: 'line', options: { data, xField: 'year', yField: 'value', smooth: true, lineStyle: { lineWidth: 2, stroke: '#1890ff', }, point: false, }, }, onChange: (range) => { const start = range[0]; const end = range[1]; view.scale({ year: { type: 'timeCat', range: [start, end], }, }); chart.render(); }, }); // 设置 slider 样式 slider.slider.component.update({ style: { backgroundColor: '#f5f5f5', }, }); slider.slider.component.get('handler').update({ style: { width: 16, height: 16, borderRadius: 8, backgroundColor: '#1890ff', marginTop: -6, marginLeft: -8, boxShadow: '0 0 4px #666', }, }); // 隐藏 slider 的 tooltip slider.slider.component.tooltip(false); // 将 slider 放置在图例上方 const legend = chart.getComponents().filter((c) => c.type === 'legend')[0]; slider.container.style.top = `${legend.container.getBBox().minY - 20}px`; // 渲染图表 chart.render(); ``` 在上述代码示例中,我们定义了一个数据源 `data`,并创建了一个基础的 G2Plot 图表实例 `chart`。接着,我们创建了一个主题视图 `view`,并在视图中绘制了柱状图和折线图。然后,我们使用 `@antv/g2-plugin-slider` 插件创建了一个滑块 `slider`,并将其放置在图例上方。最后,我们对滑块和滑块 handler 的样式进行了自定义,并隐藏了 tooltip。最终,我们渲染了图表并展示了折柱图、滑块和自定义样式。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值