echarts常见图形-时间轴(五)

6 篇文章 0 订阅
6 篇文章 0 订阅

1.最终效果图

在这里插入图片描述

2.echarts文件(timeline.js)

let option = {
  baseOption: {
    textStyle: {
      fontFamily: "BebasNeue"
    },
    timeline: {
      axisType: "category",
      // realtime: false,
      // loop: false,
      autoPlay: false,
      // currentIndex: 4,
      currentIndex: new Date().getMonth(), //当前所处月份下标
      playInterval: 20 * 1000,
      // controlStyle: {
      //     position: 'left'
      // },
      left: 10,
      right: 10,
      top: 2,
      bottom: 0,
      data: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
      label: {
        rotate: 40, //标签倾斜角度
        color: "#BBE2FF"
      },
      lineStyle: {
        color: "#1C72B4",
        width: 1
      },
      itemStyle: {
        color: "#1C72B4"
      },
      symbol: "circle",
      checkpointStyle: {
        color: "#3DABFF",
        borderColor: "rgba(28,114,180, 0.5)"
      },
      controlStyle: {
        borderColor: "#3DABFF",
        itemSize: 18,
        itemGap: 30
      },
      emphasis: {
        label: {
          color: "#3DABFF",
          fontSize: 14,
          fontWeight: 900
        },
        itemStyle: {
          color: "#1C72B4"
        },
        controlStyle: {
          borderColor: "#3DABFF"
        }
      }
    },
    calculable: true
  }
};
export default option;

3.vue文件中

3.1 html

<div style="height: 60px;width: 458px;" ref="timelineChart"></div>

3.2 js逻辑处理(引入timeline.js)

<script>
	import timelineOption from "@/assets/js/xxjyzt/timeline";
	export default {
		data(){
      		month: (new Date().getMonth() + 1).toString().padStart("2", "0")
		},
		methods: {
			//时间轴
		    timeEcharts() {
		      let _this = this;
		      let timelineChart = this.$echarts.init(this.$refs.timelineChart);
		      timelineChart.setOption(timelineOption);
		      timelineChart.off("click");
		      timelineChart.on("click", function(params) {
		        _this.month = params.name.replace("月", "").padStart(2, "0");
		      });
		      // 点击自动轮播
		      timelineChart.on("timelinechanged", function(params) {
		        let data = timelineOption.baseOption.timeline.data[params.currentIndex];
		        _this.month = data.replace("月", "").padStart(2, "0");
		      });
		      timelineChart.resize();
		    }
		},
		mounted() {
			this.timeEcharts(); //时间轴
		}
	}
</script>
  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Echarts柱状图增加时间轴的方法如下: 1.首先需要在HTML文件中引入Echarts的JS文件和CSS文件。 2.在HTML文件中创建一个div容器,用于显示Echarts图表。 3.在JS文件中定义一个Echarts实例,并设置图表的基本属性,如标题、图例、X轴和Y轴等。 4.创建一个时间轴组件,用于控制柱状图的时间范围。 5.将时间轴组件添加到Echarts实例中,并设置相应的属性,如时间轴的类型、起始时间、结束时间等。 6.创建一个柱状图系列,并将其添加到Echarts实例中。 7.设置柱状图系列的属性,如系列名称、数据、柱状图的颜色等。 8.最后调用Echarts实例的setOption方法,将以上设置应用到图表中。 下面是一个示例代码,演示如何使用Echarts实现带有时间轴的柱状图: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Echarts柱状图增加时间轴</title> <!-- 引入Echarts的JS文件和CSS文件 --> <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.min.js"></script> <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.min.css"> </head> <body> <!-- 创建一个div容器,用于显示Echarts图表 --> <div id="chart" style="width: 800px;height:600px;"></div> <script> // 定义一个Echarts实例 var myChart = echarts.init(document.getElementById('chart')); // 设置图表的基本属性 myChart.setOption({ title: { text: '柱状图增加时间轴' }, tooltip: {}, legend: { data:['销量'] }, xAxis: { data: ["1月","2月","3月","4月","5月","6月"] }, yAxis: {}, series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20], itemStyle: { color: '#FFA500' } }] }); // 创建一个时间轴组件 var timeLine = { show: true, axisType: 'category', data: ["2019","2020","2021"], left: 'center', bottom: 0, width: '80%', height: '10%', label: { color: '#000' }, symbol: 'none', lineStyle: { color: '#000' }, checkpointStyle: { color: '#000', borderColor: '#000' }, controlStyle: { showNextBtn: true, showPrevBtn: true, normal: { color: '#000', borderColor: '#000' }, emphasis: { color: '#000', borderColor: '#000' } } }; // 将时间轴组件添加到Echarts实例中 myChart.setOption({ baseOption: { timeline: { axisType: 'category', data: ["2019","2020","2021"], autoPlay: true, playInterval: 1000, left: 'center', bottom: 0, width: '80%', height: '10%', label: { color: '#000' }, symbol: 'none', lineStyle: { color: '#000' }, checkpointStyle: { color: '#000', borderColor: '#000' }, controlStyle: { showNextBtn: true, showPrevBtn: true, normal: { color: '#000', borderColor: '#000' }, emphasis: { color: '#000', borderColor: '#000' } } }, series: [{ name: '销量', type: 'bar', data: [ [5, 20, 36, 10, 10, 20], [10, 30, 20, 15, 25, 30], [20, 10, 15, 30, 20, 10] ], itemStyle: { color: '#FFA500' } }] }, options: [ { series: [{ name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20], itemStyle: { color: '#FFA500' } }] }, { series: [{ name: '销量', type: 'bar', data: [10, 30, 20, 15, 25, 30], itemStyle: { color: '#FFA500' } }] }, { series: [{ name: '销量', type: 'bar', data: [20, 10, 15, 30, 20, 10], itemStyle: { color: '#FFA500' } }] } ] }); </script> </body> </html> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值