Echarts与后台(mongoose)交互

Echarts引入地址可参考

echarts组件引入

javascript 复制代码
<template>
  <div>
    <div id="main" style="width: 600px;height:400px;"></div>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import * as echarts from 'echarts';
import api from '@/components/utils/api'

const boxlist = ref([])
const mc = ref([])
const jg = ref([])


const chafen = () => {
  boxlist.value.forEach(item => {
    mc.value.push(String(item.name))
    jg.value.push(Number(item.price))
    console.log(mc.value, jg.value);
    // console.log(item);
  });
}

const zhuxing = () => {
  // 基于准备好的dom,初始化echarts实例
  var myChart = echarts.init(document.getElementById('main'));

  // 指定图表的配置项和数据
  var option = {
    title: {
      text: 'ECharts与后台交互示例'
    },
    tooltip: {},
    legend: {
      data: ['销量']
    },
    xAxis: {
      data: mc.value
    },
    yAxis: {},
    series: [
      {
        name: '销量',
        type: 'bar',
        data: jg.value
      }
    ]
  };
  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option);
}


onMounted(async () => {
  const { data: { data } } = await api.get('zx')
  boxlist.value = data
  chafen()
  zhuxing()
})
</script>

<style lang="scss" scoped></style>

前端代码

javascript 复制代码
<template>
    <div class="body">
        <div class="box">
            <count-up v-for="(val, index) in endVal" :key="index" :end-val="val" :duration="duration"
                :decimal-places="decimals" :options="options" @init="onInit" @finished="onFinished"
                class="count"></count-up>
        </div>
        <zhuxing></zhuxing>
    </div>
</template>
  
<script setup lang="ts">
import { reactive, toRefs, onMounted,ref,computed } from 'vue'
import CountUp from 'vue-countup-v3'
import type { ICountUp, CountUpOptions } from 'vue-countup-v3'

import zhuxing from '@/components/gundong/zhuxing.vue'
import api from '@/components/utils/api'

const count=ref(null)

const data = reactive({
    startVal: 0, // 开始值
    endVal:  computed(()=>([count.value, 5000, 10000])), // 结束值 -- 可以写成动态的
    duration: 5, // 跳动时长 - 单位:秒
    decimals: 0, // 小数点位数
    countUp: undefined as ICountUp | undefined, // 跳动的对象
    // countUps: [] as Array<ICountUp> | [], // 跳动的对象数组
    options: {
        // 配置分隔符
        separator: '❤️'
    } as CountUpOptions
})

const onInit = (ctx: ICountUp) => {
    data.countUp = ctx
    console.log(`开始`, ctx)
}
const onFinished = () => {
    console.log('结束')
}
const { endVal, duration, decimals, options } = toRefs(data)

onMounted(async () => {
    const {data:{data}}=await api.get('zx')
    count.value=data.length
    console.log(count.value);
    // onInit()
    // onFinished()
})
</script>

<style lang="less" scoped>
.body {
    .box {
        display: flex;
        justify-content: flex-start;

        // font-weight: bold;
        .count {
            font-size: 20px;
            font-weight: bold;
            margin: 0 30px;
        }
    }
}
</style>
  

后端代码

后端配置参考地址

javascript 复制代码
var express = require("express");
var router = express.Router();
const mongoose = require("mongoose");

const Zhuxing = mongoose.model("zhuxing",{name:String,price:Number},'zhuxing')
// Zhuxing.create({
//   name:'周日',
//   price:6000
// })

router.get("/zx", async function (req, res, next) {
  const data = await Zhuxing.find();
  // console.log(data);
  res.send({ code: "200", message: "zx show ok", data });
});

module.exports = router;

效果查看

相关推荐
aitoolhub38 分钟前
H5设计实战技巧:从视觉到交互,打造高转化用户体验
自然语言处理·交互·ux·设计语言
小飞侠在吗1 小时前
Vue customRef
前端·javascript·vue.js
指尖跳动的光1 小时前
判断页签是否为活跃状态
前端·javascript·vue.js
嚣张丶小麦兜2 小时前
认识vite
前端·javascript·vue.js
oak隔壁找我4 小时前
Node.js的package.json
前端·javascript
支撑前端荣耀4 小时前
从零实现前端监控告警系统:SMTP + Node.js + 个人邮箱 完整免费方案
前端·javascript·面试
shanLion4 小时前
Vite项目中process报红问题的深层原因与解决方案
前端·javascript
烟袅4 小时前
从零构建一个待办事项应用:一次关于组件化与状态管理的深度思考
前端·javascript·react.js
我命由我123455 小时前
CSS 锚点定位 - 锚点定位引入(anchor-name、position-anchor)
开发语言·前端·javascript·css·学习·html·学习方法
哟哟耶耶6 小时前
js-清除首尾空白字符再进行空白匹配str.trim().match(...)
开发语言·前端·javascript