s***@163.com
s***@163.com
  • 发布:2024-07-05 09:00
  • 更新:2024-07-24 09:01
  • 阅读:320

app端可以使用echarts的自定义(custom)系列图表吗

分类:uni-app

如图


这种类型的图表是不是不支持,series中的renderItem方法一直不被触发,无法显示正常

组件代码如下

<template>  
  <view class="content">  
    <!-- #ifdef APP-PLUS || H5 -->  
    <view :prop="option" :change:prop="echarts.updateEcharts" :id="id" class="echarts"></view>  
    <!-- #endif -->  
    <!-- #ifndef APP-PLUS || H5 -->  
    <view>非 APP、H5 环境不支持</view>  
    <!-- #endif -->  
  </view>  
</template>  

<script>  
export default {  
  props: {  
    id: {  
      type: String,  
      required: true  
    },  
    newData: {  
      type: Object,  
      required: true  
    }  
  },  
  data() {  
    return {  
      minTime: null,  
      maxTime: null,  
      label:"三色灯",  
      option: {  
        backgroundColor: "transparent",  
        tooltip: {  
          formatter: function (params) {  
            return params.marker + params.data.state + ': ' + params.value[2] + ' s';  
          }  
        },  
        grid: {  
          top: 10,  
          right:30,  
          height: 50 // 调整 y 轴高度  
        },  
        xAxis: {  
          type: 'time',  
          interval: 7200000,  
          min: 1715616000000,  
          max: 1715702400000,  
          scale: false,  
          axisLabel: {  
            formatter: function (val) {  
              var date = new Date(val);  
              var hours = date.getHours();  
              var minutes = date.getMinutes();  
              return (  
                  hours.toString().padStart(2, '0') +  
                  ':' +  
                  minutes.toString().padStart(2, '0')  
              );  
            }  
          }  
        },  
        yAxis: {  
          axisTick: {show: false},  
          axisLabel: {show: false},  
          splitLine: {show: false}  
        },  
        series: [  
          {  
            type: 'custom',  
            renderItem: this.renderItem,  
            itemStyle: {  
              opacity: 1  
            },  
            encode: {  
              x: [1, 2],  
              y: 0  
            },  
            data: [  
              {  
                eno: "211-002",  
                ename: "电动葫芦桥式起重机",  
                state: "故障",  
                value: [  
                  new Date("2024-05-13T16:00:00.000Z"),  
                  new Date("2024-05-13T21:52:36.000Z"),  
                  21156  
                ],  
                itemStyle: {  
                  normal: {  
                    color: "#E74639"  
                  }  
                }  
              },  
              {  
                eno: "211-002",  
                ename: "电动葫芦桥式起重机",  
                state: "故障",  
                value: [  
                  new Date("2024-05-13T21:52:36.000Z"),  
                  new Date("2024-05-14T00:33:42.000Z"),  
                  9666  
                ],  
                itemStyle: {  
                  normal: {  
                    color: "#E74639"  
                  }  
                }  
              }  
            ]  
          }  
        ]  
      }  
    }  
  },  
  created() {  
    this.initOption(this.newData);  
  },  
  methods: {  
    getStartOfDayTimestamp(timestamp) {  
      const date = new Date(timestamp);  
      date.setHours(0);  
      date.setMinutes(0);  
      date.setSeconds(0);  
      date.setMilliseconds(0);  
      return date.getTime();  
    },  
    initOption(newData) {  
      if (!newData || !newData.data) return;  

      let timeMin = newData.data[0].value[0];  
      this.minTime = this.getStartOfDayTimestamp(timeMin);  
      this.maxTime = this.minTime + 86400000;  

      const data = newData.data.map(item => {  
        item.value[0] = new Date(item.value[0]);  
        item.value[1] = new Date(item.value[1]);  
        return item;  
      });  

      // this.option.series[0].data = data;  
      // this.option.series[0].renderItem = this.renderItem;  
      // this.option.xAxis.min = this.minTime;  
      // this.option.xAxis.max = this.maxTime;  
      // console.log(this.option.series[0].data)  

    },  
    renderItem(params, api) {  
      console.log(params,"====================")  
      var start = api.coord([api.value(0), 0]);  
      var end = api.coord([api.value(1), 0]);  
      var height = api.size([0, 1])[1] * 0.8;  
      var rectShape = echarts.graphic.clipRectByRect(  
          {  
            x: start[0],  
            y: start[1] - height / 2,  
            width: end[0] - start[0],  
            height: height  
          },  
          {  
            x: params.coordSys.x,  
            y: params.coordSys.y,  
            width: params.coordSys.width,  
            height: params.coordSys.height  
          }  
      );  
      return (  
          rectShape && {  
            type: 'rect',  
            transition: ['shape'],  
            shape: rectShape,  
            style: api.style()  
          }  
      );  
    }  
  }  
}  
</script>  

