若依前后台分离vue版,前台使用echarts动态调用后台数据

本文介绍如何使用Vue和ECharts实现新闻数量的柱状图展示。从前端到后端全面覆盖,包括Vue组件的创建、ECharts配置及后端数据接口设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、前台vue代码

在首页index.vue中添加

<el-row style="width: 100%; font-weight: bold; top: 10px;" :gutter="10" :sm="24" :lg="24" type="flex">
		<el-col :xs="18" :sm="18">
			<div class="chart-wrapper">
          <bar-chart />
        </div>
		</el-col>
		<el-col :xs="6" :sm="6"></el-col>
	</el-row>

并调用BarChart.vue

import BarChart from './dashboard/BarChart'
components: {
    BarChart,
},

然后BarChart.vue代码如下

<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>
 
<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import {newsCount } from "@/api/news/news";
import resize from './mixins/resize'
const animationDuration = 6000
 
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      item:[],
      value:[],
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'infographic')
      //this.chart.showLoading();
      newsCount(this.queryParams).then(response => {
        console.log(response);
        this.listData = response.data;
        for (let i = 0; i < this.listData.length; i++) {
          this.item.push(this.listData[i].item);
          this.value.push(this.listData[i].value)
        }
        console.log(this.item);
        this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器,坐标轴触发有效
            type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: this.item,
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: '信息数量',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: this.value,
          animationDuration
        }]
      })
      })
      
 
    }
  }
}
</script>

new.js中新增

export function newsCount(query) {
  return request({
    url: '/news/news/dateCount',
    method: 'get',
    params: query
  })
}

二、后台

Controller代码为

@ApiOperation("dateCount")
    @GetMapping("/dateCount")
    public AjaxResult dateCount(News news)
    {
        List<Map<String, Object>> listMaps  = newsService.selectListCount(news);
        return AjaxResult.success(listMaps);
 }

service代码为

@Override
    public List<Map<String, Object>> selectListCount(News news) {
 
        return newsMapper.selectListCount(news);
}

newsMapper.xml

<select id="selectListCount" resultType="java.util.Map">
        SELECT a.item,IFNULL(b.value,0) AS value
 
FROM (
 
SELECT CURDATE() AS item
 
UNION ALL
 
SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS item
 
UNION ALL
 
SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS item
 
UNION ALL
 
SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY) AS item
 
UNION ALL
 
SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY) AS item
 
UNION ALL
 
SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY) AS item
 
UNION ALL
 
SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY) AS item
 
) a LEFT JOIN (
 
SELECT DATE(create_time) AS date, count(date_format(create_time,'%Y-%m-%d')) AS value
 
FROM news_tbl
 
GROUP BY DATE(create_time)
 
) b ON a.item = b.date;
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值