当前位置:   article > 正文

微信小程序中使用Echarts(折线图)_微信小程序折线图

微信小程序折线图

一、微信中使用 Echars
直接在官网小程序使用Echarts中(文档-教程-在微信中使用Echarts),找到GitHub上的DEMO下载链接,官网有详细的使用步骤
官网:http://echarts.baidu.com/

github:https://github.com/ecomfe/echarts-for-weixin

首先,在 pages/index目录下新建以下几个文件:index.js、 index.json、 index.wxml、 index.wxss。并且在 app.json 的 pages 中增加 'pages/index/index'

index.json:

  1. {
  2. "navigationBarTitleText": "图表A",
  3. "enablePullDownRefresh": true,
  4. "usingComponents": {
  5. "ec-canvas": "../../ec-canvas/ec-canvas"
  6. }
  7. }

index.js

  1. import * as echarts from '../../ec-canvas/echarts';
  2. const app = getApp();
  3. function initChart(canvas, width, height) {
  4. const chart = echarts.init(canvas, null, {
  5. width: width,
  6. height: height
  7. });
  8. canvas.setChart(chart);
  9. var option = {
  10. title: {
  11. // text: '数据统计',
  12. left: 'center'
  13. },
  14. color: ["#37A2DA"],
  15. legend: {
  16. data: ['A','B'],
  17. top: 20,
  18. left: 'center',
  19. backgroundColor: '#dbdbdb',
  20. z: 100
  21. },
  22. grid: {
  23. left: 0,//折线图距离左边距
  24. right: 50,//折线图距离右边距
  25. top: 30,//折线图距离上边距
  26. bottom: 10,
  27. containLabel: true
  28. },
  29. tooltip: {
  30. show: true,
  31. trigger: 'axis'
  32. },
  33. xAxis: {
  34. name: '相位',
  35. type: 'category',
  36. boundaryGap: false,
  37. data: ['0°', '90°', '180°', '270°', '360°'],
  38. // axisTick: {
  39. // alignWithLabel:false
  40. // },
  41. // axisLine: {
  42. // lineStyle: {
  43. // color: '#666666'
  44. // }
  45. // },
  46. //设置x轴的样式
  47. axisLabel: {
  48. //横坐标最后的标注颜色变深
  49. // interval: 0,
  50. show: true,
  51. textStyle: {
  52. color: '#000',
  53. fontSize: '14',
  54. }
  55. },
  56. show: true
  57. },
  58. yAxis: {
  59. name: '值',
  60. x: 'center',
  61. type: 'value',
  62. splitLine: {
  63. lineStyle: {
  64. type: 'solid'
  65. }
  66. },
  67. //设置y轴字体样式
  68. axisLabel: {
  69. show: true,
  70. textStyle: {
  71. color: '#000',
  72. fontSize: '14',
  73. }
  74. },
  75. show: true
  76. },
  77. series: [{
  78. name: 'A',
  79. type: 'line',
  80. smooth: true,
  81. data: [-50,-18, 45, 65, 30, 78, 40, 0]
  82. },{
  83. name: 'B',
  84. type: 'line',
  85. smooth: true,
  86. data: [-26, -12, 40, 56, 85, 65, 20, 10]
  87. }]
  88. };
  89. chart.setOption(option);
  90. return chart;
  91. }
  92. Page({
  93. data: {
  94. ec: {
  95. onInit: initChart
  96. }
  97. },
  98. onReady() {
  99. }
  100. });

index.wxml

  1. <view>
  2. <!-- head -->
  3. <!-- <view class="head">
  4. <view class="head_config">
  5. 当前值
  6. <view>-14.8dB</view>
  7. </view>
  8. <view class="head_config">
  9. 峰值
  10. <view>-12.6dB</view>
  11. </view>
  12. <view class="head_config three">
  13. <view>注意:0</view>
  14. <view>告警:0</view>
  15. </view>
  16. <view class="head_train"></view>
  17. </view> -->
  18. <!-- 提示 -->
  19. <!-- <view class="prompt">
  20. <view class="prompt_span"><text class="prompt_first">同步:</text><text class="prompt_second"></text></view>
  21. <view class="prompt_span"><text class="prompt_first">背景:</text><text class="prompt_second">0dB</text></view>
  22. <view class="prompt_span"><text class="prompt_first">背景消去:</text><text class="prompt_second"></text></view>
  23. <view class="prompt_span"><text class="prompt_first">偏移:</text><text class="prompt_second">0°</text></view>
  24. <view class="prompt_span"><text class="prompt_first">增益:</text><text class="prompt_second">X40</text></view>
  25. <view class="prompt_span"><text class="prompt_first">滤波:</text><text class="prompt_second"></text></view>
  26. </view> -->
  27. <!-- 内容 -->
  28. <!-- <view class="component">
  29. </view> -->
  30. <!-- -->
  31. <view class="container">
  32. <ec-canvas id="mychart-dom-line" canvas-id="mychart-line" ec="{{ ec }}"></ec-canvas>
  33. </view>
  34. </view>

