Project a time series forward with a prediction interval — built into the engine, one option away.
Set showForecast on a 2D scatter or line chart and CanvasXpress fits an exponential-smoothing model to the series, draws the projected values as a dashed line and shades the prediction interval around them. The x axis extends over the forecast horizon automatically.
The engine implements R’s stats::HoltWinters (the same smoothing recursion, start values and prediction interval) and was checked against R on reference series, so a forecast matches what R reports for the same data. Because it lives in the engine, it works the same from JavaScript, R, Python, notebooks and dashboards. When you plot from R with the forecast package, CanvasXpress draws R’s own forecast instead (see From R).
The x values must be regularly spaced (numbers or dates). For monthly data with a yearly cycle, set forecastSeasonLength: 12; the forecast then uses Holt-Winters and projects two seasons ahead by default.
// AirPassengers: monthly totals, 1949-1960 (R datasets)
var cXForecast = new CanvasXpress("canvasForecast", airPassengers, {
graphType: "Scatter2D",
scatterType: "line",
xAxis: ["Year"],
yAxis: ["Passengers"],
showForecast: true,
forecastSeasonLength: 12,
fitLineColor: "#0000AA"
});
forecastMethod selects the model. The default, auto, uses Holt-Winters when a season length is set and Holt otherwise. The smoothing parameters are estimated by minimising the one-step squared error, as R does.
| Method | Models | R equivalent | Needs |
|---|---|---|---|
ses | Level (simple exponential smoothing) | HoltWinters(x, beta = FALSE, gamma = FALSE) | 3+ points |
holt | Level + trend | HoltWinters(x, gamma = FALSE) | 3+ points |
hw | Level + trend + additive seasonality | HoltWinters(x) with frequency m | 2 seasons + 1 point |
A series too short for the requested method falls back from hw to holt to ses. The prediction interval widens with the horizon, as in R’s predict(..., prediction.interval = TRUE).
| Parameter | Default | Description |
|---|---|---|
showForecast | false | true for one forecast, or the name of an annotation for one forecast per group |
forecastMethod | "auto" | auto, ses, holt or hw |
forecastHorizon | 0 | Steps to project; 0 means 10, or two seasons when seasonal |
forecastSeasonLength | 0 | Observations per season (12 for monthly data with a yearly cycle, 7 for daily data with a weekly cycle); 0 or 1 disables seasonality |
showForecastInterval | true | Show the prediction interval |
forecastLevel | 0.95 | Interval level, as a fraction or a percentage (80 is read as 0.8) |
forecastLineType | "dashed" | dashed, solid or dotted |
The line color follows fitLineColor and a single forecast’s band follows confidenceIntervalColor, like the regression and LOESS fits.
Name an annotation in showForecast to fit a separate model for each group. When the groups are also the colorBy groups, each projection and band takes its series’ color. Use this whenever the chart holds several series: pooled together they are not one regular series, so showForecast: true draws nothing. The second carrier below is an illustrative rescaled copy of AirPassengers.
var cXGroups = new CanvasXpress("canvasGroups", carriers, {
graphType: "Scatter2D",
scatterType: "line",
xAxis: ["Year"],
yAxis: ["Passengers"],
lineBy: "Carrier",
colorBy: "Carrier",
showForecast: "Carrier",
forecastSeasonLength: 12
});
Dates work too: monthly or daily dates count as regularly spaced (calendar months and daylight-saving days are within tolerance) and the projection continues at the same step. This chart also sets an 80% interval with forecastLevel.
// USAccDeaths: monthly US accidental deaths, 1973-1978 (R datasets)
var cXDates = new CanvasXpress("canvasDates", accidentalDeaths, {
graphType: "Scatter2D",
scatterType: "line",
xAxis: ["Month"],
yAxis: ["Deaths"],
xAxisTime: true,
showForecast: true,
forecastSeasonLength: 12,
forecastHorizon: 12,
forecastLevel: 0.8,
fitLineColor: "#0000AA"
});
In R, set the same options on canvasXpress(). If you already use the forecast package, convert the ggplot directly: both autoplot(forecast(...)) and geom_forecast() are drawn from R’s own forecast — whatever model R fitted (ets, arima, HoltWinters…) — with each interval level shaded in the same colors ggplot uses. Nothing is refitted in the browser.
library(canvasXpress)
library(forecast)
# native engine forecast
df <- data.frame(Year = as.numeric(time(AirPassengers)),
Passengers = as.numeric(AirPassengers))
canvasXpress(data = df, graphType = "Scatter2D", scatterType = "line",
xAxis = list("Year"), yAxis = list("Passengers"),
showForecast = TRUE, forecastSeasonLength = 12)
# R's forecast, drawn as-is
g <- autoplot(forecast(ets(USAccDeaths), h = 12))
canvasXpress(g)
No code is needed to try it: right-click a 2D scatter or line chart and open Forecast to show it, fit it per group, and change the method, horizon, season length, interval and line type. The same controls are in the Customizer under Tools.