def drawChart(_document, _values, \ chartWidth, chartHeight, chartType): # Create the subdocument chartdoc = OpenDocumentChart() mychart = PieChart(chartWidth, chartHeight, chartType) # These represent the data. Six rows in three columns mychart.values = _values mychart.datasourcehaslabels = "both" mychart(chartdoc) p = P() _document.add(p) # Create the frame. df = draw.Frame(width=chartWidth, height=chartHeight, anchortype="paragraph") p.addElement(df) # Here we add the subdocument to the main document. We get back a reference # to use in the href. objectloc = _document.addAsObject(chartdoc) do = draw.Object(href=objectloc) # Put the object inside the frame df.addElement(do)
# Title for the Y axis yaxis = chart.Axis(dimension="y", name="primary-y", stylename=axisstyle) plotarea.addElement(yaxis) yt = chart.Title() yaxis.addElement(yt) yt.addElement(text.P(text=self.y_axis)) # Data area datatable = DataTable( self.values ) datatable.datasourcehaslabels = self.datasourcehaslabels mychart.addElement(datatable()) if __name__ == "__main__": # Create the document doc = OpenDocumentChart() mychart = BarChart() mychart.title = "SPECTRE" mychart.subtitle = "SPecial Executive for Counter-intelligence, Terrorism, Revenge and Extortion" mychart.x_axis = u"Divisions" mychart.y_axis = u"€ (thousand)" # These represent the data. Six rows in three columns mychart.values = ( ('', 'Expense', 'Revenue'), ('Counterfeit', 1000, 1500), ('Murder', 1100, 1150), ('Prostitution', 3200, 2350), ('Blackmail', 1100, 1150), ('Larceny', 1000, 1750) ) mychart.datasourcehaslabels = "both"
def test_chart_style(self): """ Test that chart:style-name reference is seen in content.xml """ doc = OpenDocumentChart() chartstyle = Style(name="chartstyle", family="chart") chartstyle.addElement( GraphicProperties(stroke="none", fillcolor="#ffffff")) doc.automaticstyles.addElement(chartstyle) mychart = chart.Chart(width="576pt", height="504pt", stylename=chartstyle, attributes={'class': 'chart:bar'}) doc.chart.addElement(mychart) # Add title titlestyle = Style(name="titlestyle", family="chart") titlestyle.addElement(GraphicProperties(stroke="none", fill="none")) titlestyle.addElement( TextProperties(fontfamily="'Nimbus Sans L'", fontfamilygeneric="swiss", fontpitch="variable", fontsize="13pt")) doc.automaticstyles.addElement(titlestyle) mytitle = chart.Title(x="385pt", y="27pt", stylename=titlestyle) mytitle.addElement(text.P(text="Title")) mychart.addElement(mytitle) # Add subtitle subtitlestyle = Style(name="subtitlestyle", family="chart") subtitlestyle.addElement(GraphicProperties(stroke="none", fill="none")) subtitlestyle.addElement( TextProperties(fontfamily="'Nimbus Sans L'", fontfamilygeneric="swiss", fontpitch="variable", fontsize="10pt")) doc.automaticstyles.addElement(subtitlestyle) subtitle = chart.Subtitle(x="0pt", y="123pt", stylename=subtitlestyle) subtitle.addElement(text.P(text="my subtitle")) mychart.addElement(subtitle) # Legend legendstyle = Style(name="legendstyle", family="chart") legendstyle.addElement(GraphicProperties(fill="none")) legendstyle.addElement( TextProperties(fontfamily="'Nimbus Sans L'", fontfamilygeneric="swiss", fontpitch="variable", fontsize="6pt")) doc.automaticstyles.addElement(legendstyle) mylegend = chart.Legend(legendposition="end", legendalign="center", stylename=legendstyle) mychart.addElement(mylegend) # Plot area plotstyle = Style(name="plotstyle", family="chart") plotstyle.addElement( ChartProperties(seriessource="columns", percentage="false", stacked="false", threedimensional="true")) doc.automaticstyles.addElement(plotstyle) plotarea = chart.PlotArea(datasourcehaslabels="both", stylename=plotstyle) mychart.addElement(plotarea) # Style for the X,Y axes axisstyle = Style(name="axisstyle", family="chart") axisstyle.addElement(ChartProperties(displaylabel="true")) doc.automaticstyles.addElement(axisstyle) # Title for the X axis xaxis = chart.Axis(dimension="x", name="primary-x", stylename=axisstyle) plotarea.addElement(xaxis) xt = chart.Title() xaxis.addElement(xt) xt.addElement(text.P(text="x_axis")) # Title for the Y axis yaxis = chart.Axis(dimension="y", name="primary-y", stylename=axisstyle) plotarea.addElement(yaxis) yt = chart.Title() yaxis.addElement(yt) yt.addElement(text.P(text="y_axis")) result = doc.contentxml() # contentxml() is supposed to yeld a bytes self.assertNotEqual(-1, result.find(b'''style:family="chart"'''))