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

22 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 lineWithFocusChart(TemplateMixin, NVD3Chart): 

16 """ 

17 A lineWithFocusChart or line graph is a type of chart which displays information 

18 as a series of data points connected by straight line segments. 

19 The lineWithFocusChart provide a smaller chart that act as a selector, 

20 this is very useful if you want to zoom on a specific time period. 

21 

22 Python example:: 

23 

24 from nvd3 import lineWithFocusChart 

25 chart = lineWithFocusChart(name='lineWithFocusChart', x_is_date=True, x_axis_format="%d %b %Y") 

26 xdata = [1365026400000, 1365026500000, 1365026600000, 1365026700000, 1365026800000, 1365026900000, 1365027000000] 

27 ydata = [-6, 5, -1, 2, 4, 8, 10] 

28 

29 extra_serie = {"tooltip": {"y_start": "", "y_end": " ext"}, 

30 "date_format": "%d %b %Y"} 

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

32 chart.buildhtml() 

33 

34 Javascript generated: 

35 

36 .. raw:: html 

37 

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

39 <script> 

40 data_lineWithFocusChart=[{"values": [{"y": -6, "x": 1365026400000}, {"y": 5, "x": 1365026500000}, {"y": -1, "x": 1365026600000}], "key": "Serie 1", "yAxis": "1"}]; 

41 nv.addGraph(function() { 

42 var chart = nv.models.lineWithFocusChart(); 

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

44 var datum = data_lineWithFocusChart; 

45 chart.yAxis 

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

47 chart.y2Axis 

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

49 chart.xAxis 

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

51 chart.x2Axis 

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

53 

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

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

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

57 if(key == 'Serie 1'){ 

58 var y = String(graph.point.y) + ' ext'; 

59 } 

60 

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

62 return tooltip_str; }); 

63 

64 chart.showLegend(true); 

65 

66 d3.select('#lineWithFocusChart svg') 

67 .datum(datum) 

68 .transition().duration(500) 

69 .attr('height', 450) 

70 .call(chart); }); 

71 </script> 

72 

73 """ 

74 

75 CHART_FILENAME = "./linewfocuschart.html" 

76 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME) 

77 

78 def __init__(self, **kwargs): 

79 super(lineWithFocusChart, self).__init__(**kwargs) 

80 self.model = 'lineWithFocusChart' 

81 

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

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

84 

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

86 self.set_date_flag(True) 

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

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

89 date=True) 

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

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

92 date=True) 

93 self.set_custom_tooltip_flag(True) 

94 else: 

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

96 '.2f')) 

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

98 '.2f')) 

99 

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

101 self.create_y_axis('y2Axis', format=kwargs.get('y_axis_format', '.2f')) 

102 

103 self.set_graph_height(height) 

104 if width: 

105 self.set_graph_width(width)