multiBarChart.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 multiBarChart(TemplateMixin, NVD3Chart):
  11. """
  12. A multiple bar graph contains comparisons of two or more categories or bars.
  13. One axis represents a quantity and the other axis identifies a specific feature
  14. about the categories. Reading a multiple bar graph includes looking at extremes
  15. (tallest/longest vs. shortest) in each grouping.
  16. Python example::
  17. from nvd3 import multiBarChart
  18. chart = multiBarChart(width=500, height=400, x_axis_format=None)
  19. xdata = ['one', 'two', 'three', 'four']
  20. ydata1 = [6, 12, 9, 16]
  21. ydata2 = [8, 14, 7, 11]
  22. chart.add_serie(name="Serie 1", y=ydata1, x=xdata)
  23. chart.add_serie(name="Serie 2", y=ydata2, x=xdata)
  24. chart.buildhtml()
  25. print(chart.content)
  26. Javascript generated:
  27. .. include:: ./examples/multiBarChart.html
  28. """
  29. CHART_FILENAME = "./multibarchart.html"
  30. template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
  31. def __init__(self, **kwargs):
  32. super(multiBarChart, self).__init__(**kwargs)
  33. height = kwargs.get('height', 450)
  34. width = kwargs.get('width', None)
  35. if kwargs.get('x_is_date', False):
  36. self.set_date_flag(True)
  37. self.create_x_axis('xAxis',
  38. format=kwargs.get('x_axis_format', '%d %b %Y'),
  39. date=True)
  40. self.set_custom_tooltip_flag(True)
  41. else:
  42. self.create_x_axis('xAxis', format=kwargs.get('x_axis_format', '.2f'))
  43. self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', '.2f'))
  44. self.set_graph_height(height)
  45. if width:
  46. self.set_graph_width(width)