% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/xlsx_add_chart.R
\name{sheet_add_drawing.ms_chart}
\alias{sheet_add_drawing.ms_chart}
\title{Add an ms_chart to an Excel sheet}
\usage{
\method{sheet_add_drawing}{ms_chart}(
  x,
  value,
  sheet,
  start_col = 1L,
  start_row = 1L,
  write_data = TRUE,
  left = 1,
  top = 1,
  width = 6,
  height = 4,
  ...
)
}
\arguments{
\item{x}{an rxlsx object (created by \code{\link[officer:read_xlsx]{officer::read_xlsx()}})}

\item{value}{an \code{ms_chart} object}

\item{sheet}{sheet name where the chart and its data will be placed.
The sheet must already exist (see \code{\link[officer:add_sheet]{officer::add_sheet()}}).}

\item{start_col}{column index where chart data will be written
(default 1, i.e. column A). When \code{write_data = FALSE}, this is still
the cell position that the chart XML will point at, but no data is
written.}

\item{start_row}{row index where chart data will be written
(default 1). Same semantics with \code{write_data = FALSE} as for
\code{start_col}.}

\item{write_data}{if \code{TRUE} (the default), the chart's
\code{data_series} is written into the sheet at
\verb{(start_col, start_row)} before the chart is added.
Pass \code{FALSE} when the data is already present in the sheet (for
example to avoid rewriting it when several charts share the same
dataset, or when inserting a chart that references data written
independently via \code{\link[officer:sheet_write_data]{officer::sheet_write_data()}}).}

\item{left, top}{top-left anchor of the chart, in inches.
Defaults to \verb{(1, 1)}. Same convention as
\code{\link[officer:sheet_add_drawing]{officer::sheet_add_drawing()}} and \code{rvg::sheet_add_drawing.dml()}.}

\item{width, height}{size of the chart, in inches. Defaults
to \verb{6 x 4}.}

\item{...}{unused}
}
\value{
the rxlsx object (invisibly)
}
\description{
Add an \code{ms_chart} object to a sheet in an xlsx workbook
created with \code{\link[officer:read_xlsx]{officer::read_xlsx()}}. The chart data is written into
the sheet and the chart is displayed at the specified position.
}
\examples{
library(officer)
library(mschart)

my_chart <- ms_barchart(
  data = data.frame(
    x = c("A", "B", "C"),
    y = c(1, 3, 2),
    group = rep("serie1", 3)
  ),
  x = "x", y = "y", group = "group"
)

x <- read_xlsx()
x <- add_sheet(x, label = "chart_sheet")
x <- sheet_add_drawing(x, value = my_chart, sheet = "chart_sheet")
print(x, target = tempfile(fileext = ".xlsx"))

# Sharing one dataset between several charts on the same sheet:
# write the data once, then add each chart with write_data = FALSE.
shared <- data.frame(
  x = c("A", "B", "C"),
  y = c(1, 3, 2),
  group = rep("serie1", 3)
)
chart_a <- ms_barchart(shared, x = "x", y = "y", group = "group")
chart_b <- ms_linechart(shared, x = "x", y = "y", group = "group")

x <- read_xlsx()
x <- add_sheet(x, label = "multi")
x <- sheet_write_data(x, value = chart_a$data_series, sheet = "multi")
x <- sheet_add_drawing(x, value = chart_a, sheet = "multi",
                       write_data = FALSE,
                       left = 3, top = 0.5, width = 5, height = 3.5)
x <- sheet_add_drawing(x, value = chart_b, sheet = "multi",
                       write_data = FALSE,
                       left = 9, top = 0.5, width = 5, height = 3.5)
print(x, target = tempfile(fileext = ".xlsx"))
}
