blockcharts alpha

Gallery / Dashboards and reports /

Sales report from a CSV

A report from raw CSV rows: monthly totals, ranking, currency.

90.2 KB of code · core 29.5 + 15 blocks 60.7

Revenue per month

Revenue doubled from January to May, to $8,800, with a dip in April.

How it is made: The aggregate transform groups the rows (here by calendar month) and sums the amounts; the axes and the tooltip write them as currency with format.

Revenue by region, largest first

North brings the most revenue, $11,400 of $32,000.

What the report is made of

The composer reads the JSON below and puts into the page the core and only the blocks its charts use: 15 of the 59, 90.2 KB of code, minified, which is 42% of the 215.9 KB that the core and every block weigh together. A block that two charts use is carried once. The report file is 98.2 KB: that code, the data, the styles and the page around them.

BlockWhat it doesShare of the codeKB
core.composed-plain
A report with plain data: no checks, no decoder. Most reports.
A report with plain data: no checks, no decoder. Most reports. 29.5
lib.curve
The path of a line: straight segments, a smooth curve that never overshoots, or steps. Shared by line and area.
The path of a line: straight segments, a smooth curve that never overshoots, or steps. Shared by line and area. 1.2
lib.pointer
Where the pointer is on the chart, and following a mouse or a finger once a frame. Shared by tooltip, crosshair, zoom and brush.
Where the pointer is on the chart, and following a mouse or a finger once a frame. Shared by tooltip, crosshair, zoom and brush. 1.7
lib.ticks
Round steps and ticks (1, 2 or 5 × 10ⁿ). Shared by the number scales and the histogram bins.
Round steps and ticks (1, 2 or 5 × 10ⁿ). Shared by the number scales and the histogram bins. 0.7
scale.band
One band per category: the axis of bar charts.
One band per category: the axis of bar charts. 1.2
scale.linear
Numbers to positions, with round ticks.
Numbers to positions, with round ticks. 1.6
scale.time
Dates to positions, ticks in days, months or years (UTC).
Dates to positions, ticks in days, months or years (UTC). 4.1
transform.aggregate
Group-by: sum, mean, count and more per group; dates by month or quarter; top N groups.
Group-by: sum, mean, count and more per group; dates by month or quarter; top N groups. 5.3
mark.line
A line per series, with optional end labels.
A line per series, with optional end labels. 4.4
mark.point
Dots, bubbles and beeswarms.
Dots, bubbles and beeswarms. 3.9
mark.rect
Rectangles: bars, grouped bars, ranges, heatmap cells; the hexagons of a hexbin.
Rectangles: bars, grouped bars, ranges, heatmap cells; the hexagons of a hexbin. 4.5
mark.text
Value labels that shrink, or step aside, when they do not fit.
Value labels that shrink, or step aside, when they do not fit. 5.3
guide.axis
Axes: formatted labels, a title, long categories wrapped.
Axes: formatted labels, a title, long categories wrapped. 11.7
guide.grid
Grid lines at the ticks.
Grid lines at the ticks. 0.6
interaction.tooltip
The values under the pointer.
The values under the pointer. 10.7
renderer.svg
Draws into SVG: sharp, selectable, prints well. The default.
Draws into SVG: sharp, selectable, prints well. The default. 3.9
TotalThe core 29.5 and 15 blocks 60.790.2

The JSON

This is the whole input. data holds the datasets, each chart is a spec that names one. In a checkout of the repository: node scripts/compose.js skills/blockcharts/recipes/report-from-csv.json report.html. Saved as page.json, it composes the same with the skill's own runtime/compose.js.

report-from-csv.json

