vue实现折线图 可按时间查询

本文实例为大家分享了vue实现可按时间查询的折线图的具体代码,供大家参考,具体内容如下

1.vue前端

//查询条件
<template>
<el-date-picker
 v-model="listQuery.toptime"
 :picker-options="pickerOptions"
 style="width: 380px"
 type="daterange"
 clearable
 range-separator="至"
 start-placeholder="开始日期"
 end-placeholder="结束日期"/>
 <el-select
 v-model="listQuery.xAxis"
 placeholder="统计粒度"
 clearable
 style="width: 150px"
 >
 <el-option
 v-for="(item, index) in xAxisList"
 :key="index"
 :label="item.value"
 :value="item.id"
 />
 </el-select>
//折线图
 <el-card class="box-card">
 <div slot="header" class="clearfix">
  <span>折线图</span>
 </div>
 <div id="myChart3" :style="{width: '1400px', height: '600px'}"/>
 </el-card>
</template>

2.对应script代码

// 引入基本模板
const echarts = require('echarts/lib/echarts')
// 引入柱状图组件
require('echarts/lib/chart/bar')
require('echarts/lib/chart/pie')

// 引入提示框和title组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
require('echarts/lib/component/legend')

export default {
data() {
 return {
 listQuery: {
 page: 0,
 limit: 20,
 toptime: null,
 xAxis: null
 },
 XList: [],
 XListName: '',
 YList: [],
 YListName: '',
 xAxisList: [
 { id: 1, value: '年' }, { id: 2, value: '月' }, { id: 3, value: '周' }
 ],
 temp: {
 id: undefined,
 }
 }
 },
 methods: {
 handleFilter1() {
 const listQueryData = Object.assign({}, this.listQuery)
 if (listQueryData.toptime !== null) {
 listQueryData.toptime = JSON.stringify(this.listQuery.toptime)
 } else if (listQueryData.toptime === null) {
 const end = new Date()
 const start = new Date()
 start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)//默认按周查询
 this.listQuery.toptime = [start, end]
 listQueryData.toptime = JSON.stringify([start, end])
 }
 switch (listQueryData.xAxis) {
 case 1: {
 const end = new Date()
 const start = new Date()
 start.setTime(start.getTime() - 3600 * 1000 * 24 * 365)//按年查询
 this.listQuery.toptime = [start, end]
 listQueryData.toptime = JSON.stringify([start, end])
 break
 }
 case 2: {
 const end = new Date()
 const start = new Date()
 start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)//按月查询
 this.listQuery.toptime = [start, end]
 listQueryData.toptime = JSON.stringify([start, end])
 break
 }
 case 3: {
 const end = new Date()
 const start = new Date()
 start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)//按周查询
 this.listQuery.toptime = [start, end]
 listQueryData.toptime = JSON.stringify([start, end])
 break
 }
 }
 getShareTripCount(listQueryData).then(response => {
 this.XList = response.data.data.XList
 this.YList = response.data.data.YList
 this.YListName = response.data.data.YListName
 this.XListName = response.data.data.XListName
 this.drawLine()
 })
},
//重点
drawLine() {
 const myChart3 = echarts.init(document.getElementById('myChart3'))
 myChart3.showLoading() // 数据加载完之前先显示一段简单的loading动画
 myChart3.hideLoading() // 隐藏加载动画
 // 绘制折线图
 const option = {
 title: {
 text: '分享行程数据统计',
 subtext: ''
 },
 // tooltip: {
 // trigger: 'axis'
 // },
 legend: {
 data: ['总分享次数', '通过分享注册用户数', '今日分享次数', '今日通过注册分享数']
 },
 // toolbox: {
 // show: true,
 // feature: {
 // mark: { show: true },
 // dataView: { show: true, readOnly: false },
 // magicType: { show: true, type: ['line', 'bar'] },
 // restore: { show: true },
 // saveAsImage: { show: true }
 // }
 // },
 calculable: true,
 xAxis: {
 name: this.XListName,
 type: 'category',
 data: this.XList
 },
 yAxis: {
 name: this.YListName,
 type: 'value'
 },
 series: [
 {
 name: '总分享次数',
 type: 'line',
 data: this.YList.sharenumList
 // markPoint: {
 // data: [
 // { type: 'max', name: '最大值' },
 // { type: 'min', name: '最小值' }
 // ]
 // }
 // markLine: {
 // data: [
 // { type: 'average', name: '平均值' }
 // ]
 // }
 },
 {
 name: '通过分享注册用户数',
 type: 'line',
 data: this.YList.shareUserRegisterList
 // markPoint: {
 // data: [
 // { type: 'max', name: '最大值' },
 // { type: 'min', name: '最小值' }
 // ]
 // }
 // markLine: {
 // data: [
 // { type: 'average', name: '平均值' }
 // ]
 // }
 },
 {
 name: '今日分享次数',
 type: 'line',
 data: this.YList.shareNumByDayList
 // markPoint: {
 // data: [
 // { name: '周最低', value: -2, xAxis: 1, yAxis: -1.5 }
 // ]
 // }
 // markLine: {
 // data: [
 // { type: 'average', name: '平均值' }
 // ]
 // }
 },
 {
 name: '今日通过注册分享数',
 type: 'line',
 data: this.YList.shareUserRegisterByDayList
 // markPoint: {
 // data: [
 // { name: '周最低', value: -2, xAxis: 1, yAxis: -1.5 }
 // ]
 // }
 // markLine: {
 // data: [
 // { type: 'average', name: '平均值' }
 // ]
 // }
 }
 ]
 }
 myChart3.setOption(option)
}
 }
}

