Echarts 动态改变X轴数据量

新手上路,请多包涵

1、我使用Echarts3在一个页面渲染3张图(柱状图,饼状图,折线图),通过ajax根据选项动态获取相关数据,但是清除不掉原有的X轴多余出来的数据。使用了setOption(option,true)和clear();没有效果

图片描述

图片描述

var name = "";
var ydata = [];
var xdata = [];
var bydata = [];
var myChartBing,optionZhu;
var myChartZhe,optionZhe;
var myChartZhu,optionBing;


myChartZhu = echarts.init(document.getElementById('dazhu'));
myChartZhe = echarts.init(document.getElementById('dazhe'));
myChartBing = echarts.init(document.getElementById('dabing'));

optionZhu = {
        title: {
            text: name,
            left: '8%'
        },
        color: ['#3398DB'],
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器,坐标轴触发有效
                type: 'shadow'        // 默认为直线,可选为:'line' | 'shadow'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                data: [],
                axisTick: {
                    alignWithLabel: true
                }
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: [
            {
                name: '',
                type: 'bar',
                barWidth: '60%',
                data: []
            }
        ]
    };

    optionZhe = {
        title: {
            text: name,
            left: '8%'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#6a7985'
                }
            }
        },
        legend: {
            data: ['选择情况']
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [
            {
                type: 'category',
                boundaryGap: false,
                data: []
            }
        ],
        yAxis: [
            {
                type: 'value'
            }
        ],
        series: [
            {
                name: '选择情况',
                type: 'line',
                stack: '总量',
                areaStyle: {normal: {}},
                data: []
            }
        ]
    };
    optionBing = {
        title: {
            text: name,
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: []
        },
        series: [
            {
                name: '',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                data: [],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    myChartZhu.setOption(optionZhu,{ notMerge:true});
    myChartZhe.setOption(optionZhe,{ notMerge:true});
    myChartBing.setOption(optionBing,{ notMerge:true});
 function ajaxGetData() {
        $.ajax({
            type: "GET",
            dataType: "json",
            url: 'system/getDataByQuestionIdOrCountyId',
            data: {'questionid': currentQuestionId, 'countyid': currentCountyId},
            success: function (result) {
                optionZhu.title.text=result.data.questionname;
                optionZhe.title.text=result.data.questionname;
                optionBing.title.text=result.data.questionname;
                $(result.data.optionDTOS).each(function (index, item) {
                    ydata[index]=item.count;
                    xdata[index]=item.name;
                    bydata[index]={};
                    bydata[index].value=item.count;
                    bydata[index].name=item.name;
                });
                optionZhu.xAxis[0].data=xdata;
                optionZhu.series[0].data=ydata;
                optionZhe.xAxis[0].data=xdata;
                optionZhe.series[0].data=ydata;
                optionBing.legend.data=xdata;
                optionBing.series[0].data=bydata;
                myChartZhu.setOption(optionZhu,{ notMerge:true});
                myChartZhe.setOption(optionZhe,{ notMerge:true});
                myChartBing.setOption(optionBing,{ notMerge:true});
            },
            error: function (data) {
                layer.alert("出现异常!");
            }
        });
    }
    ajaxGetData();
阅读 14.6k
1 个回答
新手上路,请多包涵

问题解决了,我的程序有点问题,不应该将xdata,ydata设置成全局变量,这样数组的长度就会是所有请求中最多的,导致数据错乱

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题