class BaseDataSource(object):
    def __init__(*args, **kwargs):
        pass

    def get_data(self):
        "Get all the data. Subclasses should override this"
        raise GraphosException("Not Implemented")

    def get_header(self):
        "Get the header - First row. Subclasses should override this"
        raise GraphosException("Not Implemented")

    def get_first_column(self):
        """
        Get the first column. Generally would be the x axis.
        Subclasses should override this
        """
        raise GraphosException("Not Implemented") class SimpleDataSource(BaseDataSource):
    def __init__(self, data):
        """
        : param data: :type list of lists
        Example usage:
            data = [
               ['Year', 'Sales', 'Expenses', 'Items Sold', 'Net Profit'],
               ['2004', 1000, 400, 100, 600],
               ['2005', 1170, 460, 120, 310],
               ['2006', 660, 1120, 50, -460],
               ['2007', 1030, 540, 100, 200],
            ]
            sd = SimpleDataSource(data)
        """
        self.data = data

    def get_data(self):
        return self.data

    def get_header(self):
        return self.data[0]

    def get_first_column(self):
        """
        Get the first column. Generally would be the x axis.
        : return: :type list of strings
        Example:
        For the example shown in __init__, it would return the following:
            ['2004', '2005', '2006', '2007']
        """
        data_not_header = self.data[1:]
        return [el[0] for el in data_not_header] class CSVDataSource(SimpleDataSource):
    "A data source for CSV files"
    def __init__(self, csv_file, fields=None):
        """csv_file: A file like object which should be charted
        """
        reader = csv.reader(csv_file)
        data =[row for row in reader]
        self.data = data
        self.fields = fields class ModelDataSource(SimpleDataSource):
    """
    Normalizes data contained in a queryset to format usable by renderers
    """
    def __init__(self, queryset, fields=None):
        """
        : param queryset: :type Django ORM queryset
        : param fields: :type  list of strings
        Example usage:
            queryset = Account.objects.all()
            mds = ModelDataSource(queryset, fields=['year', 'sales', 'expenses'])
            # This assumes the following model Account:
            class Account(models.Model):
                year = models.IntegerField()
                sales = models.DecimalField()
                expenses = models.DecimalField()
                profit = models.DecimalField()
        """
        self.queryset = queryset
        if fields:
            self.fields = fields
        else:
            self.fields = [el.name for el in self.queryset.model._meta.fields]
        self.data = self.create_data()

    def create_data(self):
        data = [self.fields]
        for row in self.queryset:
            data.append(get_field_values(row, self.fields))
        return data class MongoDBDataSource(SimpleDataSource):
    """ MongoDBDataSource """

    def __init__(self,
                 cursor,
                 fields,
                 *args,
                 **kwargs):

        self.cursor = cursor
        self.fields = fields
        self._doc_list = []

    def get_data(self):
        if self._doc_list:
            return self._doc_list
        doc_list = []
        doc_list.append(self.fields)
        for doc in self.cursor:
            current_row = get_row(doc, self.fields)
            doc_list.append(current_row)
        self._doc_list = doc_list
        return doc_list

    @property
    def data(self):
        return self.get_data() class BaseChart(object):
    def __init__(self, data_source, html_id=None,
                 width=None, height=None,
                 options=None, encoder=GraphosEncoder,
                 *args, **kwargs):
    def get_data(self):
    def get_data_json(self):
    def get_options(self):
    def get_options_json(self):
    def get_template(self):
    def get_html_template(self):
    def get_js_template(self):
    def get_html_id(self):
    def get_context_data(self):
    def as_html(self):
    def render_html(self):
    def render_js(self):
    def zip_list(self, *args): class BaseFlotChart(BaseChart):
class PointChart(BaseFlotChart):
class LineChart(BaseFlotChart):
class BarChart(BaseFlotChart):
class ColumnChart(BaseFlotChart):
class PieChart(BaseFlotChart):
    def get_serieses(self):
    def get_series_objects(self):
    def get_series_pie_objects(self):
    def get_series_objects_json(self):
    def get_options(self):
    def get_html_template(self):
    def get_js_template(self):
    def get_options(self):
    def get_options(self):
    def get_options(self):
    def get_options(self):
    def get_options(self):
    def get_js_template(self): class BaseGChart(BaseChart):
