• uni-app:引入echarts(使用renderjs)


    效果

    代码

    1. <template>
    2. <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts" class="echarts">
    3. view>
    4. template>
    5. <script>
    6. export default {
    7. data() {
    8. return {
    9. option: '',
    10. x: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
    11. y: [5, 20, 36, 10, 10, 20]
    12. }
    13. },
    14. onLoad() {
    15. this.option = {
    16. tooltip: {},
    17. xAxis: {
    18. data: this.x
    19. },
    20. yAxis: {},
    21. series: [{
    22. name: '销量',
    23. type: 'bar',
    24. data: this.y
    25. }]
    26. }
    27. },
    28. methods: {}
    29. }
    30. script>
    31. <script module="echarts" lang="renderjs">
    32. let myChart
    33. export default {
    34. mounted() {
    35. if (typeof window.echarts === 'function') {
    36. this.initEcharts()
    37. } else {
    38. // 动态引入较大类库避免影响页面展示
    39. const script = document.createElement('script')
    40. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
    41. script.src = 'static/js/echarts.js'
    42. script.onload = this.initEcharts.bind(this)
    43. document.head.appendChild(script)
    44. }
    45. },
    46. methods: {
    47. initEcharts() {
    48. myChart = echarts.init(document.getElementById('echarts'))
    49. // 观测更新的数据在 view 层可以直接访问到
    50. myChart.setOption(this.option)
    51. },
    52. updateEcharts(newValue, oldValue, ownerInstance, instance) {
    53. // 监听 service 层数据变更
    54. if (myChart != undefined) {
    55. myChart.setOption(newValue)
    56. }
    57. },
    58. }
    59. }
    60. script>
    61. <style>
    62. .echarts {
    63. width: 100%;
    64. height: 300px;
    65. }
    66. style>

    参照代码

    renderjs-echarts-demo - DCloud 插件市场

    参照的效果

    1. <template>
    2. <view class="content">
    3. <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts" class="echarts">
    4. view>
    5. <button @click="changeOption">更新数据button>
    6. <view>非 APP、H5 环境不支持view>
    7. view>
    8. template>
    9. <script>
    10. export default {
    11. data() {
    12. return {
    13. option: {
    14. title: {
    15. text: 'ECharts 入门示例'
    16. },
    17. tooltip: {},
    18. legend: {
    19. data: ['销量']
    20. },
    21. xAxis: {
    22. data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
    23. },
    24. yAxis: {},
    25. series: [{
    26. name: '销量',
    27. type: 'bar',
    28. data: [5, 20, 36, 10, 10, 20]
    29. }]
    30. }
    31. }
    32. },
    33. onLoad() {
    34. },
    35. methods: {
    36. changeOption() {
    37. const data = this.option.series[0].data
    38. // 随机更新示例数据
    39. data.forEach((item, index) => {
    40. data.splice(index, 1, Math.random() * 40)
    41. })
    42. },
    43. onViewClick(options) {
    44. console.log(options)
    45. }
    46. }
    47. }
    48. script>
    49. <script module="echarts" lang="renderjs">
    50. let myChart
    51. export default {
    52. mounted() {
    53. if (typeof window.echarts === 'function') {
    54. this.initEcharts()
    55. } else {
    56. // 动态引入较大类库避免影响页面展示
    57. const script = document.createElement('script')
    58. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
    59. script.src = 'static/js/echarts.js'
    60. script.onload = this.initEcharts.bind(this)
    61. document.head.appendChild(script)
    62. }
    63. },
    64. methods: {
    65. initEcharts() {
    66. myChart = echarts.init(document.getElementById('echarts'))
    67. // 观测更新的数据在 view 层可以直接访问到
    68. myChart.setOption(this.option)
    69. },
    70. updateEcharts(newValue, oldValue, ownerInstance, instance) {
    71. // 监听 service 层数据变更
    72. if (myChart != undefined) {
    73. myChart.setOption(newValue)
    74. }
    75. },
    76. onClick(event, ownerInstance) {
    77. // 调用 service 层的方法
    78. ownerInstance.callMethod('onViewClick', {
    79. test: 'test'
    80. })
    81. }
    82. }
    83. }
    84. script>
    85. <style>
    86. .content {
    87. display: flex;
    88. flex-direction: column;
    89. align-items: center;
    90. justify-content: center;
    91. }
    92. .echarts {
    93. margin-top: 100px;
    94. width: 100%;
    95. height: 300px;
    96. }
    97. style>

  • 相关阅读:
    P04 Navicat 15 安装使用
    使用cmd运行第一个java程序(含有查看文件后缀名方法)
    RocketMQ源码阅读(五)MappedFileQueue
    河南分销系统开发利润怎么分配?
    qemu sriov
    线上问题:Harbor核心服务不可用
    【java_wxid项目】【第四章】【Spring Cloud Ribbon集成】
    docker怎样安装redis
    LTE 网络与互联网的连接
    孙卫琴的《精通Vue.js》读书笔记-路由管理器的基本用法
  • 原文地址:https://blog.csdn.net/weixin_46001736/article/details/133649205