multiChart.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 multiChart(TemplateMixin, NVD3Chart):
  11. """
  12. A multiChart is a type of chart which combines several plots of the same or different types.
  13. Python example::
  14. from nvd3 import multiChart
  15. chart_name = "multiChart"
  16. chart = multiChart(name=chart_name, x_is_date=False, x_axis_format="AM_PM")
  17. xdata = [1,2,3,4,5,6]
  18. ydata = [115.5,160.5,108,145.5,84,70.5]
  19. ydata2 = [48624,42944,43439,24194,38440,31651]
  20. kwargs1 = {'color': 'black'}
  21. kwargs2 = {'color': 'red'}
  22. extra_serie = {"tooltip": {"y_start": "There is ", "y_end": " calls"}}
  23. chart.add_serie(y=ydata, x=xdata, type='line', yaxis=1, name='visits', extra=extra_serie, **kwargs1)
  24. extra_serie = {"tooltip": {"y_start": "", "y_end": " min"}}
  25. chart.add_serie(y=ydata2, x=xdata, type='bar', yaxis=2,name='spend', extra=extra_serie, **kwargs2)
  26. chart.buildhtml()
  27. print(chart.content)
  28. Javascript rendered to:
  29. .. include:: ./examples/multiChart.html
  30. See the source code of this page, to see the underlying javascript.
  31. """
  32. CHART_FILENAME = "./multichart.html"
  33. template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
  34. def __init__(self, **kwargs):
  35. super(multiChart, self).__init__(**kwargs)
  36. self.model = 'multiChart'
  37. height = kwargs.get('height', 450)
  38. width = kwargs.get('width', None)
  39. if kwargs.get('x_is_date', False):
  40. self.set_date_flag(True)
  41. self.create_x_axis('xAxis',
  42. format=kwargs.get('x_axis_format', '%d %b %Y'),
  43. date=True)
  44. self.set_custom_tooltip_flag(True)
  45. else:
  46. if kwargs.get('x_axis_format') == 'AM_PM':
  47. self.x_axis_format = format = 'AM_PM'
  48. else:
  49. format = kwargs.get('x_axis_format', 'r')
  50. self.create_x_axis('xAxis', format=format,
  51. custom_format=kwargs.get('x_custom_format',
  52. False))
  53. self.create_y_axis(
  54. 'yAxis1',
  55. format=kwargs.get('y1_axis_format', '.02f'),
  56. custom_format=kwargs.get('y1_custom_format', False))
  57. self.create_y_axis(
  58. 'yAxis2',
  59. format=kwargs.get('y2_axis_format', '.02f'),
  60. custom_format=kwargs.get('y2_custom_format', False))
  61. # must have a specified height, otherwise it superimposes both chars
  62. self.set_graph_height(height)
  63. if width:
  64. self.set_graph_width(width)