traces: Add a New Trace to a 'chartjs' Plot

Description Usage Arguments Details Value Examples

Description

- make_dataset is an internal function used to make a dataset object in the format required for chartjs objects. - new_trace is the general function used to add a new trace to a chartjs object. - new_bars/new_lines/new_scatter are helper functions for new_trace where type = "bars"/"lines"/"scatter", respectively.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
make_dataset(
  data,
  y,
  label = NULL,
  type,
  ...,
  xAxis = NULL,
  yAxis = NULL,
  order = NULL,
  legend = NULL,
  fill = (type != "line"),
  bgCol = NULL,
  brdCol = bgCol,
  brdWidth = NULL,
  radius = NULL,
  hoverRadius = NULL,
  tension = NULL,
  stepped = list(NULL, FALSE, TRUE, "before", "after", "middle")[[1]],
  barPerc = NULL,
  catPerc = NULL
)

new_trace(chart, y, label = guess_label(substitute(y)), yAxis = "y", type, ...)

new_bars(chart, y, label = guess_label(substitute(y)), ...)

new_lines(chart, y, label = guess_label(substitute(y)), ...)

new_scatter(chart, y, label = guess_label(substitute(y)), ...)

Arguments

data

The raw data object that y will be evaluated inside of.

y

An expression that evaluates to a vector (see x in eval_data).

label

The label to use for the trace (character scalar).

type

The type of trace (character scalar, one of c("bar", "line", "scatter")).

...

Additional named arguments to add to the data.

xAxis, yAxis

The name of the x/y-axis to add the trace to (character scalar).

order

Controls the order that the traces are drawn in (0 is the default, lower numbers go on top, higher on bottom) (integer scalar).

legend

Whether to show the trace in the legend (boolean scalar).

fill

Whether to fill the trace in (see more in the details section).

bgCol, brdCol/brdWidth

The color/width of the background/border of the trace (character scalar).

radius, hoverRadius

The radius of the points/radius when hovered over (numeric scalar).

tension

The tension of the lines between the points (0 for straight lines, 0.4 is default) (numeric scalar).

stepped

Whether to draw the lines as stepped, or how to draw them as stepped (boolean/character scalar).

barPerc

Fraction of the available width each bar should be within the category width (numeric scalar in [0, 1]).

catPerc

Fraction of the available width each category should be within the sample width (numeric scalar in [0, 1]).

chart

A chartjs object.

Details

To see the various ways that fill can be used, look at the "Area charts" examples on this page: https://www.chartjs.org/samples/latest/.

For more information on barPerc/catPerc, see here: https://www.chartjs.org/docs/latest/charts/bar.html#barpercentage-vs-categorypercentage.

Value

make_dataset: a dataset object in the format required for chartjs objects.

new_trace: a modified chartjs object.

Examples

1
2
3
4
5
6
7
8
9
# Individual traces
chartjs(mtcars, x = 1:32) %>% new_trace(y = ~ mpg, type = "bar")
chartjs(mtcars, x = 1:32) %>% new_bars(y = ~ mpg)
chartjs(mtcars, x = 1:32) %>% new_lines(y = ~ mpg)
chartjs(mtcars, x = 1:32) %>% new_scatter(y = ~ mpg)

# Add multiple traces to the same chart
chartjs(mtcars, x = 1:32) %>% new_bars(y = ~ cyl) %>%
  new_lines(y = ~ mpg) %>% new_scatter(y = ~ qsec)

KO112/chartjs documentation built on Aug. 6, 2020, 2:35 p.m.