Postgraphs Documentation

Declarative HTML Components

Installation

Quick Start

Formatting JSON

Styling Charts

Installation

Include postgraphs.js in your html

<script src="https://postgraphs.com/v1/postgraphs.js" defer></script>

(The defer attribute will make sure postgraphs loads asynchronously and waits for the page to finish loading before drawing charts.)

Quick Start

Formatting JSON

Postgraphs has two methods of getting data pg-data-path and pg-data.

Attribute Description Examples
pg-data-path A path to a json file or endpoint. Postgraphs will fetch the data and render the chart. <div pg-data-path="/data/user_signups.json" ...
<div pg-data-path="/data/user_signups/" ...
pg-data A json object with the data to be charted, perfect for server side rendering. (Note that json requires double quotes.) <div pg-data='[{"month":"2021-08-01", "signups":29}, {"month":"2021-09-01", "signups":59}]' ...

Use attributes to tell Postgraphs which keys to use for looking up values in the json objects. See the examples page for examples.

Attribute Description Examples
pg-x The key of the objects to use for the x axis. <div pg-chart="bar" pg-x="month"
<div pg-chart="scatter" pg-x="country"
pg-y Comma separated keys to use for the y axis. <div pg-chart="line" pg-y="logins"
<div pg-chart="bar" pg-y="signups,cancellations"
pg-series In addition to providing a value for pg-y, you can provide a pg-series. <div pg-chart="line" pg-y="signups" pg-series="country"
<div pg-chart="bar" pg-y="sales" pg-series="product"
pg-r The key of the objects to use for the bubble radius. <div pg-chart="bubble" pg-r="GDP"

Postgraphs expects the JSON to be an array of data points OR an object with a key "results" which is an array of data points.

Valid
[
  {"month":"2021-07-01", "signups":29},
  {"month":"2021-08-01", "signups":39},
  {"month":"2021-09-01", "signups":59}
]
Valid
{
  "results": [
    {"month":"2021-07-01", "signups":29},
    {"month":"2021-08-01", "signups":39},
    {"month":"2021-09-01", "signups":59}
  ]
}

Each data point should contain a key for the x-axis and y-axis. When there are multiple y-axes, they can be in the same object or separate objects.

<div pg-x="month" pg-y="signups,cancellations"></div>
Valid
[
  {"month":"2021-07-01", "signups":29, "cancellations":4},
  {"month":"2021-08-01", "signups":39, "cancellations":5},
  {"month":"2021-09-01", "signups":59, "cancellations":7}
]
Valid
[
  {"month":"2021-07-01", "signups":29},
  {"month":"2021-07-01", "cancellations":4},
  {"month":"2021-08-01", "signups":39},
  {"month":"2021-08-01", "cancellations":5}
]

Styling Charts

Attribute Description Examples
pg-chart The type of chart
  • bar
  • line
  • scatter
  • bubble
  • pie
  • doughnut
<div pg-chart="bar" pg-x="month"
<div pg-chart="scatter" pg-x="month"
<div pg-chart="bubble" pg-r="population"
pg-stacked Set to true if bar charts should be stacked. <div pg-chart="bar" pg-chart="true" pg-x="month"
pg-title Provide a title for your chart
<div pg-title="Weekly Signups by Continent"
<div pg-title="UFO Sightings by Quarter"
pg-colors Postgraphs comes with built-in color palettes or you can provide an array of colors.
  • pg-blue
  • pg-green (coming soon)
  • pg-pink (coming soon)
  • pg-orange (coming soon)
  • pg-mocha (coming soon)
  • pg-doughnut (coming soon)
<div pg-colors="blue"
<div pg-colors="['#264653', '#2a9d8f', '#e9c46a', ...]"