---
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."
---

> **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 app collects both values. 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 Swift Charts view in a [custom element](flow-custom-elements).

:::tip
This recipe only covers iOS apps. Apps built with other frameworks only display the custom elements' [fallback content](flow-custom-elements#set-fallback-content). Support for Android and cross-platform frameworks is upcoming.
:::

## Before you start

- Update your app to Adapty iOS 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](get-pb-paywalls) and [display](ios-present-paywalls) flows.
- 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. Ask for the current and goal weight in the onboarding flow

In the Flow & Paywall Builder, add two [**Number** inputs](builder-inputs-and-forms) to the onboarding flow:

- One for the current weight in kilograms, with the **Element ID** `weight`.
- One for the goal weight in kilograms, with the **Element ID** `goal_weight`.

To make your app read the weights the same way in every locale, select **Integer** from the **Format** dropdown of both inputs. An input sends its value to your app as text, exactly as the user types it. The code in this recipe reads "70.5" and "70,5" differently. Whole numbers have no decimal separator.

To make sure that your app receives both values, end the onboarding flow with a screen that has no inputs. On that screen, add a button with the [**Close flow**](onboarding-actions#close-flow) action. Inputs send their values when they lose focus or when the user moves to another screen.

## 2. Add the custom element to the paywall screen

In the paywall flow, [add a custom element](flow-custom-elements#add-and-configure) to a screen, and set it up for the chart:

- **Type**: `line_chart`.
- **ID**: `weight_goal`.
- **Height**: Keep the default **Fixed** height. The chart fills the custom element.

Keep the custom element out of containers that respond to touch, such as a carousel. Otherwise, a drag along the chart also moves the carousel.

Inside the custom element, add an [**Image**](custom-media#image) with a sample weight chart. Users see it as the [fallback](flow-custom-elements#set-fallback-content) when their version of your app doesn't display the chart.

## 3. Save the current and goal weight in your app

Collect the current and goal weight from the `flow_user_input` events of the onboarding flow, as in [Process data from flows](ios-flow-input). To know that the user reached the last screen, watch for a `flow_screen_showed` event with `is_last_screen` set to `true`. When the onboarding flow closes after that screen, pass both weights to `showWeightPaywall`, the method that [opens the paywall flow with the chart](#5-match-the-chart-to-the-custom-element).

The following code is for UIKit. One view controller is the delegate of both flows. It must also implement the purchase and restore methods of the `AdaptyFlowControllerDelegate` protocol. This example leaves them out. To see them, go to [Present flows and paywalls in UIKit](ios-present-paywalls#present-flows-and-paywalls-in-uikit).

<Tabs groupId="current-os" queryString>
<TabItem value="ios" label="iOS" default>

```swift showLineNumbers title="Swift"
private var enteredWeights: [String: String] = [:]
private var reachedLastScreen = false

func flowController(
    _ controller: AdaptyFlowController,
    didReceiveAnalyticEvent name: String,
    params: [String: any Sendable]
) {
    if name == "flow_screen_showed", params["is_last_screen"] as? Bool == true {
        reachedLastScreen = true
        return
    }
    guard name == "flow_user_input",
          let elementId = params["element_id"] as? String,
          let value = params["value"] as? String
    else { return }

    enteredWeights[elementId] = value
}

func flowControllerDidDisappear(_ controller: AdaptyFlowController) {
    // This delegate receives events from both flows. Reset the entered
    // weights whenever a flow disappears, so that the paywall doesn't open
    // again when the user closes it.
    let weights = enteredWeights
    let finished = reachedLastScreen
    enteredWeights.removeAll()
    reachedLastScreen = false

    // Number inputs send their values as strings
    guard finished,
          let currentWeight = weights["weight"].flatMap({ Double($0) }),
          let goalWeight = weights["goal_weight"].flatMap({ Double($0) })
    else { return }

    Task {
        await showWeightPaywall(currentWeight: currentWeight, goalWeight: goalWeight)
    }
}
```

</TabItem>
</Tabs>

In SwiftUI, use the `didReceiveAnalyticEvent` and `didDisappear` closures of the `.flow` modifier instead of the delegate methods. Present the paywall flow with a second `.flow` modifier.

## 4. Create the chart view

Create a model of a weight plan, and a SwiftUI view with the chart. The plan has one entry per week, from the current weight to the goal weight. When the user drags along the chart, the view marks the week at the touch point, and displays the projected weight for that week:

<Tabs groupId="current-os" queryString>
<TabItem value="ios" label="iOS" default>

```swift showLineNumbers title="Swift"

struct WeightPlan {
    let entries: [WeightEntry]

    // One entry per week, from the current weight to the goal weight
    init(currentWeight: Double, goalWeight: Double) {
        let weeklyChange = 0.5 // kg per week
        let startDate = Date.now
        let weeks = Int((abs(goalWeight - currentWeight) / weeklyChange).rounded(.up))
        let step = goalWeight < currentWeight ? -weeklyChange : weeklyChange
        entries = (0...weeks).map { week in
            WeightEntry(
                date: startDate.addingTimeInterval(Double(week) * 7 * 24 * 60 * 60),
                kilograms: week == weeks ? goalWeight : currentWeight + step * Double(week)
            )
        }
    }
}

struct WeightEntry: Identifiable {
    let date: Date
    let kilograms: Double
    var id: Date { date }
}

@available(iOS 16.0, *) // Swift Charts needs iOS 16
struct WeightGoalChart: View {
    let entries: [WeightEntry]
    @State private var selectedDate: Date?

    // The entry closest to the date that the user drags to
    private var selectedEntry: WeightEntry? {
        guard let selectedDate else { return nil }
        return entries.min {
            abs($0.date.timeIntervalSince(selectedDate)) < abs($1.date.timeIntervalSince(selectedDate))
        }
    }

    var body: some View {
        if #available(iOS 17.0, *) {
            chart.chartXSelection(value: $selectedDate)
        } else {
            chart // iOS 16: no drag selection
        }
    }

    private var chart: some View {
        Chart {
            ForEach(entries) { entry in
                LineMark(
                    x: .value("Date", entry.date),
                    y: .value("Weight", entry.kilograms)
                )
            }
            if let selectedEntry {
                RuleMark(x: .value("Date", selectedEntry.date))
                    .foregroundStyle(.gray.opacity(0.3))
                PointMark(
                    x: .value("Date", selectedEntry.date),
                    y: .value("Weight", selectedEntry.kilograms)
                )
                .annotation(position: .top) {
                    Text("\(selectedEntry.kilograms, specifier: "%.1f") kg")
                        .font(.caption.bold())
                }
            }
        }
        .chartYScale(domain: .automatic(includesZero: false))
    }
}
```

</TabItem>
</Tabs>

The plan assumes that the weight changes by 0.5 kg per week. To use another rate, edit `weeklyChange`.

The drag along the chart needs iOS 17 or later. On iOS 16, users see the line, but can't drag along it.

## 5. Match the chart to the custom element

Write a [resolver](ios-flow-custom-elements#write-a-resolver-to-match-custom-elements-and-views) that returns the chart view for the `line_chart` Type and the `weight_goal` ID. Store the weight plan in a property of the resolver:

<Tabs groupId="current-os" queryString>
<TabItem value="ios" label="iOS" default>

```swift showLineNumbers title="Swift"

@available(iOS 16.0, *) // the chart needs iOS 16
struct WeightChartResolver: AdaptyUICustomElementsResolver {
    let plan: WeightPlan

    func view(
        forType type: String,
        id: String,
        context: AdaptyUICustomElementContext
    ) -> AnyView? {
        guard type == "line_chart", id == "weight_goal" else {
            return nil // another custom element: show the fallback
        }
        return AnyView(WeightGoalChart(entries: plan.entries))
    }
}
```

</TabItem>
</Tabs>

Attach the resolver to the paywall flow when you fetch its configuration. Create the resolver with the weight plan from the current and goal weight:

<Tabs groupId="current-os" queryString>
<TabItem value="ios" label="iOS" default>

```swift showLineNumbers title="Swift"
func showWeightPaywall(currentWeight: Double, goalWeight: Double) async {
    let plan = WeightPlan(currentWeight: currentWeight, goalWeight: goalWeight)
    do {
        let flow = try await Adapty.getFlow(placementId: "weight_paywall")
        let flowConfiguration = try await AdaptyUI.getFlowConfiguration(
            forFlow: flow,
            customElementsResolver: WeightChartResolver(plan: plan)
        )
        let flowController = try AdaptyUI.flowController(
            with: flowConfiguration,
            delegate: self
        )
        present(flowController, animated: true)
    } catch {
        // handle the error
    }
}
```

</TabItem>
</Tabs>

If your app sets an [app-wide resolver](ios-flow-custom-elements#set-an-app-wide-resolver), this resolver replaces it in the paywall flow. `WeightChartResolver` returns `nil` for any other Type and ID, because the paywall flow in this recipe has no other custom elements. If your paywall flow has other custom elements, pass the app-wide resolver to this one. Then call the app-wide resolver for their Types and IDs, as in [Attach a resolver to one flow](ios-flow-custom-elements#attach-a-resolver-to-one-flow).

## 6. Test on a device

The Flow & Paywall Builder can't run your app code, so its canvas only shows the fallback. To preview the chart:

1. [Publish both flows](builder-save-publish). To see the latest published versions instead of cached ones, register the device as a [test device](test-devices).
2. Run a build of your app that includes the resolver, on iOS 17 or later. Complete the onboarding flow with a current and a goal weight. Verify that the line of the chart starts at the current weight and ends at the goal weight.
3. Drag along the chart, and verify that the projected weight for the week under your finger appears above the line.

To test the fallback, remove the `customElementsResolver` argument from the `getFlowConfiguration` call, 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](ios-flow-custom-elements#test-on-a-device) section of the iOS guide.