in ailab/benchmark/templatetags/nvd3_tags.py [0:0]
def load_chart(chart_type, series, container, kw_extra=None, *args, **kwargs):
"""Loads the Chart objects in the container.
**usage**:
{% load_chart "lineWithFocusChart" data_set "div_lineWithFocusChart" %}
**Arguments**:
* ``chart_type`` - Give chart type name eg. lineWithFocusChart/pieChart
* ``series`` - Data set which are going to be plotted in chart.
* ``container`` - Chart holder in html page.
**kw_extra settings**::
* ``x_is_date`` - if enabled the x-axis will be display as date format
* ``x_axis_format`` - set the x-axis date format, ie. "%d %b %Y"
* ``tag_script_js`` - if enabled it will add the javascript tag '<script>'
* ``jquery_on_ready`` - if enabled it will load the javascript only when page is loaded
this will use jquery library, so make sure to add jquery to the template.
* ``color_category`` - Define color category (eg. category10, category20, category20c)
* ``chart_attr`` - Custom chart attributes
"""
if not chart_type:
return False
if kw_extra is None:
kw_extra = {}
if "x_is_date" not in kw_extra:
kw_extra["x_is_date"] = False
if "x_axis_format" not in kw_extra:
kw_extra["x_axis_format"] = "%d %b %Y"
if "color_category" not in kw_extra:
kw_extra["color_category"] = "category20"
if "tag_script_js" not in kw_extra:
kw_extra["tag_script_js"] = True
if "chart_attr" not in kw_extra:
kw_extra["chart_attr"] = {}
# set the container name
kw_extra["name"] = str(container)
# Build chart
chart = eval(chart_type)(**kw_extra)
xdata = series["x"]
y_axis_list = [k for k in series.keys() if k.startswith("y")]
if len(y_axis_list) > 1:
# Ensure numeric sorting
y_axis_list = sorted(y_axis_list, key=lambda x: int(x[1:]))
for key in y_axis_list:
ydata = series[key]
axis_no = key.split("y")[1]
name = series["name" + axis_no] if series.get("name" + axis_no) else None
extra = series["extra" + axis_no] if series.get("extra" + axis_no) else {}
kwargs = series["kwargs" + axis_no] if series.get("kwargs" + axis_no) else {}
chart.add_serie(name=name, y=ydata, x=xdata, extra=extra, **kwargs)
chart.display_container = False
chart.buildcontent()
html_string = chart.htmlcontent + "\n"
return mark_safe(html_string)