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

24 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 linePlusBarChart(TemplateMixin, NVD3Chart): 

16 

17 """ 

18 A linePlusBarChart Chart is a type of chart which displays information 

19 as a series of data points connected by straight line segments 

20 and with some series with rectangular bars with lengths proportional 

21 to the values that they represent. 

22 

23 Python example:: 

24 

25 from nvd3 import linePlusBarChart 

26 chart = linePlusBarChart(name="linePlusBarChart", 

27 width=500, height=400, x_axis_format="%d %b %Y", 

28 x_is_date=True, focus_enable=True, 

29 yaxis2_format="function(d) { return d3.format(',0.3f')(d) }") 

30 

31 xdata = [1338501600000, 1345501600000, 1353501600000] 

32 ydata = [6, 5, 1] 

33 y2data = [0.002, 0.003, 0.004] 

34 

35 extra_serie = {"tooltip": {"y_start": "There are ", "y_end": " calls"}, 

36 "date_format": "%d %b %Y %H:%S" } 

37 chart.add_serie(name="Serie 1", y=ydata, x=xdata, extra=extra_serie, 

38 bar=True) 

39 

40 extra_serie = {"tooltip": {"y_start": "There are ", "y_end": " min"}} 

41 chart.add_serie(name="Serie 2", y=y2data, x=xdata, extra=extra_serie) 

42 chart.buildcontent() 

43 

44 Note that in case you have two data serie with extreme different numbers, 

45 that you would like to format in different ways, 

46 you can pass a keyword *yaxis1_format* or *yaxis2_format* when 

47 creating the graph. 

48 

49 In the example above the graph created presents the values of the second 

50 data series with three digits right of the decimal point. 

51 

52 Javascript generated: 

53 

54 .. raw:: html 

55 

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

57 <script> 

58 data_linePlusBarChart=[{"bar": "true", "values": [{"y": 6, "x": 1338501600000}, {"y": 5, "x": 1345501600000}, {"y": 1, "x": 1353501600000}], "key": "Serie 1", "yAxis": "1"}, {"values": [{"y": 0.002, "x": 1338501600000}, {"y": 0.003, "x": 1345501600000}, {"y": 0.004, "x": 1353501600000}], "key": "Serie 2", "yAxis": "1"}]; 

59 nv.addGraph(function() { 

60 var chart = nv.models.linePlusBarChart(); 

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

62 var datum = data_linePlusBarChart; 

63 

64 chart.y2Axis 

65 .tickFormat(function(d) { return d3.format(',0.3f')(d) }); 

66 chart.xAxis 

67 .tickFormat(function(d) { return d3.time.format('%d %b %Y')(new Date(parseInt(d))) }); 

68 chart.y1Axis 

69 .tickFormat(function(d) { return d3.format(',f')(d) }); 

70 

71 chart.tooltipContent(function(key, y, e, graph) { 

72 var x = d3.time.format("%d %b %Y %H:%S")(new Date(parseInt(graph.point.x))); 

73 var y = String(graph.point.y); 

74 if(key.indexOf('Serie 1') > -1 ){ 

75 var y = 'There are ' + String(graph.point.y) + ' calls'; 

76 } 

77 if(key.indexOf('Serie 2') > -1 ){ 

78 var y = 'There are ' + String(graph.point.y) + ' min'; 

79 } 

80 tooltip_str = '<center><b>'+key+'</b></center>' + y + ' on ' + x; 

81 return tooltip_str; 

82 }); 

83 chart.showLegend(true); 

84 d3.select('#linePlusBarChart svg') 

85 .datum(datum) 

86 .transition().duration(500) 

87 .attr('width', 500) 

88 .attr('height', 400) 

89 .call(chart); 

90 }); 

91 </script> 

92 

93 """ 

94 CHART_FILENAME = "./lineplusbarchart.html" 

95 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME) 

96 

97 def __init__(self, **kwargs): 

98 super(linePlusBarChart, self).__init__(**kwargs) 

99 self.model = 'linePlusBarChart' 

100 

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

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

103 self.yaxis1_format = kwargs.get('yaxis1_format', 

104 "function(d) { return d3.format(',f')(d) }") 

105 self.yaxis2_format = kwargs.get('yaxis2_format', 

106 "function(d) { return d3.format(',f')(d) }") 

107 

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

109 self.set_date_flag(True) 

110 self.create_x_axis('xAxis', 

111 format=kwargs.get('x_axis_format', 

112 '%d %b %Y %H %S'), 

113 date=True) 

114 self.create_x_axis('x2Axis', format=kwargs.get('x_axis_format', 

115 '%d %b %Y %H %S'), 

116 date=True) 

117 self.set_custom_tooltip_flag(True) 

118 else: 

119 self.create_x_axis('xAxis', format=kwargs.get('x_axis_format', 

120 '.2f')) 

121 self.create_x_axis('x2Axis', format=kwargs.get('x_axis_format', 

122 '.2f')) 

123 

124 self.create_y_axis('y1Axis', format=self.yaxis1_format, 

125 custom_format=True) 

126 self.create_y_axis('y2Axis', format=self.yaxis2_format, 

127 custom_format=True) 

128 

129 self.set_graph_height(height) 

130 if width: 

131 self.set_graph_width(width)