SlideShapes.add_chart()¶
A chart is added to a slide similarly to adding any other shape. Note that
a chart is not itself a shape; the item returned by .add_shape() is
a GraphicFrame
shape which contains a Chart
object. The actual chart
object is accessed using the chart
attribute on the graphic frame
that contains it.
Adding a chart requires three items, a chart type, the position and size
desired, and a ChartData
object specifying the categories and series values
for the new chart.
Protocol¶
Creating a new chart:
>>> chart_data = ChartData()
>>> chart_data.categories = 'Foo', 'Bar'
>>> chart_data.add_series('Series 1', (1.2, 2.3))
>>> chart_data.add_series('Series 2', (3.4, 4.5))
>>> x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4)
>>> graphic_frame = shapes.add_chart(
>>> XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data
>>> )
>>> chart = graphicFrame.chart
Specimen XML¶
Chart in a graphic frame:
<p:graphicFrame>
<p:nvGraphicFramePr>
<p:cNvPr id="2" name="Chart 1"/>
<p:cNvGraphicFramePr/>
<p:nvPr>
<p:extLst>
<p:ext uri="{D42A27DB-BD31-4B8C-83A1-F6EECF244321}">
<p14:modId xmlns:p14="http://schemas.microsoft.com/office/powerpoint/2010/main"
val="11227583"/>
</p:ext>
</p:extLst>
</p:nvPr>
</p:nvGraphicFramePr>
<p:xfrm>
<a:off x="1524000" y="1397000"/>
<a:ext cx="6096000" cy="4064000"/>
</p:xfrm>
<a:graphic>
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart">
<c:chart xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
r:id="rId2"/>
</a:graphicData>
</a:graphic>
</p:graphicFrame>