linePlusBarChart.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 linePlusBarChart(TemplateMixin, NVD3Chart):
  11. """
  12. A linePlusBarChart Chart is a type of chart which displays information
  13. as a series of data points connected by straight line segments
  14. and with some series with rectangular bars with lengths proportional
  15. to the values that they represent.
  16. Python example::
  17. from nvd3 import linePlusBarChart
  18. chart = linePlusBarChart(name="linePlusBarChart",
  19. width=500, height=400, x_axis_format="%d %b %Y",
  20. x_is_date=True, focus_enable=True,
  21. yaxis2_format="function(d) { return d3.format(',0.3f')(d) }")
  22. xdata = [1338501600000, 1345501600000, 1353501600000]
  23. ydata = [6, 5, 1]
  24. y2data = [0.002, 0.003, 0.004]
  25. extra_serie = {"tooltip": {"y_start": "There are ", "y_end": " calls"},
  26. "date_format": "%d %b %Y %H:%S" }
  27. chart.add_serie(name="Serie 1", y=ydata, x=xdata, extra=extra_serie,
  28. bar=True)
  29. extra_serie = {"tooltip": {"y_start": "There are ", "y_end": " min"}}
  30. chart.add_serie(name="Serie 2", y=y2data, x=xdata, extra=extra_serie)
  31. chart.buildhtml()
  32. print(chart.content)
  33. Note that in case you have two data serie with extreme different numbers,
  34. that you would like to format in different ways,
  35. you can pass a keyword *yaxis1_format* or *yaxis2_format* when
  36. creating the graph.
  37. In the example above the graph created presents the values of the second
  38. data series with three digits right of the decimal point.
  39. Javascript generated:
  40. .. include:: ./examples/linePlusBarChart.html
  41. """
  42. CHART_FILENAME = "./lineplusbarchart.html"
  43. template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
  44. def __init__(self, **kwargs):
  45. super(linePlusBarChart, self).__init__(**kwargs)
  46. self.model = 'linePlusBarChart'
  47. height = kwargs.get('height', 450)
  48. width = kwargs.get('width', None)
  49. self.yaxis1_format = kwargs.get('yaxis1_format',
  50. "function(d) { return d3.format(',f')(d) }")
  51. self.yaxis2_format = kwargs.get('yaxis2_format',
  52. "function(d) { return d3.format(',f')(d) }")
  53. if kwargs.get('x_is_date', False):
  54. self.set_date_flag(True)
  55. self.create_x_axis('xAxis',
  56. format=kwargs.get('x_axis_format',
  57. '%d %b %Y %H %S'),
  58. date=True)
  59. self.create_x_axis('x2Axis', format=kwargs.get('x_axis_format',
  60. '%d %b %Y %H %S'),
  61. date=True)
  62. self.set_custom_tooltip_flag(True)
  63. else:
  64. self.create_x_axis('xAxis', format=kwargs.get('x_axis_format',
  65. '.2f'))
  66. self.create_x_axis('x2Axis', format=kwargs.get('x_axis_format',
  67. '.2f'))
  68. self.create_y_axis('y1Axis', format=self.yaxis1_format,
  69. custom_format=True)
  70. self.create_y_axis('y2Axis', format=self.yaxis2_format,
  71. custom_format=True)
  72. self.set_graph_height(height)
  73. if width:
  74. self.set_graph_width(width)