摘要:工作中用到的Echarts的小技巧
只需将信息拼接完后在ajax返回里面setoption即可
myChart.setOption(option);
myChart.showLoading ();
myChart.hideLoading();
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
<script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
</head>
<body>
<div id="main" style="width: 600px;height:400px;"></div>
<div id="box">
<input type="button" @click="post()" value="点我异步获取数据(Post)">
{{msg}}
</div>
<script type = "text/javascript">
var myChart = echarts.init(document.getElementById('main'));
myChart.showLoading();
var vm = new Vue({
el:'#box',
data:{
msg:'Hello World!',
vueOption:{
title : {
text: '某站点用户访问来源',
subtext: '纯属虚构',
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:[
{value:335, name:'直接访问'},
{value:310, name:'邮件营销'},
{value:234, name:'联盟广告'},
{value:135, name:'视频广告'},
{value:1548, name:'搜索引擎'}
],
itemStyle: {
emphasis: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
}
,
},
methods:{
post:function(){
//发送 post 请求
this.$http.post('test.php',{name:"菜鸟教程",url:"http://www.runoob.com"},{emulateJSON:true}).then(function(res){
//
this.vueOption.title.text = 'Eric Test';
myChart.setOption(this.vueOption)
myChart.hideLoading();
this.msg=123;
console.log(this.vueOption.series[0].name);
//
},function(res){
console.log(res.status);
});
}
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
<script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
</head>
<body>
<div id="main" style="width: 600px;height:400px;"></div>
<div id="box">
<input type="button" @click="testFunction()" value="点我异步获取数据(Post)">
{{msg}}
</div>
<script type = "text/javascript">
var myChart = echarts.init(document.getElementById('main'));
myChart.showLoading();
var vm = new Vue({
el:'#box',
data:{
msg:'Hello World!',
vueOption:{
title : {
text: '某站点用户访问来源',
subtext: '纯属虚构',
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:[
{value:335, name:'直接访问'},
{value:310, name:'邮件营销'},
{value:234, name:'联盟广告'},
{value:135, name:'视频广告'},
{value:1548, name:'搜索引擎'}
],
itemStyle: {
emphasis: {
shadowBlur: 10,
shadowOffsetX: 0,
shadowColor: 'rgba(0, 0, 0, 0.5)'
}
}
}
]
},
//
},
methods:{
testFunction:function(){
axios.post('test.php', {
firstName: 'Fred', // 参数 firstName
lastName: 'Flintstone' // 参数 lastName
})
.then(function (response) {
//注意 这里不能使用this 需要使用vm
console.log(vm.msg);
vm.msg=123;
vm.vueOption.title.text = 'Eric Test';
myChart.setOption(vm.vueOption)
myChart.hideLoading();
//
})
.catch(function (error) {
console.log(123123);
console.log(error);
});
}
}
});
</script>
</body>
</html>