blockcharts alpha

Gallery / Comparing categories /

Top N

The top N items (of one year, of each region), ranked.

75.3 KB of code · core 29.5 + 12 blocks 45.8

The eight best-selling products of 2024

The Dune sofa sold the most in 2024, ahead of the Haven bed and the Atlas desk.

How it is made: transform.filter keeps the rows of 2024; transform.sort orders them by revenue, highest first, and limit keeps eight. A band scale lists its categories in row order, so the bars come out ranked. No aggregate is needed when the rows are already one per product.

The best product in each region

Each region has its own best seller: the Echo shelf in North, the Haven bed in South, the Dune sofa in East and the Birch chair in West.

How it is made: With group, sort ranks the rows of each region on their own and limit 1 keeps the top one; regions stay in the order they first appear. The product name is written on its bar.

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: 12 of the 59, 75.3 KB of code, minified, which is 35% 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 85.8 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.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
transform.filter
Keeps the rows that pass a test: numbers, dates, text, lists.
Keeps the rows that pass a test: numbers, dates, text, lists. 2.3
transform.sort
Orders rows and keeps the top N, also per group.
Orders rows and keeps the top N, also per group. 1.7
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 12 blocks 45.875.3

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

top-n.json

{
  "title": "Top N",
  "data": {
    "products": [
      {
        "year": 2023,
        "product": "Atlas desk",
        "revenue": 210000
      },
      {
        "year": 2024,
        "product": "Atlas desk",
        "revenue": 260000
      },
      {
        "year": 2023,
        "product": "Birch chair",
        "revenue": 180000
      },
      {
        "year": 2024,
        "product": "Birch chair",
        "revenue": 150000
      },
      {
        "year": 2023,
        "product": "Cove lamp",
        "revenue": 95000
      },
      {
        "year": 2024,
        "product": "Cove lamp",
        "revenue": 120000
      },
      {
        "year": 2023,
        "product": "Dune sofa",
        "revenue": 320000
      },
      {
        "year": 2024,
        "product": "Dune sofa",
        "revenue": 380000
      },
      {
        "year": 2023,
        "product": "Echo shelf",
        "revenue": 140000
      },
      {
        "year": 2024,
        "product": "Echo shelf",
        "revenue": 175000
      },
      {
        "year": 2023,
        "product": "Fern rug",
        "revenue": 75000
      },
      {
        "year": 2024,
        "product": "Fern rug",
        "revenue": 90000
      },
      {
        "year": 2023,
        "product": "Glen table",
        "revenue": 260000
      },
      {
        "year": 2024,
        "product": "Glen table",
        "revenue": 240000
      },
      {
        "year": 2023,
        "product": "Haven bed",
        "revenue": 300000
      },
      {
        "year": 2024,
        "product": "Haven bed",
        "revenue": 345000
      },
      {
        "year": 2023,
        "product": "Iris mirror",
        "revenue": 60000
      },
      {
        "year": 2024,
        "product": "Iris mirror",
        "revenue": 85000
      },
      {
        "year": 2023,
        "product": "Juno stool",
        "revenue": 45000
      },
      {
        "year": 2024,
        "product": "Juno stool",
        "revenue": 40000
      },
      {
        "year": 2023,
        "product": "Kite bench",
        "revenue": 110000
      },
      {
        "year": 2024,
        "product": "Kite bench",
        "revenue": 165000
      },
      {
        "year": 2023,
        "product": "Loft cabinet",
        "revenue": 150000
      },
      {
        "year": 2024,
        "product": "Loft cabinet",
        "revenue": 130000
      }
    ],
    "regions": [
      {
        "region": "North",
        "product": "Atlas desk",
        "revenue": 20000
      },
      {
        "region": "North",
        "product": "Birch chair",
        "revenue": 60000
      },
      {
        "region": "North",
        "product": "Cove lamp",
        "revenue": 48000
      },
      {
        "region": "North",
        "product": "Dune sofa",
        "revenue": 24000
      },
      {
        "region": "North",
        "product": "Echo shelf",
        "revenue": 97000
      },
      {
        "region": "North",
        "product": "Fern rug",
        "revenue": 57000
      },
      {
        "region": "North",
        "product": "Glen table",
        "revenue": 67000
      },
      {
        "region": "North",
        "product": "Haven bed",
        "revenue": 62000
      },
      {
        "region": "South",
        "product": "Atlas desk",
        "revenue": 63000
      },
      {
        "region": "South",
        "product": "Birch chair",
        "revenue": 45000
      },
      {
        "region": "South",
        "product": "Cove lamp",
        "revenue": 40000
      },
      {
        "region": "South",
        "product": "Dune sofa",
        "revenue": 84000
      },
      {
        "region": "South",
        "product": "Echo shelf",
        "revenue": 33000
      },
      {
        "region": "South",
        "product": "Fern rug",
        "revenue": 54000
      },
      {
        "region": "South",
        "product": "Glen table",
        "revenue": 73000
      },
      {
        "region": "South",
        "product": "Haven bed",
        "revenue": 94000
      },
      {
        "region": "East",
        "product": "Atlas desk",
        "revenue": 54000
      },
      {
        "region": "East",
        "product": "Birch chair",
        "revenue": 28000
      },
      {
        "region": "East",
        "product": "Cove lamp",
        "revenue": 40000
      },
      {
        "region": "East",
        "product": "Dune sofa",
        "revenue": 86000
      },
      {
        "region": "East",
        "product": "Echo shelf",
        "revenue": 73000
      },
      {
        "region": "East",
        "product": "Fern rug",
        "revenue": 70000
      },
      {
        "region": "East",
        "product": "Glen table",
        "revenue": 63000
      },
      {
        "region": "East",
        "product": "Haven bed",
        "revenue": 41000
      },
      {
        "region": "West",
        "product": "Atlas desk",
        "revenue": 48000
      },
      {
        "region": "West",
        "product": "Birch chair",
        "revenue": 92000
      },
      {
        "region": "West",
        "product": "Cove lamp",
        "revenue": 21000
      },
      {
        "region": "West",
        "product": "Dune sofa",
        "revenue": 37000
      },
      {
        "region": "West",
        "product": "Echo shelf",
        "revenue": 72000
      },
      {
        "region": "West",
        "product": "Fern rug",
        "revenue": 84000
      },
      {
        "region": "West",
        "product": "Glen table",
        "revenue": 21000
      },
      {
        "region": "West",
        "product": "Haven bed",
        "revenue": 55000
      }
    ]
  },
  "charts": [
    {
      "title": "The eight best-selling products of 2024",
      "note": "The Dune sofa sold the most in 2024, ahead of the Haven bed and the Atlas desk.",
      "how": "transform.filter keeps the rows of 2024; transform.sort orders them by revenue, highest first, and limit keeps eight. A band scale lists its categories in row order, so the bars come out ranked. No aggregate is needed when the rows are already one per product.",
      "spec": {
        "data": "products",
        "size": [
          640,
          320
        ],
        "padding": {
          "right": 56
        },
        "transforms": [
          {
            "type": "filter",
            "field": "year",
            "value": 2024
          },
          {
            "type": "sort",
            "field": "revenue",
            "order": "desc",
            "limit": 8
          }
        ],
        "scales": {
          "y": {
            "type": "band"
          },
          "x": {
            "type": "linear",
            "zero": true,
            "nice": true
          }
        },
        "guides": [
          {
            "type": "grid",
            "scale": "x"
          },
          {
            "type": "axis",
            "scale": "y"
          },
          {
            "type": "axis",
            "scale": "x",
            "format": {
              "style": "currency",
              "currency": "USD",
              "notation": "compact",
              "maximumFractionDigits": 0
            }
          }
        ],
        "marks": [
          {
            "type": "rect",
            "y": "product",
            "x": "revenue",
            "radius": 2
          },
          {
            "type": "text",
            "y": "product",
            "x": "revenue",
            "text": "revenue",
            "format": {
              "style": "currency",
              "currency": "USD",
              "notation": "compact",
              "maximumFractionDigits": 0
            },
            "anchor": "start",
            "dx": 6
          }
        ],
        "interaction": [
          {
            "type": "tooltip",
            "fields": [
              "product",
              "revenue"
            ],
            "formats": {
              "revenue": {
                "style": "currency",
                "currency": "USD",
                "notation": "compact",
                "maximumFractionDigits": 0
              }
            }
          }
        ]
      }
    },
    {
      "title": "The best product in each region",
      "note": "Each region has its own best seller: the Echo shelf in North, the Haven bed in South, the Dune sofa in East and the Birch chair in West.",
      "how": "With group, sort ranks the rows of each region on their own and limit 1 keeps the top one; regions stay in the order they first appear. The product name is written on its bar.",
      "spec": {
        "data": "regions",
        "size": [
          640,
          220
        ],
        "padding": {
          "right": 72
        },
        "transforms": [
          {
            "type": "sort",
            "field": "revenue",
            "order": "desc",
            "limit": 1,
            "group": "region"
          }
        ],
        "scales": {
          "y": {
            "type": "band"
          },
          "x": {
            "type": "linear",
            "zero": true,
            "nice": true
          }
        },
        "guides": [
          {
            "type": "grid",
            "scale": "x"
          },
          {
            "type": "axis",
            "scale": "y"
          },
          {
            "type": "axis",
            "scale": "x",
            "format": {
              "style": "currency",
              "currency": "USD",
              "notation": "compact",
              "maximumFractionDigits": 0
            }
          }
        ],
        "marks": [
          {
            "type": "rect",
            "y": "region",
            "x": "revenue",
            "radius": 2
          },
          {
            "type": "text",
            "y": "region",
            "x": "revenue",
            "text": "product",
            "anchor": "start",
            "dx": 6
          }
        ],
        "interaction": [
          {
            "type": "tooltip",
            "fields": [
              "region",
              "product",
              "revenue"
            ],
            "formats": {
              "revenue": {
                "style": "currency",
                "currency": "USD",
                "notation": "compact",
                "maximumFractionDigits": 0
              }
            }
          }
        ]
      }
    }
  ]
}

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

