Vue2使用Echarts实现可视化(详细讲解)

本文介绍了如何在Vue应用中下载并配置Echarts库,展示了一个固定数据的渐进折线图实例。还展示了如何在组件中全局挂载Echarts,并通过后端数据填充动态内容,以及如何设置视觉映射和图表选项。
摘要由CSDN通过智能技术生成

一、下载Echarts

npm install echarts --save

二、main.js配置

import Vue from 'vue'
import echarts from 'echarts'
Vue.prototype.$echarts = echarts;

         全局挂载echarts 方便调用,也可以在某个组件里进行引用,这里我就不演示了

三、在.vue文件中使用

        这里的展示的是渐进折线图,数据是固定的,你们可以自己从后端获取数据后进行填充。

<template>
    <div ref="mains" style="width: 830px;height: 300px">
    </div>
</template>

<script>
    let data= [["2000-06-05", 116], ["2000-06-06", 129],
        ["2000-06-07", 135], ["2000-06-08", 86], ["2000-06-09", 73],
        ["2000-06-10", 85], ["2000-06-11", 73], ["2000-06-12", 68],
        ["2000-06-13", 92], ["2000-06-14", 130], ["2000-06-15", 245],
        ["2000-06-16", 139], ["2000-06-17", 115], ["2000-06-18", 111],
        ["2000-06-19", 309], ["2000-06-20", 206], ["2000-06-21", 137],
        ["2000-06-22", 128], ["2000-06-23", 85], ["2000-06-24", 94],
        ["2000-06-25", 71]]
    const dateList = data.map(function (item) {
        return item[0];
    });
    const valueList = data.map(function (item) {
        return item[1];
    });
    export default {
        name: "ChatHistoryAnalysis",
        data() {
            return {
                chartInstance: null,
            }
        }, mounted() {
            this.initEcharts()
        },
        methods: {
            dateList(data) {
                data.map(function (item) {
                    return item[0];
                })
            },
            valueList(data) {
                data.map(function (item) {
                    return item[1];
                });
            },
            initEcharts() {
                this.chartInstance = this.$echarts.init(this.$refs.mains);
                const option = {
                    // Make gradient line here
                    visualMap: [
                        {
                            show: false,
                            type: 'continuous',
                            seriesIndex: 0,
                            min: 0,
                            max: 400
                        },
                        {
                            show: false,
                            type: 'continuous',
                            seriesIndex: 1,
                            dimension: 0,
                            min: 0,
                            max: dateList.length-1
                        }
                    ],
                    title: [
                        {
                            left: 'center',
                            text: '近期活跃视图'
                        },
                    ],
                    tooltip: {
                        trigger: 'axis'
                    },
                    xAxis: [

                        {
                            data: dateList,
                            gridIndex: 0
                        }
                    ],
                    yAxis: [
                        {
                            gridIndex: 0
                        }
                    ],
                    grid: [
                        {
                            top: '10%'
                        }
                    ],
                    series: [
                        {
                            type: 'line',
                            showSymbol: false,
                            data: valueList
                        },

                    ]
                };
                this.chartInstance.setOption(option)
            }
        }
    }
</script>

<style scoped>

</style>

        注意 :一定要有宽度和高度,否则不会展示出来

  • 21
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
下面是一个简单的示例代码,展示了如何使用Vue.js、Neo4j和ECharts实现图谱可视化: 1. 安装依赖: ``` npm install neo4j-driver echarts --save ``` 2. 在Vue.js组件中引入neo4j-driver和echarts: ```javascript import neo4j from 'neo4j-driver'; import echarts from 'echarts'; ``` 3. 定义Neo4j连接: ```javascript const driver = neo4j.driver( 'bolt://localhost:7687', neo4j.auth.basic('neo4j', 'password') ); ``` 4. 实现查询Neo4j数据库的函数: ```javascript async function runCypherQuery(query, params) { const session = driver.session(); const result = await session.run(query, params); session.close(); return result.records; } ``` 5. 在Vue.js组件中定义ECharts实例和数据: ```javascript data() { return { chart: null, chartData: { nodes: [], links: [] } }; }, mounted() { this.chart = echarts.init(this.$refs.chart); } ``` 6. 实现查询Neo4j数据库并将结果转换为ECharts数据格式的函数: ```javascript async function queryAndTransformData() { const result = await runCypherQuery('MATCH (n)-[r]->(m) RETURN n,r,m', {}); const nodes = []; const links = []; for (const record of result) { const node1 = record.get('n'); const node2 = record.get('m'); const link = record.get('r'); const node1Index = nodes.findIndex(node => node.id === node1.identity.toString()); const node2Index = nodes.findIndex(node => node.id === node2.identity.toString()); if (node1Index === -1) { nodes.push({ id: node1.identity.toString(), name: node1.properties.name }); } if (node2Index === -1) { nodes.push({ id: node2.identity.toString(), name: node2.properties.name }); } links.push({ source: node1.identity.toString(), target: node2.identity.toString(), name: link.type }); } return { nodes, links }; } ``` 7. 在Vue.js组件中实现获取数据、更新ECharts图表的函数: ```javascript async function updateChart() { const data = await queryAndTransformData(); this.chartData = data; const option = { series: [ { type: 'graph', layout: 'force', force: { repulsion: 100, edgeLength: 50 }, data: data.nodes.map(node => ({ name: node.name, draggable: true })), links: data.links.map(link => ({ source: link.source, target: link.target, name: link.name })), label: { show: true }, lineStyle: { width: 2, curveness: 0.3, color: '#999' } } ] }; this.chart.setOption(option); } ``` 8. 在Vue.js组件中调用updateChart()函数以获取数据并更新ECharts图表: ```javascript mounted() { this.chart = echarts.init(this.$refs.chart); this.updateChart(); }, methods: { async updateChart() { // 实现updateChart函数的代码 } } ``` 希望这个示例代码能够帮助你开始使用Vue.js、Neo4j和ECharts实现图谱可视化
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱生活,更爱技术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值