discreteBarChart.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 discreteBarChart(TemplateMixin, NVD3Chart):
  11. """
  12. A discrete bar chart or bar graph is a chart with rectangular bars with
  13. lengths proportional to the values that they represent.
  14. Python example::
  15. from nvd3 import discreteBarChart
  16. chart = discreteBarChart(name='discreteBarChart', height=400, width=400)
  17. xdata = ["A", "B", "C", "D", "E", "F"]
  18. ydata = [3, 4, 0, -3, 5, 7]
  19. chart.add_serie(y=ydata, x=xdata)
  20. chart.buildhtml()
  21. print(chart.content)
  22. Javascript generated:
  23. .. include:: ./examples/discreteBarChart.html
  24. You can also disable the tooltips by passing ``tooltips=False`` when
  25. creating the bar chart.
  26. Python example::
  27. chart = discreteBarChart(name='discreteBarChart-notooltip', height=400, width=400,
  28. tooltips=False)
  29. .. raw:: html
  30. <div id="discreteBarChart-notooltip"><svg style="height:450px; width:100%"></svg></div>
  31. <script>
  32. data_discreteBarChart=[{"values": [{"y": 3, "x": "A"}, {"y": 4, "x": "B"}, {"y": 0, "x": "C"}, {"y": -3, "x": "D"}, {"y": 5, "x": "E"}, {"y": 7, "x": "F"}], "key": "Serie 1", "yAxis": "1"}];
  33. nv.addGraph(function() {
  34. var chart = nv.models.discreteBarChart();
  35. chart.margin({top: 30, right: 60, bottom: 20, left: 60});
  36. chart.tooltips(false);
  37. var datum = data_discreteBarChart;
  38. chart.yAxis
  39. .tickFormat(d3.format(',.0f'));
  40. chart.tooltipContent(function(key, y, e, graph) {
  41. var x = String(graph.point.x);
  42. var y = String(graph.point.y);
  43. var y = String(graph.point.y);
  44. tooltip_str = '<center><b>'+key+'</b></center>' + y + ' at ' + x;
  45. return tooltip_str;
  46. });
  47. d3.select('#discreteBarChart-notooltip svg')
  48. .datum(datum)
  49. .transition().duration(500)
  50. .attr('width', 400)
  51. .attr('height', 400)
  52. .call(chart);
  53. });
  54. </script>
  55. """
  56. CHART_FILENAME = "./discretebarchart.html"
  57. template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
  58. def __init__(self, **kwargs):
  59. super(discreteBarChart, self).__init__(**kwargs)
  60. self.model = 'discreteBarChart'
  61. height = kwargs.get('height', 450)
  62. width = kwargs.get('width', None)
  63. if kwargs.get('x_is_date', False):
  64. self.set_date_flag(True)
  65. self.create_x_axis('xAxis',
  66. format=kwargs.get('x_axis_format',
  67. "%d %b %Y %H %S"),
  68. date=True)
  69. else:
  70. self.create_x_axis('xAxis', format=None)
  71. self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', ".0f"))
  72. self.set_custom_tooltip_flag(True)
  73. self.set_graph_height(height)
  74. if width:
  75. self.set_graph_width(width)
  76. tooltips = kwargs.get('tooltips', True)
  77. if not tooltips:
  78. self.chart_attr = {'tooltips': 'false'}