stackedAreaChart.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 stackedAreaChart(TemplateMixin, NVD3Chart):
  11. """
  12. The stacked area chart is identical to the area chart, except the areas are stacked
  13. on top of each other, rather than overlapping. This can make the chart much easier to read.
  14. Python example::
  15. from nvd3 import stackedAreaChart
  16. chart = stackedAreaChart(name='stackedAreaChart', height=400, width=400)
  17. xdata = [100, 101, 102, 103, 104, 105, 106,]
  18. ydata = [6, 11, 12, 7, 11, 10, 11]
  19. ydata2 = [8, 20, 16, 12, 20, 28, 28]
  20. extra_serie = {"tooltip": {"y_start": "There is ", "y_end": " min"}}
  21. chart.add_serie(name="Serie 1", y=ydata, x=xdata, extra=extra_serie)
  22. chart.add_serie(name="Serie 2", y=ydata2, x=xdata, extra=extra_serie)
  23. chart.buildhtml()
  24. print(chart.content)
  25. Javascript generated:
  26. .. include:: ./examples/stackedAreaChart.html
  27. """
  28. CHART_FILENAME = "./stackedareachart.html"
  29. template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
  30. def __init__(self, **kwargs):
  31. super(stackedAreaChart, self).__init__(**kwargs)
  32. height = kwargs.get('height', 450)
  33. width = kwargs.get('width', None)
  34. self.model = 'stackedAreaChart'
  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',
  43. '.2f'))
  44. self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', '.2f'))
  45. self.set_graph_height(height)
  46. if width:
  47. self.set_graph_width(width)