In Excel a chartsheet is a worksheet that only contains a chart.
The Chartsheet class has some of the functionality of data Worksheets such as tab selection, headers, footers, margins and print properties but its primary purpose is to display a single chart. This makes it different from ordinary data worksheets which can have one or more embedded charts.
Like a data worksheet a chartsheet object isn’t instantiated directly. Instead
a new chartsheet is created by calling the add_chartsheet()
method
from a Workbook object:
workbook = xlsxwriter.Workbook('filename.xlsx')
worksheet = workbook.add_worksheet() # Required for the chart data.
chartsheet = workbook.add_chartsheet()
#...
workbook.close()
A chartsheet object functions as a worksheet and not as a chart. In order to have it display data a Chart object must be created and added to the chartsheet:
chartsheet = workbook.add_chartsheet()
chart = workbook.add_chart({'type': 'bar'})
# Configure the chart.
chartsheet.set_chart(chart)
The data for the chartsheet chart must be contained on a separate worksheet. That is why it is always created in conjunction with at least one data worksheet, as shown above.
set_chart
(chart)Add a chart to a chartsheet.
Parameters: | chart – A chart object. |
---|
The set_chart()
method is used to insert a chart into a chartsheet. A chart
object is created via the Workbook add_chart()
method where the chart
type is specified:
chart = workbook.add_chart({type, 'column'})
chartsheet.set_chart(chart)
Only one chart can be added to an individual chartsheet.
See The Chart Class, Working with Charts and Chart Examples.
The following The Worksheet Class methods are also available through a chartsheet:
activate()
select()
hide()
set_first_sheet()
protect()
set_zoom()
set_tab_color()
set_landscape()
set_portrait()
set_paper()
set_margins()
set_header()
set_footer()
get_name()
For example:
chartsheet.set_tab_color('#FF9900')
The set_zoom()
method can be used to modify the displayed size of the
chart.
See Example: Chartsheet.