antvis / G2

📊 The concise and progressive visualization grammar.
https://g2.antv.antgroup.com
MIT License
12.14k stars 1.6k forks source link

复用面积图多次渲染填充色出现异常 #6507

Open Laurettaaa opened 1 month ago

Laurettaaa commented 1 month ago

问题描述

封装了一个面积图组件,在单页面上多次渲染,会出现多个面积图同一个地方填色异常

重现链接

No response

重现步骤

<script setup>
import { Chart } from '@antv/g2';
import { stringify } from 'postcss';
import {onMounted, watch, ref} from "vue";

const props = defineProps({
  data: {
    type: Array,
    required: true
  },
})

let chart;
const container = ref(null);

onMounted(() => {
  chart = renderChart(container.value);
})

watch(() => props.data, () => {
  updateBarChart(chart, props.data);
});

const updateBarChart = (chart, data) => {
  const interval = chart.getNodesByType('interval')[0];

  interval.data(data);
  chart.render();
}

const renderChart = (container) => {
  const chart = new Chart({ container, autoFit: true });

  chart
    .data(props.data)
    // .area()
    .encode('x', 'date')
    .encode('y', 'value')
    .encode('color', 'type')
    .encode('shape', 'smooth')
    .axis('x', { title: false })
    .axis('y', { title: false })
    .scale('color', {
      relations: [
        ['instagram', '#df8cf8'], 
        ['twitter', '#fade98'], 
        ['tiktok', '#8b83f7'], 
        ['youtube', '#ef8d8a'], 
        ['facebook', '#a6fcf6'], 
        [(d) => d === undefined, 'grey'], 
      ],
    })
    .legend({
      color: {
        itemMarker: 'itemMarker',

      },
    })

    chart.area().style('fillOpacity', 0.4);
    chart.line().style('strokeWidth', 2).tooltip(false);  

  chart.render();

  return chart;
};

</script>
<template>
  <div ref="container" class="w-full h-[340px] my-sm"></div>
</template>

预期行为

No response

平台

屏幕截图或视频(可选)

企业微信截图_399bbfe1-a496-40b1-916b-fdadf629d351

补充说明(可选)

No response

hustcc commented 1 month ago

哈喽,可以来一个在线可预览的地址方便调试吗?谢谢~ 是使用的这个 demo 案例吗?https://g2.antv.antgroup.com/examples/general/area/#orderly-area