API Charts
This is the official documentation of the forestadmin-agent-django and forestadmin-agent-flask Python agents.
API charts in the legacy agents were declared using routes.
In the new agent, you will need to use either the agent.add_chart or the collection.add_chart function, depending on if the chart is to be displayed on a record of a collection or a dashboard.
Code cheatsheet
route in urls.py
agent.add_chart collection.add_chart(...)
render Chart
return result_builder.value(...) return result_builder.distribution(...)
request object
context.record_id
How to migrate
Migrating should be straightforward: the only differences are that:
dashboard charts are now declared using the
agent.add_chartfunction.collection charts are now declared using the
collection.add_chartfunction, and access the record id usingcontext.record_idinstead ofrequest.query?.record_id.Both types should use the
resultBuilderhelper to return the chart data.
Define a new route in
urls.py:Setup an action into a controller
from django_forest.utils.views.base import BaseView
class MrrChartView(BaseView):
def post(self, request):
mrr = 0
stripe = StripeClient("sk_AABBCCDD11223344")
for charge in stripe.charges.all({"limit": 3}):
mrr += charge.amount
res = {
"data": {
"attributes": {
"value": {"countCurrent": mrr}
},
"type": "stats",
"id": uuid.uuid4(),
}
}
return JsonResponse(status=200, data=res)
from forestadmin.datasource_toolkit.context.agent_context import AgentCustomizationContext
from forestadmin.datasource_toolkit.decorators.chart.result_builder import ResultBuilder
def mrr_chart(context: AgentCustomizationContext, result_builder: ResultBuilder):
mrr = 0
stripe = StripeClient("sk_AABBCCDD11223344")
for charge in stripe.charges.all({"limit": 3}):
mrr += charge.amount
return result_builder.value(mrr)
agent.add_chart("monthlyRecuringRevenue", mrr_chart)Last updated
Was this helpful?