blockcharts alpha

Gallery / Trends over time /

Wide table

Data with one column per series (a wide spreadsheet table).

94.6 KB of code · core 29.5 + 18 blocks 65.1

Revenue per channel

Online grew the fastest and overtook retail in October.

How it is made: The table has one column per channel, as exported from a spreadsheet. transform.fold turns each row into one row per channel, with the column name in "channel" and its number in "revenue", so one line mark colored by channel draws all three. No need to reshape the data by hand.

Total per quarter, split by channel

Revenue grew every quarter; in Q4 online brought in more than retail for the first time.

How it is made: After fold the long table works with every other transform: aggregate sums the months of each quarter per channel, and stack piles the channels up. The quarter is the date it starts on; the axis writes it with the "quarter" format.

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: 18 of the 59, 94.6 KB of code, minified, which is 44% 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 103.1 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.color
A color per category, from the theme palette.
A color per category, from the theme palette. 0.8
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
transform.fold
A wide table (a column per series) becomes a long one.
A wide table (a column per series) becomes a long one. 1.5
transform.stack
Stacks series on top of each other, to 100%, or around a center line (a streamgraph).
Stacks series on top of each other, to 100%, or around a center line (a streamgraph). 2.2
mark.line
A line per series, with optional end labels.
A line per series, with optional end labels. 4.4
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
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
guide.legend
Legends: categories, a color bar, bubble sizes.
Legends: categories, a color bar, bubble sizes. 6.0
interaction.legend-filter
Click a legend item to hide or show its series.
Click a legend item to hide or show its series. 3.0
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 18 blocks 65.194.6

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/wide-table.json report.html. Saved as page.json, it composes the same with the skill's own runtime/compose.js.

wide-table.json

