Resolve Code (n:History tags or direct history:// Ords

Adding this resolve code here in case anyone is interested. Takes either a component with a n:history tag or a direct history:// and resolves the bOrd to pass to the rest of the widget code.

  eChartsPieWidget.prototype.resolve = function () {
    var bOrd = arguments[0];
    if (!bOrd || String(bOrd) === 'null') {
      this._debouncedRefresh();
      return Widget.prototype.resolve.apply(this, arguments);
    }

    // This function will be called once we have a series object ready to be added.
    const addSeries = (seriesObject) => {
      if (!seriesObject || !seriesObject.ord) {
        console.warn('[PieChart Widget] Attempted to add an invalid series object.', seriesObject);
        return;
      }
      if (!this.seriesInfo.some(s => s.ord === seriesObject.ord)) {
        this.seriesInfo.push(seriesObject);
      }
      // Refresh is always called to update the chart with the new or existing data.
      this._debouncedRefresh();
    };

    // Helper to get units from a standard point component.
    const getUnitsFromPoint = (pointComponent) => {
      const outProp = pointComponent.get("out");
      if (outProp) {
        const unitsFacet = outProp.getFacets().get("units");
        if (unitsFacet) return unitsFacet.getSymbol();
      }
      return '';
    };

    // ====================================================================
    // UNIFIED PROCESSING FUNCTION
    // This function receives the object *after* it has been resolved by .get()
    // and figures out what to do with it.
    // ====================================================================
    const processResolvedObject = (resolvedObject) => {
      // CASE 1: The object is a history data object.
      if (resolvedObject && resolvedObject.$tableData && resolvedObject.$tableData.config) {
        const configArray = resolvedObject.$tableData.config.s;
        const xData = resolvedObject.$tableData.x;

        const findInConfig = (name) => {
          const prop = configArray.find(p => p && p.n === name);
          return prop ? prop.d : null;
        };

        const getUnitsFromFacets = (facetsString) => {
            if (!facetsString) return '';
            const match = facetsString.match(/units=([^,]+)/);
            return match ? match[1] : '';
        };

        const historyId = "history:" + xData.substring("historyId=".length, xData.indexOf(','));
        const name = findInConfig('navDisplayName') || 'History';
        const units = getUnitsFromFacets(findInConfig('valueFacets'));

        addSeries({ name, units, ord: historyId });

      // CASE 2: The object is a standard Baja Component.
      } else if (resolvedObject && typeof resolvedObject.getDisplayName === 'function') {
        const pointComponent = resolvedObject;
        const name = pointComponent.getDisplayName();
        const units = getUnitsFromPoint(pointComponent);

        pointComponent.tags().then(tags => {
          const historyTag = tags.get('n:history');
          if (historyTag) {
            let historyPath = String(historyTag);
            if (!historyPath.startsWith('history:')) { historyPath = 'history:' + historyPath; }
            addSeries({ name, units, ord: historyPath });
          } else {
            console.warn('[PieChart Widget] Point has no n:history tag:', String(bOrd));
            this._debouncedRefresh();
          }
        });
      } else {
        console.warn('[PieChart Widget] Received an unknown or unsupported object type for binding:', resolvedObject);
        this._debouncedRefresh();
      }
    };
2 Likes

Awesome! Thanks for sharing mate :pinched_fingers:.