Example #1
0
 def _has_data(self):
     """Check if there is any data"""
     return any([
         len([
             v for a in (s[0] if is_list_like(s) else [s])
             for v in (a if is_list_like(a) else [a]) if v is not None
         ]) for s in self.raw_series
     ])
Example #2
0
File: graph.py Project: Kozea/pygal
 def _has_data(self):
     """Check if there is any data"""
     return any([
         len([
             v for a in (s[0] if is_list_like(s) else [s])
             for v in (a if is_list_like(a) else [a]) if v is not None
         ]) for s in self.raw_series
     ])
Example #3
0
 def add(self, title, values, **kwargs):
     """Add a serie to this graph, compat api"""
     if not is_list_like(values) and not isinstance(values, dict):
         values = [values]
     kwargs['title'] = title
     self.raw_series.append((values, kwargs))
     return self
Example #4
0
 def add(self, title, values, **kwargs):
     """Add a serie to this graph, compat api"""
     if not is_list_like(values) and not isinstance(values, dict):
         values = [values]
     kwargs['title'] = title
     self.raw_series.append((values, kwargs))
     return self
Example #5
0
 def add(self, title, values, secondary=False):
     """Add a serie to this graph"""
     if not is_list_like(values) and not isinstance(values, dict):
         values = [values]
     if secondary:
         self.raw_series2.append((title, values))
     else:
         self.raw_series.append((title, values))
Example #6
0
 def add(self, title, values, secondary=False):
     """Add a serie to this graph"""
     if not is_list_like(values) and not isinstance(values, dict):
         values = [values]
     if secondary:
         self.raw_series2.append((title, values))
     else:
         self.raw_series.append((title, values))
Example #7
0
 def format_maybe_quartile(x):
     if is_list_like(x):
         if self.mode == "extremes":
             return 'Min: %s Q1: %s Q2: %s Q3: %s Max: %s' % tuple(map(sup, x))
         else:
             return 'Q1: %s Q2: %s Q3: %s' % tuple(map(sup, x[1:4]))
     else:
         return sup(x)
Example #8
0
 def add(self, title, values, **kwargs):
     """Add a serie to this graph"""
     if not is_list_like(values) and not isinstance(values, dict):
         values = [values]
     if kwargs.get('secondary', False):
         self.raw_series2.append((title, values, kwargs))
     else:
         self.raw_series.append((title, values, kwargs))
Example #9
0
 def add(self, title, values, **kwargs):
     """Add a serie to this graph"""
     if not is_list_like(values) and not isinstance(values, dict):
         values = [values]
     if kwargs.get('secondary', False):
         self.raw_series2.append((title, values, kwargs))
     else:
         self.raw_series.append((title, values, kwargs))
Example #10
0
 def format_maybe_quartile(x):
     if is_list_like(x):
         if self.mode == "extremes":
             return 'Min: %s Q1: %s Q2: %s Q3: %s Max: %s' % tuple(
                 map(sup, x))
         else:
             return 'Q1: %s Q2: %s Q3: %s' % tuple(map(sup, x[1:4]))
     else:
         return sup(x)
Example #11
0
def humanize(number):
    """Format a number to engineer scale"""
    if is_list_like(number):
        return ", ".join(map(humanize, number))
    order = number and int(floor(log(abs(number)) / log(1000)))
    human_readable = ORDERS.split(" ")[int(order > 0)]
    if order == 0 or order > len(human_readable):
        return float_format(number / (1000 ** int(order)))
    return float_format(number / (1000 ** int(order))) + human_readable[int(order) - int(order > 0)]
Example #12
0
 def add(self, title, values, secondary=False, lineConfig=None):
     """Add a serie to this graph"""
     if not is_list_like(values) and not isinstance(values, dict):
         values = [values]
     if lineConfig is None:
         lineConfig = LineConfig()
     if secondary:
         self.raw_series2.append((title, values, lineConfig))
     else:
         self.raw_series.append((title, values, lineConfig))
Example #13
0
def humanize(number):
    """Format a number to engineer scale"""
    if is_list_like(number):
        return', '.join(map(humanize, number))
    order = number and int(floor(log(abs(number)) / log(1000)))
    human_readable = ORDERS.split(" ")[int(order > 0)]
    if order == 0 or order > len(human_readable):
        return float_format(number / (1000 ** int(order)))
    return (
        float_format(number / (1000 ** int(order))) +
        human_readable[int(order) - int(order > 0)])
