echart在多个div上实例化后自己会有联系。标准的例子如下:
<div id='main' style="height: 500px;margin:50px; display:block"></div>
//初始化
var option = {
title: {
text: "车辆费用统计图",
x: "center",
y: "top"
},
tooltip: {
trigger: 'item', //触发类型,默认数据触发,可选为:'item' | 'axis'
formatter: "{a} <br/>{b} : {c} ({d}%)" //内容格式器:{string}(Template) | {Function},支持异步回调
},
legend: {
orient: 'vertical',
x: 'left',
data: name1,
legendHoverLink: true,
},
toolbox: {
show: true,
feature: {
mark: { show: true },
dataView: { show: true, readOnly: false },
magicType: {
show: true,
type: ['pie', 'funnel'],
option: {
funnel: {
x: '25%',
width: '50%',
funnelAlign: 'left',
max: 1548
}
}
},
restore: {
show: true
},
saveAsImage: { show: true }
}
},
calculable: true,//是否数据拖拽合并
series: [{
//clickable: true,
name: '费用(元)',
type: 'pie',
minAngle: 5,
radius: ['30%', '45%'],//'25%', //半径,支持绝对值(px)和百分比,百分比计算比,min(width, height) / 2 * 75%, 传数组实现环形图,[内半径,外半径]
center: ['50%', '60%'],//圆心坐标,支持绝对值(px)和百分比,百分比计算min(width, height) * 50%
data: vvv1,
itemStyle: {
normal: {
label: {
show: true,
formatter: '{b} : {c} ({d}%)'
},
labelLine: { show: true }
}
}
}],
};
require(['echarts', 'echarts/chart/pie', 'echarts/chart/line'], function (ec) {
myChart = ec.init(document.getElementById('main'), 'macarons');
//var ecConfig = require('echarts/config');
//myChartPie.on(ecConfig.EVENT.CLICK, eConsole);
myChart.setOption(option, true);
});
//切换视图
var option = {
tooltip: {
show: true
},
title: {
x: 'center',
text: '车辆费用统计图',
},
legend: {
y: 'bottom',
data: ['费用(元)']
},
toolbox: {
show: true,
feature: {
mark: { show: true },
dataView: { show: true, readOnly: false },
magicType: { show: true, type: ['bar'] },
restore: { show: true },
saveAsImage: { show: true }
}
},
xAxis: [
{
type: 'category',
data: name1
}
],
yAxis: [
{
name: '费用(元)',
type: 'value'
}
],
series: [{
name: '费用)',
type: 'bar',
data: value1,
barWidth: 100,//固定柱子宽度
}
]
};
myChart.clear();
myChart.setOption(option);
如果想动态调整大小,可以
$(“#echart1”).width(widthHalf);
echart1.resize();