You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cumulocity自定义图表组件数据点选择功能实现咨询

在Cumulocity自定义图表组件中实现数据点选择功能

我帮你梳理下在Cumulocity平台给自定义图表组件添加数据点选择功能的具体步骤,这在Cumulocity的组件开发里是很常见的需求:

核心思路

Cumulocity的c8yComponentsProvider不仅能控制设备目标的启用,还能通过配置开启数据点选择面板,并指定组件支持的数据点类型(比如测量、事件对应的片段),平台会自动帮你生成对应的选择UI,不需要自己从头开发。

具体实现步骤

1. 注入并配置c8yComponentsProvider

首先在你的组件中注入C8yComponentsProvider,然后在组件初始化阶段配置目标选项,重点开启dataPoints并指定支持的数据点:

import { Component, OnInit } from '@angular/core';
import { C8yComponentsProvider } from '@c8y/ngx-components';

@Component({
  selector: 'your-custom-chart',
  templateUrl: './your-custom-chart.component.html',
  styleUrls: ['./your-custom-chart.component.css']
})
export class YourCustomChartComponent implements OnInit {
  constructor(private c8yComponentsProvider: C8yComponentsProvider) {}

  ngOnInit(): void {
    // 配置组件的目标选择规则
    this.c8yComponentsProvider.configure({
      target: {
        enabled: true, // 启用设备选择(必须开启,数据点选择依赖设备)
        dataPoints: true, // 关键:开启数据点选择功能
        // 指定组件支持的数据点类型(可选,如果不指定则显示设备所有可用数据点)
        supportedSeries: [
          { 
            name: '温度', 
            fragment: 'c8y_Temperature', 
            series: 'T' // 对应测量中的数据系列字段
          },
          { 
            name: '湿度', 
            fragment: 'c8y_Humidity', 
            series: 'H' 
          }
        ]
      }
    });
  }
}

2. 监听数据点选择事件

配置完成后,平台会自动在组件的配置面板中显示数据点选择选项。你需要监听onTargetChange事件来获取用户选中的设备和数据点,用于图表渲染:

ngOnInit(): void {
  // ... 上面的配置代码

  // 监听目标(设备+数据点)变化事件
  this.c8yComponentsProvider.onTargetChange.subscribe((selectedTarget) => {
    if (selectedTarget.device && selectedTarget.dataPoints?.length > 0) {
      // 获取选中的设备ID和数据点信息
      const deviceId = selectedTarget.device.id;
      const selectedDataPoints = selectedTarget.dataPoints;

      // 调用你的图表渲染逻辑,传入设备和数据点
      this.renderChart(deviceId, selectedDataPoints);
    }
  });
}

// 示例:图表渲染方法
private renderChart(deviceId: string, dataPoints: any[]) {
  // 这里根据设备ID和数据点查询Cumulocity的测量/事件数据,然后渲染图表
  // 比如调用c8y API获取历史数据:this.c8yMeasurements.list(...)
}

3. 确保模块依赖正确

别忘了在你的组件所属的Angular模块中导入C8yComponentsModule,否则c8yComponentsProvider无法正常工作:

import { NgModule } from '@angular/core';
import { C8yComponentsModule } from '@c8y/ngx-components';
import { YourCustomChartComponent } from './your-custom-chart.component';

@NgModule({
  declarations: [YourCustomChartComponent],
  imports: [C8yComponentsModule],
  exports: [YourCustomChartComponent]
})
export class YourCustomChartModule {}

注意事项

  • 如果希望支持设备的所有可用数据点,可以省略supportedSeries配置,平台会自动加载该设备的所有测量/事件片段。
  • supportedSeries中的fragment和series必须和设备上报的测量数据结构匹配,否则对应的数据点不会显示在选择面板中。
  • 如果你的组件是作为Widget开发,需要在widget.json中确保settings配置包含目标选择相关的入口(通常Cumulocity会自动识别,但如果没有可以手动添加)。

内容的提问来源于stack exchange,提问作者WahahaXDD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:24:06