Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/python_nvd3-0.14.2-py3.8.egg/nvd3/multiBarChart.py: 33%

18 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-10-03 06:25 +0000

1#!/usr/bin/python 

2# -*- coding: utf-8 -*- 

3 

4""" 

5Python-nvd3 is a Python wrapper for NVD3 graph library. 

6NVD3 is an attempt to build re-usable charts and chart components 

7for d3.js without taking away the power that d3.js gives you. 

8 

9Project location : https://github.com/areski/python-nvd3 

10""" 

11 

12from .NVD3Chart import NVD3Chart, TemplateMixin 

13 

14 

15class multiBarChart(TemplateMixin, NVD3Chart): 

16 """ 

17 A multiple bar graph contains comparisons of two or more categories or bars. 

18 One axis represents a quantity and the other axis identifies a specific feature 

19 about the categories. Reading a multiple bar graph includes looking at extremes 

20 (tallest/longest vs. shortest) in each grouping. 

21 

22 Python example:: 

23 

24 from nvd3 import multiBarChart 

25 chart = multiBarChart(width=500, height=400, x_axis_format=None) 

26 xdata = ['one', 'two', 'three', 'four'] 

27 ydata1 = [6, 12, 9, 16] 

28 ydata2 = [8, 14, 7, 11] 

29 

30 chart.add_serie(name="Serie 1", y=ydata1, x=xdata) 

31 chart.add_serie(name="Serie 2", y=ydata2, x=xdata) 

32 chart.buildhtml() 

33 

34 Javascript generated: 

35 

36 .. raw:: html 

37 

38 <div id="multiBarChart"><svg style="height:450px; width:100%"></svg></div> 

39 <script> 

40 

41 data_multiBarChart=[{"values": 

42 [{"y": 6, "x": "one"}, 

43 {"y": 12, "x": "two"}, 

44 {"y": 9, "x": "three"}, 

45 {"y": 16, "x": "four"}], 

46 "key": "Serie 1", "yAxis": "1"}, 

47 {"values": 

48 [{"y": 8, "x": "one"}, 

49 {"y": 14, "x": "two"}, 

50 {"y": 7, "x": "three"}, 

51 {"y": 11, "x": "four"}], 

52 "key": "Serie 2", "yAxis": "1"}]; 

53 

54 nv.addGraph(function() { 

55 var chart = nv.models.multiBarChart(); 

56 chart.margin({top: 30, right: 60, bottom: 20, left: 60}); 

57 var datum = data_multiBarChart; 

58 chart.yAxis 

59 .tickFormat(d3.format(',.2f')); 

60 chart.showLegend(true); 

61 d3.select('#multiBarChart svg') 

62 .datum(datum) 

63 .transition().duration(500) 

64 .attr('width', 500) 

65 .attr('height', 400) 

66 .call(chart); 

67 }); 

68 

69 

70 </script> 

71 

72 """ 

73 

74 CHART_FILENAME = "./multibarchart.html" 

75 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME) 

76 

77 def __init__(self, **kwargs): 

78 super(multiBarChart, self).__init__(**kwargs) 

79 

80 height = kwargs.get('height', 450) 

81 width = kwargs.get('width', None) 

82 

83 if kwargs.get('x_is_date', False): 

84 self.set_date_flag(True) 

85 self.create_x_axis('xAxis', 

86 format=kwargs.get('x_axis_format', '%d %b %Y'), 

87 date=True) 

88 self.set_custom_tooltip_flag(True) 

89 else: 

90 self.create_x_axis('xAxis', format=kwargs.get('x_axis_format', '.2f')) 

91 self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', '.2f')) 

92 

93 self.set_graph_height(height) 

94 if width: 

95 self.set_graph_width(width)