In a page of your own

The charts above use the composition kit (54.9 KB gzipped), the smallest one that has the blocks this example needs: lib.pointer, lib.ticks, scale.band, scale.linear, transform.filter, transform.sort, 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/composition.js"></script>

<script type="application/json" id="bc-data">
{"products":[{"year":2023,"product":"Atlas desk","revenue":210000},{"year":2024,"product":"Atlas desk","revenue":260000},{"year":2023,"product":"Birch chair","revenue":180000},{"year":2024,"product":"Birch chair","revenue":150000},{"year":2023,"product":"Cove lamp","revenue":95000},{"year":2024,"product":"Cove lamp","revenue":120000},{"year":2023,"product":"Dune sofa","revenue":320000},{"year":2024,"product":"Dune sofa","revenue":380000},{"year":2023,"product":"Echo shelf","revenue":140000},{"year":2024,"product":"Echo shelf","revenue":175000},{"year":2023,"product":"Fern rug","revenue":75000},{"year":2024,"product":"Fern rug","revenue":90000},{"year":2023,"product":"Glen table","revenue":260000},{"year":2024,"product":"Glen table","revenue":240000},{"year":2023,"product":"Haven bed","revenue":300000},{"year":2024,"product":"Haven bed","revenue":345000},{"year":2023,"product":"Iris mirror","revenue":60000},{"year":2024,"product":"Iris mirror","revenue":85000},{"year":2023,"product":"Juno stool","revenue":45000},{"year":2024,"product":"Juno stool","revenue":40000},{"year":2023,"product":"Kite bench","revenue":110000},{"year":2024,"product":"Kite bench","revenue":165000},{"year":2023,"product":"Loft cabinet","revenue":150000},{"year":2024,"product":"Loft cabinet","revenue":130000}],"regions":[{"region":"North","product":"Atlas desk","revenue":20000},{"region":"North","product":"Birch chair","revenue":60000},{"region":"North","product":"Cove lamp","revenue":48000},{"region":"North","product":"Dune sofa","revenue":24000},{"region":"North","product":"Echo shelf","revenue":97000},{"region":"North","product":"Fern rug","revenue":57000},{"region":"North","product":"Glen table","revenue":67000},{"region":"North","product":"Haven bed","revenue":62000},{"region":"South","product":"Atlas desk","revenue":63000},{"region":"South","product":"Birch chair","revenue":45000},{"region":"South","product":"Cove lamp","revenue":40000},{"region":"South","product":"Dune sofa","revenue":84000},{"region":"South","product":"Echo shelf","revenue":33000},{"region":"South","product":"Fern rug","revenue":54000},{"region":"South","product":"Glen table","revenue":73000},{"region":"South","product":"Haven bed","revenue":94000},{"region":"East","product":"Atlas desk","revenue":54000},{"region":"East","product":"Birch chair","revenue":28000},{"region":"East","product":"Cove lamp","revenue":40000},{"region":"East","product":"Dune sofa","revenue":86000},{"region":"East","product":"Echo shelf","revenue":73000},{"region":"East","product":"Fern rug","revenue":70000},{"region":"East","product":"Glen table","revenue":63000},{"region":"East","product":"Haven bed","revenue":41000},{"region":"West","product":"Atlas desk","revenue":48000},{"region":"West","product":"Birch chair","revenue":92000},{"region":"West","product":"Cove lamp","revenue":21000},{"region":"West","product":"Dune sofa","revenue":37000},{"region":"West","product":"Echo shelf","revenue":72000},{"region":"West","product":"Fern rug","revenue":84000},{"region":"West","product":"Glen table","revenue":21000},{"region":"West","product":"Haven bed","revenue":55000}]}
</script>