index.wxss:

  1. /* pages/weather/weather.wxss */
  2. ec-canvas {
  3. width: 100%;
  4. height: 50%;
  5. }
  6. .container {
  7. position: absolute;
  8. top: 0;
  9. bottom: 0;
  10. left: 0;
  11. right: 0;
  12. display: flex;
  13. flex-direction: column;
  14. align-items: center;
  15. justify-content: space-between;
  16. box-sizing: border-box;
  17. }
  18. .picker-pos {
  19. margin-top: -130rpx;
  20. margin-left: 150rpx;
  21. color: blueviolet;
  22. }
  23. /* .head {
  24. overflow: hidden;
  25. margin: 10rpx;
  26. }
  27. .head_config {
  28. float: left;
  29. margin-left: 6rpx;
  30. width: 200rpx;
  31. height: 100rpx;
  32. padding: 8rpx;
  33. border-radius: 12rpx;
  34. background-color: green;
  35. color: white;
  36. font-size: 36rpx;
  37. }
  38. .head_train{
  39. margin-left:8rpx;
  40. float: left;
  41. width: 40rpx;
  42. height: 110rpx;
  43. background-color: #dbdbdb;
  44. }
  45. .three{
  46. width: 160rpx;
  47. }
  48. .prompt{
  49. width: 100%;
  50. height: 60rpx;
  51. line-height: 60rpx;
  52. margin-top: 10rpx;
  53. display: flex;
  54. flex-direction: row;
  55. align-items: center;
  56. justify-content: space-between;
  57. box-sizing: border-box;
  58. background-color: lightblue;
  59. overflow: hidden
  60. }
  61. .prompt_span{
  62. float: left;
  63. font-size: 28rpx;
  64. }
  65. .prompt_first{
  66. color: white;
  67. }
  68. .prompt_second{
  69. color: red;
  70. } */

效果如下:

其中对于全局的Echarts的样式改法,找到ec-canvas文件,如下所示,若设置背景颜色、边框之类的问题,可以在ec-canvas.wxss文件里面修改:

  1. .ec-canvas {
  2. width: 100%;
  3. height: 100%;
  4. /* 给echarts图形添加边框 */
  5. /* border: 2rpx solid #DBDBDB; */
  6. /* 修改图形距离上边距 */
  7. margin-top: 0;
  8. /*给eecharts图形添加背景颜色*/
  9. background-color: #DBDBDB;
  10. }

使用中遇到的问题:

  • 不得不说,很多可用的信息没有,没有那么完整的为小程序服务的文档。
  • 比如我的需求是怎么将x轴的精确度缩小的很小,假如我可以显示50个点,范围是[0,50],最小的刻度即是1,那么wx-echarts是怎么设置的呢?
  • 还有我的横坐标轴的样式问题可以在axisLabel 对象中设置,比如color、fontSize,那么如果我想改我的坐标轴的标题,即图中的“相位”和“值”,他们分别对应是xAxis和yAxis对象中的name属性值,这个我该如何修改显示的样式呢?
  • 通过设置option中的grid对象的属性值,可以将图表内容显示在所给宽高的最大化,
  1. grid: {
  2. left: 0,//折线图距离左边距
  3. right: 50,//折线图距离右边距
  4. top: 30,//折线图距离上边距
  5. bottom: 10,
  6. containLabel: true
  7. }
  • 对于横纵坐标轴中的axisLabel 属性对象特别重要,比如我要复制一份表格,如果我可能一个轴设置了,另一个轴没有设置,结果会导致复制到别的地方的图表显示大小不至于的问题。如下所示:

本期的微信小程序中关于echarts的部分介绍就到这里结束了,2019的最后一个月,祝大家都能"庆余年",在工作中收获满满。

参考:https://www.echartsjs.com/zh/option.html#xAxis.scale

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