{
  "title": "Sales report from a CSV",
  "data": {
    "orders": {
      "csv": "date,region,product,amount\n2024-01-05,North,Chairs,1200\n2024-01-19,South,Tables,2300\n2024-01-28,East,Chairs,900\n2024-02-03,North,Tables,3100\n2024-02-14,West,Chairs,1500\n2024-02-25,South,Chairs,1100\n2024-03-02,East,Tables,2700\n2024-03-11,North,Chairs,1800\n2024-03-21,West,Tables,3300\n2024-04-06,South,Tables,2900\n2024-04-17,North,Chairs,1400\n2024-04-30,East,Chairs,1000\n2024-05-08,West,Tables,3600\n2024-05-19,North,Tables,3900\n2024-05-27,South,Chairs,1300"
    }
  },
  "charts": [
    {
      "title": "Revenue per month",
      "note": "Revenue doubled from January to May, to $8,800, with a dip in April.",
      "how": "The aggregate transform groups the rows (here by calendar month) and sums the amounts; the axes and the tooltip write them as currency with `format`.",
      "spec": {
        "data": "orders",
        "size": [640, 300],
        "transforms": [
          {
            "type": "aggregate",
            "groupby": [{ "field": "date", "unit": "month", "as": "month" }],
            "measures": [{ "op": "sum", "field": "amount", "as": "revenue" }],
            "sort": "month"
          }
        ],
        "scales": { "x": { "type": "time" }, "y": { "type": "linear", "zero": true } },
        "guides": [
          { "type": "grid", "scale": "y" },
          { "type": "axis", "scale": "x", "format": { "month": "short" }, "count": 12, "anchor": "end" },
          { "type": "axis", "scale": "y", "title": "Revenue", "format": { "style": "currency", "currency": "USD", "maximumFractionDigits": 0 } }
        ],
        "marks": [
          { "type": "line", "x": "month", "y": "revenue", "stroke": "var(--bc-c0, #3a6fe4)" },
          { "type": "point", "x": "month", "y": "revenue", "fill": "var(--bc-c0, #3a6fe4)" }
        ],
        "interaction": [
          { "type": "tooltip", "labels": { "revenue": "Revenue" }, "formats": { "month": { "month": "long", "year": "numeric" }, "revenue": { "style": "currency", "currency": "USD", "maximumFractionDigits": 0 } } }
        ]
      }
    },
    {
      "title": "Revenue by region, largest first",
      "note": "North brings the most revenue, $11,400 of $32,000.",
      "spec": {
        "data": "orders",
        "size": [640, 300],
        "transforms": [
          {
            "type": "aggregate",
            "groupby": ["region"],
            "measures": [{ "op": "sum", "field": "amount", "as": "revenue" }],
            "sort": "revenue",
            "order": "desc"
          }
        ],
        "scales": { "x": { "type": "band" }, "y": { "type": "linear", "zero": true } },
        "guides": [
          { "type": "grid", "scale": "y" },
          { "type": "axis", "scale": "x" },
          { "type": "axis", "scale": "y", "format": { "style": "currency", "currency": "USD", "notation": "compact" } }
        ],
        "marks": [
          { "type": "rect", "x": "region", "y": "revenue", "radius": 2 },
          { "type": "text", "x": "region", "y": "revenue", "text": "revenue", "format": { "style": "currency", "currency": "USD", "notation": "compact", "maximumFractionDigits": 1 }, "baseline": "auto", "dy": -5 }
        ],
        "interaction": [{ "type": "tooltip", "labels": { "revenue": "Revenue" }, "formats": { "revenue": { "style": "currency", "currency": "USD", "maximumFractionDigits": 0 } } }]
      }
    }
  ]
}

The report the composer makes from it is one standalone file (98.2 KB): open it from disk, mail it, print it.

In a page of your own

The charts above use the dashboard kit (55.5 KB gzipped), the smallest one that has the blocks this example needs: lib.curve, lib.pointer, lib.ticks, scale.band, scale.linear, scale.time, transform.aggregate, mark.line, mark.point, mark.rect, mark.text, guide.axis, guide.grid, interaction.tooltip, renderer.svg. The HTML below loads it from the jsDelivr CDN, pinned to version 0.0.5; to serve it from your own site, put its file there (the files of a release) and change the address.

HTML

<script src="https://cdn.jsdelivr.net/npm/blockcharts@0.0.5/release/kits/dashboard.js"></script>