3.对应后端controller代码

@RequestMapping(value = "/getShareTripCount", method = RequestMethod.POST)
 @ResponseBody
 public JSONResult getShareTripCount(HttpServletRequest request) {
 try {
  String topTime = request.getParameter("toptime");
  String xAxis = request.getParameter("xAxis");
  Map map = new HashMap();
  if(!StringUtils.isEmpty(xAxis)){
  switch (xAxis){
   case "1":{
   break;
   }
   case "2":{
   map= getShareTripCountByTime(topTime);
   break;
   }
   case "3":{
   map= getShareTripCountByTime(topTime);
   break;
   }
   default:{
   map= getShareTripCountByTime(topTime);
   break;
   }
  }
  }else{
  map=getShareTripCountByTime(topTime);
  }
  return new JSONResult(map, 0, "成功", true);
 } catch (Exception e) {
  e.printStackTrace();
  return new JSONResult(null, 101, "服务器获取失败", false);
 }
 }

private Map getShareTripCountByTime(String topTime) throws ParseException {
 Map map=new HashMap();
 Sort.Order so = new Sort.Order(Sort.Direction.DESC, "id");
 Sort sort = new Sort(so);
 if (!StringUtils.isEmpty(topTime)) {
 topTime = topTime.replace("Z", " UTC");
 Gson gson = new Gson();
 List<String> timeList = gson.fromJson(topTime, new TypeToken<List<String>>() {
 }.getType());
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
 Date endTime = format.parse(timeList.get(1));
 Date beginTime = format.parse(timeList.get(0));
 List<ShareCount> shareCountList = mongoTemplate.find(Query.query(Criteria.where("createTime").gte(beginTime).lte(endTime)).with(sort), ShareCount.class);
 Calendar c = Calendar.getInstance();
 c.setTime(beginTime);
 int month = c.get(Calendar.MONTH);
 int year = c.get(Calendar.YEAR);
 int day = c.get(Calendar.DATE);
 int dayMax = DateUtil.daysBetween(beginTime, endTime);
 List<String> dayList = new ArrayList<>();
 int monthMaxDay = DateUtil.getDaysByYearMonth(year, month);
 List<String> sharenumList = new ArrayList<>();
 List<String> shareUserRegisterList = new ArrayList<>();
 List<String> shareNumByDayList = new ArrayList<>();
 List<String> shareUserRegisterByDayList = new ArrayList<>();
 int j = 1;
 for (int i = 1; i <= dayMax; i++) {
  String sub = "";
  int yue;
  int di;
  if (monthMaxDay >= i + day) {
  di = day + i;
  yue = month + 1;
  sub = yue + "-" + di;
  } else {
  yue = month + 2;
  di = j;
  sub = yue + "-" + di;
  j++;
  }
  int sharenum = 0;
  String sharenums ="";
  int shareUserRegister = 0;
  String shareUserRegisters ="";
  int shareNumByDay = 0;
  String shareNumByDays ="";
  int shareUserRegisterByDay = 0;
  String shareUserRegisterByDays ="";
  for (ShareCount shareCount : shareCountList) {
  c.setTime(shareCount.getCreateTime());
  int months = c.get(Calendar.MONTH) + 1;
  int years = c.get(Calendar.YEAR);
  int days = c.get(Calendar.DATE);
  if (year == years && yue == months && di == days) {
   sharenum = sharenum + shareCount.getShareNum();
   shareUserRegister = shareUserRegister + shareCount.getShareUserRegister();
   shareNumByDay=shareNumByDay+ shareCount.getShareNumByDay();
   shareUserRegisterByDay=shareUserRegisterByDay+shareCount.getShareUserRegisterByDay();
  }
  }
  sharenums=String.valueOf(sharenum);
  shareUserRegisters=String.valueOf(shareUserRegister);
  shareNumByDays=String.valueOf(shareNumByDay);
  shareUserRegisterByDays=String.valueOf(shareUserRegisterByDay);
  dayList.add(sub);
  sharenumList.add(sharenums);
  shareUserRegisterList.add(shareUserRegisters);
  shareNumByDayList.add(shareNumByDays);
  shareUserRegisterByDayList.add(shareUserRegisterByDays);
 }
 Map maps=new HashMap();
 maps.put("sharenumList", sharenumList);
 maps.put("shareUserRegisterList", shareUserRegisterList);
 maps.put("shareNumByDayList", shareNumByDayList);
 maps.put("shareUserRegisterByDayList", shareUserRegisterByDayList);

 map.put("type", "month");
 map.put("YList", maps);
 map.put("YListName", "次");
 map.put("XListName", "日期");
 map.put("XList", dayList);
 }
 return map;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Vue实现点击时间获取时间段查询功能

    本文实例为大家分享了vue按时间段查询的案例,效果图如下 html代码 <template> <div class="personalReport_time"> <input type="date" :max="this.endTime" value="" v-model="startTime"/> <div></div> <input ty

  • Vue按时间段查询数据组件使用详解

    本文实例为大家分享了Vue按时间段查询数据组件的具体使用代码,供大家参考,具体内容如下 首先是前端效果: 界面代码如下: <template> <a-col :md="6" :sm="10"> <a-form-item label="执行时间" :labelCol="labelCol" :wrapperCol="wrapperCol"> <a-range-picke

  • Vue 按照创建时间和当前时间显示操作(刚刚,几小时前,几天前)

    在methods中创建方法showtime,传入要跟当前时间要对比的时间 showtime(time) { let date = typeof time === "number" ? new Date(time) : new Date((time || "").replace(/-/g, "/")); let diff = (new Date().getTime() - date.getTime()) / 1000; let dayDiff =

  • vue实现折线图 可按时间查询

    本文实例为大家分享了vue实现可按时间查询的折线图的具体代码,供大家参考,具体内容如下 1.vue前端 //查询条件 <template> <el-date-picker v-model="listQuery.toptime" :picker-options="pickerOptions" style="width: 380px" type="daterange" clearable range-separa

  • 使用laravel和ECharts实现折线图效果的例子

    1.首先引入echart.js <script type="text/javascript" src="{{ asset('/public/js/echarts.js') }}"></script> 2.html页面,要有一个布局容器,用来显示图像,一定要设置宽和高 <div class="contain" style="width: 84%;" id="contain">

  • python数据可视化之日期折线图画法

    本文实例为大家分享了python日期折线图画法的具体代码,供大家参考,具体内容如下 引入 什么是折线图: 折线图是排列在工作表的列或行中的数据可以绘制到折线图中.折线图可以显示随时间(根据常用比例设置)而变化的连续数据,因此非常适用于显示在相等时间间隔下数据的趋势.在折线图中,类别数据沿水平轴均匀分布,所有值数据沿垂直轴均匀分布. 以上引用自 百度百科 ,简单来说一般折线图 是以时间作为 X 轴 数据 作为 Y轴,这当然不是固定的,是可以自行设置的. 话不多说~ 进入正题 第一种画法: impo

  • vue+echarts实现可拖动节点的折线图(支持拖动方向和上下限的设置)

    本篇文档主要是利用echarts实现可拖动节点的折线图,在echarts中找到了一个demo,传送门:https://echarts.baidu.com/examples/editor.html?c=line-draggable,但是不是用vue写的,并且在改写为vue组件的过程中遇到了很多问题,在百度过程中发现并没有相关的文档,所以决定自己开发,并在demo的基础上开发了一些实用的功能,所以把这个过程记录下来.文档中还有很多不够完善的地方,欢迎讨论哈! 需求:制作一个折线图用于显示当前24小时

  • 在vue中使用echarts(折线图的demo,markline用法)

    公司最近在用vue开发项目,项目接近尾声了,趁休息时间写点demo-- vue引入echarts(折线图的demo) 主要是解决引入echarts,markline的使用(基准线) 这是demo的效果图: vue脚手架不多赘述 1.安装依赖 cnpm install echarts -S 2.在main.js中引入echarts import echarts from 'echarts' 3.在main.js中安装 Vue.prototype.echarts = echarts; 一般来说能正常

  • vue+echarts实现动态折线图的方法与注意

    之前公司有个绘制实时盈利率折线图的需求,实现的还不错,今天来分享下vue+echarts实现动态折线图的方法. 实现代码 <template> <div id="myChart"></div> </template> <script> import echarts from 'echarts' export default { name: 'DynamicLineChart', data () { return { // 实时

  • VUE项目中封装Echart折线图的方法

    本文实例为大家分享了VUE项目中封装Echart折线图的具体代码,供大家参考,具体内容如下 封装Echart折线图,可显示多条折线 1. 首先在项目中全局引入Echarts,main.js中: import * as echarts from 'echarts' Vue.prototype.$echarts = echarts 2.components中新建组件baseLineChart.vue,以下代码直接复制: <template>     <div       id="b

  • vue+F2生成折线图的方法

    本文实例为大家分享了vue+F2生成折线图的具体代码,供大家参考,具体内容如下 1.效果图 2.打开命令窗口,通过 npm 安装F2 npm install @antv/f2 --save 3.使用 import 或 require 引入F2 const F2 = require('@antv/f2'); 4.在页面上创建一个具备宽高的 canvas 标签,并指定 id: <template>   <div class="container">     <

  • Vue echarts实例项目地区销量趋势堆叠折线图实现详解

    最终效果如图 组件结构设计 外部 Trendpage.vue <!--针对于/trendpage 这条路径显示 测试显示组件--> <template> <div class="comP1"> <Trend></Trend> </div> </template> <script> import Trend from "@/components/Trend"; export

  • vue+echarts实现多条折线图

    本文实例为大家分享了vue+echarts实现多条折线图的具体代码,供大家参考,具体内容如下 数据未使用json格式,直接写在页面 大致效果 页面代码: <template>     <!--为echarts准备一个具备大小的容器dom-->     <div id="main" style="width: 100%;height: 300px;"></div> </template> <script

随机推荐