barchart.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 Chart.
  21. #
  22. # Opendocument charts are usually not found in the wild. They are intended to be
  23. # subobojects of e.g. spreadsheets. But the KDE application called kchart
  24. # (http://www.koffice.org/kchart/) is able to read and write ODC files.
  25. #
  26. # Kchart is able to understand a document without <chart:series>, whereas
  27. # OOo misinterprets the label rows and columns. So if you embed the
  28. # spectre-balance.odc file in an OOo Writer document, expect to see some
  29. # oddities.
  30. from odf.opendocument import OpenDocumentChart
  31. from odf import chart, style, table, text
  32. # import a support class from the examples directory
  33. from datatable import DataTable
  34. class BarChart(object):
  35. def __init__(self):
  36. self.charttype = 'chart:bar'
  37. self.subtype = 'normal' # 'percentage', 'stacked' or 'normal'
  38. self.threedimensional = "true"
  39. self.x_axis = "X"
  40. self.y_axis = "Y"
  41. self.values = (1,2,3)
  42. self.title = None
  43. self.subtitle = None
  44. def __call__(self, doc):
  45. chartstyle = style.Style(name="chartstyle", family="chart")
  46. chartstyle.addElement( style.GraphicProperties(stroke="none", fillcolor="#ffffff"))
  47. doc.automaticstyles.addElement(chartstyle)
  48. mychart = chart.Chart( width="576pt", height="504pt", stylename=chartstyle, attributes={'class':self.charttype})
  49. doc.chart.addElement(mychart)
  50. # Title
  51. if self.title:
  52. titlestyle = style.Style(name="titlestyle", family="chart")
  53. titlestyle.addElement( style.GraphicProperties(stroke="none", fill="none"))
  54. titlestyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'",
  55. fontfamilygeneric="swiss", fontpitch="variable", fontsize="13pt"))
  56. doc.automaticstyles.addElement(titlestyle)
  57. mytitle = chart.Title(x="385pt", y="27pt", stylename=titlestyle)
  58. mytitle.addElement( text.P(text=self.title))
  59. mychart.addElement(mytitle)
  60. # Subtitle
  61. if self.subtitle:
  62. subtitlestyle = style.Style(name="subtitlestyle", family="chart")
  63. subtitlestyle.addElement( style.GraphicProperties(stroke="none", fill="none"))
  64. subtitlestyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'",
  65. fontfamilygeneric="swiss", fontpitch="variable", fontsize="10pt"))
  66. doc.automaticstyles.addElement(subtitlestyle)
  67. subtitle = chart.Subtitle(x="0pt", y="123pt", stylename=subtitlestyle)
  68. subtitle.addElement( text.P(text= self.subtitle))
  69. mychart.addElement(subtitle)
  70. # Legend
  71. legendstyle = style.Style(name="legendstyle", family="chart")
  72. legendstyle.addElement( style.GraphicProperties(fill="none"))
  73. legendstyle.addElement( style.TextProperties(fontfamily="'Nimbus Sans L'",
  74. fontfamilygeneric="swiss", fontpitch="variable", fontsize="6pt"))
  75. doc.automaticstyles.addElement(legendstyle)
  76. mylegend = chart.Legend(legendposition="end", legendalign="center", stylename=legendstyle)
  77. mychart.addElement(mylegend)
  78. # Plot area
  79. plotstyle = style.Style(name="plotstyle", family="chart")
  80. if self.subtype == "stacked": percentage="false"; stacked="true"
  81. elif self.subtype == "percentage": percentage="true"; stacked="false"
  82. else: percentage="false"; stacked="false"
  83. plotstyle.addElement( style.ChartProperties(seriessource="columns",
  84. percentage=percentage, stacked=stacked,
  85. threedimensional=self.threedimensional))
  86. doc.automaticstyles.addElement(plotstyle)
  87. plotarea = chart.PlotArea(datasourcehaslabels=self.datasourcehaslabels, stylename=plotstyle)
  88. mychart.addElement(plotarea)
  89. # Style for the X,Y axes
  90. axisstyle = style.Style(name="axisstyle", family="chart")
  91. axisstyle.addElement( style.ChartProperties(displaylabel="true"))
  92. doc.automaticstyles.addElement(axisstyle)
  93. # Title for the X axis
  94. xaxis = chart.Axis(dimension="x", name="primary-x", stylename=axisstyle)
  95. plotarea.addElement(xaxis)
  96. xt = chart.Title()
  97. xaxis.addElement(xt)
  98. xt.addElement(text.P(text=self.x_axis))
  99. # Title for the Y axis
  100. yaxis = chart.Axis(dimension="y", name="primary-y", stylename=axisstyle)
  101. plotarea.addElement(yaxis)
  102. yt = chart.Title()
  103. yaxis.addElement(yt)
  104. yt.addElement(text.P(text=self.y_axis))
  105. # Data area
  106. datatable = DataTable( self.values )
  107. datatable.datasourcehaslabels = self.datasourcehaslabels
  108. mychart.addElement(datatable())
  109. if __name__ == "__main__":
  110. # Create the document
  111. doc = OpenDocumentChart()
  112. mychart = BarChart()
  113. mychart.title = "SPECTRE"
  114. mychart.subtitle = "SPecial Executive for Counter-intelligence, Terrorism, Revenge and Extortion"
  115. mychart.x_axis = u"Divisions"
  116. mychart.y_axis = u"€ (thousand)"
  117. # These represent the data. Six rows in three columns
  118. mychart.values = (
  119. ('', 'Expense', 'Revenue'),
  120. ('Counterfeit', 1000, 1500),
  121. ('Murder', 1100, 1150),
  122. ('Prostitution', 3200, 2350),
  123. ('Blackmail', 1100, 1150),
  124. ('Larceny', 1000, 1750)
  125. )
  126. mychart.datasourcehaslabels = "both"
  127. mychart(doc)
  128. doc.save("spectre-balance", True)