cumulativeLineChart.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 cumulativeLineChart(TemplateMixin, NVD3Chart):
  11. """
  12. A cumulative line chart is used when you have one important grouping representing
  13. an ordered set of data and one value to show, summed over time.
  14. Python example::
  15. from nvd3 import cumulativeLineChart
  16. chart = cumulativeLineChart(name='cumulativeLineChart', x_is_date=True)
  17. xdata = [1365026400000, 1365026500000, 1365026600000]
  18. ydata = [6, 5, 1]
  19. y2data = [36, 55, 11]
  20. extra_serie = {"tooltip": {"y_start": "There are ", "y_end": " calls"}}
  21. chart.add_serie(name="Serie 1", y=ydata, x=xdata, extra=extra_serie)
  22. extra_serie = {"tooltip": {"y_start": "", "y_end": " mins"}}
  23. chart.add_serie(name="Serie 2", y=y2data, x=xdata, extra=extra_serie)
  24. chart.buildhtml()
  25. print(chart.content)
  26. Javascript generated:
  27. .. include:: ./examples/cumulativeLineChart.html
  28. """
  29. CHART_FILENAME = "./cumulativelinechart.html"
  30. template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
  31. def __init__(self, **kwargs):
  32. super(cumulativeLineChart, self).__init__(**kwargs)
  33. self.model = 'cumulativeLineChart'
  34. height = kwargs.get('height', 450)
  35. width = kwargs.get('width', None)
  36. if kwargs.get('x_is_date', False):
  37. self.set_date_flag(True)
  38. self.create_x_axis('xAxis',
  39. format=kwargs.get('x_axis_format', '%d %b %Y'),
  40. date=True)
  41. self.set_custom_tooltip_flag(True)
  42. else:
  43. self.create_x_axis('xAxis', format=kwargs.get(
  44. 'x_axis_format', '.2f'))
  45. self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', '.1%'))
  46. self.set_graph_height(height)
  47. if width:
  48. self.set_graph_width(width)