lineWithFocusChart.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 lineWithFocusChart(TemplateMixin, NVD3Chart):
  11. """
  12. A lineWithFocusChart or line graph is a type of chart which displays information
  13. as a series of data points connected by straight line segments.
  14. The lineWithFocusChart provide a smaller chart that act as a selector,
  15. this is very useful if you want to zoom on a specific time period.
  16. Python example::
  17. from nvd3 import lineWithFocusChart
  18. chart = lineWithFocusChart(name='lineWithFocusChart', x_is_date=True, x_axis_format="%d %b %Y")
  19. xdata = [1365026400000, 1365026500000, 1365026600000, 1365026700000, 1365026800000, 1365026900000, 1365027000000]
  20. ydata = [-6, 5, -1, 2, 4, 8, 10]
  21. extra_serie = {"tooltip": {"y_start": "", "y_end": " ext"},
  22. "date_format": "%d %b %Y"}
  23. chart.add_serie(name="Serie 1", y=ydata, x=xdata, extra=extra_serie)
  24. chart.buildhtml()
  25. print(chart.content)
  26. Javascript generated:
  27. .. include:: ./examples/lineWithFocusChart.html
  28. """
  29. CHART_FILENAME = "./linewfocuschart.html"
  30. template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
  31. def __init__(self, **kwargs):
  32. super(lineWithFocusChart, self).__init__(**kwargs)
  33. self.model = 'lineWithFocusChart'
  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', format=kwargs.get('x_axis_format',
  39. '%d %b %Y %H %S'),
  40. date=True)
  41. self.create_x_axis('x2Axis', format=kwargs.get('x_axis_format',
  42. '%d %b %Y %H %S'),
  43. date=True)
  44. self.set_custom_tooltip_flag(True)
  45. else:
  46. self.create_x_axis('xAxis', format=kwargs.get('x_axis_format',
  47. '.2f'))
  48. self.create_x_axis('x2Axis', format=kwargs.get('x_axis_format',
  49. '.2f'))
  50. self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', '.2f'))
  51. self.create_y_axis('y2Axis', format=kwargs.get('y_axis_format', '.2f'))
  52. self.set_graph_height(height)
  53. if width:
  54. self.set_graph_width(width)