Skip to content
moxon home

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.

posthttps://api.moxon.dev/v5/chart
Try itpost/v5/chart

Parameters

The request body must be JSON:

  • typestringrequired

    Chart type: bar, line, pie, or donut

    • bar
    • line
    • pie
    • donut
  • datajsonrequired

    Chart data object (see structure below)

  • titlestringoptional

    Chart title displayed above the chart

  • widthnumberoptional

    SVG width in pixels (responsive if omitted)

  • heightnumberoptional

    SVG height in pixels (responsive if omitted)

  • colorsstringoptional

    Array of hex colors for datasets (defaults to a built-in palette)

  • bgstringoptional

    Background fill color (hex, transparent if omitted)

  • legendbooleanoptional

    Show legend: true (default) or false

  • mode"svg" | "data"optional

    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:

{
    "labels": ["Jan", "Feb", "Mar"],
    "datasets": [
        { "label": "Sales", "values": [120, 200, 150] },
        { "label": "Costs", "values": [80, 95, 110] }
    ]
}

Pie and donut charts:

{
    "labels": ["Jan", "Feb", "Mar"],
    "values": [120, 200, 150]
}
  • labels: array of strings — max 20 labels
  • datasets (bar/line): array of dataset objects — max 5 datasets, each with a label and a values array matching the length of labels
  • values (pie/donut): single array of non-negative numbers matching the length of labels

The data field must be a JSON object, not a JSON string. Send the request with Content-Type: application/json.

Response

  • When mode is "svg" (default): returns Content-Type: image/svg+xml with the SVG body
  • When mode is "data": returns a JSON object with the chart data

Code Examples

Error Handling

If parameters are missing or invalid, the API will return an error:

  • 400Please provide a valid chart type (?type=bar|line|pie|donut)The type parameter is missing or invalid
  • 400data must contain a labels arrayThe data.labels field is missing
  • 400data must contain a datasets arrayThe data.datasets field is missing
  • 400Too many labels (max 20)The labels array exceeds 20 entries
  • 400Too many datasets (max 5)The datasets array exceeds 5 entries
  • 400Each dataset must have the same number of values as labelsDataset length mismatch