VUE 以组件形式使用 Echarts

本文介绍了如何在Vue项目中以组件的形式使用ECharts,包括在main.js中引入组件,编写自定义组件如饼图,并展示了数据组合的注意事项。通过实例展示了ECharts在Vue中的应用,适用于各种浏览器和设备。
摘要由CSDN通过智能技术生成

介绍:
ECharts 是一个使用 JavaScript 实现的开源可视化库,涵盖各行业图表,满足各种需求。
ECharts 遵循 Apache-2.0 开源协议,免费商用。
ECharts 兼容当前绝大部分浏览器(IE8/9/10/11,Chrome,Firefox,Safari等)及兼容多种设备,可随时随地任性展示。

第一步:先在main.js引入所需的组件,如下图

import ECharts from 'vue-echarts';
import 'echarts/lib/chart/line'; //折线图
import 'echarts/lib/chart/pie'; //饼形图
import 'echarts/lib/chart/bar'; //柱状图
import 'echarts/lib/component/legend'; //描述
import 'echarts/lib/component/tooltip'; //鼠标提示
import 'echarts/lib/component/title'; //图标题
import 'echarts/lib/component/grid';
Vue.component('v-chart', ECharts);

第二步:根据项目的组件加载方式,编写相对应的组件。

//这里的作用是同一在一个地方加载组件文件,不用每个组件都要写一次加载流程。
import Vue from 'vue'
import {
    importAllVueFile } from '@/utils/common.js'
const routeModules = importAllVueFile(
    require.context('./modules', false, /\.vue$/)
)
routeModules.forEach(({
    camelModuleName, module }) => {
   
    Vue.component("xxx-" + camelModuleName, module)
})

common.js文件:

//读取文件的文件,处理文件名
const jsFileNameRE = /^\.\/([^\s]+)\.js$/
const vueFileNameRE = /^\.\/([^\s]+)\.vue$/
export function importAllJsFile(modulesContext) {
   
    return modulesContext.keys().map((modulesPath) => {
   
        const moduleName = modulesPath.match(
            jsFileNameRE
        )
        return {
   
            moduleName,
            camelModuleName: moduleName[1].replace(
                /-(\w)/g,
                (_, c) => (c ? c.toUpperCase() : '')
            ),
            module: modulesContext(modulesPath).default
        }
    })
}
export function importAllVueFile(modulesContext) {
   
    return modulesContext.keys().map((modulesPath) => {
   
        const moduleName = modulesPath.match(
            vueFileNameRE
        )
        return {
   
            moduleName,
            camelModuleName: moduleName[1].replace(
                /-(\w)/g,
                (_, c) => {
   
                    (c ? c.toUpperCase() : '')
                }
            ),
            module: modulesContext(modulesPath).default
        }
    })
}

下面是我自己写的组件,可能不适用全部情况,按需取

//柱状图
<template>
    <v-chart
        ref="bar_chart"
        style="width: 100%;height: 100%;"
        :options="orgOptions"
        :initOptions="orgOptions"
        :autoresize="true"
    ></v-chart>
</template>

<script>
import colorList from '@/utils/colorList';

export default {
   
    data() {
   
        return {
   
            orgOptions: {
   },
            metricsSub: this.metrics,
            colorList: colorList,
            customColorSub: this.customColor,
            XDimensionSub: this.XDimension,
            legendShowCom: this.legendShow,
        };
    },
    mounted() {
   
        this.orgOptions = {
   
            title: {
   
                show: this.titleShow,
                top: 40,
                left: 60,
                text: '哈哈哈'+ ': ' + this.unitName,
                textStyle: {
   
                    fontStyle: 'lighter',
                },
            },
            tooltip: {
   
                trigger: 'axis',
                axisPointer: {
   
                    type: 'shadow',
                    textStyle: {
   
                        align: 'left',
                        color: '#fff',
                    },
                },
                formatter: function(params, ticket, callback) {
   
                    let newParams;
                    let tooltipString = [];
                    newParams = [...params];
                    newParams.sort((a, b) => {
   
                        return a.seriesName.localeCompare(b.seriesName);
                    });
                    let xLabel = params[0].name+"<br>";
                    tooltipString.push(xLabel);
                    let total = newParams.forEach((p) => {
   
                        const cont =
                            p.marker +
                            ' ' +
                            p.seriesName +
                            ': ' +
                            p.value +
                            '<br/>';
                        tooltipString.push(cont);
                        total = total + p.value;
                    });
                    return tooltipString.join('');
                },
            },
            grid: {
   
                borderWidth: 0,
                top: '18%',
                bottom: '10%',
                width: '85%',
            },
            legend: {
   
                show: this.legendShowCom,
                x: this.b
Vue和Spring Boot都是非常流行的开发框架,可以用来构建现代化的Web应用程序。而Echarts是一个非常强大的数据可视化库,可以帮助开发者将数据以图表的形式展现出来。 在Vue使用Echarts,可以通过引入Echarts的JavaScript文件,然后在Vue组件使用Echarts的API来创建图表。具体步骤如下: 1. 在Vue项目中安装Echarts: ``` npm install echarts --save ``` 2. 在Vue组件中引入Echarts: ``` import echarts from 'echarts' ``` 3. 在Vue组件中创建一个div元素,用于显示图表: ``` <div ref="chart" style="width: 100%; height: 400px;"></div> ``` 4. 在Vue组件的mounted生命周期钩子中,使用Echarts的API创建图表: ``` mounted() { const chart = echarts.init(this.$refs.chart) chart.setOption({ // 图表配置项 }) } ``` 在Spring Boot中使用Echarts,可以通过在Controller中返回一个JSON格式的数据,然后在前端使用Echarts的API来创建图表。具体步骤如下: 1. 在Spring Boot项目中引入Echarts的JavaScript文件: ``` <script src="https://cdn.bootcdn.net/ajax/libs/echarts/4.9.0/echarts.min.js"></script> ``` 2. 在Controller中返回一个JSON格式的数据: ``` @GetMapping("/data") @ResponseBody public Map<String, Object> getData() { Map<String, Object> data = new HashMap<>(); // 将数据放入data中 return data; } ``` 3. 在前端使用Vue或其他框架,通过Ajax请求获取数据,并使用Echarts的API创建图表: ``` mounted() { axios.get('/data').then(response => { const chart = echarts.init(this.$refs.chart) chart.setOption({ // 图表配置项 series: [{ type: 'bar', data: response.data }] }) }) } ``` 以上就是使用Vue和Spring Boot创建Echarts图表的基本步骤。当然,具体的实现方式还需要根据具体的需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值