pieChart.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Python-nvd3 is a Python wrapper for NVD3 graph library.
  5. NVD3 is an attempt to build re-usable charts and chart components
  6. for d3.js without taking away the power that d3.js gives you.
  7. Project location : https://github.com/areski/python-nvd3
  8. """
  9. from .NVD3Chart import NVD3Chart, TemplateMixin
  10. class pieChart(TemplateMixin, NVD3Chart):
  11. """
  12. A pie chart (or a circle graph) is a circular chart divided into sectors,
  13. illustrating numerical proportion. In chart, the arc length of each sector
  14. is proportional to the quantity it represents.
  15. Python example::
  16. from nvd3 import pieChart
  17. chart = pieChart(name='pieChart', color_category='category20c',
  18. height=400, width=400)
  19. xdata = ["Orange", "Banana", "Pear", "Kiwi", "Apple", "Strawbery",
  20. "Pineapple"]
  21. ydata = [3, 4, 0, 1, 5, 7, 3]
  22. extra_serie = {"tooltip": {"y_start": "", "y_end": " cal"}}
  23. chart.add_serie(y=ydata, x=xdata, extra=extra_serie)
  24. chart.buildhtml()
  25. print(chart.content)
  26. Javascript generated:
  27. .. include:: ./examples/pieChart.html
  28. """
  29. CHART_FILENAME = "./piechart.html"
  30. template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
  31. def __init__(self, **kwargs):
  32. super(pieChart, self).__init__(**kwargs)
  33. height = kwargs.get('height', 450)
  34. width = kwargs.get('width', None)
  35. self.donut = kwargs.get('donut', False)
  36. self.donutRatio = kwargs.get('donutRatio', 0.35)
  37. self.color_list = []
  38. self.create_x_axis('xAxis', format=None)
  39. self.create_y_axis('yAxis', format=None)
  40. # must have a specified height, otherwise it superimposes both chars
  41. if height:
  42. self.set_graph_height(height)
  43. if width:
  44. self.set_graph_width(width)
  45. self.donut = kwargs.get('donut', False)
  46. self.donutRatio = kwargs.get('donutRatio', 0.35)
  47. self.callback = kwargs.get('callback', None)