def draw_number_line(name, list_x, list_y, max_y = 0): line = Line() line.disable_xml_declaration = True line.js = [] line.x_label_rotation = 45 line.x_labels_major_count = 31 #line.y_labels_major_every = 3 line.show_legend = False line.print_values = False line.print_zeroes = False line.width = 1280 line.height = 720 line.value_formatter = lambda x:str(int(x)) #line.major_label_font_size = 20 #line.print_zeroes = True line.show_minor_x_labels = False #line.show_minor_y_labels = True line.show_dots = False #line.interpolate = 'hermite' line.title = name list_int_y = [0] for y in list_y: if y != None: list_int_y.append(y) line.range = (0, max(int(max(list_int_y)*1.1), max_y)) line.x_labels = list_x #line.y_labels = map(lambda x:x*100, range(int((min(list_y)/100)-1), int((max(list_y)/100+4)))) line.add(name, list_y) return line.render()
def draw_multi_price_line(name, dict_data): line = Line() line.disable_xml_declaration = True line.js = [] line.x_label_rotation = 45 line.y_labels_major_every = 3 #line.show_legend = False line.legend_at_bottom = True line.print_values = False line.width = 1280 line.height = 720 line.value_formatter = lambda x:str(int(x)) #line.major_label_font_size = 20 #line.print_zeroes = True #line.show_minor_x_labels = True #line.show_minor_y_labels = True line.title = name min_y = None max_y = None for name in dict_data: #print(name) list_x, list_y = dict_data[name] line.add(name, list_y) min_list_y = min(list_y) max_list_y = max(list_y) if min_y == None or min_list_y < min_y : min_y = min_list_y if max_y == None or max_list_y > max_y : max_y = max_list_y if min_y == None : min_y = 0 if max_y == None : max_y = 0 line.range = (min_y-100, max_y+300) line.x_labels = map(str, list_x) line.y_labels = map(lambda x:x*100, range(int((min_y/100)-1), int((max_y/100+4)))) return line.render()
def test_parametric_styles_with_parameters(): """Test a parametric style with parameters""" line = Line(style=RotateStyle( '#de3804', step=12, max_=180, base_style=LightStyle)) line.add('_', [1, 2, 3]) line.x_labels = 'abc' assert line.render()
def draw_price_line(name, list_x, list_y): line = Line() line.disable_xml_declaration = True line.js = [] line.x_label_rotation = 45 line.y_labels_major_every = 3 line.show_legend = False line.print_values = False line.width = 1280 line.height = 720 line.value_formatter = lambda x:str(int(x)) #line.major_label_font_size = 20 #line.print_zeroes = True #line.show_minor_x_labels = True #line.show_minor_y_labels = True line.title = name digital_y = list(filter(lambda x:x != None, list_y)) if digital_y == [] : digital_y = [0] line.range = (min(digital_y)-100, max(digital_y)+300) line.x_labels = list_x line.y_labels = map(lambda x:x*100, range(int((min(digital_y)/100)-1), int((max(digital_y)/100+4)))) line.add(name, list_y) return line.render()
def test_interpolate_secondary(): chart = Line(title=u"Some different points", interpolate="cubic") chart.add("line", [1000, 2000, 7000]) chart.add("other line", [100, 500, 500], secondary=True) chart.range = 0, 10000 chart.secondary_range = 0, 1000 return chart.render_response()
def test_interpolate_secondary(): chart = Line(title=u'Some different points', interpolate='cubic') chart.add('line', [1000, 2000, 7000]) chart.add('other line', [100, 500, 500], secondary=True) chart.range = 0, 10000 chart.secondary_range = 0, 1000 return chart.render_response()
def _generate_time_chart(self, datas): """ After generate a time chart,save to file and return the chart path Keyword arguments: datas -- Dict object of parsed information for a time chart """ if not datas: return "" line_chart = Line(x_label_rotation=30, sstyle=LightStyle, human_readable=True) indices = sorted(datas.keys()) time_format = self.options["time_format"] line_chart.x_labels = map(lambda x: dateutil.parser.parse(x).strftime(time_format), indices) chart_data = {} for index in indices: for data in datas[index]: if chart_data.get(data[0]): chart_data.get(data[0]).append(data[1]) else: chart_data[data[0]] = [data[1]] for key in chart_data: line_chart.add(key, chart_data[key]) path = os.path.join(tempfile.gettempdir(), "time{}.svg".format(str(int(time.time())))) line_chart.render_to_file(path) logging.info("Time chart was created successfully.") return path
def test_only_major_dots_every(): """Test major dots""" line = Line(show_only_major_dots=True, x_labels_major_every=3) line.add('test', range(12)) line.x_labels = map(str, range(12)) q = line.render_pyquery() assert len(q(".dots")) == 4
def test_stroke_config(): line = Line(stroke_style={'width': .5}) line.add('test_no_line', range(12), stroke=False) line.add('test', reversed(range(12)), stroke_style={'width': 3}) line.add( 'test_no_dots', [5] * 12, show_dots=False, stroke_style={ 'width': 2, 'dasharray': '12, 31' } ) line.add( 'test_big_dots', [randint(1, 12) for _ in range(12)], dots_size=5 ) line.add( 'test_fill', [randint(1, 3) for _ in range(12)], fill=True, stroke_style={ 'width': 5, 'dasharray': '4, 12, 7, 20' } ) line.x_labels = [ 'lol', 'lol1', 'lol2', 'lol3', 'lol4', 'lol5', 'lol6', 'lol7', 'lol8', 'lol9', 'lol10', 'lol11' ] return line.render_response()
def test_only_major_dots_count(): line = Line(show_only_major_dots=True) line.add('test', range(12)) line.x_labels = map(str, range(12)) line.x_labels_major_count = 2 q = line.render_pyquery() assert len(q(".dots")) == 2
def test_value_formatter(): line = Line(value_formatter=lambda x: str(x) + u('‰')) line.add('_', [10 ** 4, 10 ** 5, 23 * 10 ** 4]) q = line.render_pyquery() assert len(q(".y.axis .guides")) == 11 assert q(".axis.y text").map(texts) == list(map( lambda x: str(x) + u('‰'), map(float, range(20000, 240000, 20000))))
def test_only_major_dots(): line = Line(show_only_major_dots=True,) line.add('test', range(12)) line.x_labels = map(str, range(12)) line.x_labels_major = ['1', '5', '11'] q = line.render_pyquery() assert len(q(".dots")) == 3
def test_only_major_dots(): line = Line(show_only_major_dots=True, ) line.add('test', range(12)) line.x_labels = map(str, range(12)) line.x_labels_major = ['1', '5', '11'] q = line.render_pyquery() assert len(q(".dots")) == 3
def test_simple_line(): """Simple line test""" line = Line() rng = range(-30, 31, 5) line.add('test1', [cos(x / 10) for x in rng]) line.add('test2', [sin(x / 10) for x in rng]) line.add('test3', [cos(x / 10) - sin(x / 10) for x in rng]) line.x_labels = map(str, rng) line.title = "cos sin and cos - sin" q = line.render_pyquery() assert len(q(".axis.x")) == 1 assert len(q(".axis.y")) == 1 assert len(q(".plot .series path")) == 3 assert len(q(".legend")) == 3 assert len(q(".x.axis .guides")) == 13 assert len(q(".y.axis .guides")) == 13 assert len(q(".dots")) == 3 * 13 assert q(".axis.x text").map(texts) == [ '-30', '-25', '-20', '-15', '-10', '-5', '0', '5', '10', '15', '20', '25', '30'] assert q(".axis.y text").map(texts) == [ '-1.2', '-1', '-0.8', '-0.6', '-0.4', '-0.2', '0', '0.2', '0.4', '0.6', '0.8', '1', '1.2'] assert q(".title").text() == 'cos sin and cos - sin' assert q(".legend text").map(texts) == ['test1', 'test2', 'test3']
def test_simple_line(): """Simple line test""" line = Line() rng = range(-30, 31, 5) line.add('test1', [cos(x / 10) for x in rng]) line.add('test2', [sin(x / 10) for x in rng]) line.add('test3', [cos(x / 10) - sin(x / 10) for x in rng]) line.x_labels = map(str, rng) line.title = "cos sin and cos - sin" q = line.render_pyquery() assert len(q(".axis.x")) == 1 assert len(q(".axis.y")) == 1 assert len(q(".plot .series path")) == 3 assert len(q(".legend")) == 3 assert len(q(".x.axis .guides")) == 13 assert len(q(".y.axis .guides")) == 13 assert len(q(".dots")) == 3 * 13 assert q(".axis.x text").map(texts) == [ '-30', '-25', '-20', '-15', '-10', '-5', '0', '5', '10', '15', '20', '25', '30' ] assert q(".axis.y text").map(texts) == [ '-1.2', '-1', '-0.8', '-0.6', '-0.4', '-0.2', '0', '0.2', '0.4', '0.6', '0.8', '1', '1.2' ] assert q(".title").text() == 'cos sin and cos - sin' assert q(".legend text").map(texts) == ['test1', 'test2', 'test3']
def test_parametric_styles_with_parameters(): """Test a parametric style with parameters""" line = Line( style=RotateStyle('#de3804', step=12, max_=180, base_style=LightStyle)) line.add('_', [1, 2, 3]) line.x_labels = 'abc' assert line.render()
def test_stroke_config(): line = Line(stroke_style={"width": 0.5}) line.add("test_no_line", range(12), stroke=False) line.add("test", reversed(range(12)), stroke_style={"width": 3}) line.add("test_no_dots", [5] * 12, show_dots=False, stroke_style={"width": 2, "dasharray": "12, 31"}) line.add("test_big_dots", [randint(1, 12) for _ in range(12)], dots_size=5) line.add( "test_fill", [randint(1, 3) for _ in range(12)], fill=True, stroke_style={"width": 5, "dasharray": "4, 12, 7, 20"}, ) line.x_labels = [ "lol", "lol1", "lol2", "lol3", "lol4", "lol5", "lol6", "lol7", "lol8", "lol9", "lol10", "lol11", ] return line.render_response()
def test_config_behaviours(): """Test that all different way to set config produce same results""" line1 = Line() line1.show_legend = False line1.fill = True line1.pretty_print = True line1.no_prefix = True line1.x_labels = ['a', 'b', 'c'] line1.add('_', [1, 2, 3]) l1 = line1.render() q = line1.render_pyquery() assert len(q(".axis.x")) == 1 assert len(q(".axis.y")) == 1 assert len(q(".plot .series path")) == 1 assert len(q(".legend")) == 0 assert len(q(".x.axis .guides")) == 3 assert len(q(".y.axis .guides")) == 11 assert len(q(".dots")) == 3 assert q(".axis.x text").map(texts) == ['a', 'b', 'c'] line2 = Line( show_legend=False, fill=True, pretty_print=True, no_prefix=True, x_labels=['a', 'b', 'c']) line2.add('_', [1, 2, 3]) l2 = line2.render() assert l1 == l2 class LineConfig(Config): show_legend = False fill = True pretty_print = True no_prefix = True x_labels = ['a', 'b', 'c'] line3 = Line(LineConfig) line3.add('_', [1, 2, 3]) l3 = line3.render() assert l1 == l3 line4 = Line(LineConfig()) line4.add('_', [1, 2, 3]) l4 = line4.render() assert l1 == l4 line_config = Config() line_config.show_legend = False line_config.fill = True line_config.pretty_print = True line_config.no_prefix = True line_config.x_labels = ['a', 'b', 'c'] line5 = Line(line_config) line5.add('_', [1, 2, 3]) l5 = line5.render() assert l1 == l5
def test_major_dots(): line = Line(x_labels_major_count=2, show_only_major_dots=True) line.add('test', range(12)) line.x_labels = [ 'lol', 'lol1', 'lol2', 'lol3', 'lol4', 'lol5', 'lol6', 'lol7', 'lol8', 'lol9', 'lol10', 'lol11'] # line.x_labels_major = ['lol3'] return line.render_response()
def test_show_dots(): line = Line() line.add('_', [1, 2, 3]) q = line.render_pyquery() assert len(q(".dots")) == 3 line.show_dots = False q = line.render_pyquery() assert len(q(".dots")) == 0
def test_no_data_sparktext(): """Test no data sparktext""" chart2 = Line() chart2.add('_', []) assert chart2.render_sparktext() == u('') chart3 = Line() assert chart3.render_sparktext() == u('')
def test_one_dot(): line = Line() line.add('one dot', [12]) line.x_labels = ['one'] q = line.render_pyquery() assert len(q(".axis.x")) == 1 assert len(q(".axis.y")) == 1 assert len(q(".y.axis .guides")) == 1
def test_human_readable(): line = Line() line.add("_", [10 ** 4, 10 ** 5, 23 * 10 ** 4]) q = line.render_pyquery() assert q(".axis.y text").map(texts) == list(map(str, map(float, range(20000, 240000, 20000)))) line.human_readable = True q = line.render_pyquery() assert q(".axis.y text").map(texts) == list(map(lambda x: "%dk" % x, range(20, 240, 20)))
def test_same_max_and_relative_values_sparktext(): chart = Line() chart.add('_', [0, 0, 0, 0, 0]) assert chart.render_sparktext() == u('▁▁▁▁▁') chart2 = Line() chart2.add('_', [1, 1, 1, 1, 1]) assert chart2.render_sparktext(relative_to=1) == u('▁▁▁▁▁')
def test_interruptions(): chart = Line(allow_interruptions=True) chart.add('interrupt', [22, 34, 43, 12, None, 12, 55, None, 56], allow_interruptions=False) chart.add('not interrupt', [ -a if a else None for a in (22, 34, 43, 12, None, 12, 55, None, 56) ]) return chart.render_response()
def simulation(): x = w.x_odeint(w.initial) line = Line(show_dots=False,show_minor_x_labels=False) for c in x: line.add(c,x[c]) line.x_labels = map(str,x.index.values) line.x_labels_major = map(str,range(x.index[0],x.index[-1]+50,50)) return line.render_response()
def test_another_sparktext(): chart = Line() chart.add('_', [0, 30, 55, 80, 33, 150]) assert chart.render_sparktext() == u('▁▂▃▄▂█') assert chart.render_sparktext() == chart.render_sparktext() chart2 = Bar() chart2.add('_', [0, 30, 55, 80, 33, 150]) assert chart2.render_sparktext() == chart.render_sparktext()
def test_show_legend(): line = Line() line.add('_', [1, 2, 3]) q = line.render_pyquery() assert len(q(".legend")) == 1 line.show_legend = False q = line.render_pyquery() assert len(q(".legend")) == 0
def test_not_equal_x_labels(): line = Line() line.add('test1', range(100)) line.x_labels = map(str, range(11)) q = line.render_pyquery() assert len(q(".dots")) == 100 assert len(q(".axis.x")) == 1 assert q(".axis.x text").map(texts) == ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
def test_interruptions(): chart = Line(allow_interruptions=True) chart.add( 'interrupt', [22, 34, 43, 12, None, 12, 55, None, 56], allow_interruptions=False) chart.add('not interrupt', [ -a if a else None for a in (22, 34, 43, 12, None, 12, 55, None, 56)]) return chart.render_response()
def test_human_readable(): line = Line() line.add('_', [10 ** 4, 10 ** 5, 23 * 10 ** 4]) q = line.render_pyquery() assert q(".axis.y text").map(texts) == list(map( str, list(map(float, list(range(20000, 240000, 20000)))))) line.human_readable = True q = line.render_pyquery() assert q(".axis.y text").map(texts) == ['%dk' % x for x in range(20, 240, 20)]
def graphing(budget_list): the_graph = Line() the_graph.title = 'Maximum profits based on given prices' the_graph.x_labels = sorted(budget_list) the_graph.add('Profits', all_profits_from_prices(budget_list)) # option to save to local dir # the_graph.render_to_file('profits.svg') return the_graph.render_data_uri()
def test_parametric_styles(): chart = None for style in STYLES: line = Line(style=style('#f4e83a')) line.add('_', [1, 2, 3]) line.x_labels = 'abc' new_chart = line.render() assert chart != new_chart chart = new_chart
def test_logarithmic_bad_interpolation(): try: import scipy except ImportError: return line = Line(logarithmic=True, interpolate='cubic') line.add('_', [.001, .00000001, 1]) q = line.render_pyquery() assert len(q(".y.axis .guides")) == 40
def test_another_sparktext(): """Test that same data produces same sparktext""" chart = Line() chart.add('_', [0, 30, 55, 80, 33, 150]) assert chart.render_sparktext() == u('▁▂▃▄▂█') assert chart.render_sparktext() == chart.render_sparktext() chart2 = Bar() chart2.add('_', [0, 30, 55, 80, 33, 150]) assert chart2.render_sparktext() == chart.render_sparktext()
def test_serie_precedence_over_global_config(): """Test that per serie configuration overide global configuration""" chart = Line(stroke=False) chart.add('1', s1, stroke=True) chart.add('2', s2) q = chart.render_pyquery() assert len(q('.serie-0 .line')) == 1 assert len(q('.serie-1 .line')) == 0 assert len(q('.serie-0 .dot')) == 5 assert len(q('.serie-1 .dot')) == 6
def test_human_readable(): line = Line() line.add('_', [10 ** 4, 10 ** 5, 23 * 10 ** 4]) # q = line.render_pyquery() # assert q(".axis.y text").map(texts) == map( # str, range(20000, 240000, 20000)) line.human_readable = True q = line.render_pyquery() assert q(".axis.y text").map(texts) == map( lambda x: '%dk' % x, range(20, 240, 20))
def test_serie_config(): """Test per serie configuration""" chart = Line() chart.add('1', s1, stroke=False) chart.add('2', s2) q = chart.render_pyquery() assert len(q('.serie-0 .line')) == 0 assert len(q('.serie-1 .line')) == 1 assert len(q('.serie-0 .dot')) == 5 assert len(q('.serie-1 .dot')) == 6
def test_global_config(): """Test global configuration""" chart = Line(stroke=False) chart.add('1', s1) chart.add('2', s2) q = chart.render_pyquery() assert len(q('.serie-0 .line')) == 0 assert len(q('.serie-1 .line')) == 0 assert len(q('.serie-0 .dot')) == 5 assert len(q('.serie-1 .dot')) == 6
def test_parametric_styles(): """Test that no parametric produce the same result""" chart = None for style in STYLES: line = Line(style=style('#f4e83a')) line.add('_', [1, 2, 3]) line.x_labels = 'abc' new_chart = line.render() assert chart != new_chart chart = new_chart
def test_human_readable(): line = Line() line.add('_', [10**4, 10**5, 23 * 10**4]) q = line.render_pyquery() assert q(".axis.y text").map(texts) == list( map(str, map(float, range(20000, 240000, 20000)))) line.human_readable = True q = line.render_pyquery() assert q(".axis.y text").map(texts) == list( map(lambda x: '%dk' % x, range(20, 240, 20)))
def test_erfinv(): from scipy import stats as sstats chart = Line(show_dots=False) chart.add('scipy', [sstats.norm.ppf(x / 1000) for x in range(1, 999)]) chart.add('approx', [stats.ppf(x / 1000) for x in range(1, 999)]) # chart.add('approx', [ # special.erfinv(x/1000) - erfinv(x/1000) # for x in range(-999, 1000)]) return chart.render_response()