Skip to content
OpenChart

Canvas mode

Draw thousands of points on a canvas layer while axes and text stay SVG, and when the library switches by itself.

Canvas mode paints a scatter’s dots onto one <canvas> element instead of one SVG circle each, which keeps a chart of many thousands of points quick to draw and hover. This chart draws the center of each of the 3,108 counties and county equivalents in the lower 48 states and DC that way.

Every county in the lower 48 states and DC. This chart is drawn in the browser.

Every county in the lower 48 states and DC. Source: US Census Bureau via us-atlas.
{
  "mark": {
    "type": "point",
    "size": 1.5,
    "opacity": 0.6,
    "trendline": false
  },
  "data": [
    {
      "lon": -113.76,
      "lat": 35.7
    },
    {
      "lon": -90.4,
      "lat": 30.63
    },
    {
      "lon": -114.14,
      "lat": 43
    },
    {
      "lon": -96.4,
      "lat": 47.77
    },
    {
      "lon": -97.25,
      "lat": 46.93
    },
    {
      "lon": -103.79,
      "lat": 44.36
    },
    // … 3,102 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "lon",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Longitude (°)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "lat",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Latitude (°)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "chrome": {
    "title": "The lower 48 states and DC have 3,108 counties and county equivalents",
    "subtitle": "Geographic center of each county or county equivalent, by longitude and latitude"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 3,108 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "size": 1.5,
    "opacity": 0.6,
    "trendline": false
  },
  "encoding": {
    "x": {
      "field": "lon",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Longitude (°)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "lat",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Latitude (°)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "chrome": {
    "title": "The lower 48 states and DC have 3,108 counties and county equivalents",
    "subtitle": "Geographic center of each county or county equivalent, by longitude and latitude"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 3,108 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "size": 1.5,
    "opacity": 0.6,
    "trendline": false
  },
  "encoding": {
    "x": {
      "field": "lon",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Longitude (°)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "lat",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Latitude (°)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "chrome": {
    "title": "The lower 48 states and DC have 3,108 counties and county equivalents",
    "subtitle": "Geographic center of each county or county equivalent, by longitude and latitude"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 3,108 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "size": 1.5,
    "opacity": 0.6,
    "trendline": false
  },
  "encoding": {
    "x": {
      "field": "lon",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Longitude (°)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "lat",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Latitude (°)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "chrome": {
    "title": "The lower 48 states and DC have 3,108 counties and county equivalents",
    "subtitle": "Geographic center of each county or county equivalent, by longitude and latitude"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 3,108 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "size": 1.5,
    "opacity": 0.6,
    "trendline": false
  },
  "encoding": {
    "x": {
      "field": "lon",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Longitude (°)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "lat",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Latitude (°)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "chrome": {
    "title": "The lower 48 states and DC have 3,108 counties and county equivalents",
    "subtitle": "Geographic center of each county or county equivalent, by longitude and latitude"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

The chart sets no renderer. It has more than 1,000 points, so the library draws it on the canvas by itself.

Turning it on

Canvas is a choice of the page that mounts the chart, so it is a mount option rather than a spec field:

chart.ts
createChart(container, spec, { renderer: "canvas" });

React, Vue and Svelte take the same value as the renderer prop on <Chart>. Only point marks move to the canvas. A line, bar or area chart, and any faceted, layered or sparkline chart, stays SVG, and an explicit "canvas" there logs a warning.

A chart with encoding.time always draws SVG, whatever its point count, because playback moves each bubble’s own element from frame to frame. An explicit renderer: "canvas" on one logs a warning once and draws SVG. The engine warns when a period has more than 1,000 points, the count above which "auto" would otherwise pick the canvas. See bubble playback.

Counties west of the 100th meridian. This chart is drawn in the browser.

Counties west of the 100th meridian. Source: US Census Bureau via us-atlas.
{
  "mark": {
    "type": "point",
    "size": 2,
    "opacity": 0.7,
    "trendline": false
  },
  "data": [
    {
      "lon": -113.76,
      "lat": 35.7
    },
    {
      "lon": -114.14,
      "lat": 43
    },
    {
      "lon": -103.79,
      "lat": 44.36
    },
    {
      "lon": -101.7,
      "lat": 40.52
    },
    {
      "lon": -106.19,
      "lat": 37.2
    },
    {
      "lon": -102.61,
      "lat": 34.97
    },
    // … 593 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "lon",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Longitude (°)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "lat",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Latitude (°)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "chrome": {
    "title": "599 counties in the lower 48 states lie west of the 100th meridian",
    "subtitle": "Geographic center of each county west of 100°W, by longitude and latitude"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 599 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "size": 2,
    "opacity": 0.7,
    "trendline": false
  },
  "encoding": {
    "x": {
      "field": "lon",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Longitude (°)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "lat",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Latitude (°)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "chrome": {
    "title": "599 counties in the lower 48 states lie west of the 100th meridian",
    "subtitle": "Geographic center of each county west of 100°W, by longitude and latitude"
  }
};

export function Example() {
  return <Chart spec={spec} renderer="canvas" />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 599 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "size": 2,
    "opacity": 0.7,
    "trendline": false
  },
  "encoding": {
    "x": {
      "field": "lon",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Longitude (°)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "lat",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Latitude (°)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "chrome": {
    "title": "599 counties in the lower 48 states lie west of the 100th meridian",
    "subtitle": "Geographic center of each county west of 100°W, by longitude and latitude"
  }
};
</script>

<template>
  <Chart :spec="spec" renderer="canvas" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 599 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "size": 2,
    "opacity": 0.7,
    "trendline": false
  },
  "encoding": {
    "x": {
      "field": "lon",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Longitude (°)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "lat",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Latitude (°)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "chrome": {
    "title": "599 counties in the lower 48 states lie west of the 100th meridian",
    "subtitle": "Geographic center of each county west of 100°W, by longitude and latitude"
  }
};
</script>

<Chart {spec} renderer="canvas" />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 599 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "point",
    "size": 2,
    "opacity": 0.7,
    "trendline": false
  },
  "encoding": {
    "x": {
      "field": "lon",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Longitude (°)",
        "format": ".0f"
      }
    },
    "y": {
      "field": "lat",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Latitude (°)",
        "format": ".0f",
        "grid": true
      }
    }
  },
  "chrome": {
    "title": "599 counties in the lower 48 states lie west of the 100th meridian",
    "subtitle": "Geographic center of each county west of 100°W, by longitude and latitude"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec, { renderer: "canvas" });

This chart passes renderer: "canvas" as a mount option, and its Code tabs show it as a prop or option in each framework. The option applies at any size, including the 599 points here.

The canvas also paints the gridlines. The axes, tick labels and title stay SVG, so text stays sharp at any zoom, and the SVG keeps the chart’s accessible name. Tooltips and hover work as before: the pointer passes through to the canvas, which finds the nearest dot.

The automatic switch

Leave renderer out and the library decides: above 1,000 points it uses the canvas, at 1,000 or fewer it draws SVG. The first chart on this page sets no renderer and has 3,108 points, so it draws on the canvas. Set renderer: "svg" to keep SVG at any size.

What changes on a canvas

  • Dots have no elements of their own, so a keyboard cannot step from dot to dot and edit mode cannot select one. The screen-reader data table stays, capped at 1,000 rows with a caption giving the full count.
  • A trendline draws above the dots even when set to draw below, because the canvas sits under the whole SVG.
  • Gradient fills show their first color on screen.

Exports re-render the chart in full. A PNG matches the SVG version of the chart pixel for pixel, and an SVG file is all vector up to 5,000 points; above that the dots go in as one embedded image while text and gridlines stay vector.

Fields that matter

Field What it does
renderer (mount option or prop) "auto" (the default), "canvas" or "svg".
mark: "point" The only mark that can draw on a canvas.
mark.opacity Lower it on dense scatters so overlapping dots stay readable.
encoding.key A field that names each point, so an update can move dots instead of redrawing them.
  • Scatter covers the point chart this page draws.
  • Symbol map puts points on a projected map instead of plain axes.
  • Marks reference documents canvas mode and its export behavior.