XML technologies / SpreadsheetML / DrawingML
The article SpreadsheetML explained the OOXML table model in detail. It mainly implements functions already available in the previous version of SpreadsheetML for Office 2003.
Within OOXML, DrawingML represents objects placed on worksheets and integrates them into the Excel document. Objects include images, clip art, shapes, SmartArt, charts and text boxes. This article covers only images and charts.
DrawingML handles both embedding and rendering objects, using different namespaces in different parts (see the table "Namespaces in DrawingML").
namespace URI | Purpose | |
|---|---|---|
xdr | schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing | Embedding objects |
a | schemas.openxmlformats.org/drawingml/2006/main | Rendering text objects |
c | schemas.openxmlformats.org/drawingml/2006/chart | Rendering charts |
Table: namespaces in DrawingML
Part one of OOXML also describes DrawingML, distinguishing object embedding and rendering in separate chapters. The following discussion is based on these chapters and Excel’s behaviour.
The following parts embed charts and images in Excel documents:
/xl/drawings/drawing[i].xml
/xl/charts/chart[i].xml
/xl/media/image[i].[ Format ]
The first parts define the frame placing a chart or image on a worksheet, linking the sheet to the object and specifying its position, size and aspect ratio. Each sheet may have at most one drawing part. It handles all objects placed on that sheet.
The chart parts define charts, including columns, lines, pies, areas and scatter plots. They also contain the displayed data and references to the worksheet cells holding it.
A special case is the image parts describing worksheet images. They are binary image files copied into the container, not XML. Only the copy’s name differs from the original. The extension in the part name above is represented by [Format]. Part numbering ([i]) is independent of it. For example, if the images logo_gross.jpg, logo_mittel.png and logo_klein.jpg are embedded in Excel, the first is copied as the part /xl/media/image1.jpg into the XLSX container, the second as /xl/media/image2.png and the last as /xl/media/image3.jpg.
Embedding in SpreadsheetML
To give a worksheet a drawing part, add at the end of the sheet part a <drawing> element.
<worksheet>
[...]
<drawing r:id="rId1"/>
</worksheet>The r:id attribute references the relationship in the associated relationship part. That relationship identifies the drawing part.
drawing part
The drawing part offers three ways to place objects, represented by three top-level types inside <xdr:wsDr>. <xdr:twoCellAnchor> defines the frame by anchors in <xdr:from> and <xdr:to>, its top-left and bottom-right corners. <xdr:oneCellAnchor> instead specifies the top-left corner through <xdr:from> and the frame’s width and height through <xdr:ext>. The third option, <xdr:absoluteAnchor>, places a chart on its own sheet without spreadsheet functions. <xdr:pos> gives its top-left position and <xdr:ext> its width and height. Unlike <xdr:oneCellAnchor>, where <xdr:from> locates the corner by row and column, <xdr:pos> gives absolute distances from the top and left edges.
Positioning
<xdr:from>, <xdr:to>, <xdr:ext> and <xdr:pos> define frame position and dimensions. The first two use cells; the other two specify absolute positions or widths and heights.
<xdr:from> and <xdr:to> share a content model: <xdr:col>, <xdr:colOff>, <xdr:row> and <xdr:rowOff>. <xdr:col> and <xdr:row> contain positive integers identifying column and row indices, with column A and the first row indexed 0.
Within the cell, <xdr:colOff> and <xdr:rowOff> set the anchor’s distance from its left and top edges. If an offset exceeds the cell’s width or height, the point is placed at its right or bottom edge.
<xdr:from>
<xdr:col>11</xdr:col>
<xdr:colOff>752475</xdr:colOff>
<xdr:row>0</xdr:row>
<xdr:rowOff>66675</xdr:rowOff>
</xdr:from>The example’s top-left corner lies in L1, since L is the twelfth column. Distances in <xdr:colOff> and <xdr:rowOff>, like all DrawingML lengths, use English Metric Units (EMU), Microsoft’s "high precision coordinate space" ( ECMA-376 1st edition Part 4, page 3694). One EMU is 1/360000 cm or 1/914400 inch (web article by Rick Jelliffe).
<xdr:pos> and <xdr:ext> each use two attributes for position or dimensions. For <xdr:ext>, these are cx and cy, giving width (cx) and height (cy) in EMUs. Dimensions cannot be automatic, so fixed values may distort images.
The absolute frame position uses the <xdr:pos> attributes x and y.
Objects
Positioning works independently of object type. The following element selects the object: <xdr:graphicFrame>, <xdr:pic>, <xdr:sp>, <xdr:grpSp>, <xdr:cxnSp> or <xdr:contentPart>.
<xdr:graphicFrame> inserts a chart; <xdr:pic> references an image file. Other elements insert shapes or text frames and are not described further here.
Graphic frame
Besides charts, a graphic frame can hold SmartArt, not covered here. This example shows an <xdr:graphicFrame> containing a chart:
<xdr:graphicFrame>
<xdr:nvGraphicFramePr>
<xdr:cNvPr id="2" name="Diagramm 1"/>
<xdr:cNvGraphicFramePr/>
</xdr:nvGraphicFramePr>
<xdr:xfrm/>
<a:graphic>
<a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/chart">
<c:chart r:id="rId1"/>
</a:graphicData>
</a:graphic>
</xdr:graphicFrame>The key is <a:graphic> and its <a:graphicData> child, establishing a relationship to another part. The <a:graphicData> element indicates the content type through its uri attribute containing the referenced part’s root namespace URI.
Its child already belongs to this namespace. For charts, it is <c:chart> in the namespace schemas.openxmlformats.org/drawingml/2006/chart. Besides namespace declarations, <c:chart> carries the attribute r:id, referencing a relationship in the associated relationship part, which identifies the chart part.
Image object
Using <xdr:pic> instead of a graphic frame inserts an image. The following example shows its placement:
<xdr:pic>
<xdr:nvPicPr>
<xdr:cNvPr id="2" name="Grafik 2"/>
<xdr:cNvPicPr/>
</xdr:nvPicPr>
<xdr:blipFill>
<a:blip r:embed="rId2" cstate="print"/>
<a:stretch/>
</xdr:blipFill>
<xdr:spPr>
<a:prstGeom prst="rect"/>
</xdr:spPr>
</xdr:pic>Again, we examine only the main elements. <a:blip> inside <xdr:blibFill> establishes a relationship to another part. Here, the r:embed attribute references the relationship in the associated relationship part, which identifies the image part.
Charts
Fig. 13: Excel’s standard chart types
Charts include columns, scatter plots, lines and other types. This article covers Excel’s standard chart types (Figure 13), excluding 3D charts for now. We first describe shared structures, then type-specific features using line charts, followed by other standard types through their differences from line charts.
Structural view
Fig. 14: chart areas
A chart has several areas (Figure 14):
Title
Legend
Plot area
Matrix
Data is drawn only in the chart matrix. Its rendering distinguishes these chart types, among others:
Column chart
Line chart
Bar chart
Scatter plot
Area chart
Pie chart
...
Other areas—legend, title and axes—are largely shared between chart types and use the same XML structures. Pie charts are the exception because they have no axes.
Chart data must be grouped into series, each containing any number of values or value pairs in a fixed order that some chart types may ignore. All listed types allow multiple series except pie charts, which show only one.
Structure in DrawingML
The same structure appears in the chart parts, responsible for DrawingML chart rendering:
<c:chartSpace>
<c:chart>
<c:title>
[...]
</c:title>
<c:plotArea>
[...]
</c:plotArea>
<c:legend>
[...]
</c:legend>
</c:chart>
[...]
</c:chartSpace> Each child of <c:chart> represents an area (Figure 14): <c:title> the title, <c:plotArea> the plot area and <c:legend> the legend. Only <c:plotArea> is required. Omitting <c:legend> or <c:title> omits that area.
Legend
An empty <c:legend> generates and places the legend automatically. Since it usually derives from chart data and DrawingML describes manual changes, we discuss only placement. <c:legend> receives <c:legendPos> and <c:layout> children for this purpose.
Reserved positioning area
The empty <c:legendPos> specifies, through the val attribute, placement relative to <c:plotArea>: right (r), left (l), above (t) or below (b), or upper-right (tr). Besides default placement unless <c:layout> overrides it, this reserves space by shrinking the plot area. With a right-side legend (<c:legendPos val="r"/>), the plot area leaves enough space on the frame’s right. This applies only to automatically positioned plot areas.
Fig. 15: upper-right legend placement
Without other settings, the legend is centred vertically and horizontally in its reserved area. Other placements work similarly, except <c:legendPos val="tr"/>, which reserves space both above and to the right. The default legend occupies the rectangle between the top-right corners of the plot area and outer frame (Figure 15).
Manual positioning
For manual positioning, <c:layout> contains <c:manualLayout>:
<c:legend>
<c:legendPos val="r"/>
<c:layout>
<c:manualLayout>
<c:xMode val="edge"/>
<c:yMode val="edge"/>
<c:x val="0.80"/>
<c:y val="0.40"/>
<c:w val="0.20"/>
<c:h val="0.20"/>
</c:manualLayout>
</c:layout>
</c:legend>For now, ignore <c:xMode> and <c:yMode>, which merely ensure absolute positioning from the outer frame’s left and top edges. Values of the val attributes are factors of frame width (<c:x> and <c:w>) or height (<c:y> and <c:h>). The val attribute of <c:x> gives the left offset, that of <c:y> the top offset, <c:w> the legend width and <c:h> its height (Figure 17):
Fig. 16: positioning the legend with factors
If <c:manualLayout> has no <c:><w> or <c:><h>, legend width or height is calculated from its content. Without <c:x> or <c:y>, the corresponding default position from <c:legendPos> is used.
If the val attributes of <c:xMode> and <c:yMode> replace edge with factor, positioning is no longer necessarily relative to the frame’s top-left corner, but depends on <c:legendPos>. Its unusual behaviour is not relevant enough to discuss here.
Title
Like legends, an empty <c:title> creates an automatically placed default title (Excel: "Diagrammtitel"). Manual positioning uses a <c:layout> child of <c:title>, just as for legends.
Reserved area
Instead of <c:legendPos>, <c:title> receives <c:overlay>. Its val attribute takes a Boolean value. Values 0 or false reserve room above the plot area, whereas these values make title and plot areas overlap: 1 or true. Reserved space is always above. <c:overlay> is also honoured only when the plot area is not manually positioned.
Title content
<c:tx> defines title content, either fixed text or a cell reference. Both approaches are outlined below.
Fixed-text title
Fixed text uses a <c:rich> child of <c:tx>, with a content model resembling WordML structures:
<c:title>
<c:tx>
<c:rich>
<a:bodyPr/>
<a:p>
<a:r>
<a:t>Diagrammtitel</a:t>
</a:r>
</a:p>
</c:rich>
</c:tx>
<c:layout/>
</c:title><a:p> represents a paragraph, <a:r> divides it into uniformly formatted runs and <a:t> contains their text. This supports inline formatting, which we will not describe here.
Referenced title
Replacing <c:rich> with a <c:strRef> child of <c:tx> creates a reference. Its <c:f> child contains a cell reference or Excel formula calculating the title:
<c:tx>
<c:strRef>
<c:f>Tabelle1!A1</c:f>
</c:strRef>
</c:tx>Cell references must always include the worksheet name. Ordinary Excel formulas require this only across sheets, but charts require absolute references of this kind. The notation is: Tabellenblattname!Zellenname. In the example, Tabelle1 is the worksheet name, and A1 is the referenced cell on that sheet.
Plot area
The plot area consists of matrix and axes. Besides a type-specific matrix element, <c:plotArea> has <c:valAx> or <c:catAx> for axes and <c:layout> for positioning:
<c:plotArea>
<c:layout>
[...]
</c:layout>
[Element zur Beschreibung der Matrix]
<c:catAx>
[...]
</c:catAx>
<c:valAx>
[...]
</c:valAx>
</c:plotArea>Positioning
Plot positioning is like legend positioning, except that its <c:manualLayout> may also contain <c:layoutTarget>:
<c:layout>
<c:manualLayout>
<c:layoutTarget val="inner"/>
[...übliche Positionierung analog zum c:legend-Element...]
</c:manualLayout>
</c:layout>Fig. 17: outer (l) and inner (r) plot-area boundaries
The value of the val attribute selects the inner (inner) or outer (outer) plot-area boundaries. Outer boundaries include axes and labels; inner boundaries enclose only the chart matrix.
axes
Charts usually have two axes, but may have more. Besides specialised axes such as dates, DrawingML distinguishes numeric axes (<c:valAx>) and category axes (<c:catAx>).
A category axis has discrete, equally sized sections, one per category, with no inherent category order. Numeric axes are continuous and can use arbitrarily small steps. Category values select a category section, while numeric values determine position numerically.
A column chart has a horizontal category axis and vertical numeric axis. Scatter plots instead have two numeric axes. Bar charts reverse the column chart’s axes: horizontal numeric and vertical category.
Category axis
As noted, <c:catAx> inside <c:plotArea> defines a category axis:
<c:catAx>
<c:axId val="74518912"/>
<c:scaling>
<c:orientation val="minMax"/>
</c:scaling>
<c:axPos val="b"/>
<c:crossAx val="74521216"/>
</c:catAx>The <c:axID> element uses the val attribute to assign an axis ID for references. <c:scaling> defines scaling and may contain <c:orientation>, which uses the val attribute to set orientation to either minMax and maxMin. The first preserves the specified category order; the second reverses it.
The other required children of <c:catAx> are <c:axPos> and <c:crossAx>. <c:axPos> uses the val attribute to place the axis above (t), below (b), right (r) or left (l) of the matrix. This setting appears to have no observable effect in Excel; axis position seems determined by chart type. <c:crossAx> also uses the val attribute to identify the intersecting axis by its ID.
Numeric axis
<c:valAx> identifies a numeric axis. For the elements discussed here, its content model is the same as a category axis:
<c:valAx>
<c:axId val="74521216"/>
<c:scaling>
<c:orientation val="minMax"/>
</c:scaling>
<c:axPos val="l"/>
<c:crossAx val="74518912"/>
</c:valAx> Line chart
Fig. 18: line chart
Excel line charts have a horizontal category x-axis and vertical numeric y-axis. Each value becomes a point at its category and numeric height. Categories follow series order, with one value per category. Points are normally connected by lines, hence the name, but may also be displayed unconnected.
Structure
<c:plotArea> must therefore contain <c:catAx>, <c:valAx> and the type-specific <c:lineChart>:
<c:plotArea>
<c:layout/>
<c:lineChart>
<c:grouping val="standard"/>
<c:ser>[...]</c:ser>
<c:ser>[...]</c:ser>
<c:ser>[...]</c:ser>
<c:axId val="73934720"/>
<c:axId val="73936256"/>
</c:lineChart>
<c:catAx>
<c:axId val="73934720"/>
[...]
</c:catAx>
<c:valAx>
<c:axId val="73936256"/>
[...]
</c:valAx>
</c:plotArea><c:lineChart> contains a <c:ser> per series and references each axis through <c:axId>, using IDs defined in <c:catAx> or <c:valAx>. Reference order matters: x-axis first, y-axis second.
The <c:grouping> element uses its val attribute to select these settings (Table 8):
Attribute value | Meaning |
|---|---|
standard | Values are plotted as already described. |
stacked | Values are stacked by category. The first series is unchanged; each later value adds the sum of preceding series in that category. Series order is therefore important and is explained below. The last series contains each category’s total. |
percentStacked | As with stacked, values are stacked by category, but displayed as a percentage of that category’s total. The last series is therefore 100% in every category. |
Table: values of the val attribute on <c:grouping>
Data series
The <c:ser> element receives an index through the val attribute of <c:idx>. This orders series, affecting stacking and default names such as "Datenreihen 1", "Datenreihen 2". The required <c:order> supposedly serves a similar purpose, but no rendering effect is visible, leaving its exact role unclear. The remaining structure is shown here:
<c:ser>
<c:idx val="0"/>
<c:order val="0"/>
<c:cat>
<c:strRef><c:f>Tabelle1!A2:A6</c:f></c:strRef>
</c:cat>
<c:val>
<c:numRef><c:f>Tabelle1!B2:B6</c:f></c:numRef>
</c:val>
</c:ser>
<c:ser>
<c:idx val="1"/>
<c:order val="1"/>
<c:val>
<c:numLit>
<c:pt idx="0">
<c:v>1</c:v>
</c:pt>
<c:pt idx="1">
<c:v>2</c:v>
</c:pt>
[...]
</c:numLit>
</c:val>
</c:ser>
<c:ser>
<c:idx val="2"/>
<c:order val="2"/>
[...]
</c:ser>Series values
For series values, <c:ser> has <c:val>, supporting a cell-range reference as in the first <c:ser> example, or fixed XML values as in the second series.
References use <c:numRef>; fixed values use <c:numLit>. For references, <c:f> contains an absolute Excel cell-range formula. The range must be a single row or column, ordered left to right or top to bottom. Here the order is B2, B3, B4, B5, B6.
<c:numLit> contains one <c:pt> per value, with an idx attribute specifying the value’s series index and a <c:v> child holding it.
Defining categories
At least one series has <c:cat> for categories. Only the <c:cat> of the lowest-index series is used. The content model of <c:cat> resembles <c:val>, supporting references or fixed values, but distinguishes numeric and string categories.
Numeric categories use <c:numRef> and <c:numLit> as before; strings use <c:strRef> and <c:strLit>. <c:strRef> references cells like <c:numRef>, while <c:strLit>, like <c:numLit>, holds <c:pt> elements for fixed categories.
Series name
A <c:tx> child of <c:ser> defines the series name, used for example in the legend:
<c:ser>
<c:idx val="0"/>
<c:order val="0"/>
<c:tx>
<c:strRef>
<c:f>Tabelle1!C1</c:f>
</c:strRef>
</c:tx>
[...]
</c:ser><c:tx> may reference a cell through <c:strRef>, or use a <c:v> child containing a fixed name string.
Area chart
An Excel area chart differs from a line chart only visually: the area between line and x-axis is filled with colour.
In DrawingML, the main difference is replacing the enclosing <c:lineChart> with <c:areaChart>:
<c:areaChart>
<c:grouping val="standard"/>
<c:ser>[...]</c:ser>
<c:ser>[...]</c:ser>
<c:ser>[...]</c:ser>
<c:axId val="73143040"/>
<c:axId val="73144576"/>
</c:areaChart>Areas of higher-index series cover lower-index series.
Column chart
Fig. 19: column chart
Column charts share line-chart axes and value positioning. Instead of connecting points, they draw narrow rectangles from the x-axis to each point, their height representing the y-value. Width is arbitrary and equal for all values. Unlike line-chart points aligned vertically within a category, columns are offset horizontally to avoid overlap.
Structure
The defining column-chart element is <c:barChart>:
<c:barChart>
<c:barDir val="col"/>
[...]
</c:barChart>To distinguish bars, which also use <c:barChart>, a <c:barDir> child has these possible values col and bar for the val attribute: the first means columns, the second bars.
Because column and line charts are structurally equivalent, there are few other DrawingML differences. The <c:lineChart> children described above also apply to <c:barChart>.
Bar chart
Bars and columns differ only in perspective: columns use category x and numeric y axes; bars reverse them. A bar chart is a column chart rotated 90°.
Their only DrawingML difference is therefore the value of the val attribute of <c:barDir> (bar).
Pie chart
Pie charts show one series without axes. Values are represented by their share of the total: the circle is divided into sectors, each assigned to one value, with proportional size.
Values have categories, which a legend can identify for each sector.
Because pie charts still have a series of values and categories, their main DrawingML structure resembles a line chart:
<c:plotArea>
<c:pieChart>
<c:varyColors val="1"/>
<c:ser>
<c:idx val="1"/>
<c:order val="0"/>
<c:tx>[...]</c:tx>
<c:cat>[...]</c:cat>
<c:val>[...]</c:val>
</c:ser>
</c:pieChart>
</c:plotArea>Pie plot areas have no axis elements. Their defining <c:pieChart> therefore has no <c:axId> and only one <c:ser>. The <c:varyColors> element uses a Boolean value in its val attribute to choose one shared colour (0 or false) or different colours (1 or true).
Scatter plot
Apart from pies, scatter plots differ most from the preceding types. Both horizontal and vertical axes are numeric. Series contain x/y value pairs, plotted by both values rather than categories. There is therefore no fixed order of pairs.
Fig. 20: creating scatter-plot value pairs
Excel defines a series’ pairs through separate x- and y-value sequences (Figure 20). Their order determines pairing: the first x and first y form the first pair.
Structure
The defining scatter-plot element is <c:scatterChart>:
<c:scatterChart>
<c:scatterStyle val="line"/>
<c:ser>[...]</c:ser>
<c:ser>[...]</c:ser>
<c:axId val="77213056"/>
<c:axId val="79689216"/>
</c:scatterChart>Instead of <c:grouping>, it contains <c:scatterStyle>, intended to select markers only (marker), connected lines (line) or smoothed lines (smooth). This setting seems to have no effect in Excel: points are always connected by unsmoothed lines.
<c:scatter><Chart> otherwise allows any number of <c:ser> and two <c:axId> elements. Series use the former; the latter reference numeric axes. The first <c:axId> references x, the second y.
Data series
A series now holds value pairs, or two value sequences, so its <c:ser> structure differs from preceding charts with only one value sequence.
<c:ser>
<c:idx val="0"/>
<c:order val="0"/>
<c:tx>[...]</c:tx>
<c:xVal>
<c:numRef><c:f>Tabelle1!B2:B6</c:f></c:numRef>
</c:xVal>
<c:yVal>
<c:numRef><c:f>Tabelle1!C2:C6</c:f></c:numRef>
</c:yVal>
</c:ser><c:idx>, <c:order> and <c:tx> remain unchanged. Instead of <c:val>, use <c:xVal> and <c:yVal> for x- and y-value sequences. As with <c:val>, their <c:numRef> and <c:numLit> children support cell references or fixed values.
|