Chart
The /chart endpoint generates charts from data. It supports bar, line, pie, and donut chart types, and returns either an SVG image or raw JSON data.
POST/v5/chart
Parameters
The request body must be JSON:
| Parameter | Required | Description |
|---|---|---|
type | Yes | Chart type: bar, line, pie, or donut |
data | Yes | Chart data object (see structure below) |
title | No | Chart title displayed above the chart |
width | No | SVG width in pixels (responsive if omitted) |
height | No | SVG height in pixels (responsive if omitted) |
colors | No | Array of hex colors for datasets (defaults to a built-in palette) |
bg | No | Background fill color (hex, transparent if omitted) |
legend | No | Show legend: true (default) or false |
mode | No | Response format: "svg" (default, returns SVG) or "data" (returns JSON) |
Data Format
data must be a JSON object (not a string) in the request body. The structure depends on the chart type.
Bar and line charts:
json
{
"labels": ["Jan", "Feb", "Mar"],
"datasets": [
{ "label": "Sales", "values": [120, 200, 150] },
{ "label": "Costs", "values": [80, 95, 110] }
]
}Pie and donut charts:
json
{
"labels": ["Jan", "Feb", "Mar"],
"values": [120, 200, 150]
}labels: array of strings — max 20 labelsdatasets(bar/line): array of dataset objects — max 5 datasets, each with alabeland avaluesarray matching the length oflabelsvalues(pie/donut): single array of non-negative numbers matching the length oflabels
The
datafield must be a JSON object, not a JSON string. Send the request withContent-Type: application/json.
Response
- When
modeis"svg"(default): returnsContent-Type: image/svg+xmlwith the SVG body - When
modeis"data": returns a JSON object with the chart data
Code Examples
curl -X POST \
-H "Content-Type: application/json" \
-d '{
"type": "bar",
"data": {
"labels": [
"Jan",
"Feb",
"Mar"
],
"datasets": [
{
"label": "Sales",
"values": [
120,
200,
150
]
}
]
},
"title": "Monthly Sales"
}' \
"https://api.sylvain.sh/v5/chart"Try It
Error Handling
If parameters are missing or invalid, the API will return an error:
| Error Message | Description |
|---|---|
Please provide a valid chart type (?type=bar|line|pie|donut) | The type parameter is missing or invalid |
data must contain a labels array | The data.labels field is missing |
data must contain a datasets array | The data.datasets field is missing |
Too many labels (max 20) | The labels array exceeds 20 entries |
Too many datasets (max 5) | The datasets array exceeds 5 entries |
Each dataset must have the same number of values as labels | Dataset length mismatch |