class LineChart(BaseGChart):
class GaugeChart(BaseGChart):
class ColumnChart(BaseGChart):
class BarChart(BaseGChart):
class CandlestickChart(BaseGChart):
class PieChart(BaseGChart):
class TreeMapChart(BaseGChart):
class AreaChart(BaseGChart):
    def get_html_template(self):
    def get_js_template(self):
    def get_js_template(self):
    def get_js_template(self):
    def get_js_template(self):
    def get_options(self):
    def get_js_template(self):
    def get_js_template(self):
    def get_js_template(self):
    def get_js_template(self): class BaseYuiChart(BaseChart):
class LineChart(BaseYuiChart):
class SplineChart(BaseYuiChart):
class BarChart(BaseYuiChart):
class ColumnChart(BaseYuiChart):
class PieChart(BaseYuiChart):
class AreaChart(BaseYuiChart):
class AreaSplineChart(BaseYuiChart):
class ComboChart(BaseYuiChart):
class ComboSplineChart(BaseYuiChart):
class MarkerSeriesChart(BaseYuiChart):
    def get_data(self):
    def get_category_key(self):
    def get_html_template(self):
    def get_js_template(self):
    def get_chart_type(self):
    def get_js_template(self):
    def get_chart_type(self):
    def get_js_template(self):
    def get_chart_type(self):
    def get_js_template(self):
    def get_chart_type(self):
    def get_js_template(self):
    def get_chart_type(self):
    def get_data(self):
    def get_js_template(self):
    def get_chart_type(self):
    def get_js_template(self):
    def get_chart_type(self):
    def get_js_template(self):
    def get_chart_type(self):
    def get_js_template(self):
    def get_chart_type(self):
    def get_js_template(self):
    def get_chart_type(self): class BaseMatplotlibChart(BaseChart):
class LineChart(BaseMatplotlibChart):
class BarChart(BaseMatplotlibChart):
    def get_html_template(self):
    def get_serieses(self):
    def render_js(self):
    def get_image(self):
    def get_image(self): class BaseHighCharts(BaseChart):
class LineChart(BaseHighCharts):
class BarChart(BaseHighCharts):
class ColumnChart(BaseHighCharts):
class AreaChart(BaseHighCharts):
class ScatterChart(BaseHighCharts):
class ColumnLineChart(BaseHighCharts):
class LineColumnChart(BaseHighCharts):
class PieChart(BaseHighCharts):
class DonutChart(PieChart):
class MultiAxisChart(BaseHighCharts):
class HighMap(BaseHighCharts):
class HeatMap(BaseHighCharts):
class Funnel(BaseHighCharts):
class TreeMap(BaseHighCharts):
class PieDonut(BaseHighCharts):
class Bubble(BaseHighCharts):
    def get_html_template(self):
    def get_js_template(self):
    def get_series(self):
    def add_series_options(self, serieses):
    def get_series_json(self):
    def get_categories(self):
    def get_categories_json(self):
    def get_title(self):
    def get_title_json(self):
    def get_subtitle(self):
    def get_subtitle_json(self):
    def get_chart(self):
    def get_chart_type(self):
    def get_chart_json(self):
    def get_plot_options(self):
    def get_plot_options_json(self):
    def get_x_axis_title(self):
    def get_x_axis(self):
    def get_x_axis_json(self):
    def get_y_axis_title(self):
    def get_y_axis(self):
    def get_y_axis_json(self):
    def get_tooltip(self):
    def get_tooltip_json(self):
    def get_credits(self):
    def get_credits_json(self):
    def get_exporting(self):
    def get_exporting_json(self):
    def get_legend(self):
    def get_legend_json(self):
    def get_navigation(self):
    def get_navigation_json(self):
    def get_chart_type(self):
    def get_chart_type(self):
    def get_chart_type(self):
    def get_chart_type(self):
    def __init__(self, *args, **kwargs):
    def get_chart_type(self):
    def get_series(self):
    def calculate_single_series(self):
    def calculate_multi_series(self):
    def get_x_axis_title(self):
    def get_x_axis(self):
    def get_y_axis_title(self):
    def get_series(self):
    def get_chart_type(self):
    def get_series(self):
    def get_chart_type(self):
    def get_series(self):
    def get_plot_options(self):
    def get_chart_type(self):
    def get_chart(self):
    def get_plot_options(self):
    def get_series(self):
    def get_y_axis(self):
    def get_chart_type(self):
    def __init__(self, *args, **kwargs):
    def get_series(self):
    def calculate_multi_series(self):
    def calculate_single_series(self):
    def get_js_template(self):
    def get_map(self):
    def get_series_name(self):
    def get_color_axis(self):
    def get_color_axis_json(self):
    def get_plot_options(self):
    def get_chart_type(self):