{
  "title": "Wide table",
  "data": {
    "sales": [
      {
        "month": "2024-01-01",
        "Online": 42000,
        "Retail": 68000,
        "Partners": 18000
      },
      {
        "month": "2024-02-01",
        "Online": 45000,
        "Retail": 64000,
        "Partners": 19000
      },
      {
        "month": "2024-03-01",
        "Online": 51000,
        "Retail": 66000,
        "Partners": 21000
      },
      {
        "month": "2024-04-01",
        "Online": 49000,
        "Retail": 70000,
        "Partners": 24000
      },
      {
        "month": "2024-05-01",
        "Online": 56000,
        "Retail": 69000,
        "Partners": 26000
      },
      {
        "month": "2024-06-01",
        "Online": 61000,
        "Retail": 72000,
        "Partners": 25000
      },
      {
        "month": "2024-07-01",
        "Online": 58000,
        "Retail": 67000,
        "Partners": 28000
      },
      {
        "month": "2024-08-01",
        "Online": 63000,
        "Retail": 65000,
        "Partners": 31000
      },
      {
        "month": "2024-09-01",
        "Online": 70000,
        "Retail": 71000,
        "Partners": 33000
      },
      {
        "month": "2024-10-01",
        "Online": 74000,
        "Retail": 73000,
        "Partners": 34000
      },
      {
        "month": "2024-11-01",
        "Online": 88000,
        "Retail": 84000,
        "Partners": 38000
      },
      {
        "month": "2024-12-01",
        "Online": 102000,
        "Retail": 96000,
        "Partners": 41000
      }
    ]
  },
  "charts": [
    {
      "title": "Revenue per channel",
      "note": "Online grew the fastest and overtook retail in October.",
      "how": "The table has one column per channel, as exported from a spreadsheet. transform.fold turns each row into one row per channel, with the column name in \"channel\" and its number in \"revenue\", so one line mark colored by channel draws all three. No need to reshape the data by hand.",
      "spec": {
        "data": "sales",
        "size": [
          680,
          300
        ],
        "transforms": [
          {
            "type": "fold",
            "fields": [
              "Online",
              "Retail",
              "Partners"
            ],
            "key": "channel",
            "value": "revenue"
          }
        ],
        "scales": {
          "x": {
            "type": "time"
          },
          "y": {
            "type": "linear",
            "zero": true,
            "nice": true
          },
          "color": {
            "type": "color"
          }
        },
        "guides": [
          {
            "type": "grid",
            "scale": "y"
          },
          {
            "type": "axis",
            "scale": "x",
            "format": "month"
          },
          {
            "type": "axis",
            "scale": "y",
            "format": {
              "style": "currency",
              "currency": "USD",
              "notation": "compact",
              "maximumFractionDigits": 0
            }
          },
          {
            "type": "legend",
            "scale": "color",
            "position": "top"
          }
        ],
        "marks": [
          {
            "type": "line",
            "x": "month",
            "y": "revenue",
            "color": "channel",
            "strokeWidth": 2
          }
        ],
        "interaction": [
          {
            "type": "legend-filter"
          },
          {
            "type": "tooltip",
            "fields": [
              "month",
              "channel",
              "revenue"
            ],
            "formats": {
              "revenue": {
                "style": "currency",
                "currency": "USD",
                "notation": "compact",
                "maximumFractionDigits": 0
              },
              "month": "month"
            }
          }
        ]
      }
    },
    {
      "title": "Total per quarter, split by channel",
      "note": "Revenue grew every quarter; in Q4 online brought in more than retail for the first time.",
      "how": "After fold the long table works with every other transform: aggregate sums the months of each quarter per channel, and stack piles the channels up. The quarter is the date it starts on; the axis writes it with the \"quarter\" format.",
      "spec": {
        "data": "sales",
        "size": [
          680,
          300
        ],
        "transforms": [
          {
            "type": "fold",
            "fields": [
              "Online",
              "Retail",
              "Partners"
            ],
            "key": "channel",
            "value": "revenue"
          },
          {
            "type": "aggregate",
            "groupby": [
              {
                "field": "month",
                "unit": "quarter",
                "as": "quarter"
              },
              "channel"
            ],
            "measures": [
              {
                "op": "sum",
                "field": "revenue",
                "as": "revenue"
              }
            ]
          },
          {
            "type": "stack",
            "field": "revenue",
            "by": "quarter",
            "group": "channel"
          }
        ],
        "scales": {
          "x": {
            "type": "band"
          },
          "y": {
            "type": "linear",
            "zero": true,
            "nice": true
          },
          "color": {
            "type": "color"
          }
        },
        "guides": [
          {
            "type": "grid",
            "scale": "y"
          },
          {
            "type": "axis",
            "scale": "x",
            "format": "quarter"
          },
          {
            "type": "axis",
            "scale": "y",
            "format": {
              "style": "currency",
              "currency": "USD",
              "notation": "compact",
              "maximumFractionDigits": 0
            }
          },
          {
            "type": "legend",
            "scale": "color",
            "position": "top"
          }
        ],
        "marks": [
          {
            "type": "rect",
            "x": "quarter",
            "y": "revenue1",
            "y2": "revenue0",
            "color": "channel"
          }
        ],
        "interaction": [
          {
            "type": "legend-filter"
          },
          {
            "type": "tooltip",
            "fields": [
              "quarter",
              "channel",
              "revenue"
            ],
            "formats": {
              "revenue": {
                "style": "currency",
                "currency": "USD",
                "notation": "compact",
                "maximumFractionDigits": 0
              },
              "quarter": "quarter"
            }
          }
        ]
      }
    }
  ]
}

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

In a page of your own

The charts above use the full kit (81.8 KB gzipped), the smallest one that has the blocks this example needs: lib.curve, lib.pointer, lib.ticks, scale.band, scale.color, scale.linear, scale.time, transform.aggregate, transform.fold, transform.stack, mark.line, mark.rect, guide.axis, guide.grid, guide.legend, interaction.legend-filter, 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/full.js"></script>

