subobject.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2007 Søren Roug, European Environment Agency
  4. #
  5. # This is free software. You may redistribute it under the terms
  6. # of the Apache license and the GNU General Public License Version
  7. # 2 or at your option any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public
  15. # License along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. #
  18. # Contributor(s):
  19. #
  20. # This is an example of an OpenDocument Text with an embedded Chart.
  21. #
  22. from odf.opendocument import OpenDocumentChart, OpenDocumentText
  23. from odf import chart, style, table, text, draw
  24. # import a support class from the examples directory
  25. from datatable import DataTable
  26. class BarChart(object):
  27. def __init__(self):
  28. self.charttype = 'chart:bar'
  29. self.subtype = 'normal' # 'percentage', 'stacked' or 'normal'
  30. self.threedimensional = "false"
  31. self.x_axis = "X"
  32. self.y_axis = "Y"
  33. self.values = (1,2,3)
  34. self.title = None
  35. self.subtitle = None
  36. def __call__(self, doc):
  37. chartstyle = style.Style(name="chartstyle", family="chart")
  38. chartstyle.addElement( style.GraphicProperties(stroke="none", fillcolor="#ffffff"))
  39. doc.automaticstyles.addElement(chartstyle)
  40. mychart = chart.Chart(width="476pt", height="404pt",stylename=chartstyle, attributes={'class':self.charttype})
  41. doc.chart.addElement(mychart)
  42. # Title
  43. if self.title:
  44. titlestyle = style.Style(name="titlestyle", family="chart")
  45. titlestyle.addElement( style.GraphicProperties(stroke="none", fill="none"))
  46. titlestyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'",
  47. fontfamilygeneric="swiss", fontpitch="variable", fontsize="13pt"))
  48. doc.automaticstyles.addElement(titlestyle)
  49. mytitle = chart.Title(x="185pt", y="27pt", stylename=titlestyle)
  50. mytitle.addElement( text.P(text=self.title))
  51. mychart.addElement(mytitle)
  52. # Subtitle
  53. if self.subtitle:
  54. subtitlestyle = style.Style(name="subtitlestyle", family="chart")
  55. subtitlestyle.addElement( style.GraphicProperties(stroke="none", fill="none"))
  56. subtitlestyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'",
  57. fontfamilygeneric="swiss", fontpitch="variable", fontsize="10pt"))
  58. doc.automaticstyles.addElement(subtitlestyle)
  59. subtitle = chart.Subtitle(x="50pt", y="50pt", stylename=subtitlestyle)
  60. subtitle.addElement( text.P(text= self.subtitle))
  61. mychart.addElement(subtitle)
  62. # Legend
  63. legendstyle = style.Style(name="legendstyle", family="chart")
  64. legendstyle.addElement( style.GraphicProperties(fill="none"))
  65. legendstyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'",
  66. fontfamilygeneric="swiss", fontpitch="variable", fontsize="8pt"))
  67. doc.automaticstyles.addElement(legendstyle)
  68. mylegend = chart.Legend(legendposition="end", legendalign="center", stylename=legendstyle)
  69. mychart.addElement(mylegend)
  70. # Plot area
  71. plotstyle = style.Style(name="plotstyle", family="chart")
  72. if self.subtype == "stacked": percentage="false"; stacked="true"
  73. elif self.subtype == "percentage": percentage="true"; stacked="false"
  74. else: percentage="false"; stacked="false"
  75. plotstyle.addElement( style.ChartProperties(seriessource="columns",
  76. percentage=percentage, stacked=stacked,
  77. threedimensional=self.threedimensional))
  78. doc.automaticstyles.addElement(plotstyle)
  79. plotarea = chart.PlotArea(datasourcehaslabels=self.datasourcehaslabels, stylename=plotstyle)
  80. mychart.addElement(plotarea)
  81. # Style for the X,Y axes
  82. axisstyle = style.Style(name="axisstyle", family="chart")
  83. axisstyle.addElement( style.ChartProperties(displaylabel="true"))
  84. axisstyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'",
  85. fontfamilygeneric="swiss", fontpitch="variable", fontsize="8pt"))
  86. doc.automaticstyles.addElement(axisstyle)
  87. # Title for the X axis
  88. xaxis = chart.Axis(dimension="x", name="primary-x", stylename=axisstyle)
  89. plotarea.addElement(xaxis)
  90. xt = chart.Title()
  91. xaxis.addElement(xt)
  92. xt.addElement(text.P(text=self.x_axis))
  93. # Title for the Y axis
  94. yaxis = chart.Axis(dimension="y", name="primary-y", stylename=axisstyle)
  95. plotarea.addElement(yaxis)
  96. yt = chart.Title()
  97. yaxis.addElement(yt)
  98. yt.addElement(text.P(text=self.y_axis))
  99. # Set up the data series. OOo doesn't show correctly without them.
  100. s = chart.Series(valuescellrangeaddress="local-table.B2:.B6", labelcelladdress="local-table.B1")
  101. s.addElement(chart.DataPoint(repeated=5))
  102. plotarea.addElement(s)
  103. s = chart.Series(valuescellrangeaddress="local-table.C2:.C6", labelcelladdress="local-table.C1")
  104. s.addElement(chart.DataPoint(repeated=5))
  105. plotarea.addElement(s)
  106. # The data are placed in a table inside the chart object - but could also be a
  107. # table in the main document
  108. datatable = DataTable(self.values)
  109. datatable.datasourcehaslabels = self.datasourcehaslabels
  110. mychart.addElement(datatable())
  111. if __name__ == "__main__":
  112. # Create the subdocument
  113. chartdoc = OpenDocumentChart()
  114. mychart = BarChart()
  115. mychart.title = "SPECTRE"
  116. mychart.subtitle = "SPecial Executive for Counter-intelligence, Terrorism, Revenge and Extortion"
  117. mychart.x_axis = "Divisions"
  118. mychart.y_axis = u"€ (thousand)"
  119. # These represent the data. Six rows in three columns
  120. mychart.values = (
  121. ('','Expense','Revenue'),
  122. ('Counterfeit',1000,1500),
  123. ('Murder',1100,1150),
  124. ('Prostitution',3200,2350),
  125. ('Blackmail',1100,1150),
  126. ('Larceny',1000,1750)
  127. )
  128. mychart.datasourcehaslabels = "both"
  129. mychart(chartdoc)
  130. # Create the containg document
  131. textdoc = OpenDocumentText()
  132. # Create a paragraph to contain the frame. You can put the frame directly
  133. # as a child og textdoc.text, but both Kword and OOo has problems wiht
  134. # this approach.
  135. p = text.P()
  136. textdoc.text.addElement(p)
  137. # Create the frame.
  138. df = draw.Frame(width="476pt", height="404pt", anchortype="paragraph")
  139. p.addElement(df)
  140. # Here we add the subdocument to the main document. We get back a reference
  141. # to use in the href.
  142. objectloc = textdoc.addObject(chartdoc)
  143. do = draw.Object(href=objectloc)
  144. # Put the object inside the frame
  145. df.addElement(do)
  146. textdoc.save("spectre-balance", True)