echarts 动态写入数据(demo)
程序员文章站
2022-06-07 20:08:14
...
引用相关js,css文件
<script src="/resource/echart/echarts.min.js"></script>
<link rel="stylesheet" href="/resource/echart/css/echarts_part.css">
echart图表的放置位置:
<div style="text-align:left;float:left;width:80%;height:110px;" id="ask_info"></div>
<script>
var ask_info = echarts.init(document.getElementById('ask_info'));
var option_ask_info = {
tooltip : {
trigger: 'axis',
axisPointer : { // 坐标轴指示器,坐标轴触发有效
type : 'shadow' // 默认为直线,可选为:'line' | 'shadow'
}
},
legend: {
data: [],
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: {
type: 'value'
},
yAxis: {
type: 'category',
data: ['数据分布']
},
series: [
]
};
$(document).ready(function(){
$.each(data_json, function(k, v){
option_ask_info.legend.data.push(k);//动态添加
$obj={
name: k,
type: 'bar',
stack: '总量',
label: {
normal: {
show: true,
position: 'insideLeft'
}
},
data:[v],
},
option_ask_info.series.push($obj);//动态添加对象
});
ask_info.setOption(option_ask_info);
})
</script>