• uniapp 微信小程序使用ec-canvas图表


    微信小程序中使用到了ec-canvas图表,从DCloud插件市场中下载echarts-for-wx;

    uniapp项目中找到js-sdk文件夹,把其中的uni-ec-canvas放到要用的包的components中。

    在文件中导入:

    饼图:

    1. <template>
    2. <view>
    3. <uni-ec-canvas
    4. force-use-old-canvas="true"
    5. id="mychartDom"
    6. canvas-id="mychart-bar"
    7. :ec="ec"
    8. ref="canvas"
    9. canvasHeight="170"
    10. class="canvas_size"
    11. ></uni-ec-canvas>
    12. </view>
    13. </template>
    14. import uniEcCanvas from '../components/uni-ec-canvas/uni-ec-canvas.vue'
    15. import * as echarts from '../components/uni-ec-canvas/echarts.js'
    16. let chartA = null;
    17. export default {
    18. components: {
    19. uniEcCanvas
    20. },
    21. methods: {
    22. initChart(canvas, width, height, canvasDpr) {
    23. chart = echarts.init(canvas, null, {
    24. width: width,
    25. height: height,
    26. devicePixelRatio: canvasDpr
    27. })
    28. canvas.setChart(chart)
    29. var data=[{value:this.abnormal, name:'正常',"itemStyle":{"normal":{"color":"#00CCB8"}}},
    30. {value:this.absence, name:'异常',"itemStyle":{"normal":{"color":"#FFCB96"}}}];
    31. var dataName = data.reduce((per,cur,i)=>{per[i]=cur.name;return per},[]);
    32. var a=0;
    33. for(var i=0; i<data.length; i++)
    34. {
    35. a+=data[i].value;
    36. }
    37. data.push({value:a, name:'__other', itemStyle:{normal:{color:'rgba(0,0,0,0)'}}});
    38. let option = {
    39. legend: {
    40. orient: "horizontal",//图例的布局,水平布局、垂直布局
    41. icon:'circle',
    42. bottom:50,
    43. textStyle: {//图例字体样式
    44. color: "#00CCB8",
    45. fontSize: 15,
    46. fontFamily: "微软雅黑"
    47. },
    48. data:dataName,
    49. formatter: e =>{
    50. let val=0;
    51. data.forEach(el => {
    52. if(e == el.name) val = el.value
    53. });
    54. return `${e}${val}天`
    55. }
    56. },
    57. series : [
    58. {
    59. name: '上下班统计',
    60. type: 'pie',
    61. startAngle:-180,
    62. radius : '95%',
    63. center: ['50%', '60%'],
    64. data:data,
    65. itemStyle: {
    66. borderRadius:0,
    67. borderColor:'#fff',
    68. borderWidth:5
    69. },
    70. label: {
    71. normal: {
    72. show: false,
    73. },
    74. },
    75. labelLine: {
    76. normal: {
    77. show: false
    78. }
    79. },
    80. }
    81. ]
    82. };
    83. chart.setOption(option)
    84. return chart
    85. },
    86. mounted() {
    87. this.$refs.canvas.init(this.initChart)
    88. }
    89. }

    折线图:

    1. initChart(canvas, width, height, canvasDpr) {
    2. chartA = echarts.init(canvas, null, {
    3. width: width,
    4. height: height,
    5. devicePixelRatio: canvasDpr
    6. })
    7. canvas.setChart(chartA)
    8. let option = {
    9. title: {
    10. text: ''
    11. },
    12. tooltip: {
    13. trigger: 'axis',
    14. formatter: '{b}\r\n{c0}人',
    15. axisPointer: {
    16. type: 'line',
    17. axis: 'x',
    18. label: {
    19. backgroundColor: '#000000'
    20. }
    21. }
    22. },
    23. grid: {
    24. left: '6%',
    25. right: '6%',
    26. top: '6%',
    27. bottom: '6%',
    28. containLabel: true
    29. },
    30. xAxis: {
    31. type: 'category',
    32. boundaryGap: false,
    33. data: ['2-12', '2-14', '2-16', '2-18', '2-20', '2-22', '2-24'],
    34. axisLine: {
    35. // y轴
    36. show: false
    37. },
    38. axisTick: {
    39. // y轴刻度线
    40. show: false
    41. },
    42. splitLine: {
    43. // 网格线
    44. show: false
    45. }
    46. },
    47. yAxis: {
    48. type: 'value',
    49. axisLine: {
    50. // y轴
    51. show: false
    52. },
    53. axisTick: {
    54. // y轴刻度线
    55. show: false
    56. },
    57. splitLine: {
    58. // 网格线
    59. show: false
    60. }
    61. },
    62. series: [{
    63. name: '浏览量',
    64. type: 'line',
    65. smooth: true,
    66. lineStyle: {
    67. color: '#EF5959'
    68. },
    69. data: [120, 132, 101, 134, 90, 230, 210]
    70. }]
    71. };
    72. chartA.setOption(option)
    73. return chartA
    74. },

  • 相关阅读:
    构建Java线程间的默契:学习wait()、notify()和notifyAll()方法的巧妙运用
    Docker 第十五章 : Docker 三剑客之 Compose(一)
    C++笔记之通用多态函数包装器std::function
    树莓派远程音乐播放器
    AUTOCAD——中心线绘制、CAD默认线宽是多少?可以修改吗?
    数组模拟队列进阶版本——环形队列(真正意义上的排队)
    建模的常用手段:组合和聚合
    mysql 导出查询结果成 excel 文件
    [Java·基础] jdk8的优点
    C++标准模板库(STL)-list介绍
  • 原文地址:https://blog.csdn.net/liuye066/article/details/138847109