Vue集成Echarts 实测可以显示(柱状图和旭日图)

Vue集成Echarts

官网:https://echarts.apache.org/zh/index.html

不过没有集成vue的步骤 我实现了以后总结如下:

我们需要以下几个东西:

安装和引入echarts

# 安装echarts
npm install echarts --save

引入Echarts

#使用echarts的组件
Vue.use(echarts);
#可以通过$echarts记性操作
Vue.prototype.$echarts = echarts

组件中引入echarts

import echarts from 'echarts'

视图层代码

<!-- 做一个放echarts的 dom  id需要绑定-->
<div id="main" :style="{width: '1400px', height: '400px'}"></div>

数据层代码

// 初始化echarts
this.myChart = echarts.init(document.getElementById('main'));
// 设置echarts的参数
this.myChart.setOption({
    // 设置标题
                    title: {
                        text: 'ECharts 入门示例'
                    },
                    tooltip: {},
    // 设置X轴
                    xAxis: {
                        data: this.xAxis
                    },
    // 设置Y轴
                    yAxis: {},
    // 设置参数
                    series: [{
                        name: '销量',
                        type: 'bar',
                        data: this.cData
                    }]
                });

完成的代码为:

<template>

    <div id="main" :style="{width: '1400px', height: '400px'}"></div>

</template>

<script>
    import echarts from 'echarts'

    export default {
        name: "rule-info-view-charts",
        components: {
            echarts
        },
        data(){
            return{
                myChart:"",
                xAxis:['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],
                cData:[5, 20, 36, 10, 10, 20],
            }
        },
        mounted() {
            this.init()
        },
        methods:{
            init(){
                  this.myChart = echarts.init(document.getElementById('main'));
                  this.setChartsInfo()

            },
            setChartsInfo(){
                this.myChart.setOption({
                    title: {
                        text: 'ECharts 入门示例'
                    },
                    tooltip: {},
                    xAxis: {
                        data: this.xAxis
                    },
                    yAxis: {},
                    series: [{
                        name: '销量',
                        type: 'bar',
                        data: this.cData
                    }]
                });
            },

        }
    }

</script>

<style scoped>

</style>

其中setOption里面的是可以变的,其他的都是模板。 剩下的就需要我们一点一点琢磨了。

在这里插入图片描述

实例:集成旭日图

官方文档:旭日图

在这里插入图片描述

series: {
        type: 'sunburst',
        data: [{
            name: 'A',
            value: 10,
            children: [{
                value: 3,
                name: 'Aa'
            }, {
                value: 5,
                name: 'Ab'
            }]
        }, {
            name: 'B',
            children: [{
                name: 'Ba',
                value: 4
            }, {
                name: 'Bb',
                value: 2
            }]
        }, {
            name: 'C',
            value: 3
        }]
    }

将这个放入之前说的this.myChart.setOption(),里面

即:

setChartsInfo(){
                this.myChart.setOption({
                    series: {
                        type: 'sunburst',
                        data: [{
                            name: 'A',
                            value: 10,
                            children: [{
                                value: 3,
                                name: 'Aa'
                            }, {
                                value: 5,
                                name: 'Ab'
                            }]
                        }, {
                            name: 'B',
                            children: [{
                                name: 'Ba',
                                value: 4
                            }, {
                                name: 'Bb',
                                value: 2
                            }]
                        }, {
                            name: 'C',
                            value: 3
                        }]
                    }
                });
            }

整体代码为:

<template>

    <div id="main" :style="{width: '1400px', height: '400px'}"></div>

</template>

<script>
    import echarts from 'echarts'

    export default {
        name: "rule-info-view-charts",
        components: {
            echarts
        },
        data(){
            return{
               
            }
        },
        mounted() {
            this.init()
        },
        methods:{
            init(){
              this.myChart = echarts.init(document.getElementById('main'));
              this.setChartsInfo()
                this.dataDeal()

            },
            setChartsInfo(){
                this.myChart.setOption({
                    series: {
                        type: 'sunburst',
                        data: [{
                            name: 'A',
                            value: 10,
                            children: [{
                                value: 3,
                                name: 'Aa'
                            }, {
                                value: 5,
                                name: 'Ab'
                            }]
                        }, {
                            name: 'B',
                            children: [{
                                name: 'Ba',
                                value: 4
                            }, {
                                name: 'Bb',
                                value: 2
                            }]
                        }, {
                            name: 'C',
                            value: 3
                        }]
                    }
                });
            },


        }
    }

</script>

<style scoped>

</style>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值