abstra-docs
Search…
Adding charts
Using something like Google Charts you can easily build dashboards inside Abstra. Let's build the following line chart as an example.
We have to start loading an external script from Google Chart docs when our page is loaded.
The Load external script action is configured with the loader script URL and blocking any user interaction until it's loaded. You can read further on how to setup this kind of action here.
Then you should add an HTML element to your page where the chart will be rendered by dragging and dropping it to your canvas.
Then you can set your HTML content to be a div element with an id of my-chart and configure it's width and height with some custom CSS.
Consider we have some data at our sales variable with the following format:
1
[
2
{
3
"date": "2020-01-01T03:00:00.000Z",
4
"amount": 213
5
},
6
{
7
"date": "2020-02-01T03:00:00.000Z",
8
"amount": 386
9
},
10
...
11
]
Copied!
Then we can use this data adding a script based on the docs to render a line chart using the Execute JavaScript code action. You can read more about this action here.
The code we added is the following:
1
google.charts.load('current', {packages: ['line', 'corechart']});
2
google.charts.setOnLoadCallback(drawChart);
3
4
function drawChart() {
5
var data = new google.visualization.DataTable();
6
data.addColumn('date', 'Month');
7
data.addColumn('number', "Sales");
8
data.addRows(
9
$.sales.map(d => [new Date(d.date), d.amount])
10
);
11
var options = {
12
legend: { position: 'bottom' }
13
};
14
var chart = new google.charts.Line(
15
document.getElementById('my-chart')
16
);
17
18
chart.draw(data, options);
19
}
Copied!
In this code we add all the basic setup from the docs and add the data rows using our sales variable
1
data.addRows(
2
$.sales.map(d => [new Date(d.date), d.amount])
3
);
Copied!
Then we render the line chart inside our HTML element we added earlier
1
var chart = new google.charts.LineChart(
2
document.getElementById('my-chart')
3
);
4
5
chart.draw(data, options);
Copied!
And after clicking to preview our application this is the end result
If you need to debug your draw chart action, you can always add a Show message action to catch errors and fix your chart.
Last modified 14d ago
Copy link