• vue中使用echarts(vue+vue-cli+axios+jsonp+echarts)


    一、安装echarts:

    cnpm i echarts -D

    二、在vue-cli的main.js文件中引用echarts:

    import charts from 'echarts'
    Vue.prototype.$echarts = charts

    三、echarts详细代码:

    echarts.vue:

    <template>
      <div>
        <div id="myChart">
    
        </div>
      </div>
    </template>
    
    <script>
      export default {
        methods: {
          drawLine(){
            // 基于准备好的dom,初始化echarts实例
            let myChart = this.$echarts.init(document.getElementById('myChart'));
    
            this.$axios.get("http://127.0.0.1:8000/get_data")
              .then(function(res){
                // 绘制图表
                myChart.setOption({
                  title: { text: res.data.title },
                  tooltip: {},
                  xAxis: {
                    data: res.data.xAxisData
                  },
                  yAxis: {},
                  series: [{
                    name: '销量',
                    type: 'bar',
                    data: res.data.seriesData
                  }]
                });
              })
              .catch(function(err){
                console.log(err);
              })
          }
        },
        mounted(){
          this.drawLine();
        }
      }
    </script>
    
    <style>
    #myChart{
      height: 500px;
    }
    </style>

    四、上面的图表数据通过axios获取,node.js代码如下:

    let express = require("express");
    let app = express();
     
    app.get("/get_data", function(req, res, next){
    	res.header("Access-Control-Allow-Origin", "*");
        let response = {
        	title: '在Vue中使用echarts',
        	xAxisData: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"],
        	seriesData: [10, 26, 16, 20, 16, 30]
        };
        res.type('application/json');
        res.jsonp(response);
    });
     
    app.listen(8000, function(){
        console.log("开始执行请求");
    });
  • 相关阅读:
    学校的破网,你别再掉了
    PhotoShop SDK的获取
    我的C++博客开张了
    一个新的嵌入式门户
    试用Bloglines.com
    PhotoShop的插件体系
    VB6 to VB.NET Migration Guide Community Preview #4
    看看Microsoft都买了些什么
    Borland CTO辞职
    PhotoShop插件的开发
  • 原文地址:https://www.cnblogs.com/samve/p/10884748.html
Copyright © 2020-2023  润新知