scatterChart.py 2.3 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 scatterChart(TemplateMixin, NVD3Chart):
  11. """
  12. A scatter plot or scattergraph is a type of mathematical diagram using Cartesian
  13. coordinates to display values for two variables for a set of data.
  14. The data is displayed as a collection of points, each having the value of one variable
  15. determining the position on the horizontal axis and the value of the other variable
  16. determining the position on the vertical axis.
  17. Python example::
  18. from nvd3 import scatterChart
  19. chart = scatterChart(name='scatterChart', height=400, width=400)
  20. xdata = [3, 4, 0, -3, 5, 7]
  21. ydata = [-1, 2, 3, 3, 15, 2]
  22. ydata2 = [1, -2, 4, 7, -5, 3]
  23. kwargs1 = {'shape': 'circle', 'size': '1'}
  24. kwargs2 = {'shape': 'cross', 'size': '10'}
  25. extra_serie = {"tooltip": {"y_start": "", "y_end": " call"}}
  26. chart.add_serie(name="series 1", y=ydata, x=xdata, extra=extra_serie, **kwargs1)
  27. extra_serie = {"tooltip": {"y_start": "", "y_end": " min"}}
  28. chart.add_serie(name="series 2", y=ydata2, x=xdata, extra=extra_serie, **kwargs2)
  29. chart.buildhtml()
  30. print(chart.content)
  31. Javascript generated:
  32. .. include:: ./examples/scatterChart.html
  33. """
  34. CHART_FILENAME = "./scatterchart.html"
  35. template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
  36. def __init__(self, **kwargs):
  37. super(scatterChart, self).__init__(**kwargs)
  38. self.model = 'scatterChart'
  39. height = kwargs.get('height', 450)
  40. width = kwargs.get('width', None)
  41. self.create_x_axis('xAxis', format=kwargs.get('x_axis_format', '.02f'),
  42. label=kwargs.get('x_axis_label', None))
  43. self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', '.02f'),
  44. label=kwargs.get('y_axis_label', None))
  45. self.set_graph_height(height)
  46. if width:
  47. self.set_graph_width(width)