该问题已经解决,下面是解决思路!
问题描述:
axis.getAxesOnZeroOf is not a function
使用echart 出现报这句错误,请求解决方案!
问题原因:
我给坐标设置了最大值和最小值导致的,怎么说?客户需求坐标的刻度需要保留两位小数如 2.00,我们是可以通过设置 axisLabel 的 formatter 来搞定的。但如果是自动设置了最大值,最小值就要仔细检查了。如果我的问题不是导致你的问题,请根据我的方式去分析,就可以找到答案。下面看我的代码
var getDefaultY = function (colorIndex, measure) { return { type: 'value', min: function (value) { var min = value.min; var max = value.max; //报错误 axis.getAxesOnZeroOf is not a function //min应该返回的是数字型,我使用了toFixed变为了string return (Math.floor((Math.round((min - (max - min) * 0.1) * 100))) / 100).toFixed(2); //改为如下正常 return parseFloat((Math.floor((Math.round((min - (max - min) * 0.1) * 100))) / 100).toFixed(2)) //我进一步取整 var val = parseFloat((Math.floor((Math.round((min - (max - min) * 0.1) * 100))) / 100).toFixed(2)); return Math.floor(val) }, max: function (value) { var min = value.min; var max = value.max; var val = parseFloat((Math.ceil((Math.round((max + (max - min) * 0.1) * 100))) / 100).toFixed(2)); return Math.ceil(val); }, splitLine: { show: false }, splitArea: { show: false }, axisLabel: { //使用formatter返回string是非常正确的 formatter: function (value, index) { var measure = measure || ""; if (typeof (value) == 'number') { return value.toFixed(2) + measure; } return value + measure; } } }; };
问题分析过程
//第一步:将optiog转化为string,然后将string转发为json放在管网案例上取执行 option = JSON.stringify(option); option = JSON.parse(option); gb.chart.setOption(option, true)
这步中我们的方法会被去掉的,因为function不能转化为 JSON.下一步你就要检查你的 option 中所带的方法了。那么我的 option 主要有三个方法,分别是min ,max,还有tooltip中的formatter
obj.tooltip = { trigger: 'axis', axisPointer: { type: 'cross' }, formatter: function (datas) { try { var date = new Date(datas[0].data.measureTime); var res = " " + date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + '<br/>'; var length = datas.length; for (var i = 0; i < length; i++) { const element = datas[i].data; var value = element[datas[i].dimensionNames[1]]; if (value != undefined) { value = value.toFixed(2); } else { value = ""; } res += '<span style="display:inline-block;margin-right:5px;border-radius:10px;10px;height:10px;background-color:' + datas.color + ';"></span>' + datas[i].seriesName + ':' + value + '<br/>'; } return res; } catch (e) { return ""; } } }
然后使用google浏览器,然后将三个方法自己写的代码去掉,使用排除法就可以解决找到该问题根源!