zoukankan      html  css  js  c++  java
  • vue 基于 webpack 中使用 ECharts

    echarts教程

    Webpack 是目前比较流行的模块打包工具,你可以在使用 webpack 的项目中轻松的引入和打包 ECharts,这里假设你已经对 webpack 具有一定的了解并且在自己的项目中使用。

    可以使用国内的淘宝镜像。

    npm install -g cnpm --registry=https://registry.npm.taobao.org

    cnpm install echarts -S

    全局引入ECharts

    通过 npm 上安装的 ECharts 和 zrender 会放在node_modules目录下。可以直接在项目代码中 require('echarts') 得到 ECharts。

    我们安装完成之后,可以在 main.js 中全局引入 echarts

    import echarts from "echarts";
    Vue.prototype.$echarts = echarts;
    

    在.vue中编辑

    <template>
    <div id="app">
        <div id="main" style=" 600px;height:400px;"></div>
    </div>
    </template>
    export default {
    name: "app",
    methods: {
        drawChart() {
        // 基于准备好的dom,初始化echarts实例
        let myChart = this.$echarts.init(document.getElementById("main"));
        // 指定图表的配置项和数据
        let option = {
            title: {
            text: "ECharts 入门示例"
            },
            tooltip: {},
            legend: {
            data: ["销量"]
            },
            xAxis: {
            data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
            },
            yAxis: {},
            series: [
            {
                name: "销量",
                type: "bar",
                data: [5, 20, 36, 10, 10, 20]
            }
            ]
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
        }
    },
    mounted() {
        this.drawChart();
    }
    };
    </script>
    

    按需引入 ECharts 图表和组件

    默认使用 require('echarts') 得到的是已经加载了所有图表和组件的 ECharts 包,因此体积会比较大,如果在项目中对体积要求比较苛刻,也可以只按需引入需要的模块。

    例如上面示例代码中只用到了柱状图,提示框和标题组件,因此在引入的时候也只需要引入这些模块,可以有效的将打包后的体积从 400 多 KB 减小到 170 多 KB。

    // 引入 ECharts 主模块
    var echarts = require('echarts/lib/echarts');
    // 引入柱状图
    require('echarts/lib/chart/bar');
    // 引入提示框和标题组件
    require('echarts/lib/component/tooltip');
    require('echarts/lib/component/title');
    
    // 基于准备好的dom,初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'));
    // 绘制图表
    myChart.setOption({
        title: {
            text: 'ECharts 入门示例'
        },
        tooltip: {},
        xAxis: {
            data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [{
            name: '销量',
            type: 'bar',
            data: [5, 20, 36, 10, 10, 20]
        }]
    });
    

    可以按需引入的模块列表见 https://github.com/apache/incubator-echarts/blob/master/index.js

    第二种方法,使用 Vue-ECharts 组件

    cnpm install vue-echarts -S

    使用组件

    <template>
    <div id="app">
        <v-chart class="my-chart" :options="bar"/>
    </div>
    </template>
    <script>
    import ECharts from "vue-echarts/components/ECharts";
    import "echarts/lib/chart/bar";
    export default {
    name: "App",
    components: {
        "v-chart": ECharts
    },
    data: function() {
        return {
        bar: {
            title: {
            text: "ECharts 入门示例"
            },
            tooltip: {},
            legend: {
            data: ["销量"]
            },
            xAxis: {
            data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
            },
            yAxis: {},
            series: [
            {
                name: "销量",
                type: "bar",
                data: [5, 20, 36, 10, 10, 20]
            }
            ]
        }
        };
    }
    };
    </script>
    <style>
    .my-chart {
     800px;
    height: 500px;
    }
    </style>
  • 相关阅读:
    maven 查看依赖jar包,或获取版版本号 、license
    windows 强制关掉端口
    项目经理是如何选择的?
    windows 10 多系统引导下安装debian linux 系统 引导文件
    在 Mac 环境下 emulator 模拟器不能上网 Android Studio AVD
    WordPress
    使用blob对H5视频播放进行加密
    算法——动态规划
    算法——贪婪算法
    算法——权重最短路径算法
  • 原文地址:https://www.cnblogs.com/justblue/p/13285831.html
Copyright © 2011-2022 走看看