R/encharter.R, R/encharter_chart.R, R/encharter_chartex.R
encharter.RdFactory function that initialises an R6 chart object. Returns a Chart
object for standard OOXML chart types (bar, line, scatter, …) or a
ChartEx object for modern extended chart types (waterfall, treemap, …).
The `Chart` class provides a flexible interface to build Office OpenXML (OOXML) chart objects. It allows for granular control over gridlines, secondary axes, and combined chart types (e.g., Bar and Line) within a single plot area.
An R6 class to create and manipulate Office OpenXML (OOXML) Extended Charts (ChartEx), including Waterfall, Sunburst, Treemap, and Region Maps, which are not supported by standard Office Open XML chart types.
encharter(type = "lineChart")
ec(type = "lineChart")An R6 object of class Chart or ChartEx.
Supported Chart Types:
Bar/Column: "barChart", "barplot",
"hist", "histogram"
Line/Area: "lineChart", "line",
"areaChart", "area"
Scatter: "scatterChart", "scatter",
"point"
Pie/Doughnut: "pieChart", "pie",
"doughnutChart", "doughnut"
Extended (ChartEx): "waterfall", "treemap",
"sunburst", "regionMap",
"boxWhisker" / "boxplot", "funnel"
Bar vs Column direction:
For bar/column charts, orientation is set via the dir argument in
$add_series(): "col" (vertical, default) or "bar"
(horizontal).
This class is designed to work with the `openxlsx2` package by generating
the underlying XML required for the add_chart_xml method.
This class uses XML to manipulate the underlying XML structure and integrates with `openxlsx2` for workbook generation.
EncharterBase -> Chart
x2_titleList containing text and style for the secondary X-axis.
y2_titleList containing text and style for the secondary Y-axis.
first_slice_angInteger. Rotation of the first slice (0-360).
expansionInteger. Size of the expansion for pie charts.
hole_sizeInteger. Size of the hole for doughnut charts (0-90).
show_data_tableLogical if a data table should be added.
drop_linesLogical; show lines from points to the axis.
high_low_linesLogical; show lines between max/min points.
up_down_barsLogical; show bars between first and last series.
bubble_scaleNumeric; the scale factor for bubbles (default 100).
show_neg_bubblesLogical; whether to show bubbles with negative values.
disp_blanks_asCharacter; "gap", "span", or "zero".
Inherited methods
Chart$set_x2_title()Set the secondary X-axis title.
Only takes effect if at least one series has been assigned to the
secondary X-axis via add_series(secondary = "x"). Issues a warning
and returns self silently otherwise.
Chart$set_x2_title(
text,
font_size = NULL,
font_name = NULL,
font_color = NULL,
bold = NULL,
italic = NULL,
fill = NULL,
line = NULL,
line_width = NULL
)textTitle string.
font_sizeNumeric font size in points.
font_nameFont typeface name.
font_colorSix-digit hex color for the title text.
bold, italicLogical font style.
fillSix-digit hex color for the title background box.
lineSix-digit hex color for the title border.
line_widthNumeric border width in points.
ec("scatter") |>
add_series(data = "Sheet1!A1:A10", secondary = "x") |>
set_x2_title("Secondary X", font_color = "888888")Chart$set_y2_title()Set the secondary Y-axis title.
Only takes effect if at least one series has been assigned to the
secondary Y-axis via add_series(secondary = TRUE) or
secondary = "y". Issues a warning otherwise.
Chart$set_y2_title(
text,
font_size = NULL,
font_name = NULL,
font_color = NULL,
bold = NULL,
italic = NULL,
fill = NULL,
line = NULL,
line_width = NULL
)textTitle string.
font_sizeNumeric font size in points.
font_nameFont typeface name.
font_colorSix-digit hex color for the title text.
bold, italicLogical font style.
fillSix-digit hex color for the title background box.
lineSix-digit hex color for the title border.
line_widthNumeric border width in points.
ec("line") |>
add_series(data = "Sheet1!A1:A10") |>
add_series(data = "Sheet1!B1:B10", secondary = TRUE) |>
set_y2_title("Growth Rate (%)")Chart$set_y2_axis()Set Secondary Y-axis scaling, units, and format.
Chart$set_y2_axis(
min = NULL,
max = NULL,
major = NULL,
minor = NULL,
major_time = NULL,
minor_time = NULL,
base_time = NULL,
major_tick = NULL,
minor_tick = NULL,
format = NULL,
log_base = NULL,
color = NULL,
font_name = NULL,
font_size = NULL,
bold = NULL,
italic = NULL,
font_color = NULL,
rot = NULL,
grid_color = NULL,
gridlines = NULL,
minor_grid_color = NULL,
minor_gridlines = NULL,
cross_between = NULL,
line_width = NULL,
grid_width = NULL,
minor_grid_width = NULL,
crosses = "max",
crosses_at = NULL,
label_pos = NULL
)minMinimum value for the axis.
maxMaximum value for the axis.
majorNumeric value for major unit interval.
minorNumeric value for minor unit interval.
major_timeTime unit for major steps ("days", "months", "years"). Used for date axes.
minor_timeTime unit for minor steps ("days", "months", "years"). Used for date axes.
base_timeBase time unit for date axes ("days", "months", "years").
major_tick, minor_tickTick marks for major and minor ("cross", "in", "none", "out").
formatA number format string (e.g., "#,##0" or "yyyy-mm-dd").
log_baseBase for logarithmic scaling (e.g., 10).
color, font_colorHex color for the axis lines and label (or independent label color).
font_nameFont typeface name (e.g., "Arial", "Calibri").
font_sizeFont size for the axis labels.
boldLogical; if `TRUE`, axis labels will be bold.
italicLogical; if `TRUE`, axis labels will be italicized.
rotRotation in degrees.
grid_color, minor_grid_colorHex color for the gridlines.
gridlines, minor_gridlinesLogical. Show or hide gridlines.
cross_betweenSpecifies how the value axis crosses the category axis ('between' or 'midCat').
line_width, grid_width, minor_grid_widthNumeric. Change the width of the axis and gridlines.
crossesIntersection: "autoZero" (default), "min" (start), or "max" (end).
crosses_atNumeric axis value for intersection. Overrides 'crosses'.
label_posLabel position: "nextTo" (default), "low" (edge of chart), "high" (opposite edge), or "none".
Chart$set_x2_axis()Set Secondary X-axis scaling, units, and format.
Chart$set_x2_axis(
min = NULL,
max = NULL,
major = NULL,
minor = NULL,
major_time = NULL,
minor_time = NULL,
base_time = NULL,
major_tick = NULL,
minor_tick = NULL,
format = NULL,
log_base = NULL,
color = NULL,
font_name = NULL,
font_size = NULL,
bold = NULL,
italic = NULL,
font_color = NULL,
rot = NULL,
grid_color = NULL,
gridlines = NULL,
minor_grid_color = NULL,
minor_gridlines = NULL,
cross_between = NULL,
line_width = NULL,
grid_width = NULL,
minor_grid_width = NULL,
crosses = "max",
crosses_at = NULL,
label_pos = NULL
)minMinimum value for the axis.
maxMaximum value for the axis.
majorNumeric value for major unit interval.
minorNumeric value for minor unit interval.
major_timeTime unit for major steps ("days", "months", "years"). Used for date axes.
minor_timeTime unit for minor steps ("days", "months", "years"). Used for date axes.
base_timeBase time unit for date axes ("days", "months", "years").
major_tick, minor_tickTick marks for major and minor ("cross", "in", "none", "out").
formatA number format string (e.g., "#,##0" or "yyyy-mm-dd").
log_baseBase for logarithmic scaling (e.g., 10).
color, font_colorHex color for the axis lines and label (or independent label color).
font_nameFont typeface name (e.g., "Arial", "Calibri").
font_sizeFont size for the axis labels.
boldLogical; if `TRUE`, axis labels will be bold.
italicLogical; if `TRUE`, axis labels will be italicized.
rotRotation in degrees.
grid_color, minor_grid_colorHex color for the gridlines.
gridlines, minor_gridlinesLogical. Show or hide gridlines.
cross_betweenSpecifies how the value axis crosses the category axis ('between' or 'midCat').
line_width, grid_width, minor_grid_widthNumeric. Change the width of the axis and gridlines.
crossesIntersection: "autoZero" (default), "min" (start), or "max" (end).
crosses_atNumeric axis value for intersection. Overrides 'crosses'.
label_posLabel position: "nextTo" (default), "low" (edge of chart), "high" (opposite edge), or "none".
Chart$add_series()Add a data series to the chart with independent styling.
Chart$add_series(
header = NULL,
data,
cat = NULL,
z_data = NULL,
color = "4472C4",
type = NULL,
secondary = FALSE,
dir = "col",
grouping = "standard",
overlap = NULL,
gap_width = NULL,
smooth = FALSE,
show_line = TRUE,
marker = "none",
marker_size = 5,
marker_fill = NULL,
marker_line = NULL,
marker_line_width = 0.75,
show_val = NULL,
show_cat = NULL,
line_type = NULL,
line_width = 1,
line_color = NULL,
filled = FALSE,
error_bars = FALSE,
trendline = FALSE
)headerCell range or string for series name.
dataCell range for series values.
catCell range for category labels.
z_dataCell range for bubble sizes (bubbleChart only).
colorPrimary Hex color for the series (used as default for line and markers).
typeChart type for this specific series (for combo charts).
secondaryLogical. Set to TRUE to move series to secondary axis.
dirBar direction ("col" or "bar").
groupingChart grouping ("standard", "stacked", "percentStacked").
overlapInteger between -100 and 100 for bar charts.
gap_widthInteger between 0 and 500 for bar charts.
smoothLogical. Enable line smoothing for line/scatter charts.
show_lineLogical. Show the line connecting points.
markerMarker type ("none", "circle", "square", "diamond", "triangle").
marker_sizeInteger size of marker.
marker_fillHex color for the interior of the marker. Defaults to `color`.
marker_lineHex color for the marker border. Defaults to `color`.
marker_line_widthNumeric width of the marker border.
show_valLogical. Override global label settings for this series (show value).
show_catLogical. Override global label settings for this series (show category).
line_typeLine style: "dashed", "dotted", "dashDot", or "solid".
line_widthNumeric width of the connecting line.
line_colorHex color for the connecting line. Defaults to `color`.
filledLogical; for radar charts, fills the interior area. Default FALSE.
error_barsA list of error bar properties:
type: The error value type (ST_ErrValType).
Must be one of: "fixedVal" (Fixed Value), "percentage" (Percentage),
"stdDev" (Standard Deviation), "stdErr" (Standard Error),
or "cust" (Custom).
value: The numeric value for the error bars (e.g., 10 for 10
direction: Direction of bars. One of "both", "plus", or "minus".
color: Hex color code for the bars (e.g., "FF0000").
trendlineA list of regression line properties:
type: The regression type (ST_TrendlineType).
Must be one of: "linear" (Linear), "exp" (Exponential),
"log" (Logarithmic), "movingAvg" (Moving Average),
"poly" (Polynomial), or "power" (Power).
order: Required for "poly"; an integer between 2 and 6.
period: Required for "movingAvg"; an integer representing the window size.
color: Hex color code for the line.
show_r2: Logical; if TRUE, displays the R-squared value on the chart.
Chart$render()Generate the final XML string for the chart.
Chart$render(
u_ids = c("53178645", "60812428", "64752656", "81893617", "90007639")
)EncharterBase -> ChartEx
color_xmlcolor
style_xmlstyle
Inherited methods
ChartEx$add_series()Add a data series to the chart.
ChartEx$add_series(
header = NULL,
data,
cat = NULL,
type = NULL,
color = "auto",
line_color = NULL,
line_width = 1,
gap_width = NULL,
subtotals = NULL,
statistics = NULL,
binning = NULL,
visibility = NULL,
parent_label = "overlapping"
)headerCell range for the series name.
dataCell range for the numeric values.
catCell range for the category labels.
typeType of chart (waterfall, sunburst, treemap, regionMap).
colorHex color or "auto".
line_colorBorder color.
line_widthBorder width.
gap_widthInteger between 0 and 500.
subtotalsNumeric vector of indices to treat as subtotals (Waterfall only).
statisticsQuartile method: "inclusive" or "exclusive".
binningA list for Histogram/BoxWhisker: `binSize` (numeric), `binCount` (integer), `intervalClosed` ("left", "right"), `underflow` (numeric or "auto"), `overflow` (numeric or "auto").
visibilityA named list of logicals for BoxWhisker/Waterfall: `connectorLines`, `meanLine`, `meanMarker`, `nonoutliers`, `outliers`.
parent_labelTreemap label style: "overlapping", "banner", or "none".
# Standard line chart
ec("lineChart")
#> An encharter object
#> Number of Series: 0
# Extended waterfall chart
ec("waterfall")
#> An encharter object
#> Number of Series: 0
# R-style alias
ec("barplot")
#> An encharter object
#> Number of Series: 0
## ------------------------------------------------
## Method `Chart$set_x2_title()`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ec("scatter") |>
add_series(data = "Sheet1!A1:A10", secondary = "x") |>
set_x2_title("Secondary X", font_color = "888888")
} # }
## ------------------------------------------------
## Method `Chart$set_y2_title()`
## ------------------------------------------------
if (FALSE) { # \dontrun{
ec("line") |>
add_series(data = "Sheet1!A1:A10") |>
add_series(data = "Sheet1!B1:B10", secondary = TRUE) |>
set_y2_title("Growth Rate (%)")
} # }