blockcharts alpha

Gallery / Relationships and patterns /

Scatter with groups

Two numeric variables, groups by color.

71.0 KB of code · core 29.5 + 10 blocks 41.5

Height and weight

Weight rises with height in both teams; Team B weighs more at the same height.

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, 71.0 KB of code, minified, which is 33% of the 215.9 KB that the core and every block weigh together. The report file is 77.6 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.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
mark.point
Dots, bubbles and beeswarms.
Dots, bubbles and beeswarms. 3.9
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.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 41.571.0

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

scatter-groups.json

{
  "title": "Scatter with groups",
  "data": {
    "people": [
      { "height": 152, "weight": 51, "group": "Team A" }, { "height": 158, "weight": 55, "group": "Team A" },
      { "height": 163, "weight": 60, "group": "Team A" }, { "height": 169, "weight": 64, "group": "Team A" },
      { "height": 174, "weight": 70, "group": "Team A" }, { "height": 181, "weight": 77, "group": "Team A" },
      { "height": 155, "weight": 58, "group": "Team B" }, { "height": 160, "weight": 63, "group": "Team B" },
      { "height": 166, "weight": 69, "group": "Team B" }, { "height": 172, "weight": 73, "group": "Team B" },
      { "height": 178, "weight": 82, "group": "Team B" }, { "height": 185, "weight": 88, "group": "Team B" }
    ]
  },
  "charts": [
    {
      "title": "Height and weight",
      "note": "Weight rises with height in both teams; Team B weighs more at the same height.",
      "spec": {
        "data": "people",
        "size": [640, 340],
        "scales": {
          "x": { "type": "linear", "padding": 0.05 },
          "y": { "type": "linear", "padding": 0.05 },
          "color": { "type": "color" }
        },
        "guides": [
          { "type": "grid", "scale": "x" },
          { "type": "grid", "scale": "y" },
          { "type": "axis", "scale": "x", "title": "Height, cm" },
          { "type": "axis", "scale": "y", "title": "Weight, kg" },
          { "type": "legend", "scale": "color" }
        ],
        "marks": [{ "type": "point", "x": "height", "y": "weight", "color": "group", "r": 4, "opacity": 0.85 }],
        "interaction": [{ "type": "tooltip", "labels": { "height": "Height, cm", "weight": "Weight, kg", "group": "Team" } }]
      }
    }
  ]
}

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

In a page of your own

The charts above use the basic kit (42.9 KB gzipped), the smallest one that has the blocks this example needs: lib.pointer, lib.ticks, scale.color, scale.linear, mark.point, guide.axis, guide.grid, 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/basic.js"></script>

<script type="application/json" id="bc-data">
{"people":[{"height":152,"weight":51,"group":"Team A"},{"height":158,"weight":55,"group":"Team A"},{"height":163,"weight":60,"group":"Team A"},{"height":169,"weight":64,"group":"Team A"},{"height":174,"weight":70,"group":"Team A"},{"height":181,"weight":77,"group":"Team A"},{"height":155,"weight":58,"group":"Team B"},{"height":160,"weight":63,"group":"Team B"},{"height":166,"weight":69,"group":"Team B"},{"height":172,"weight":73,"group":"Team B"},{"height":178,"weight":82,"group":"Team B"},{"height":185,"weight":88,"group":"Team B"}]}
</script>

<script type="application/json" data-bc-chart>
{
  "data": "people",
  "size": [
    640,
    340
  ],
  "scales": {
    "x": {
      "type": "linear",
      "padding": 0.05
    },
    "y": {
      "type": "linear",
      "padding": 0.05
    },
    "color": {
      "type": "color"
    }
  },
  "guides": [
    {
      "type": "grid",
      "scale": "x"
    },
    {
      "type": "grid",
      "scale": "y"
    },
    {
      "type": "axis",
      "scale": "x",
      "title": "Height, cm"
    },
    {
      "type": "axis",
      "scale": "y",
      "title": "Weight, kg"
    },
    {
      "type": "legend",
      "scale": "color"
    }
  ],
  "marks": [
    {
      "type": "point",
      "x": "height",
      "y": "weight",
      "color": "group",
      "r": 4,
      "opacity": 0.85
    }
  ],
  "interaction": [
    {
      "type": "tooltip",
      "labels": {
        "height": "Height, cm",
        "weight": "Weight, kg",
        "group": "Team"
      }
    }
  ],
  "alt": "Height and weight"
}
</script>