blockcharts alpha

Gallery / Parts of a whole /

Pie and donut

Parts of a whole, as one circle.

60.5 KB of code · core 29.5 + 7 blocks 31.0

Browser share

Chrome holds almost two thirds of the market, Safari a fifth. Click a legend item to hide its slice: the others are shared out to 100% again.

How it is made: One slice per row, its angle proportional to the value. innerRadius turns the pie into a donut; label writes the percentage outside each slice. Clicking a legend item hides that slice (interaction.legend-filter) and the rest recompute their share against the smaller total automatically — mark.arc always divides by the total of only the visible rows, so hiding is not a separate feature, just a consequence of how the angles are computed. Double-click a legend item to isolate it.

What the report is made of

The composer reads the JSON below and puts into the page the core and only the blocks its chart uses: 7 of the 59, 60.5 KB of code, minified, which is 28% of the 215.9 KB that the core and every block weigh together. The report file is 66.5 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
scale.color
A color per category, from the theme palette.
A color per category, from the theme palette. 0.8
mark.arc
Pie and donut slices; gauges and progress rings.
Pie and donut slices; gauges and progress rings. 4.9
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 7 blocks 31.060.5

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

pie.json

{
  "title": "Pie and donut",
  "data": {
    "browsers": [
      { "browser": "Chrome", "share": 64 },
      { "browser": "Safari", "share": 19 },
      { "browser": "Edge", "share": 5.5 },
      { "browser": "Firefox", "share": 3.2 },
      { "browser": "Other", "share": 8.3 }
    ]
  },
  "charts": [
    {
      "title": "Browser share",
      "note": "Chrome holds almost two thirds of the market, Safari a fifth. Click a legend item to hide its slice: the others are shared out to 100% again.",
      "how": "One slice per row, its angle proportional to the value. innerRadius turns the pie into a donut; label writes the percentage outside each slice. Clicking a legend item hides that slice (interaction.legend-filter) and the rest recompute their share against the smaller total automatically — mark.arc always divides by the total of only the visible rows, so hiding is not a separate feature, just a consequence of how the angles are computed. Double-click a legend item to isolate it.",
      "spec": {
        "data": "browsers",
        "size": [640, 340],
        "scales": { "color": { "type": "color" } },
        "guides": [{ "type": "legend", "scale": "color" }],
        "marks": [{ "type": "arc", "value": "share", "color": "browser", "innerRadius": 0.5, "label": "percent", "sort": "desc" }],
        "interaction": [
          {
            "type": "tooltip",
            "heading": "browser",
            "fields": ["share"],
            "labels": { "share": "Share" },
            "formats": { "share": { "maximumFractionDigits": 1, "suffix": "%" } }
          },
          { "type": "legend-filter" }
        ]
      }
    }
  ]
}

The report the composer makes from it is one standalone file (66.5 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, scale.color, mark.arc, 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/composition.js"></script>

<script type="application/json" id="bc-data">
{"browsers":[{"browser":"Chrome","share":64},{"browser":"Safari","share":19},{"browser":"Edge","share":5.5},{"browser":"Firefox","share":3.2},{"browser":"Other","share":8.3}]}
</script>

<script type="application/json" data-bc-chart>
{
  "data": "browsers",
  "size": [
    640,
    340
  ],
  "scales": {
    "color": {
      "type": "color"
    }
  },
  "guides": [
    {
      "type": "legend",
      "scale": "color"
    }
  ],
  "marks": [
    {
      "type": "arc",
      "value": "share",
      "color": "browser",
      "innerRadius": 0.5,
      "label": "percent",
      "sort": "desc"
    }
  ],
  "interaction": [
    {
      "type": "tooltip",
      "heading": "browser",
      "fields": [
        "share"
      ],
      "labels": {
        "share": "Share"
      },
      "formats": {
        "share": {
          "maximumFractionDigits": 1,
          "suffix": "%"
        }
      }
    },
    {
      "type": "legend-filter"
    }
  ],
  "alt": "Browser share"
}
</script>