Example #1
0
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)
Example #2
0
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)
Example #3
0
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)
Example #4
0
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)
Example #5
0
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)
Example #6
0
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)
Example #7
0
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)
Example #8
0
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)
Example #9
0
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)
Example #10
0
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)
Example #11
0
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)
Example #12
0
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)
Example #13
0
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)
Example #14
0
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)
Example #15
0
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)
Example #16
0
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)
Example #17
0
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)
Example #18
0
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)