blockcharts alpha

Gallery / Relationships and patterns /

Heatmap

A number for every pair of two categories; a table of figures, each cell colored by its value.

77.3 KB of code · core 29.5 + 10 blocks 47.8

Visits by weekday and time of day

Evenings are the busiest from Monday to Thursday, Wednesday evening most of all; on Friday, noon is the busiest.

How it is made: Two band scales make the grid, a sequential scale turns the number into a color, and a text mark writes the number in every cell: its on is the field of the cell's color, so a figure turns light on a dark cell. With the numbers it reads as a table of figures as well as a picture. A cell without a row stays empty.

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: 10 of the 59, 77.3 KB of code, minified, which is 36% of the 215.9 KB that the core and every block weigh together. The report file is 83.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.sequential
Numbers to a color ramp: heatmaps, hexbins.
Numbers to a color ramp: heatmaps, hexbins. 2.2
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.legend
Legends: categories, a color bar, bubble sizes.
Legends: categories, a color bar, bubble sizes. 6.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 10 blocks 47.877.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/heatmap.json report.html. Saved as page.json, it composes the same with the skill's own runtime/compose.js.

heatmap.json

{
  "title": "Heatmap",
  "data": {
    "activity": {
      "columns": {
        "day":    ["Mon", "Mon", "Mon", "Mon", "Tue", "Tue", "Tue", "Tue", "Wed", "Wed", "Wed", "Wed", "Thu", "Thu", "Thu", "Thu", "Fri", "Fri", "Fri", "Fri"],
        "period": ["Morning", "Noon", "Evening", "Night", "Morning", "Noon", "Evening", "Night", "Morning", "Noon", "Evening", "Night", "Morning", "Noon", "Evening", "Night", "Morning", "Noon", "Evening", "Night"],
        "visits": [120, 340, 410, 60, 150, 380, 430, 55, 170, 420, 460, 70, 160, 400, 440, 65, 130, 300, 280, 90]
      }
    }
  },
  "charts": [
    {
      "title": "Visits by weekday and time of day",
      "note": "Evenings are the busiest from Monday to Thursday, Wednesday evening most of all; on Friday, noon is the busiest.",
      "how": "Two band scales make the grid, a sequential scale turns the number into a color, and a text mark writes the number in every cell: its `on` is the field of the cell's color, so a figure turns light on a dark cell. With the numbers it reads as a table of figures as well as a picture. A cell without a row stays empty.",
      "spec": {
        "data": "activity",
        "size": [640, 300],
        "scales": {
          "x": { "type": "band", "paddingInner": 0.06, "paddingOuter": 0 },
          "y": { "type": "band", "paddingInner": 0.06, "paddingOuter": 0 },
          "color": { "type": "sequential" }
        },
        "guides": [
          { "type": "axis", "scale": "x" },
          { "type": "axis", "scale": "y" },
          { "type": "legend", "scale": "color" }
        ],
        "marks": [
          { "type": "rect", "x": "day", "y": "period", "color": "visits" },
          { "type": "text", "x": "day", "y": "period", "text": "visits", "on": "visits" }
        ],
        "interaction": [{ "type": "tooltip" }]
      }
    }
  ]
}

The report the composer makes from it is one standalone file (83.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.sequential, mark.rect, mark.text, guide.axis, guide.legend, 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">
{"activity":{"columns":{"day":["Mon","Mon","Mon","Mon","Tue","Tue","Tue","Tue","Wed","Wed","Wed","Wed","Thu","Thu","Thu","Thu","Fri","Fri","Fri","Fri"],"period":["Morning","Noon","Evening","Night","Morning","Noon","Evening","Night","Morning","Noon","Evening","Night","Morning","Noon","Evening","Night","Morning","Noon","Evening","Night"],"visits":[120,340,410,60,150,380,430,55,170,420,460,70,160,400,440,65,130,300,280,90]}}}
</script>

<script type="application/json" data-bc-chart>
{
  "data": "activity",
  "size": [
    640,
    300
  ],
  "scales": {
    "x": {
      "type": "band",
      "paddingInner": 0.06,
      "paddingOuter": 0
    },
    "y": {
      "type": "band",
      "paddingInner": 0.06,
      "paddingOuter": 0
    },
    "color": {
      "type": "sequential"
    }
  },
  "guides": [
    {
      "type": "axis",
      "scale": "x"
    },
    {
      "type": "axis",
      "scale": "y"
    },
    {
      "type": "legend",
      "scale": "color"
    }
  ],
  "marks": [
    {
      "type": "rect",
      "x": "day",
      "y": "period",
      "color": "visits"
    },
    {
      "type": "text",
      "x": "day",
      "y": "period",
      "text": "visits",
      "on": "visits"
    }
  ],
  "interaction": [
    {
      "type": "tooltip"
    }
  ],
  "alt": "Visits by weekday and time of day"
}
</script>