<script type="application/json" id="bc-data">
{"orders":{"columns":{"date":["2024-01-05","2024-01-19","2024-01-28","2024-02-03","2024-02-14","2024-02-25","2024-03-02","2024-03-11","2024-03-21","2024-04-06","2024-04-17","2024-04-30","2024-05-08","2024-05-19","2024-05-27"],"region":["North","South","East","North","West","South","East","North","West","South","North","East","West","North","South"],"product":["Chairs","Tables","Chairs","Tables","Chairs","Chairs","Tables","Chairs","Tables","Tables","Chairs","Chairs","Tables","Tables","Chairs"],"amount":[1200,2300,900,3100,1500,1100,2700,1800,3300,2900,1400,1000,3600,3900,1300]}}}
</script>

<script type="application/json" data-bc-chart>
{
  "data": "orders",
  "size": [
    640,
    300
  ],
  "transforms": [
    {
      "type": "aggregate",
      "groupby": [
        {
          "field": "date",
          "unit": "month",
          "as": "month"
        }
      ],
      "measures": [
        {
          "op": "sum",
          "field": "amount",
          "as": "revenue"
        }
      ],
      "sort": "month"
    }
  ],
  "scales": {
    "x": {
      "type": "time"
    },
    "y": {
      "type": "linear",
      "zero": true
    }
  },
  "guides": [
    {
      "type": "grid",
      "scale": "y"
    },
    {
      "type": "axis",
      "scale": "x",
      "format": {
        "month": "short"
      },
      "count": 12,
      "anchor": "end"
    },
    {
      "type": "axis",
      "scale": "y",
      "title": "Revenue",
      "format": {
        "style": "currency",
        "currency": "USD",
        "maximumFractionDigits": 0
      }
    }
  ],
  "marks": [
    {
      "type": "line",
      "x": "month",
      "y": "revenue",
      "stroke": "var(--bc-c0, #3a6fe4)"
    },
    {
      "type": "point",
      "x": "month",
      "y": "revenue",
      "fill": "var(--bc-c0, #3a6fe4)"
    }
  ],
  "interaction": [
    {
      "type": "tooltip",
      "labels": {
        "revenue": "Revenue"
      },
      "formats": {
        "month": {
          "month": "long",
          "year": "numeric"
        },
        "revenue": {
          "style": "currency",
          "currency": "USD",
          "maximumFractionDigits": 0
        }
      }
    }
  ],
  "alt": "Revenue per month"
}
</script>

<script type="application/json" data-bc-chart>
{
  "data": "orders",
  "size": [
    640,
    300
  ],
  "transforms": [
    {
      "type": "aggregate",
      "groupby": [
        "region"
      ],
      "measures": [
        {
          "op": "sum",
          "field": "amount",
          "as": "revenue"
        }
      ],
      "sort": "revenue",
      "order": "desc"
    }
  ],
  "scales": {
    "x": {
      "type": "band"
    },
    "y": {
      "type": "linear",
      "zero": true
    }
  },
  "guides": [
    {
      "type": "grid",
      "scale": "y"
    },
    {
      "type": "axis",
      "scale": "x"
    },
    {
      "type": "axis",
      "scale": "y",
      "format": {
        "style": "currency",
        "currency": "USD",
        "notation": "compact"
      }
    }
  ],
  "marks": [
    {
      "type": "rect",
      "x": "region",
      "y": "revenue",
      "radius": 2
    },
    {
      "type": "text",
      "x": "region",
      "y": "revenue",
      "text": "revenue",
      "format": {
        "style": "currency",
        "currency": "USD",
        "notation": "compact",
        "maximumFractionDigits": 1
      },
      "baseline": "auto",
      "dy": -5
    }
  ],
  "interaction": [
    {
      "type": "tooltip",
      "labels": {
        "revenue": "Revenue"
      },
      "formats": {
        "revenue": {
          "style": "currency",
          "currency": "USD",
          "maximumFractionDigits": 0
        }
      }
    }
  ],
  "alt": "Revenue by region, largest first"
}
</script>