Example #14
0
 def format_maybe_quartile(x):
     if is_list_like(x):
         if self.box_mode == "extremes":
             return 'Min: %s Q1: %s Q2: %s Q3: %s Max: %s' \
                 % tuple(map(sup, x[1:6]))
         elif self.box_mode in ["tukey", "stdev", "pstdev"]:
             return 'Min: %s Lower Whisker: %s Q1: %s Q2: %s Q3: %s '\
                 'Upper Whisker: %s Max: %s' % tuple(map(sup, x))
         elif self.box_mode == '1.5IQR':
             # 1.5IQR mode
             return 'Q1: %s Q2: %s Q3: %s' % tuple(map(sup, x[2:5]))
     else:
         return sup(x)
Example #15
0
 def format_maybe_quartile(x):
     if is_list_like(x):
         if self.box_mode == "extremes":
             return ('Min: %s\nQ1 : %s\nQ2 : %s\nQ3 : %s\nMax: %s' %
                     tuple(map(sup, x[1:6])))
         elif self.box_mode in ["tukey", "stdev", "pstdev"]:
             return (
                 'Min: %s\nLower Whisker: %s\nQ1: %s\nQ2: %s\nQ3: %s\n'
                 'Upper Whisker: %s\nMax: %s' % tuple(map(sup, x)))
         elif self.box_mode == '1.5IQR':
             # 1.5IQR mode
             return 'Q1: %s\nQ2: %s\nQ3: %s' % tuple(map(sup, x[2:5]))
     else:
         return sup(x)
Example #16
0
    def prepare_values(self, raw, offset=0):
        """Prepare the values to start with sane values"""
        from pygal.graph.map import BaseMap
        from pygal import Histogram

        if self.zero == 0 and isinstance(self, BaseMap):
            self.zero = 1

        for key in ('x_labels', 'y_labels'):
            if getattr(self, key):
                setattr(self, key, list(getattr(self, key)))
        if not raw:
            return

        adapters = list(self._adapters) or [lambda x:x]
        if self.logarithmic:
            for fun in not_zero, positive:
                if fun in adapters:
                    adapters.remove(fun)
            adapters = adapters + [positive, not_zero]
        adapters = adapters + [decimal_to_float]
        adapter = reduce(compose, adapters) if not self.strict else ident
        x_adapter = reduce(
            compose, self._x_adapters) if getattr(
                self, '_x_adapters', None) else None
        series = []

        raw = [(
            title,
            list(raw_values) if not isinstance(
                raw_values, dict) else raw_values,
            serie_config_kwargs
        ) for title, raw_values, serie_config_kwargs in raw]

        width = max([len(values) for _, values, _ in raw] +
                    [len(self.x_labels or [])])

        for title, raw_values, serie_config_kwargs in raw:
            metadata = {}
            values = []
            if isinstance(raw_values, dict):
                if isinstance(self, BaseMap):
                    raw_values = list(raw_values.items())
                else:
                    value_list = [None] * width
                    for k, v in raw_values.items():
                        if k in self.x_labels:
                            value_list[self.x_labels.index(k)] = v
                    raw_values = value_list

            for index, raw_value in enumerate(
                    raw_values + (
                        (width - len(raw_values)) * [None]  # aligning values
                        if len(raw_values) < width else [])):
                if isinstance(raw_value, dict):
                    raw_value = dict(raw_value)
                    value = raw_value.pop('value', None)
                    metadata[index] = raw_value
                else:
                    value = raw_value

                # Fix this by doing this in charts class methods
                if isinstance(self, Histogram):
                    if value is None:
                        value = (None, None, None)
                    elif not is_list_like(value):
                        value = (value, self.zero, self.zero)
                    value = list(map(adapter, value))
                elif self._dual:
                    if value is None:
                        value = (None, None)
                    elif not is_list_like(value):
                        value = (value, self.zero)
                    if x_adapter:
                        value = (x_adapter(value[0]), adapter(value[1]))
                    if isinstance(self, BaseMap):
                        value = (adapter(value[0]), value[1])
                    else:
                        value = list(map(adapter, value))
                else:
                    value = adapter(value)

                values.append(value)
            serie_config = SerieConfig()
            serie_config(**dict((k, v) for k, v in self.state.__dict__.items()
                              if k in dir(serie_config)))
            serie_config(**serie_config_kwargs)
            series.append(
                Serie(offset + len(series),
                      title, values, serie_config, metadata))
        return series