def column(matrix, i):
def pie_column(matrix, i):
    def get_series(self):
    def get_y_axis(self):
    def get_color_axis(self):
    def get_color_axis_json(self):
    def get_chart_type(self):
    def get_js_template(self):
    def get_plot_options(self):
    def get_series(self):
    def get_chart_type(self):
    def get_plot_options(self):
def nested_list_to_tree(data):
def generate_treemap_data(root, no_of_column):
def generate_pie_donut_data(root, no_of_column):
    def get_series(self):
    def get_chart_type(self):
    def get_plot_options(self):
    def get_js_template(self):
    def get_series(self):
    def get_chart_type(self):
    def __init__(self, *args, **kwargs):
    def get_series(self):
    def calculate_single_series(self):
    def calculate_multi_series(self):
    def get_x_axis_title(self):
    def get_x_axis(self):
    def get_y_axis_title(self):
    def get_chart_type(self): DEFAULT_HEIGHT = 400 DEFAULT_WIDTH = 800 def get_default_options(graph_type="lines"):
    """ default options """
    options = {"series": {"%s" % graph_type: {"show": "true"}},
               "legend": {"position": 'ne'},
               "title": "Chart"}
    return options def get_db(db_name=None):
    """ GetDB - simple function to wrap getting a database
    connection from the connection pool.
    """
    import pymongo
    return pymongo.Connection(host=DB_HOST,
                              port=DB_PORT)[db_name]
from django.test import TestCase
from pymongo.errors import CollectionInvalid
from .sources.base import BaseDataSource
from .sources.simple import SimpleDataSource
from .sources.csv_file import CSVDataSource
from .sources.model import ModelDataSource
from .sources.mongo import MongoDBDataSource
from .renderers import base, flot, gchart, yui, matplotlib_renderer, highcharts
from .exceptions import GraphosException
from .utils import DEFAULT_HEIGHT, DEFAULT_WIDTH, get_default_options, get_db
from demo.models import Account
import os
import json






current_path = os.path.dirname(os.path.abspath(__file__))


class TestSources(TestCase):
    def test_base_data_source(self):
        data_source = BaseDataSource()
        self.assertTrue(hasattr(data_source, "get_data"))
        self.assertRaises(GraphosException, data_source.get_data,)
        self.assertTrue(hasattr(data_source, "get_header"))
        self.assertRaises(GraphosException, data_source.get_header)
        self.assertTrue(hasattr(data_source, "get_first_column"))
        self.assertRaises(GraphosException, data_source.get_first_column)

    def test_simple_data_source(self):
        data = [
            ['Year', 'Sales', 'Expenses'],
            ['2004', 1000, 400],
            ['2005', 1170, 460],
            ['2006', 660, 1120],
            ['2007', 1030, 540]
        ]
        data_source = SimpleDataSource(data)
        self.assertEqual(data_source.get_data(), data)
        self.assertEqual(data_source.get_header(),
                         ['Year', 'Sales', 'Expenses'])
        self.assertEqual(data_source.get_first_column(),
                         ['2004', '2005', '2006', '2007'])

    def test_csv_data_source(self):
        data = [
            ['Year', 'Sales', 'Expense'],
            ['2006', '1000', '400'],
            ['2007', '1170', '460'],
            ['2008', '660', '1120'],
            ['2009', '1030', '540']
        ]
        csv_file = open(os.path.join(current_path, "test_data/accounts.csv"),
                        "r")
        data_source = CSVDataSource(csv_file)
        self.assertEqual(data, data_source.get_data())
        self.assertEqual(data_source.get_header(),
                         ['Year', 'Sales', 'Expense'])
        self.assertEqual(data_source.get_first_column(),
                         ['2006', '2007', '2008', '2009'])

    def test_model_data_source(self):
        data = [
            ['year', 'sales', 'expenses'],
            [u'2004', 1000, 400],
            [u'2005', 1170, 460],
            [u'2006', 660, 1120],
            [u'2007', 1030, 540]
        ]
        #Create some rows
        Account.objects.create(year="2004", sales=1000,
                               expenses=400, ceo="Welch")
        Account.objects.create(year="2005", sales=1170,
                               expenses=460, ceo="Jobs")
        Account.objects.create(year="2006", sales=660,
                               expenses=1120, ceo="Page")
        Account.objects.create(year="2007", sales=1030,
                               expenses=540, ceo="Welch")
        query_set = Account.objects.all()

        data_source = ModelDataSource(query_set, ['year', 'sales', 'expenses'])