{# This template adds attributes unique
   to pieChart #}

{% extends "content.html" %}
{% block body %}

    data_{{ chart.name }}={{ chart.series_js }};

    nv.addGraph(function() {
        var chart = nv.models.{{ chart.model }}(){% if chart.use_interactive_guideline %}.useInteractiveGuideline(true){% endif %};
        chart.margin({top: {{ chart.margin_top }}, right: {{ chart.margin_right }}, bottom: {{ chart.margin_bottom }}, left: {{ chart.margin_left }}});
        var datum = data_{{ chart.name }}[0].values;

    {% if not chart.color_list and chart.color_category %}
        chart.color(d3.scale.{{ chart.color_category }}().range());
    {% endif %}

    chart.tooltipContent(function(key, y, e, graph) {
          var x = String(key);
              {{ chart.tooltip_condition_string }}
              tooltip_str = '<center><b>'+x+'</b></center>' + y;
              return tooltip_str;
              });
    {# showLabels only supported in pieChart #}
        chart.showLabels({{chart.show_labels|lower}});

        {% if chart.donut %}
            chart.donut(true);
            chart.donutRatio({{ chart.donutRatio }});
        {% else %}
            chart.donut(false);
        {% endif %}

    chart.showLegend({{chart.show_legend|lower}});

    {# add custom chart attributes #}
    {% for attr, value in chart.chart_attr.items() %}
        {% if value is string and value.startswith(".") %}:
            chart.{{ attr }}{{ value }};
        {% else %}
            chart.{{ attr }}({{ value }});
        {% endif %}
    {% endfor %}

    {% if chart.resize %}
        nv.utils.windowResize(chart.update);
    {% endif %}

    {% if chart.color_list %}
        var mycolor = new Array();
        {% for color in chart.color_list %}
            mycolor[{{ loop.index - 1}}] = "{{ color }}";
        {% endfor %}
    {% endif %}

        chart
            .x(function(d) { return d.label })
            .y(function(d) { return d.value });

    {% if chart.width %}
        chart.width({{ chart.width }});
    {% endif %}

    {% if chart.height %}
        chart.height({{ chart.height }});
    {% endif %}

    {% if chart.color_list %}
        chart.color(mycolor);
    {% endif %}

    {% block inject %}
    {{super()}}
    {% endblock inject %}

    {% block close %}
    {{ super() }}
    {% endblock close %}

{% endblock body %}