Example #17
0
File: util.py Project: Bouska/pygal
def prepare_values(raw, config, cls):
    """Prepare the values to start with sane values"""
    from pygal.graph.datey import DateY
    from pygal.graph.histogram import Histogram
    from pygal.graph.worldmap import Worldmap
    if config.x_labels is None and hasattr(cls, 'x_labels'):
        config.x_labels = cls.x_labels
    if config.zero == 0 and issubclass(cls, Worldmap):
        config.zero = 1

    for key in ('x_labels', 'y_labels'):
        if getattr(config, key):
            setattr(config, key, list(getattr(config, key)))
    if not raw:
        return

    adapters = list(cls._adapters) or [lambda x:x]
    if config.logarithmic:
        for fun in not_zero, positive:
            if fun in adapters:
                adapters.remove(fun)
        adapters = adapters + [positive, not_zero]
    adapter = reduce(compose, adapters) if not config.strict else ident
    series = []

    raw = [(
        title,
        list(raw_values) if not isinstance(raw_values, dict) else raw_values
    ) for title, raw_values in raw]

    width = max([len(values) for _, values in raw] +
                [len(config.x_labels or [])])

    for title, raw_values in raw:
        metadata = {}
        values = []
        if isinstance(raw_values, dict):
            if issubclass(cls, Worldmap):
                raw_values = list(raw_values.items())
            else:
                value_list = [None] * width
                for k, v in raw_values.items():
                    if k in config.x_labels:
                        value_list[config.x_labels.index(k)] = v
                raw_values = value_list

        for index, raw_value in enumerate(
                raw_values + (
                    (width - len(raw_values)) * [None]  # aligning values
                    if len(raw_values) < width else [])):
            if isinstance(raw_value, dict):
                raw_value = dict(raw_value)
                value = raw_value.pop('value', None)
                metadata[index] = raw_value
            else:
                value = raw_value

            # Fix this by doing this in charts class methods
            if issubclass(cls, Histogram):
                if value is None:
                    value = (None, None, None)
                elif not is_list_like(value):
                    value = (value, config.zero, config.zero)
                value = list(map(adapter, value))
            elif cls._dual:
                if value is None:
                    value = (None, None)
                elif not is_list_like(value):
                    value = (value, config.zero)
                if issubclass(cls, DateY) or issubclass(cls, Worldmap):
                    value = (adapter(value[0]), value[1])
                else:
                    value = list(map(adapter, value))
            else:
                value = adapter(value)
            values.append(value)
        series.append(Serie(title, values, metadata))
    return series
Example #18
0
    def prepare_values(self, raw, offset=0):
        """Prepare the values to start with sane values"""
        from pygal import Histogram
        from pygal.graph.map import BaseMap

        if self.zero == 0 and isinstance(self, BaseMap):
            self.zero = 1

        if self.x_label_rotation:
            self.x_label_rotation %= 360

        if self.y_label_rotation:
            self.y_label_rotation %= 360

        for key in ('x_labels', 'y_labels'):
            if getattr(self, key):
                setattr(self, key, list(getattr(self, key)))
        if not raw:
            return

        adapters = list(self._adapters) or [lambda x: x]
        if self.logarithmic:
            for fun in not_zero, positive:
                if fun in adapters:
                    adapters.remove(fun)
            adapters = adapters + [positive, not_zero]
        adapters = adapters + [decimal_to_float]

        self._adapt = reduce(compose, adapters) if not self.strict else ident
        self._x_adapt = reduce(
            compose, self._x_adapters
        ) if not self.strict and getattr(self, '_x_adapters', None) else ident

        series = []

        raw = [(
            list(raw_values) if not isinstance(raw_values, dict) else
            raw_values, serie_config_kwargs
        ) for raw_values, serie_config_kwargs in raw]

        width = max([len(values)
                     for values, _ in raw] + [len(self.x_labels or [])])

        for raw_values, serie_config_kwargs in raw:
            metadata = {}
            values = []
            if isinstance(raw_values, dict):
                if isinstance(self, BaseMap):
                    raw_values = list(raw_values.items())
                else:
                    value_list = [None] * width
                    for k, v in raw_values.items():
                        if k in (self.x_labels or []):
                            value_list[self.x_labels.index(k)] = v
                    raw_values = value_list

            for index, raw_value in enumerate(raw_values + (
                (width - len(raw_values)) * [None]  # aligning values
                    if len(raw_values) < width else [])):
                if isinstance(raw_value, dict):
                    raw_value = dict(raw_value)
                    value = raw_value.pop('value', None)
                    metadata[index] = raw_value
                else:
                    value = raw_value

                # Fix this by doing this in charts class methods
                if isinstance(self, Histogram):
                    if value is None:
                        value = (None, None, None)
                    elif not is_list_like(value):
                        value = (value, self.zero, self.zero)
                    elif len(value) == 2:
                        value = (1, value[0], value[1])
                    value = list(map(self._adapt, value))
                elif self._dual:
                    if value is None:
                        value = (None, None)
                    elif not is_list_like(value):
                        value = (value, self.zero)
                    if self._x_adapt:
                        value = (
                            self._x_adapt(value[0]), self._adapt(value[1])
                        )
                    if isinstance(self, BaseMap):
                        value = (self._adapt(value[0]), value[1])
                    else:
                        value = list(map(self._adapt, value))
                else:
                    value = self._adapt(value)

                values.append(value)
            serie_config = SerieConfig()
            serie_config(
                **dict((k, v) for k, v in self.state.__dict__.items()
                       if k in dir(serie_config))
            )
            serie_config(**serie_config_kwargs)
            series.append(
                Serie(offset + len(series), values, serie_config, metadata)
            )
        return series
