def timeline(data, options=None, **kwargs): """produce a gantt timeline chart""" chart_id = get_chart_id(**kwargs) title = kwargs.pop('title', None) title = title and '<div class="visjs-title">{}</div>'.format(title) or '' default_options = {} options = merge_options(merge_options(default_options, options), kwargs) content = """ %s <div class="dz-visjs-chart placeholder" id="%s"></div> """ % (title, chart_id) js = """ $(function(){ var container = document.getElementById("%(selector)s"); var chart = new vis.DataSet( %(data)s ); var options = %(options)s; var timeline = new vis.Timeline(container, chart, options); // make the object accessible later $("#%(selector)s").data('visjs-chart', chart); }); """ % dict(selector=chart_id, data=data, options=json.dumps(options, indent=4)) return component(content, js=js, libs=libs, styles=styles, css=css)
def donut(data, options=None, **kwargs): """produce a donut chart""" chart_id = get_chart_id(**kwargs) default_options = { 'data': { 'columns': data, 'type': 'donut', }, 'tooltip': { 'format': { 'value': '<<formatter>>', }, }, 'bindto': '#' + chart_id, } options = merge_options(merge_options(default_options, options), kwargs) content = """ <div class="dz-c3-chart placeholder" id="{}"></div> """.format(chart_id) formatter = 'function(x){return x}' options = json.dumps(options, indent=4).replace( '"<<formatter>>"', formatter) js = """ $(function(){ var chart = c3.generate(%(options)s); }); """ % dict(options=options) return component(content, js=js, libs=libs, styles=styles, css=css)
def hbar(data, options=None, **kwargs): default_options = { 'axis': { 'rotated': True, }, } options = merge_options(merge_options(default_options, options), kwargs) return bar(data, options, **kwargs)
def area(data, options=None, **kwargs): """produce an area chart""" default_options = { 'data': { 'type': 'area', }, } options = merge_options(merge_options(default_options, options), kwargs) return line(data, options, **kwargs)
def pie(data, options=None, **kwargs): """produce a pie chart""" default_options = { 'data': { 'type': 'pie', }, } options = merge_options(merge_options(default_options, options), kwargs) return donut(data, options, **kwargs)
def step(data, options=None, **kwargs): """produce a bar chart""" default_options = { 'data': { 'type': 'area-step' }, } options = merge_options(merge_options(default_options, options), kwargs) return line(data, options, **kwargs)
def line(data, legend=None, options=None, **kwargs): """produce a line chart""" # pylint: disable=star-args # It's reasonable in this case. chart_id = kwargs.pop('chart_id', 'chart_' + uuid.uuid4().hex) data = zip(*data) default_options = { 'legend': { 'position': 'inset' }, } options = merge_options(merge_options(default_options, options), kwargs) #legend = options.get('legend', None) labels = data[0] rows = [] rows.append(['x'] + list(labels)) if legend: if len(legend) != len(data) - 1: msg = '{} legend must match number of data columns' raise Exception(msg.format(__file__)) for n, label in enumerate(legend): rows.append([label] + list(data[n + 1])) content = """ <div class="dz-c3-chart placeholder" id="%s"></div> """ % chart_id js = """ $(function(){ var chart = c3.generate({ bindto: '#%(chart_id)s', title: {text: '%(title)s'}, data: { x: 'x', columns: %(data)s }, axis: { x: { type: 'category' } } }); }); """ % dict( chart_id=chart_id, data=json.dumps(rows), title=kwargs.get('title', ''), ) return component(content, js=js, libs=libs, styles=styles, css=css)
def stacked_step(data, options=None, **kwargs): """produces a stacked area chart""" stacks = kwargs.pop('stacks', []) default_options = { 'data': { 'groups': stacks, }, } options = merge_options(merge_options(default_options, options), kwargs) return step(data, options, **kwargs)
def bar(data, options=None, **kwargs): """produce a line chart""" chart_id = get_chart_id(**kwargs) data = zip(*data) legend = kwargs.pop('legend', None) labels = data[0] rows = [] rows.append(['x'] + list(labels)) if legend: if len(legend) != len(data) - 1: msg = '{} legend must match number of data columns' raise Exception(msg.format(__file__)) for n, label in enumerate(legend): rows.append([label] + list(data[n + 1])) default_options = { 'data': { 'x': 'x', 'columns': rows, 'type': 'bar' }, 'legend': { 'position': 'inset' }, 'bar': { 'width': { 'ratio': 0.5 } }, 'axis': { 'x': { 'type': 'category' } }, 'legend': { 'position': 'inset' }, 'bindto': '#' + chart_id } options = merge_options(merge_options(default_options, options), kwargs) content = """ <div class="dz-c3-chart placeholder" id="{}"></div> """.format(chart_id) js = """ $(function(){ var chart = c3.generate(%(options)s); }); """ % dict(options=json.dumps(options, indent=4)) return component(content, js=js, libs=libs, styles=styles, css=css)
def stacked_area_spline(data, options=None, **kwargs): """produces a stacked area chart""" stacks = kwargs.pop('stacks', []) default_options = { 'data': { 'type': 'area-spline', 'groups': stacks, }, } options = merge_options(merge_options(default_options, options), kwargs) return area(data, options, **kwargs)
def line(data, options=None, **kwargs): """produce a line chart""" # pylint: disable=star-args # It's reasonable in this case. chart_id = get_chart_id(**kwargs) legend = kwargs.pop('legend', None) data = zip(*data) labels = data[0] rows = [] rows.append(['x'] + list(labels)) if legend: if len(legend) != len(data) - 1: msg = '{} legend must match number of data columns' raise Exception(msg.format(__file__)) for n, label in enumerate(legend): rows.append([label] + list(data[n + 1])) default_options = { 'data': { 'x': 'x', 'columns': rows, }, 'legend': { 'position': 'inset', }, 'axis': { 'x': { 'type': 'category' }, }, 'bindto': '#' + chart_id } options = merge_options(merge_options(default_options, options), kwargs) content = """ <div class="dz-c3-chart placeholder" id="%s"></div> """ % chart_id js = """ $(function(){ var chart = c3.generate(%(options)s); // make the object accessible later $("#%(selector)s").data('c3-chart', chart); }); """ % dict(selector=chart_id, options=json.dumps(options, indent=4)) return component(content, js=js, libs=libs, styles=styles, css=css)
def scatter(data, options=None, **kwargs): """produce a scatter plot""" rotate_axis = kwargs.pop('rotate_axis', False) default_options = { 'data': { 'type': 'scatter', }, 'axis': { 'rotated': rotate_axis } } options = merge_options(merge_options(default_options, options), kwargs) return line(data, options, **kwargs)
def bar(data, options=None, **kwargs): """produce a bar chart""" default_options = { 'data': { 'type': 'bar' }, 'legend': { 'position': 'inset' }, 'bar': { 'width': { 'ratio': 0.5 } }, } options = merge_options(merge_options(default_options, options), kwargs) return line(data, options, **kwargs)
def render_options(default_options, options, k=None): """Merges options with default options and inserts plugins""" is_plugin = r'\"\$\.jqplot\.(.*)"' combined = merge_options(merge_options(default_options, options), k) result = json.dumps(combined, sort_keys=True, indent=4) return re.sub(is_plugin, lambda a: '$.jqplot.'+a.group(1), result)
def render_options(default_options, options, k=None): """Merges options with default options and inserts plugins""" is_plugin = r'\"\$\.jqplot\.(.*)"' combined = merge_options(merge_options(default_options, options), k) result = json.dumps(combined, sort_keys=True, indent=4) return re.sub(is_plugin, lambda a: '$.jqplot.' + a.group(1), result)