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 lineChart(TemplateMixin, NVD3Chart):
16
17 """
18 A line chart or line graph is a type of chart which displays information
19 as a series of data points connected by straight line segments.
20
21 Python example::
22
23 from nvd3 import lineChart
24 chart = lineChart(name="lineChart", x_is_date=False, x_axis_format="AM_PM")
25
26 xdata = range(24)
27 ydata = [0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 4, 3, 3, 5, 7, 5, 3, 16, 6, 9, 15, 4, 12]
28 ydata2 = [9, 8, 11, 8, 3, 7, 10, 8, 6, 6, 9, 6, 5, 4, 3, 10, 0, 6, 3, 1, 0, 0, 0, 1]
29
30 extra_serie = {"tooltip": {"y_start": "There are ", "y_end": " calls"}}
31 chart.add_serie(y=ydata, x=xdata, name='sine', extra=extra_serie)
32 extra_serie = {"tooltip": {"y_start": "", "y_end": " min"}}
33 chart.add_serie(y=ydata2, x=xdata, name='cose', extra=extra_serie)
34 chart.buildhtml()
35 print(chart.content)
36
37 Javascript generated:
38
39 .. include:: ./examples/lineChart.html
40
41 See the source code of this page, to see the underlying javascript.
42 """
43 CHART_FILENAME = "./linechart.html"
44 template_chart_nvd3 = NVD3Chart.template_environment.get_template(CHART_FILENAME)
45
46 def __init__(self, **kwargs):
47 super(lineChart, self).__init__(**kwargs)
48 self.model = 'lineChart'
49
50 height = kwargs.get('height', 450)
51 width = kwargs.get('width', None)
52
53 if kwargs.get('x_is_date', False):
54 self.set_date_flag(True)
55 self.create_x_axis('xAxis',
56 format=kwargs.get('x_axis_format', '%d %b %Y'),
57 date=True)
58 self.set_custom_tooltip_flag(True)
59 else:
60 if kwargs.get('x_axis_format') == 'AM_PM':
61 self.x_axis_format = format = 'AM_PM'
62 else:
63 format = kwargs.get('x_axis_format', 'r')
64 self.create_x_axis('xAxis', format=format,
65 custom_format=kwargs.get('x_custom_format',
66 False))
67 self.create_y_axis(
68 'yAxis',
69 format=kwargs.get('y_axis_format', '.02f'),
70 custom_format=kwargs.get('y_custom_format', False))
71
72 # must have a specified height, otherwise it superimposes both chars
73 self.set_graph_height(height)
74 if width:
75 self.set_graph_width(width)