<script type="application/json" id="bc-data">
{"sales":[{"month":"2024-01-01","Online":42000,"Retail":68000,"Partners":18000},{"month":"2024-02-01","Online":45000,"Retail":64000,"Partners":19000},{"month":"2024-03-01","Online":51000,"Retail":66000,"Partners":21000},{"month":"2024-04-01","Online":49000,"Retail":70000,"Partners":24000},{"month":"2024-05-01","Online":56000,"Retail":69000,"Partners":26000},{"month":"2024-06-01","Online":61000,"Retail":72000,"Partners":25000},{"month":"2024-07-01","Online":58000,"Retail":67000,"Partners":28000},{"month":"2024-08-01","Online":63000,"Retail":65000,"Partners":31000},{"month":"2024-09-01","Online":70000,"Retail":71000,"Partners":33000},{"month":"2024-10-01","Online":74000,"Retail":73000,"Partners":34000},{"month":"2024-11-01","Online":88000,"Retail":84000,"Partners":38000},{"month":"2024-12-01","Online":102000,"Retail":96000,"Partners":41000}]}
</script>

<script type="application/json" data-bc-chart>
{
  "data": "sales",
  "size": [
    680,
    300
  ],
  "transforms": [
    {
      "type": "fold",
      "fields": [
        "Online",
        "Retail",
        "Partners"
      ],
      "key": "channel",
      "value": "revenue"
    }
  ],
  "scales": {
    "x": {
      "type": "time"
    },
    "y": {
      "type": "linear",
      "zero": true,
      "nice": true
    },
    "color": {
      "type": "color"
    }
  },
  "guides": [
    {
      "type": "grid",
      "scale": "y"
    },
    {
      "type": "axis",
      "scale": "x",
      "format": "month"
    },
    {
      "type": "axis",
      "scale": "y",
      "format": {
        "style": "currency",
        "currency": "USD",
        "notation": "compact",
        "maximumFractionDigits": 0
      }
    },
    {
      "type": "legend",
      "scale": "color",
      "position": "top"
    }
  ],
  "marks": [
    {
      "type": "line",
      "x": "month",
      "y": "revenue",
      "color": "channel",
      "strokeWidth": 2
    }
  ],
  "interaction": [
    {
      "type": "legend-filter"
    },
    {
      "type": "tooltip",
      "fields": [
        "month",
        "channel",
        "revenue"
      ],
      "formats": {
        "revenue": {
          "style": "currency",
          "currency": "USD",
          "notation": "compact",
          "maximumFractionDigits": 0
        },
        "month": "month"
      }
    }
  ],
  "alt": "Revenue per channel"
}
</script>

<script type="application/json" data-bc-chart>
{
  "data": "sales",
  "size": [
    680,
    300
  ],
  "transforms": [
    {
      "type": "fold",
      "fields": [
        "Online",
        "Retail",
        "Partners"
      ],
      "key": "channel",
      "value": "revenue"
    },
    {
      "type": "aggregate",
      "groupby": [
        {
          "field": "month",
          "unit": "quarter",
          "as": "quarter"
        },
        "channel"
      ],
      "measures": [
        {
          "op": "sum",
          "field": "revenue",
          "as": "revenue"
        }
      ]
    },
    {
      "type": "stack",
      "field": "revenue",
      "by": "quarter",
      "group": "channel"
    }
  ],
  "scales": {
    "x": {
      "type": "band"
    },
    "y": {
      "type": "linear",
      "zero": true,
      "nice": true
    },
    "color": {
      "type": "color"
    }
  },
  "guides": [
    {
      "type": "grid",
      "scale": "y"
    },
    {
      "type": "axis",
      "scale": "x",
      "format": "quarter"
    },
    {
      "type": "axis",
      "scale": "y",
      "format": {
        "style": "currency",
        "currency": "USD",
        "notation": "compact",
        "maximumFractionDigits": 0
      }
    },
    {
      "type": "legend",
      "scale": "color",
      "position": "top"
    }
  ],
  "marks": [
    {
      "type": "rect",
      "x": "quarter",
      "y": "revenue1",
      "y2": "revenue0",
      "color": "channel"
    }
  ],
  "interaction": [
    {
      "type": "legend-filter"
    },
    {
      "type": "tooltip",
      "fields": [
        "quarter",
        "channel",
        "revenue"
      ],
      "formats": {
        "revenue": {
          "style": "currency",
          "currency": "USD",
          "notation": "compact",
          "maximumFractionDigits": 0
        },
        "quarter": "quarter"
      }
    }
  ],
  "alt": "Total per quarter, split by channel"
}
</script>