#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Python-nvd3 is a Python wrapper for NVD3 graph library.
NVD3 is an attempt to build re-usable charts and chart components
for d3.js without taking away the power that d3.js gives you.

Project location : https://github.com/areski/python-nvd3
"""

from .NVD3Chart import NVD3Chart, TemplateMixin
from jinja2 import Environment, FileSystemLoader
import os


class discreteBarChart(NVD3Chart):
    """
    A discrete bar chart or bar graph is a chart with rectangular bars with
    lengths proportional to the values that they represent.

    Python example::

        from nvd3 import discreteBarChart
        chart = discreteBarChart(name='discreteBarChart', height=400, width=400)

        xdata = ["A", "B", "C", "D", "E", "F"]
        ydata = [3, 4, 0, -3, 5, 7]

        chart.add_serie(y=ydata, x=xdata)
        chart.buildhtml()

    Javascript generated:

    .. raw:: html

        <div id="discreteBarChart"><svg style="height:450px;"></svg></div>
        <script>
            data_discreteBarChart=[{"values": [{"y": 3, "x": "A"}, {"y": 4, "x": "B"}, {"y": 0, "x": "C"}, {"y": -3, "x": "D"}, {"y": 5, "x": "E"}, {"y": 7, "x": "F"}], "key": "Serie 1", "yAxis": "1"}];

            nv.addGraph(function() {
                var chart = nv.models.discreteBarChart();

                chart.margin({top: 30, right: 60, bottom: 20, left: 60});

                var datum = data_discreteBarChart;
                        chart.yAxis
                            .tickFormat(d3.format(',.0f'));
                        chart.tooltipContent(function(key, y, e, graph) {
                            var x = String(graph.point.x);
                            var y = String(graph.point.y);
                            var y = String(graph.point.y);

                            tooltip_str = '<center><b>'+key+'</b></center>' + y + ' at ' + x;
                            return tooltip_str;
                        });

                d3.select('#discreteBarChart svg')
                    .datum(datum)
                    .transition().duration(500)
                    .attr('width', 400)
                    .attr('height', 400)
                    .call(chart);
            });
        </script>


    """
    CHART_FILENAME = "./discretebarchart.html"
    template_environment = Environment(lstrip_blocks=True, trim_blocks=True)
    template_environment.loader = FileSystemLoader(os.path.join(
        os.path.dirname(__file__), 'templates'))
    template_chart_nvd3 = template_environment.get_template(CHART_FILENAME)

    def __init__(self, **kwargs):
        NVD3Chart.__init__(self, **kwargs)
        # self.slugify_name(kwargs.get('name', 'discreteBarChart'))
        height = kwargs.get('height', 450)
        width = kwargs.get('width', None)

        if kwargs.get('x_is_date', False):
            self.set_date_flag(True)
            self.create_x_axis('xAxis',
                               format=kwargs.get('x_axis_format',
                                                 "%d %b %Y %H %S"),
                               date=True)
        else:
            self.create_x_axis('xAxis', format=None)

        self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', ".0f"))

        self.set_custom_tooltip_flag(True)

        # must have a specified height, otherwise it superimposes both charts
        if height:
            self.set_graph_height(height)
        if width:
            self.set_graph_width(width)

    def buildjschart(self):
        NVD3Chart.buildjschart(self)
        self.jschart = self.template_chart_nvd3.render(chart=self)


class DiscreteBarChart(TemplateMixin, NVD3Chart):
    CHART_FILENAME = "./discretebarchart.html"
    template_environment = Environment(lstrip_blocks=True, trim_blocks=True)
    template_environment.loader = FileSystemLoader(os.path.join(
        os.path.dirname(__file__), 'templates'))
    template_chart_nvd3 = template_environment.get_template(CHART_FILENAME)

    def __init__(self, **kwargs):
        super(DiscreteBarChart, self).__init__(**kwargs)
        self.model = 'discreteBarChart'
        height = kwargs.get('height', 450)
        width = kwargs.get('width', None)

        if kwargs.get('x_is_date', False):
            self.set_date_flag(True)
            self.create_x_axis('xAxis',
                               format=kwargs.get('x_axis_format',
                                                 "%d %b %Y %H %S"),
                               date=True)
        else:
            self.create_x_axis('xAxis', format=None)

        self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', ".0f"))

        self.set_custom_tooltip_flag(True)

        self.set_graph_height(height)
        if width:
            self.set_graph_width(width)

@@ -402,17 +402,17 @@
 %0A%0Aclass 
-d
+D
 iscreteB
@@ -419,16 +419,31 @@
 arChart(
+TemplateMixin, 
 NVD3Char
@@ -2584,1424 +2584,8 @@
 E)%0A%0A
-    def __init__(self, **kwargs):%0A        NVD3Chart.__init__(self, **kwargs)%0A        # self.slugify_name(kwargs.get('name', 'discreteBarChart'))%0A        height = kwargs.get('height', 450)%0A        width = kwargs.get('width', None)%0A%0A        if kwargs.get('x_is_date', False):%0A            self.set_date_flag(True)%0A            self.create_x_axis('xAxis',%0A                               format=kwargs.get('x_axis_format',%0A                                                 %22%25d %25b %25Y %25H %25S%22),%0A                               date=True)%0A        else:%0A            self.create_x_axis('xAxis', format=None)%0A%0A        self.create_y_axis('yAxis', format=kwargs.get('y_axis_format', %22.0f%22))%0A%0A        self.set_custom_tooltip_flag(True)%0A%0A        # must have a specified height, otherwise it superimposes both charts%0A        if height:%0A            self.set_graph_height(height)%0A        if width:%0A            self.set_graph_width(width)%0A%0A    def buildjschart(self):%0A        NVD3Chart.buildjschart(self)%0A        self.jschart = self.template_chart_nvd3.render(chart=self)%0A%0A%0Aclass DiscreteBarChart(TemplateMixin, NVD3Chart):%0A    CHART_FILENAME = %22./discretebarchart.html%22%0A    template_environment = Environment(lstrip_blocks=True, trim_blocks=True)%0A    template_environment.loader = FileSystemLoader(os.path.join(%0A        os.path.dirname(__file__), 'templates'))%0A    template_chart_nvd3 = template_environment.get_template(CHART_FILENAME)%0A%0A
     
@@ -3341,16 +3341,16 @@
  width:%0A
-
         
@@ -3381,8 +3381,45 @@
 (width)%0A
+%0AdiscreteBarChart = DiscreteBarChart%0A

Replace old class with an alias to the new class