bulletChart.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 bulletChart(TemplateMixin, NVD3Chart):
  11. '''
  12. A bullet chart is a variation of a bar graph
  13. used to indicate the value of a single variable
  14. in relation to a set of qualitative ranges. It is
  15. inspired by a dashboard gauge or thermometer chart.
  16. Python example:
  17. from nvd3.bulletChart import bulletChart
  18. chart = bulletChart.bulletChart(name=chart_name, height=100, width=500)
  19. title = 'Revenue',
  20. subtitle = 'US$, in thousands'
  21. ranges = [150, 225, 300]
  22. measures = [220, 270]
  23. markers = [250]
  24. chart.add_serie(
  25. title=title,
  26. subtitle=subtitle,
  27. ranges=ranges,
  28. measures=measures,
  29. markers=markers)
  30. chart.buildhtml()
  31. print(chart.content)
  32. JavaScript generated:
  33. .. include:: ./examples/bulletChart.html
  34. '''
  35. CHART_FILENAME = './bulletchart.html'
  36. template_chart_nvd3 = NVD3Chart.template_environment.get_template(
  37. CHART_FILENAME)
  38. def __init__(self, **kwargs):
  39. super(bulletChart, self).__init__(**kwargs)
  40. self.model = 'bulletChart'
  41. height = kwargs.get('height', None)
  42. width = kwargs.get('width', 200)
  43. if height:
  44. self.set_graph_height(height)
  45. if width:
  46. self.set_graph_width(width)
  47. def add_serie(self, ranges, measures, title, subtitle, markers=None,
  48. name=None, **kwargs):
  49. if not name:
  50. name = "Serie %d" % (self.serie_no)
  51. if markers:
  52. serie = [{
  53. 'title': title,
  54. 'subtitle': subtitle,
  55. 'ranges': ranges,
  56. 'measures': measures,
  57. 'markers': markers
  58. }]
  59. else:
  60. serie = [{
  61. 'title': title,
  62. 'subtitle': subtitle,
  63. 'ranges': ranges,
  64. 'measures': measures,
  65. }]
  66. data_keyvalue = {'values': serie, 'key': name}
  67. self.serie_no += 1
  68. self.series.append(data_keyvalue)