当前位置:   article > 正文

react echarts制作曲线图(以天气预报为例)_react实现曲线图

react实现曲线图

React中使用echarts图表,制作一张24小时天气曲线图,显示温度,湿度曲线和各个小时的天气情况和具体温度;

描述:

 设置了3个x轴,设置了时间,温度,天气

效果图:

1. 安装:

  1. npm install --save echarts-for-react
  2. npm install echarts --save

2. 在图表页面引入Echarts

  1. import React from 'react';
  2. import ReactEcharts from 'echarts-for-react';

3. 具体设置:

  1. import React, {Component} from 'react';
  2. import EChartsReact from 'echarts-for-react';
  3. class LineChart extends Component{
  4. getOptions =(hours,temps, humiditys,weathers) => {
  5. return {
  6. title: {
  7. text: '天气预报'
  8. },
  9. tooltip: {},
  10. legend: {
  11. data: ['温度', '湿度']
  12. },
  13. // xAxis: {//单X轴
  14. // data: hours,
  15. // // splitLine: {
  16. // // show:true
  17. // // },
  18. // axisLabel: {
  19. // interval:0,//代表显示所有x轴标签显示
  20. // rotate:45, //代表逆时针旋转45
  21. // },
  22. // axisLine: {//x轴线和标签的样式
  23. // lineStyle: {
  24. // color: '#333333',
  25. // }
  26. // },
  27. // },
  28. // 多个x轴
  29. xAxis: [
  30. {
  31. data: hours,
  32. splitLine: {//显示分隔线
  33. show:true,
  34. },
  35. zlevel: 2//zlevel: 2时是底部的轴线,为1或其他数值时都是顶部的轴线
  36. },
  37. //上方的x轴
  38. {
  39. data:temps,
  40. axisLine: { //轴线不显示
  41. show: false,
  42. },
  43. // zlevel: 1
  44. position: 'top',
  45. offset: -5
  46. },
  47. {
  48. data:weathers,
  49. axisLine: { //轴线不显示
  50. show: false,
  51. },
  52. position:'bottom',//x轴线位置
  53. offset: -30 //轴线偏移量,当position:'bottom',正值向下移,负值向上移
  54. }
  55. ],
  56. yAxis: {
  57. showLine: true,
  58. // axisLabel: {
  59. // interval:0,//代表显示所有x轴标签显示
  60. // rotate:45, //代表逆时针旋转45
  61. // },
  62. axisLine: {//轴线和标签的样式
  63. show: true,//是否显示轴线
  64. lineStyle: {
  65. color: '#333333',
  66. }
  67. }
  68. },
  69. series: [{
  70. name: '温度',
  71. type: 'line',
  72. smooth: true,
  73. data: temps,
  74. showSymbol :true,//是否显示拐点
  75. // symbolSize: function (val) {
  76. // return val[2] * 40;
  77. // },
  78. }, {
  79. name: '湿度',
  80. type: 'line',
  81. smooth: true,
  82. data: humiditys,
  83. showSymbol :true,
  84. }],
  85. dataZoom: {
  86. //type: 'slider':dataZoom 组件在外边。type: 'inside':看不到组件,但可拖拉查看
  87. type: 'inside', // 这个 dataZoom 组件是 inside 型 dataZoom 组件
  88. start: 0,// 左边在 10% 的位置。
  89. end: 30 // 右边在 60% 的位置。
  90. },
  91. };
  92. }
  93. render() {
  94. const {hourWeather} = this.props;
  95. const hours = [];
  96. const temps = [];
  97. const humiditys = [];
  98. const weathers = [];
  99. Object.keys(hourWeather).forEach((item, index)=> {
  100. let itemArr = item.split(' ');
  101. let itemStr = itemArr[1].substring(0,5);
  102. console.log('hourWeather',item);
  103. hours.push(itemStr);
  104. console.log('hourWeather',hourWeather[item]);
  105. temps.push(hourWeather[item].temp);
  106. humiditys.push(hourWeather[item].humidity);
  107. weathers.push(hourWeather[item].condition);
  108. });
  109. return (
  110. <div className='scrollItem'>
  111. <EChartsReact option={this.getOptions(hours,temps, humiditys,weathers)}/>
  112. </div>
  113. );
  114. }
  115. }
  116. export default LineChart;

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/208713
推荐阅读