有群友觉得thingsboard的图例设置不能满足需求,想要最大值减最小值。怎么自己实现呢?

效果说明

原版未提供 显示最大值减最小值选项。「差值」和「显示最大减最小」想改成别的文字,在最后面的locale.constant-zh_CN.json中修改即可。image.pngimage.png

数据准备

使用下面这篇文章的数据 https://www.yuque.com/kuwei-g0lft/tb/rkwzft
仪表改造-图例(Label)自动加设备名称

ui-ngx代码修改

改的内容不多,为了容易找到对应位置,贴出代码较多。
ui-ngx/src/app/shared/models/widget.models.ts 增加有注释的内容

  1. export interface LegendConfig {
  2. position: LegendPosition;
  3. direction?: LegendDirection;
  4. sortDataKeys: boolean;
  5. showMin: boolean;
  6. showMax: boolean;
  7. showAvg: boolean;
  8. showDelta: boolean; //添加是否显示最大值最小值差值
  9. showTotal: boolean;
  10. }
  11. export function defaultLegendConfig(wType: widgetType): LegendConfig {
  12. return {
  13. direction: LegendDirection.column,
  14. position: LegendPosition.bottom,
  15. sortDataKeys: false,
  16. showMin: false,
  17. showMax: false,
  18. showDelta: false, //默认不显示
  19. showAvg: wType === widgetType.timeseries,
  20. showTotal: false
  21. };
  22. }
  23. export interface LegendKeyData {
  24. min: string;
  25. max: string;
  26. avg: string;
  27. total: string;
  28. delta: string; //LegendKeyData添加delta
  29. hidden: boolean;
  30. }

ui-ngx/src/app/modules/home/components/widget/legend.component.html 增加有注释的内容

  1. <tr class="tb-legend-header" *ngIf="!isRowDirection">
  2. <th colspan="2"></th>
  3. <th *ngIf="legendConfig.showMin === true">{{ 'legend.min' | translate }}</th>
  4. <th *ngIf="legendConfig.showMax === true">{{ 'legend.max' | translate }}</th>
  5. <th *ngIf="legendConfig.showAvg === true">{{ 'legend.avg' | translate }}</th>
  6. <!--增加下面这行-->
  7. <th *ngIf="legendConfig.showDelta === true">{{ 'legend.delta' | translate }}</th>
  8. <th *ngIf="legendConfig.showTotal === true">{{ 'legend.total' | translate }}</th>
  9. </tr>
  10. <tr class="tb-legend-keys" *ngFor="let legendKey of legendKeys()">
  11. <td><span class="tb-legend-line" [ngStyle]="{backgroundColor: legendKey.dataKey.color}"></span></td>
  12. <td class="tb-legend-label"
  13. (click)="toggleHideData(legendKey.dataIndex)"
  14. [ngClass]="{ 'tb-hidden-label': legendKey.dataKey.hidden, 'tb-horizontal': isHorizontal }">
  15. {{ legendKey.dataKey.label }}
  16. </td>
  17. <td class="tb-legend-value" *ngIf="legendConfig.showMin === true">{{ legendData.data[legendKey.dataIndex].min }}</td>
  18. <td class="tb-legend-value" *ngIf="legendConfig.showMax === true">{{ legendData.data[legendKey.dataIndex].max }}</td>
  19. <td class="tb-legend-value" *ngIf="legendConfig.showAvg === true">{{ legendData.data[legendKey.dataIndex].avg }}</td>
  20. <td class="tb-legend-value" *ngIf="legendConfig.showTotal === true">{{ legendData.data[legendKey.dataIndex].total }}</td>
  21. <!--增加下面这行-->
  22. <td class="tb-legend-value" *ngIf="legendConfig.showDelta === true">{{ legendData.data[legendKey.dataIndex].delta }}</td>
  23. </tr>
  24. <tr class="tb-legend-keys" *ngIf="isRowDirection && legendConfig.showTotal === true">
  25. <td class="tb-legend-type">{{ 'legend.total' | translate }}</td>
  26. <td class="tb-legend-value" *ngFor="let legendKey of legendKeys()">
  27. {{ legendData.data[legendKey.dataIndex].total }}
  28. </td>
  29. </tr>
  30. <!--增加下面tr全部-->
  31. <tr class="tb-legend-keys" *ngIf="isRowDirection && legendConfig.showDelta === true">
  32. <td class="tb-legend-type">{{ 'legend.delta' | translate }}</td>
  33. <td class="tb-legend-value" *ngFor="let legendKey of legendKeys()">
  34. {{ legendData.data[legendKey.dataIndex].delta }}
  35. </td>
  36. </tr>

