Documentation
- Installation
- Data Format
- Basics
- High/Low Bands
- Series, Scales, Axes, Grid
- Multiple Scales & Axes
- Scale Opts
- Axis & Grid Opts
- WIP: #48
Installation
<link rel="stylesheet" href="dist/uPlot.min.css"> <script src="dist/uPlot.iife.min.js"></script>
Data Format
let data = [ [1546300800, 1546387200], // x-values (timestamps) [ 35, 71], // y-values (series 1) [ 90, 15], // y-values (series 2) ];
uPlot expects a columnar data format as shown above.
- x-values must be numbers, unique, and in ascending order.
- y-values must be numbers (or
nulls for missing data). - x-values and y-values arrays must be of equal lengths >= 2.
By default, x-values are assumed to be unix timestamps (seconds since 1970-01-01 00:00:00) but can be treated as plain numbers via scales.x.time = false.
JavaScript uses millisecond-precision timestamps, but this precision is rarely necessary on calendar-aware time: true scales/plots, which honor DST, timezones, leap years, etc.
For sub-second periods, it's recommended to set time: false and simply use ms offsets from 0.
If you truly need calendar-aware ms level precision, simply provide the timestamps as floats, e.g. 1575354886.419.
More info....
This format has implications that can make uPlot an awkward choice for multi-series datasets which cannot be easily aligned along their x-values.
If one series is data-dense and the other is sparse, then the latter will need to be filled in with mostly null y-values.
If each series has data at arbitrary x-values, then the x-values array must be augmented with all x-values, and all y-values arrays must be augmented with nulls, potentially leading to exponential growth in dataset size, and a structure consisting of mostly nulls.
This does not mean that all series must have identical x-values - just that they are alignable. For instance, it is possible to plot series that express different time periods, because the data is equally spaced.
Before choosing uPlot, ensure your data can conform to these requirements.
Basics
let opts = { title: "My Chart", id: "chart1", class: "my-chart", width: 800, height: 600, series: [ {}, { // initial toggled state (optional) show: true, spanGaps: false, // in-legend display label: "RAM", value: (self, rawValue) => rawValue == null ? '' : "$" + rawValue.toFixed(2), // series style stroke: "red", width: 1, fill: "rgba(255, 0, 0, 0.3)", dash: [10, 5], } ], }; let uplot = new uPlot(opts, data, document.body);
idandclassare optional HTML attributes to set on the chart's container<div>(uplot.root).widthandheightare required dimensions in plotting area, axes & ticks, but excludingtitleorlegenddimensions (which can be variable based on user CSS).spanGapscan be set totrueto connectnulldata points.- For a series to be rendered, it must be specified in the opts; simply having it in the data is insufficient.
- All series' options are optional;
labelwill default to "Value" andstrokewill default to "black". widthis the series' line width in CSS pixels.stroke,width,fill, anddashmap directly to Canvas API's ctx.strokeStyle, ctx.lineWidth, ctx.fillStyle, and ctx.setLineDash.
High/Low Bands
High/Low bands are defined by two adjacent data series in low,high order and matching opts with series.band = true.
const opts = { series: [ {}, { label: "Low", fill: "rgba(0, 255, 0, .2)", band: true, }, { label: "High", fill: "rgba(0, 255, 0, .2)", band: true, }, ], };
Series, Scales, Axes, Grid
uPlot's API strives for brevity, uniformity and logical consistency.
Understanding the roles and processing order of data, series, scales, and axes will help with the remaining topics.
The high-level rendering flow is this:
datais the first input into the system.seriesholds the config of each dataset, such as visibility, styling, labels & value display in the legend, and thescalekey along which they should be drawn. Implicit scale keys arexfor thedata[0]series andyfordata[1..N].scalesreflect the min/max ranges visible within the view. All view range adjustments such as zooming and pagination are done here. If not explicitly set via opts,scalesare automatically initialized using theseriesconfig and auto-ranged using the provideddata.axesrender the ticks, values, labels and grid along theirscale. Tick & grid spacing, value granularity & formatting, timezone & DST handling is done here.
You may have noticed in the previous examples that series and axes arrays begin with {}.
This represents options/overrides for the x series and axis.
They are required due to the way uPlot sets defaults:
-
data[0],series[0]andaxes[0]represent & inheritxdefaults, e.g:"x"scale w/auto: false- temporal
- hz orientation, bottom position
- larger minimum tick spacing
-
data[1..N],series[1..N]andaxes[1..N]represent & inheritydefaults, e.g:"y"scale w/auto: true- numeric
- vt orientation, left position
- smaller minimum tick spacing
While somewhat unusual, keeping x & y opts in flat arrays [rather than splitting them] serves several purposes:
- API & structural uniformity. e.g.
series[i]maps todata[i] - Hooks receive an unambiguous
iinto the arrays without needing further context - Internals don't need added complexity to conceal the fact that everything is merged & DRY
Multiple Scales & Axes
Series with differing units can be plotted along additional scales and display corresponding y-axes.
- Use the same
series.scalekey. - Optionally, specify an additional
axiswith thescalekey.
let opts = { series: [ {}, { label: "CPU", stroke: "red", scale: "%", value: (self, rawValue) => rawValue == null ? '' : rawValue.toFixed(1) + "%", } { label: "RAM", stroke: "blue", scale: "%", value: (self, rawValue) => rawValue == null ? '' : rawValue.toFixed(1) + "%", }, { label: "TCP", stroke: "green", scale: "mb", value: (self, rawValue) => rawValue == null ? '' : rawValue.toFixed(2) + "MB", }, ], axes: [ {}, { scale: "%", values: (self, ticks) => ticks.map(rawValue => rawValue.toFixed(1) + "%"), }, { scale: "mb", values: (self, ticks) => ticks.map(rawValue => rawValue.toFixed(2) + "MB"), side: 1, grid: {show: false}, }, ], };
sideis the where to place the axis (0: top, 1: right, 2: bottom, 3: left).
Axes for Alternate Units
Sometimes it's useful to provide an additional axis to display alternate units, e.g. °F / °C. This is done using dependent scales.
let opts = { series: [ {}, { label: "Temp", stroke: "red", scale: "F", }, ], axes: [ {}, { scale: "F", values: (self, ticks) => ticks.map(rawValue => rawValue + "° F"), }, { scale: "C", values: (self, ticks) => ticks.map(rawValue => rawValue + "° C"), side: 1, grid: {show: false}, } ], scales: { "C": { from: "F", range: (self, fromMin, fromMax) => [ (fromMin - 32) * 5/9, (fromMax - 32) * 5/9, ], } },
fromspecifies the scale on which this one depends.rangeconvertsfrom's min/max into this one's min/max.
Scale Opts
If a scale does not need auto-ranging from the visible data, you can provide static min/max values. This is also a performance optimization, since the data does not need to be scanned on every view change.
let opts = { scales: { "%": { auto: false, range: [0, 100], } }, }
The default x scale is temporal, but can be switched to plain numbers. This can be used to plot functions.
let opts = { scales: { "x": { time: false, } }, }
A scale's default distribution is linear distr: 1, but can be switched to indexed/evenly-spaced.
This is useful when you'd like to squash periods with no data, such as weekends.
Keep in mind that this will prevent logical temporal tick baselines such as start of day or start of month.
let opts = { scales: { "x": { distr: 2, } }, }
Axis & Grid Opts
Most options are self-explanatory:
let opts = { axes: [ {}, { show: true, label: "Population", labelSize: 30, labelFont: "bold 12px Arial", font: "12px Arial", gap: 5, size: 50, stroke: "red", grid: { show: true, stroke: "#eee", width: 2, dash: [], }, ticks: { show: true, stroke: "#eee", width: 2, dash: [], size: 10, } } ] }
size&labelSizerepresent the perpendicular dimensions assigned tovaluesandlabelsDOM elements, respectively. In the above example, the full width of this y-axis would be 30 + 50; for an x-axis, it would be its height.gapis the space between axis ticks andvalues.
Customizing the tick/grid spacing, value formatting and granularity is somewhat more involved:
let opts = { axes: [ { space: 40, incrs: [ // minute divisors (# of secs) 1, 5, 10, 15, 30, // hour divisors 60, 60 * 5, 60 * 10, 60 * 15, 60 * 30, // day divisors 3600, // ... ], // [0]: minimum num secs in found axis split (tick incr) // [1]: default tick format // [2-7]: rollover tick formats // [8]: mode: 0: replace [1] -> [2-7], 1: concat [1] + [2-7] values: [ // tick incr default year month day hour min sec mode [3600 * 24 * 365, "{YYYY}", null, null, null, null, null, null, 1], [3600 * 24 * 28, "{MMM}", "\n{YYYY}", null, null, null, null, null, 1], [3600 * 24, "{M}/{D}", "\n{YYYY}", null, null, null, null, null, 1], [3600, "{h}{aa}", "\n{M}/{D}/{YY}", null, "\n{M}/{D}", null, null, null, 1], [60, "{h}:{mm}{aa}", "\n{M}/{D}/{YY}", null, "\n{M}/{D}", null, null, null, 1], [1, ":{ss}", "\n{M}/{D}/{YY} {h}:{mm}{aa}", null, "\n{M}/{D} {h}:{mm}{aa}", null, "\n{h}:{mm}{aa}", null, 1], [0.001, ":{ss}.{fff}", "\n{M}/{D}/{YY} {h}:{mm}{aa}", null, "\n{M}/{D} {h}:{mm}{aa}", null, "\n{h}:{mm}{aa}", null, 1], ], // splits: } ], }
spaceis the minimum space between adjacent ticks; a smaller number will result in smaller selected divisors. can also be a function of the form(self, axisIdx, scaleMin, scaleMax, dim) => spacewheredimis the dimension of the plot along the axis in CSS pixels.incrsare divisors available for segmenting the axis to produce ticks. can also be a function of the form(self) => divisors.valuescan be:- a function with the form
(self, ticks, space) => valueswhereticksis an array of raw values along the axis' scale,spaceis the determined tick spacing in CSS pixels andvaluesis an array of formatted tick labels. - array of tick formatters with breakpoints.
- a function with the form