lineChart.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 lineChart(TemplateMixin, NVD3Chart):
  11. """
  12. A line chart or line graph is a type of chart which displays information
  13. as a series of data points connected by straight line segments.
  14. Python example::
  15. from nvd3 import lineChart
  16. chart = lineChart(name="lineChart", x_is_date=False, x_axis_format="AM_PM")
  17. xdata = range(24)
  18. ydata = [0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 4, 3, 3, 5, 7, 5, 3, 16, 6, 9, 15, 4, 12]
  19. ydata2 = [9, 8, 11, 8, 3, 7, 10, 8, 6, 6, 9, 6, 5, 4, 3, 10, 0, 6, 3, 1, 0, 0, 0, 1]
  20. extra_serie = {"tooltip": {"y_start": "There are ", "y_end": " calls"}}
  21. chart.add_serie(y=ydata, x=xdata, name='sine', extra=extra_serie)
  22. extra_serie = {"tooltip": {"y_start": "", "y_end": " min"}}
  23. chart.add_serie(y=ydata2, x=xdata, name='cose', extra=extra_serie)
  24. chart.buildhtml()
  25. print(chart.content)
  26. Javascript generated:
  27. .. include:: ./examples/lineChart.html
  28. See the source code of this page, to see the underlying javascript.
  29. """
  30. CHART_FILENAME = "./linechart.html"
  31. template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
  32. def __init__(self, **kwargs):
  33. super(lineChart, self).__init__(**kwargs)
  34. self.model = 'lineChart'
  35. height = kwargs.get('height', 450)
  36. width = kwargs.get('width', None)
  37. if kwargs.get('x_is_date', False):
  38. self.set_date_flag(True)
  39. self.create_x_axis('xAxis',
  40. format=kwargs.get('x_axis_format', '%d %b %Y'),
  41. date=True)
  42. self.set_custom_tooltip_flag(True)
  43. else:
  44. if kwargs.get('x_axis_format') == 'AM_PM':
  45. self.x_axis_format = format = 'AM_PM'
  46. else:
  47. format = kwargs.get('x_axis_format', 'r')
  48. self.create_x_axis('xAxis', format=format,
  49. custom_format=kwargs.get('x_custom_format',
  50. False))
  51. self.create_y_axis(
  52. 'yAxis',
  53. format=kwargs.get('y_axis_format', '.02f'),
  54. custom_format=kwargs.get('y_custom_format', False))
  55. # must have a specified height, otherwise it superimposes both chars
  56. self.set_graph_height(height)
  57. if width:
  58. self.set_graph_width(width)