我如何清除apache Echart?我需要重新初始化它

how do I clear a apache Echart? I need to re-initialize it

我在单个页面上放置了堆叠式水平条形图,每个图表都会根据用户的选择(下拉 Select)而变化。我有一个检索数据的 ajax 调用,因此数据会根据用户选择而变化并且是动态的。我在清除旧数据时遇到问题。如果没有数据,它应该显示水平条形图。但它显示以前的数据。如果没有数据,它不会变空。基本上,我只想在每次选择后重新初始化图表并从头开始。我该怎么做?

<script type = "text/javascript" >
    var series;
$("#sub_project3").change(function() {
    $.ajax({
        url: "<?php echo base_url("
        Manage_procurement_plan / load_bar_chart ");?>",
        type: "POST",
        data: {
            drop_value: $(this).val()
        },
        dataType: "text",
        cache: false,
        success: function(data) {
            series = data;
            var dom = document.getElementById("main");
            var myChart = echarts.init(dom);
            var app = {};
            var option;
            getBarGraph(series);

            function getBarGraph(data) {
                option = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        }
                    },
                    legend: {
                        top: '3%',
                    },
                    grid: {
                        top: '28%',
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true,
                    },
                    xAxis: {
                        type: 'value',
                    },
                    yAxis: {
                        type: 'category',
                        data: ['Actual Avg', 'ADB Min Standard']
                    },
                    series: JSON.parse(data),
                };
                /*if (option && typeof option === 'object') {
                    
                    myChart.setOption(option);
                }*/
                myChart.setOption(option);

            }
        }
    });
}); 
</script>

每次进行新选择时,您可以 're-initialize' 您的 series(将其设为空列表),然后再将新数据推入其中。因此,如果没有数据,series 将保持为空。

然后,您可以使用 notMerge = true 更新您的图表。根据 echarts doc :

notMerge Optional. Whether not to merge with previous option. false by default, means merge, see more details in Component Merging Modes. If true all of the current components will be removed and new components will be created according to the new option.

myChart.setOption(option, true);