<script type="application/json" data-bc-chart>
{
  "data": "products",
  "size": [
    640,
    320
  ],
  "padding": {
    "right": 56
  },
  "transforms": [
    {
      "type": "filter",
      "field": "year",
      "value": 2024
    },
    {
      "type": "sort",
      "field": "revenue",
      "order": "desc",
      "limit": 8
    }
  ],
  "scales": {
    "y": {
      "type": "band"
    },
    "x": {
      "type": "linear",
      "zero": true,
      "nice": true
    }
  },
  "guides": [
    {
      "type": "grid",
      "scale": "x"
    },
    {
      "type": "axis",
      "scale": "y"
    },
    {
      "type": "axis",
      "scale": "x",
      "format": {
        "style": "currency",
        "currency": "USD",
        "notation": "compact",
        "maximumFractionDigits": 0
      }
    }
  ],
  "marks": [
    {
      "type": "rect",
      "y": "product",
      "x": "revenue",
      "radius": 2
    },
    {
      "type": "text",
      "y": "product",
      "x": "revenue",
      "text": "revenue",
      "format": {
        "style": "currency",
        "currency": "USD",
        "notation": "compact",
        "maximumFractionDigits": 0
      },
      "anchor": "start",
      "dx": 6
    }
  ],
  "interaction": [
    {
      "type": "tooltip",
      "fields": [
        "product",
        "revenue"
      ],
      "formats": {
        "revenue": {
          "style": "currency",
          "currency": "USD",
          "notation": "compact",
          "maximumFractionDigits": 0
        }
      }
    }
  ],
  "alt": "The eight best-selling products of 2024"
}
</script>