Example #19
0
def prepare_values(raw, config, cls):
    """Prepare the values to start with sane values"""
    from pygal.graph.datey import DateY
    from pygal.graph.histogram import Histogram
    from pygal.graph.worldmap import Worldmap
    from pygal.graph.frenchmap import FrenchMapDepartments
    if config.x_labels is None and hasattr(cls, 'x_labels'):
        config.x_labels = cls.x_labels
    if config.zero == 0 and issubclass(cls, (Worldmap, FrenchMapDepartments)):
        config.zero = 1

    for key in ('x_labels', 'y_labels'):
        if getattr(config, key):
            setattr(config, key, list(getattr(config, key)))
    if not raw:
        return

    adapters = list(cls._adapters) or [lambda x: x]
    if config.logarithmic:
        for fun in not_zero, positive:
            if fun in adapters:
                adapters.remove(fun)
        adapters = adapters + [positive, not_zero]
    adapter = reduce(compose, adapters) if not config.strict else ident
    series = []

    raw = [
        (title,
         list(raw_values) if not isinstance(raw_values, dict) else raw_values)
        for title, raw_values in raw
    ]

    width = max([len(values)
                 for _, values in raw] + [len(config.x_labels or [])])

    for title, raw_values in raw:
        metadata = {}
        values = []
        if isinstance(raw_values, dict):
            if issubclass(cls, (Worldmap, FrenchMapDepartments)):
                raw_values = list(raw_values.items())
            else:
                value_list = [None] * width
                for k, v in raw_values.items():
                    if k in config.x_labels:
                        value_list[config.x_labels.index(k)] = v
                raw_values = value_list

        for index, raw_value in enumerate(raw_values + (
            (width - len(raw_values)) * [None]  # aligning values
                if len(raw_values) < width else [])):
            if isinstance(raw_value, dict):
                raw_value = dict(raw_value)
                value = raw_value.pop('value', None)
                metadata[index] = raw_value
            else:
                value = raw_value

            # Fix this by doing this in charts class methods
            if issubclass(cls, Histogram):
                if value is None:
                    value = (None, None, None)
                elif not is_list_like(value):
                    value = (value, config.zero, config.zero)
                value = list(map(adapter, value))
            elif cls._dual:
                if value is None:
                    value = (None, None)
                elif not is_list_like(value):
                    value = (value, config.zero)
                if issubclass(cls, DateY) or issubclass(
                        cls, (Worldmap, FrenchMapDepartments)):
                    value = (adapter(value[0]), value[1])
                else:
                    value = list(map(adapter, value))
            else:
                value = adapter(value)
            values.append(value)
        series.append(Serie(title, values, metadata))
    return series
Example #20
0
 def format_maybe_quartile(x):
     if is_list_like(x):
         return 'Q1: %s Q2: %s Q3: %s' % tuple(map(sup, x[1:4]))
     else:
         return sup(x)
Example #21
0
 def format_maybe_quartile(x):
     if is_list_like(x):
         if len(x) == 5:
             return 'Q1: %s Q2: %s Q3: %s' % tuple(map(sup, x[1:4]))
     else:
         return sup(x)