ui-ngx/src/app/modules/home/components/widget/legend-config-panel.component.html 增加注释部分

  1. <mat-checkbox formControlName="showMax">
  2. {{ 'legend.show-max' | translate }}
  3. </mat-checkbox>
  4. <!--增加下面mat-checkbox全部-->
  5. <mat-checkbox formControlName="showDelta">
  6. {{ 'legend.show-delta' | translate }}
  7. </mat-checkbox>

ui-ngx/src/app/modules/home/components/widget/legend-config-panel.component.ts 增加注释部分

  1. this.legendConfigForm = this.fb.group({
  2. direction: [this.data.legendConfig.direction, []],
  3. position: [this.data.legendConfig.position, []],
  4. sortDataKeys: [this.data.legendConfig.sortDataKeys, []],
  5. showMin: [this.data.legendConfig.showMin, []],
  6. showMax: [this.data.legendConfig.showMax, []],
  7. showAvg: [this.data.legendConfig.showAvg, []],
  8. //增加下面这行
  9. showDelta: [this.data.legendConfig.showDelta, []],
  10. showTotal: [this.data.legendConfig.showTotal, []]
  11. });

ui-ngx/src/app/core/api/widget-subscription.ts 增加注释部分

  1. const legendKeyData: LegendKeyData = {
  2. min: null,
  3. max: null,
  4. avg: null,
  5. //增加下面这行
  6. delta: null,
  7. total: null,
  8. hidden: false
  9. };
  10. private updateLegend(dataIndex: number, data: DataSet, detectChanges: boolean) {
  11. const dataKey = this.legendData.keys.find(key => key.dataIndex === dataIndex).dataKey;
  12. const decimals = isDefinedAndNotNull(dataKey.decimals) ? dataKey.decimals : this.decimals;
  13. const units = dataKey.units && dataKey.units.length ? dataKey.units : this.units;
  14. const legendKeyData = this.legendData.data[dataIndex];
  15. if (this.legendConfig.showMin) {
  16. legendKeyData.min = this.ctx.widgetUtils.formatValue(calculateMin(data), decimals, units);
  17. }
  18. if (this.legendConfig.showMax) {
  19. legendKeyData.max = this.ctx.widgetUtils.formatValue(calculateMax(data), decimals, units);
  20. }
  21. if (this.legendConfig.showAvg) {
  22. legendKeyData.avg = this.ctx.widgetUtils.formatValue(calculateAvg(data), decimals, units);
  23. }
  24. if (this.legendConfig.showTotal) {
  25. legendKeyData.total = this.ctx.widgetUtils.formatValue(calculateTotal(data), decimals, units);
  26. }
  27. //增加if全部内容
  28. if (this.legendConfig.showDelta) {
  29. legendKeyData.delta = this.ctx.widgetUtils.formatValue(calculateMax(data) - calculateMin(data), decimals, units);
  30. }
  31. this.callbacks.legendDataUpdated(this, detectChanges !== false);
  32. }

ui-ngx/src/assets/locale/locale.constant-zh_CN.json 增加有delta的部分,不要加注释

  1. "legend": {
  2. "avg": "平均值",
  3. "comparison-time-ago": {
  4. "days": "(一天前)",
  5. "months": "(一个月前)",
  6. "weeks": "(一周前)",
  7. "years": "(一年前)"
  8. },
  9. "direction": "图例方向",
  10. "max": "最大值",
  11. "min": "最小值",
  12. "delta": "差值",//使用时去掉注释
  13. "position": "图例位置",
  14. "settings": "图例设置",
  15. "show-avg": "显示平均值",
  16. "show-max": "显示最大值",
  17. "show-min": "显示最小值",
  18. "show-total": "显示总数",
  19. "show-delta": "显示最大减最小",//使用时去掉注释
  20. "sort-legend": "在图例中排序数据键",
  21. "total": "总数"
  22. },

一言不合就点赞.gif