<script module="echarts" lang="renderjs">  
let myChart;  

export default {  
  mounted() {  
    if (typeof window.echarts === 'function') {  
      this.initEcharts();  
    } else {  
      const script = document.createElement('script');  
      script.src = 'static/js/echarts.min.js';  
      script.onload = this.initEcharts.bind(this);  
      document.head.appendChild(script);  
    }  
  },  
  methods: {  
    initEcharts() {  
      myChart = echarts.init(document.getElementById(this.id));  
      myChart.setOption(this.option);  
    },  
    updateEcharts(newValue) {  
      myChart && myChart.setOption(newValue);  
    }  
  }  
}  
</script>  

<style>  
.content {  
  display: flex;  
  flex-direction: column;  
  align-items: center;  
  justify-content: center;  
}  

.echarts {  
  width: 100vw;  
  height: 80px;  
}  
</style>  
2024-07-05 09:00 负责人:无 分享
已邀请:
套马杆的套子

套马杆的套子 - 没有解决不了的问题,只有解决不完的问题

可以使用,你了解下renderjs,然后给echarts下载到本地,引用使用,类似于下面这个

<template>  
    <view class="content">  
        <uni-section title="在岗率" type="line">  
            <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts"  
                class="echarts">  
            </view>  
        </uni-section>  
        <view style="padding-top: 40rpx;">  
            <uni-section title="一周走势" type="line">  
                <view :prop="option1" :change:prop="echarts.updateEcharts1" id="echarts1" class="echarts1"></view>  
            </uni-section>  
        </view>  

    </view>  
</template>  

<script module="echarts" lang="renderjs">  
    let myChart;  
    let myChart1;  
    export default {  
        mounted() {  
            if (typeof window.echarts === 'function') {  
                this.initEcharts()  
            } else {  
                // 动态引入较大类库避免影响页面展示  
                const script = document.createElement('script')  
                // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算  
                script.src = 'static/echarts.js'  
                script.onload = this.initEcharts.bind(this)  
                document.head.appendChild(script)  
            }  
        },  
        methods: {  
            initEcharts() {  
                myChart = echarts.init(document.getElementById('echarts'))  
                myChart1 = echarts.init(document.getElementById('echarts1'))  
                // 观测更新的数据在 view 层可以直接访问到  
                myChart.setOption(this.option)  
                myChart1.setOption(this.option1)  
            },  
            updateEcharts(newValue, oldValue, ownerInstance, instance) {  
                // 监听 service 层数据变更  
                myChart.setOption(newValue)  

            },  
            updateEcharts1(newValue, oldValue, ownerInstance, instance) {  
                // 监听 service 层数据变更  
                myChart1.setOption(newValue)  

            },  
            onClick(event, ownerInstance) {  
                // 调用 service 层的方法  
                ownerInstance.callMethod('onViewClick', {  
                    test: 'test'  
                })  
            }  
        }  
    }  
</script>
  • s***@163.com (作者)

    您好,我写在option中的方法全都不能使用,类似 xAxis: {

    type: 'value',

    interval: 7200,

    min: 0,

    max: 86400,

    splitLine: { show: false },

    axisLabel: {

    formatter: function (val) {

    var date = new Date((val + 1715616000) * 1000);

    var hours = date.getHours();

    var minutes = date.getMinutes();

    return (

    hours.toString().padStart(2, '0') +

    ':' +

    minutes.toString().padStart(2, '0')

    );

    }

    }

    },

    这个格式化的方法就不能使用,有什么解决办法吗

    2024-07-05 14:35

  • s***@163.com (作者)

    回复 s***@163.com: 用了市场上封装好的插件已经解决

    2024-07-05 14:51

  • 套马杆的套子

    回复 s***@163.com: 好的

    2024-07-05 16:56

陌上华年

陌上华年

市场上不是有封装好的吗

  • s***@163.com (作者)

    封装好的组件里面我没找到介绍中有说明支持custom系列图表的,不确定能不能使用,就想先问一下

    2024-07-05 11:00

  • s***@163.com (作者)

    谢谢,用了封装好的option里面写的函数就能被触发了

    2024-07-05 14:50

7***@qq.com

7***@qq.com

hello,可以问下怎么解决的吗,遇到同样的问题。

要回复问题请先登录注册