---
title: "Show an interactive weight goal chart in a paywall"
description: "Display the projected weight of each user as a chart in a paywall flow. Users drag along the chart to see the weight for any week."
---

> For the complete documentation index, see [llms.txt](https://adapty.io/docs/llms.txt).

> **AI agents**: to search Adapty docs faster and with fewer tokens, install the Adapty skills: `npx adapty skills install --yes`

Follow this recipe to display the projected weight of each user as a chart in a paywall flow. The user enters their current and goal weight in an onboarding flow. Your C# code collects both values and sends them to your native code. The chart shows the weekly progress that the user can expect with your app, from one value to the other. Your app renders the chart as a native view in a [custom element](flow-custom-elements). On iOS, the view uses Swift Charts. On Android, the view draws the chart without a chart library.

This recipe reuses the flow setup and the native views of the [iOS recipe](custom-element-interactive-chart) and the [Android recipe](android-custom-element-interactive-chart). Each step of this recipe says what to copy from them, and what to do differently in a Unity project.

## Before you start

- Update your app to Adapty Unity SDK 4.4 or later. Earlier SDK versions show the [fallback content](flow-custom-elements#set-fallback-content) for every custom element.
- Set up your app to [fetch](unity-get-pb-paywalls) and [display](unity-present-paywalls) flows.
- Set up your app for custom elements, as in [Display custom elements with the Unity SDK](unity-flow-custom-elements). This recipe adds code to the C# and Java `CustomElements` classes from that guide.
- On iOS, the drag along the chart needs iOS 17 or later. On iOS 16, users see the line, but can't drag along it.

Create two separate flows — one for the onboarding, and one for the paywall. Add each to a [placement](placements). The code in this recipe fetches the paywall flow from the placement with the ID `weight_paywall`.

## 1. Set up the onboarding flow and the paywall flow

Set up both flows as in steps 1 and 2 of the [iOS recipe](custom-element-interactive-chart#1-ask-for-the-current-and-goal-weight-in-the-onboarding-flow): two **Number** inputs in the onboarding flow, and a custom element for the chart in the paywall flow.

## 2. Add the native code

The iOS and Android recipes attach a resolver to the paywall flow. The Unity SDK has no API for a resolver of one flow. Instead, your app-wide resolver reads the weight plan from a store, and your C# code fills the store before the paywall flow opens.

### iOS

In a Swift file in `Assets/Plugins/iOS`, add the weight plan and the chart view from step 4 of the [iOS recipe](custom-element-interactive-chart#4-create-the-chart-view). Then add a store for the weight plan, a resolver that reads the store, and two functions that your C# code calls. `RegisterCustomElements` sets the resolver at launch, and `SetWeightPlan` fills the store:

```swift showLineNumbers title="Assets/Plugins/iOS/CustomElements.swift"
import AdaptyUI
import AdaptyUIBuilder
import SwiftUI

// the weight plan that your C# code sends before the paywall flow opens
@MainActor
final class WeightPlanStore {
    var plan: WeightPlan?
}

struct CustomElementsResolver: AdaptyUICustomElementsResolver {
    let weightPlanStore: WeightPlanStore

    func view(
        forType type: String,
        id: String,
        context: AdaptyUICustomElementContext
    ) -> AnyView? {
        switch type {
        case "line_chart" where id == "weight_goal":
            guard #available(iOS 16.0, *), let plan = weightPlanStore.plan else {
                return nil // no weight plan: show the fallback
            }
            return AnyView(WeightGoalChart(entries: plan.entries))
        // the other cases of your resolver
        default:
            return nil
        }
    }
}

@MainActor let weightPlanStore = WeightPlanStore()

// Called once from C# at launch, before any flow is created.
@_cdecl("RegisterCustomElements")
public func RegisterCustomElements() {
    Task { @MainActor in
        AdaptyUI.setCustomElementsResolver(CustomElementsResolver(weightPlanStore: weightPlanStore))
    }
}

// Called from C# before the paywall flow opens.
@_cdecl("SetWeightPlan")
public func SetWeightPlan(_ currentWeight: Double, _ goalWeight: Double) {
    Task { @MainActor in
        weightPlanStore.plan = WeightPlan(currentWeight: currentWeight, goalWeight: goalWeight)
    }
}
```

### Android

Copy the `WeightPlan`, `WeightEntry`, and `WeightGoalChartView` classes from the **Java** tab of step 4 of the [Android recipe](android-custom-element-interactive-chart#4-create-the-chart-view). Save each class in its own file in `Assets/Plugins/Android`, such as `WeightPlan.java`. Start each file with `package com.example.app;` and the imports that its class uses.

In `CustomElements.java`, set a resolver that reads the weight plan, and add a `setWeightPlan` method for your C# code:

```java showLineNumbers title="Assets/Plugins/Android/CustomElements.java"
package com.example.app;

import android.content.Context;
import com.adapty.ui.AdaptyCustomElementContent;
import com.adapty.ui.AdaptyUI;

public final class CustomElements {
    // the weight plan that your C# code sends before the paywall flow opens
    private static volatile WeightPlan weightPlan;

    // Called once from C# at launch, before any flow is created.
    // This resolver doesn't use the context.
    public static void register(Context context) {
        AdaptyUI.setCustomElementsResolver((type, id) -> {
            WeightPlan plan = weightPlan;
            if ("line_chart".equals(type) && "weight_goal".equals(id) && plan != null) {
                return AdaptyCustomElementContent.view(
                        (viewContext, elementContext) -> new WeightGoalChartView(viewContext, plan.entries)
                );
            }
            return null; // no weight plan, or another custom element: show the fallback
        });
    }

    // Called from C# before the paywall flow opens.
    public static void setWeightPlan(double currentWeight, double goalWeight) {
        weightPlan = new WeightPlan(currentWeight, goalWeight);
    }
}
```

Until your C# code sends the current and goal weight, the resolver returns no view, and users see the fallback.

## 3. Send the weights from your C# code

In the C# `CustomElements` class, add a `SendWeightPlan` method that passes both weights to your native code. The highlighted lines show what to add to the class from the [Unity guide](unity-flow-custom-elements#pass-the-resolver-to-the-sdk):

```csharp showLineNumbers title="Assets/Scripts/CustomElements.cs" {9-10,29-40}
using UnityEngine;

public static class CustomElements
{
#if UNITY_IOS && !UNITY_EDITOR
    [System.Runtime.InteropServices.DllImport("__Internal")]
    private static extern void RegisterCustomElements();

    [System.Runtime.InteropServices.DllImport("__Internal")]
    private static extern void SetWeightPlan(double currentWeight, double goalWeight);
#endif

    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    private static void Register()
    {
#if UNITY_IOS && !UNITY_EDITOR
        RegisterCustomElements();
#elif UNITY_ANDROID && !UNITY_EDITOR
        using (var player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
        using (var activity = player.GetStatic<AndroidJavaObject>("currentActivity"))
        using (var context = activity.Call<AndroidJavaObject>("getApplicationContext"))
        using (var resolver = new AndroidJavaClass("com.example.app.CustomElements"))
        {
            resolver.CallStatic("register", context);
        }
#endif
    }

    // call before the paywall flow opens
    public static void SendWeightPlan(double currentWeight, double goalWeight)
    {
#if UNITY_IOS && !UNITY_EDITOR
        SetWeightPlan(currentWeight, goalWeight);
#elif UNITY_ANDROID && !UNITY_EDITOR
        using (var customElements = new AndroidJavaClass("com.example.app.CustomElements"))
        {
            customElements.CallStatic("setWeightPlan", currentWeight, goalWeight);
        }
#endif
    }
}
```

Collect the current and goal weight from the onboarding flow with the `FlowViewDidReceiveAnalyticEvent` method of the BMI example in [Pass data to flows](unity-flow-custom-tags#pass-the-bmi-to-the-paywall). `FlowViewDidDisappear` runs only after your app dismisses the onboarding flow, so dismiss it on the **Close flow** action, as in [Close flows and paywalls](unity-handle-paywall-actions#close-flows-and-paywalls). When the user closes the onboarding flow after its last screen, send both weights to your native code, and open the paywall flow:

```csharp showLineNumbers title="Unity"
public void FlowViewDidDisappear(AdaptyUIFlowView view) {
    // The listener receives events from every flow. Reset `bodyAnswers` and
    // `reachedLastScreen` whenever a flow disappears, so that the paywall
    // doesn't open again when the user closes it.
    var answers = new Dictionary<string, string>(bodyAnswers);
    var finished = reachedLastScreen;
    bodyAnswers.Clear();
    reachedLastScreen = false;

    // Number inputs send their values as strings.
    if (!finished) return;
    if (!answers.TryGetValue("weight", out var currentText)
        || !double.TryParse(currentText, out var currentWeight)) return;
    if (!answers.TryGetValue("goal_weight", out var goalText)
        || !double.TryParse(goalText, out var goalWeight)) return;

    // the resolver reads the weights when the paywall flow opens
    CustomElements.SendWeightPlan(currentWeight, goalWeight);
    ShowWeightPaywall();
}

private void ShowWeightPaywall() {
    Adapty.GetFlow("weight_paywall", (flow, flowError) => {
        if (flowError != null) {
            // handle the error
            return;
        }

        AdaptyUI.CreateFlowView(flow, new AdaptyUICreateFlowViewParameters(), (view, viewError) => {
            if (viewError != null) {
                // handle the error
                return;
            }

            view.Present((presentError) => {
                if (presentError != null) {
                    // handle the error
                }
            });
        });
    });
}
```

## 4. Test on a device

Test both flows as in step 6 of the [iOS](custom-element-interactive-chart#6-test-on-a-device) and [Android](android-custom-element-interactive-chart#6-test-on-a-device) recipes. To test the fallback, make the resolver return no view for the `weight_goal` ID, and open the paywall flow again.

To find out why the fallback appears instead of the chart, follow the instructions in the [Test on a device](unity-flow-custom-elements#test-on-a-device) section of the Unity guide.