Echarts利用多X轴实现七天天气预报效果

  • UI设计图
  • Echarts示例效果
  • 前言
  • 示例代码
  • 最终效果

UI设计图

Echarts示例效果

前言

对于UI给出的设计图,各个气象网站都有类似的效果,实现方式大可归为两种:

  1. 网格布局+图表框架绘制温度曲线;
  2. 网格布局+canvas自绘温度曲线;

这两种实现方式的共同点都是将曲线和上面的描述文字拆分开来,这样做难点是要实现日期图标部分和气温曲线部分的自适应对齐。因为我CSS经验相对比较薄弱,并且使用Echarts图表框架相对较多,所以决定尝试使用Echarts(版本:4.6.0)来实现上面的效果。查看文档后发现Echarts支持多X轴和富文本显示,可以通过调整X轴偏移量来控制显示位置,同时富文本支持设置背景图标,可以用来显示天气图标。一番测试后得到下面的示例代码。

示例代码

下面这段代码可以考入Echarts直接运行:

var option = {grid: {show: true,backgroundColor: 'transparent',opacity: 0.3,borderWidth: '0',top: '180',bottom: '0'},tooltip: {trigger: 'axis'},legend: {show: false},xAxis: [// 日期{type: 'category',boundaryGap: false,position: 'top',offset: 130,zlevel: 100,axisLine: {show: false},axisTick: {show: false},axisLabel: {interval: 0,formatter: ['{a|{value}}'].join('\n'),rich: {a: {// color: 'white',fontSize: 18}}},nameTextStyle: {},data: ["25日","26日","27日","28日","29日","30日","31日"]},// 星期{type: 'category',boundaryGap: false,position: 'top',offset: 110,zlevel: 100,axisLine: {show: false},axisTick: {show: false},axisLabel: {interval: 0,formatter: ['{a|{value}}'].join('\n'),rich: {a: {// color: 'white',fontSize: 14}}},nameTextStyle: {fontWeight: 'bold',fontSize: 19},data: ["周一","周二","周三","周四","周五","周六","周日"]},// 天气图标{type: 'category',boundaryGap: false,position: 'top',offset: 20,zlevel: 100,axisLine: {show: false},axisTick: {show: false},axisLabel: {interval: 0,formatter: function(value, index) {return '{' + index + '| }\n{b|' + value + '}'},rich: {0: {backgroundColor: {// image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[0]] + '.png')image: 'https://d.scggqx.com/forecast/img/小雨.png'},height: 40,width: 40},1: {backgroundColor: {// image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[1]] + '.png')image: 'https://d.scggqx.com/forecast/img/小雨.png'},height: 40,width: 40},2: {backgroundColor: {// image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[2]] + '.png')image: 'https://d.scggqx.com/forecast/img/阴.png'},height: 40,width: 40},3: {backgroundColor: {// image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[3]] + '.png')image: 'https://d.scggqx.com/forecast/img/小雨.png'},height: 40,width: 40},4: {backgroundColor: {// image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[4]] + '.png')image: 'https://d.scggqx.com/forecast/img/多云.png'},height: 40,width: 40},5: {backgroundColor: {// image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[5]] + '.png')image: 'https://d.scggqx.com/forecast/img/小雨.png'},height: 40,width: 40},6: {backgroundColor: {// image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[6]] + '.png')image: 'https://d.scggqx.com/forecast/img/小雨.png'},height: 40,width: 40},b: {// color: 'white',fontSize: 12,lineHeight: 30,height: 20}}},nameTextStyle: {fontWeight: 'bold',fontSize: 19},// data: this.weatherdata.weatherdata: ["小雨","小雨","阴","小雨","多云","小雨","小雨"]}],yAxis: {type: 'value',show: false,axisLabel: {formatter: '{value} °C',color: 'white'}},series: [{name: '最高气温',type: 'line',data: ["16.3","16.2","17.6","14.2","17.6","15.7","14.3"],symbol: 'emptyCircle',symbolSize: 10,showSymbol: true,smooth: true,itemStyle: {normal: {color: '#C95843'}},label: {show: true,position: 'top',// color: 'white',formatter: '{c} °C'},lineStyle: {width: 1,// color: 'white'},areaStyle: {opacity: 1,color: 'transparent'}},{name: '最低气温',type: 'line',data: ["13.4","12.8","13.5","12.5","12.4","13.2","13"],symbol: 'emptyCircle',symbolSize: 10,showSymbol: true,smooth: true,itemStyle: {normal: {color: 'blue'}},label: {show: true,position: 'bottom',// color: 'white',formatter: '{c} °C'},lineStyle: {width: 1,// color: 'white'},areaStyle: {opacity: 1,color: 'transparent'}}]}

上面的代码,最难的部分就是天气图标的设置,由于axisLabel的formatter方法中的value值没法在富文本中使用,所以这里在formatter方法中将value的下标设置成了富文本中的css名,然后在设置天气图标时使用下标获取需要显示的图标名称。

// axisLabel的formatter方法
formatter: function(value, index) {return '{' + index + '| }\n{b|' + value + '}'
}// axisLabel的rich方法
rich: {index: {backgroundColor: {image: require('@/assets/weather_icon/' + this.weatherIconDic[this.weatherdata.weather[index]] + '.png')},height: 40,width: 40}}


1、this.weatherIconDic是我本地定义的一个天气图标的数据字典。(如:{ ‘晴’: ‘a00’, ‘多云’: ‘a01’, ‘阴’: ‘a02’, ‘阵雨’: ‘a03’, ‘雷阵雨’: ‘a04’, ‘冰雹’: ‘a05’, ‘雨夹雪’: ‘a06’, ‘小雨’: ‘a07’, ‘中雨’: ‘a08’, ‘大雨’: ‘a09’, ‘暴雨’: ‘a10’, ‘大暴雨’: ‘a11’, ‘特大暴雨’: ‘a12’, ‘阵雪’: ‘a13’, ‘小雪’: ‘a14’, ‘中雪’: ‘a15’, ‘大雪’: ‘a16’, ‘暴雪’: ‘a17’, ‘雾’: ‘a18’, ‘冻雨’: ‘a19’, ‘沙尘暴’: ‘a20’, ‘小到中雨’: ‘a21’, ‘中雨-大雨’: ‘a22’, ‘大雨-暴雨’: ‘a23’, ‘暴雨-大暴雨’: ‘a24’, ‘大暴雨-特大暴雨’: ‘a25’, ‘小雪-中雪’: ‘a26’, ‘中雪-大雪’: ‘a27’, ‘大雪-暴雪’: ‘a28’, ‘浮尘’: ‘a29’, ‘扬沙’: ‘a30’, ‘强沙尘暴’: ‘a31’ })
2、this.weatherdata.weather是后端传回来的天气类型。(如:[“小雨”,“小雨”,“阴”,“小雨”,“多云”,“小雨”,“小雨”])

最终效果

Echarts利用多X轴实现七天天气预报效果相关推荐

  1. Echarts实践:实现一个未来七天天气预报,图表轮播效果及插入svg或img图标

    七天效果图 轮播效果图 轮播效果实现 <el-button class="button" icon="el-icon-arrow-left" @click ...

  2. echarts折线图怎么从y轴开始_基于echarts的双y轴实时更新折线图

    一款基于echarts的双y轴实时更新折线图效果,页面加载后开始自动更新数据并绘制对应的折线图,可以点击右上角的按钮:显示数据视图.刷新数据和将数据存储为png的图片. 查看演示 下载资源: 46 次 ...

  3. 14. echarts画双y轴

    用echarts实现双y轴,并且实现指定数据使用y轴 参考链接:https://blog.csdn.net/qq_44827865/article/details/107000180 在使用echar ...

  4. Echarts图表设置x轴y轴均随滚轮滚动缩+放 区域缩放

    文章目录 echarts中设置x轴和y轴均随滚轮进行缩放 当数据实时渲染时,也可进行区域缩放 echarts中设置x轴和y轴均随滚轮进行缩放 dataZoom 组件 用于区域缩放,从而能自由关注细节的 ...

  5. Echarts 柱状图调整 y 轴单位标题与图表之间的距离

    Echarts 柱状图调整 y 轴单位标题与图表之间的距离 在我们绘制图标的时候,有时 y 轴单位标题的位置距离图标太近,影响美观,如图所示: 我们想把 元 离图表远一些,怎么操作? option = ...

  6. Echarts (option.yAxis) Y轴 的属性

    Echarts (option.yAxis) Y轴 的属性 yAxis: [{show: true, // 是否显示 Y轴type: 'value', //('value''category''tim ...

  7. ECharts图表设置x轴和y轴显示数值

    ECharts图表设置x轴和y轴显示数值 未设置的效果: 想要达到的效果: 重要代码: xAxis.axisLabel.interval = 5; // 设置x轴间隔5个显示 // 设置y轴显示的最大 ...

  8. echarts的x时间轴显示的月份是英文缩写

    echarts的x时间轴显示的月份是英文缩写 这是因为echarts默认是英文的显示,只需要改成中文就可以显示成中文了,在init的时候加上{ locale: "ZH" }(nul ...

  9. 设置echarts图表上Y轴的单位

    设置echarts图表上Y轴的单位 代码如下: yAxis: [{type: 'value',axisLabel: {show: true,interval: 'auto',formatter: '{ ...

最新文章

  1. java开发需要掌握哪些东西_java开发需要掌握哪些技能
  2. error C1189: #error : WINDOWS.H already included. MFC apps must not #include windows.h
  3. 26 行 ABAP 代码使用 HTTP_GET 函数下载百度网站的首页数据
  4. docx 图片预处理 Java_如何在java中将docx文件头图像复制到另一个docx文件中
  5. logback:RollingFileAppender
  6. 异构计算 软硬协同设计_优雅的设计CNN并行架构-软硬协同之位宽设置(2)
  7. PR 审批界面增加显示项方法
  8. Java变量的默认值和初始化
  9. Android 测试入门之---Monkey test
  10. Hive的下载和安装
  11. Access数据库多表联合查询
  12. Unity资源加载方式以及管理
  13. autocad型源代码_autocad 二次开发的一些源码实例
  14. github 邮箱_GitHub基本操作(一)
  15. linux下为php添加GD库(重新编译php)
  16. Python tkinter Text 多行文本框变化事件
  17. 苹果9是5g手机吗_苹果手机可以量体温?这是真的吗
  18. pr系统兼容性报告不支持视频驱动程序有什么影响?怎么解决?
  19. HikariCP连接池8小时后只有一个连接或session问题
  20. (Android学习)Bundle

热门文章

  1. 20秒的业务优化到底有多必要?看完你就懂了
  2. step 文件在sw怎么编辑_如何使用Solidworks打开Stp或Step文件_使用Solidworks打开Stp或Step文件的方法-系统城...
  3. 乡镇信息员计算机基础培训教程,农村信息员培训2-计算机基础知识.ppt
  4. 材料微观结构表征技术:电子背散射衍射EBSD
  5. echarts pie ajax赋值,echart-pie-ajax
  6. 硬核测评,谷歌翻译被碾压!全球首个翻译引擎进化归来,“细节狂魔”搞定方言文言文...
  7. python中函数的调用_python函数的调用、函数中变量的使用之详解
  8. LCD MIPI解析
  9. 在windows上,用cmake 交叉编译arm程序
  10. 写给开发们的色彩理论