<script type="application/json" data-bc-chart>
{
  "data": "regions",
  "size": [
    640,
    220
  ],
  "padding": {
    "right": 72
  },
  "transforms": [
    {
      "type": "sort",
      "field": "revenue",
      "order": "desc",
      "limit": 1,
      "group": "region"
    }
  ],
  "scales": {
    "y": {
      "type": "band"
    },
    "x": {
      "type": "linear",
      "zero": true,
      "nice": true
    }
  },
  "guides": [
    {
      "type": "grid",
      "scale": "x"
    },
    {
      "type": "axis",
      "scale": "y"
    },
    {
      "type": "axis",
      "scale": "x",
      "format": {
        "style": "currency",
        "currency": "USD",
        "notation": "compact",
        "maximumFractionDigits": 0
      }
    }
  ],
  "marks": [
    {
      "type": "rect",
      "y": "region",
      "x": "revenue",
      "radius": 2
    },
    {
      "type": "text",
      "y": "region",
      "x": "revenue",
      "text": "product",
      "anchor": "start",
      "dx": 6
    }
  ],
  "interaction": [
    {
      "type": "tooltip",
      "fields": [
        "region",
        "product",
        "revenue"
      ],
      "formats": {
        "revenue": {
          "style": "currency",
          "currency": "USD",
          "notation": "compact",
          "maximumFractionDigits": 0
        }
      }
    }
  ],
  "alt": "The best product in each region"
}
</script>