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

SAPUI5 VizFrame时间序列图需按日期取最小Rest Value值求助

解决方案

方法1:前端数据预处理(前端聚合)

VizFrame的FlattenedDataset本身不支持直接聚合计算,可先从OData模型获取原始发票数据,按Date和Condition维度分组,计算每组的最小Rest Value后再绑定到图表。

步骤与代码示例

  1. 从OData模型加载发票数据
  2. 按Date+Condition分组,保留每组最小的RestValue
  3. 将聚合后的数据作为本地JSON模型绑定到VizFrame
// 假设OData模型名称为"invoiceModel"
this.getView().getModel("invoiceModel").read("/InvoiceSet", {
  success: function(oData) {
    const aRawData = oData.results;
    const oAggregatedMap = {};
    // 分组聚合逻辑
    aRawData.forEach(oItem => {
      const sGroupKey = `${oItem.Date}_${oItem.Condition}`;
      // 仅保留当前组内最小的RestValue
      if (!oAggregatedMap[sGroupKey] || oItem.RestValue < oAggregatedMap[sGroupKey].RestValue) {
        oAggregatedMap[sGroupKey] = {
          Date: oItem.Date,
          Condition: oItem.Condition,
          RestValue: oItem.RestValue
        };
      }
    });
    // 转换为数组并创建本地模型
    const aAggregatedData = Object.values(oAggregatedMap);
    const oLocalModel = new sap.ui.model.json.JSONModel(aAggregatedData);
    // 更新VizFrame的数据集绑定
    const oVizFrame = this.getView().byId("idVizFrame");
    const oDataset = oVizFrame.getDataset();
    oDataset.setModel(oLocalModel);
    oDataset.bindData({ path: "/" });
    oVizFrame.invalidate();
  }.bind(this),
  error: function(oError) {
    console.error("发票数据加载失败", oError);
  }
});

方法2:后端OData聚合(推荐)

如果后端OData服务支持$apply聚合操作(OData 4.0+或SAP Gateway已配置聚合权限),可直接通过OData请求获取聚合后的数据,性能更优。

代码示例

this.getView().getModel("invoiceModel").read("/InvoiceSet", {
  urlParameters: {
    "$apply": "groupby((Date,Condition), aggregate(RestValue with min as RestValue))"
  },
  success: function(oData) {
    const oLocalModel = new sap.ui.model.json.JSONModel(oData.results);
    const oVizFrame = this.getView().byId("idVizFrame");
    const oDataset = oVizFrame.getDataset();
    oDataset.setModel(oLocalModel);
    oDataset.bindData({ path: "/" });
    oVizFrame.invalidate();
  }.bind(this),
  error: function(oError) {
    console.error("聚合数据加载失败", oError);
  }
});

注意:需确认后端已启用OData聚合功能,SAP Gateway需在服务定义中配置允许聚合操作。

方法3:临时排序调整(不推荐长期使用)

若无法通过前后端聚合,可临时调整OData请求的排序规则,让同一Date+Condition下最小的RestValue排在首位,使VizFrame自动取到最小值。但该方法依赖数据排序逻辑,后续数据结构变化可能失效。

代码示例

this.getView().getModel("invoiceModel").read("/InvoiceSet", {
  urlParameters: {
    "$orderby": "Date asc, Condition asc, RestValue asc"
  },
  // 后续绑定逻辑同原有代码
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 03:05:18