def fill_opacity(value): """ValueRef : int or float, opacity of the fill (0 to 1) """ if value.value: _assert_is_type('fill_opacity.value', value.value, (float, int)) if value.value < 0 or value.value > 1: raise ValueError('fill_opacity must be between 0 and 1')
def stroke_width(value): """ValueRef : int, width of the stroke in pixels """ if value.value: _assert_is_type('stroke_width.value', value.value, int) if value.value < 0: raise ValueError('stroke width cannot be negative')
def values(value): """list : Data contents Data is represented in tabular form, where each element of ``values`` corresponds to a row of data. Each row of data is represented by a dict or a raw number. The keys of the dict are columns and the values are individual data points. The keys of the dicts must be strings for the data to correctly serialize to JSON. The data will often have an "index" column representing the independent variable, with the remaining columns representing the dependent variables, though this is not required. The ``Data`` class itself, however, is agnostic to which columns are dependent and independent. For example, the values attribute ``[{'x': 0, 'y': 3.2}, {'x': 1, 'y': 1.3}]`` could represent two rows of two variables - possibly an independent variable ``'x'`` and a dependent variable ``'y'``. For simple data sets, an alternative values attribute could be a simple list of numbers such as ``[2, 12, 3, 5]``. It may be more convenient to load data from pandas or NumPy objects. See the methods :func:`Data.from_pandas` and :func:`Data.from_numpy`. """ for row in value: _assert_is_type('values row', row, (float, int, dict))
def stroke_opacity(value): """ValueRef : number, opacity of the stroke (0 to 1) """ if value.value: _assert_is_type('stroke_opacity.value', value.value, (float, int)) if value.value < 0 or value.value > 1: raise ValueError('stroke_opacity must be between 0 and 1')
def axes(value): """list or KeyedList of ``Axis`` : Axis definitions Axes define the locations of the data being mapped by the scales. See the :class:`Axis` class for details. """ for i, entry in enumerate(value): _assert_is_type('axes[{0}]'.format(i), entry, Axis)
def data(value): """list or KeyedList of ``Data`` : Data definitions This defines the data being visualized. See the :class:`Data` class for details. """ for i, entry in enumerate(value): _assert_is_type('data[{0}]'.format(i), entry, Data)
def path(value): """ValueRef : string, SVG path string This would typically be used for maps and other things where the path is taken from the data. """ if value.value: _assert_is_type('path.value', value.value, str)
def legends(value): """list or KeyedList of ``Legends`` : Legend definitions Legends visualize scales, and take one or more scales as their input. They can be customized via a LegendProperty object. """ for i, entry in enumerate(value): _assert_is_type('legends[{0}]'.format(i), entry, Legend)
def stroke(value): """ValueRef : color, stroke color for the mark Colors can be specified in standard HTML hex notation or as CSS3 compatible strings. The color string is not validated due to its large number of valid values. """ if value.value: _assert_is_type('stroke.value', value.value, str)
def fill_opacity(value): """ValueRef : int or float, opacity of the fill (0 to 1) """ if value.value: _assert_is_type('fill_opacity.value', value.value, (float, int)) if value.value < 0 or value.value > 1: raise ValueError( 'fill_opacity must be between 0 and 1')
def scales(value): """list or KeyedList of ``Scale`` : Scale definitions Scales map the data from the domain of the data to some visualization space (such as an x-axis). See the :class:`Scale` class for details. """ for i, entry in enumerate(value): _assert_is_type('scales[{0}]'.format(i), entry, Scale)
def stroke_opacity(value): """ValueRef : number, opacity of the stroke (0 to 1) """ if value.value: _assert_is_type('stroke_opacity.value', value.value, (float, int)) if value.value < 0 or value.value > 1: raise ValueError( 'stroke_opacity must be between 0 and 1')
def marks(value): """list or KeyedList of ``Mark`` : Mark definitions Marks are the visual objects (such as lines, bars, etc.) that represent the data in the visualization space. See the :class:`Mark` class for details. """ for i, entry in enumerate(value): _assert_is_type('marks[{0}]'.format(i), entry, Mark)
def from_numpy(cls, np_obj, name, columns, index=None, index_key=None, **kwargs): """Load values from a numpy array Parameters ---------- np_obj : numpy.ndarray numpy array to load data from name : string ``name`` field for the data columns : iterable Sequence of column names, from left to right. Must have same length as the number of columns of ``np_obj``. index : iterable, default None Sequence of indices from top to bottom. If ``None`` (default), then the indices are integers starting at 0. Must have same length as the number of rows of ``np_obj``. index_key : string, default None Key to use for the index. If ``None`` (default), ``idx`` is used. **kwargs : dict Additional arguments passed to the :class:`Data` constructor Notes ----- The individual elements of ``np_obj``, ``columns``, and ``index`` must return valid values from :func:`Data.serialize`. """ if not np: raise LoadError('numpy could not be imported') _assert_is_type('numpy object', np_obj, np.ndarray) # Integer index if none is provided index = index or range(np_obj.shape[0]) # Explicitly map dict-keys to strings for JSON serializer. columns = map(str, columns) index_key = index_key or cls._default_index_key if len(index) != np_obj.shape[0]: raise LoadError( 'length of index must be equal to number of rows of array') elif len(columns) != np_obj.shape[1]: raise LoadError( 'length of columns must be equal to number of columns of ' 'array') data = cls(name=name, **kwargs) data.values = [ dict([(index_key, cls.serialize(idx))] + [(col, x) for col, x in zip(columns, row)]) for idx, row in zip(index, np_obj.tolist())] return data
def shape(value): """ValueRef : string, type of symbol to use Possible values are ``'circle'`` (default), ``'square'``, ``'cross'``, ``'diamond'``, ``'triangle-up'``, and ``'triangle-down'``. Only used if ``type`` is ``'symbol'``. """ if value.value: _assert_is_type('shape.value', value.value, str) if value.value not in PropertySet._valid_shapes: raise ValueError(value.value + ' is not a valid shape')
def size(value): """ValueRef : number, area of the mark in pixels This is the total area of a symbol. For example, a value of 500 and a ``shape`` of ``'circle'`` would result in circles with an area of 500 square pixels. Only used if ``type`` is ``'symbol'``. """ if value.value: _assert_is_type('size.value', value.value, int) if value.value < 0: raise ValueError('size cannot be negative')
def viewport(value): """2-element list of ints : Dimensions of the viewport The viewport is a bounding box containing the visualization. If the dimensions of the visualization are larger than the viewport, then the visualization will be scrollable. If undefined, then the full visualization is shown. """ if len(value) != 2: raise ValueError('viewport must have 2 dimensions') for v in value: _assert_is_type('viewport dimension', v, int) if v < 0: raise ValueError('viewport dimensions cannot be negative')
def padding(value): """int or dict : Padding around visualization The padding defines the distance between the edge of the visualization canvas to the visualization box. It does not count as part of the visualization width/height. Values cannot be negative. If a dict, padding must have all keys ``''top'``, ``'left'``, ``'right'``, and ``'bottom'`` with int values. """ if isinstance(value, dict): required_keys = ['top', 'left', 'right', 'bottom'] for key in required_keys: if key not in value: error = ('Padding must have keys "{0}".' .format('", "'.join(required_keys))) raise ValueError(error) _assert_is_type('padding: {0}'.format(key), value[key], int) if value[key] < 0: raise ValueError('Padding cannot be negative.') else: if value < 0: raise ValueError('Padding cannot be negative.')
def padding(value): """int or dict : Padding around visualization The padding defines the distance between the edge of the visualization canvas to the visualization box. It does not count as part of the visualization width/height. Values cannot be negative. If a dict, padding must have all keys ``''top'``, ``'left'``, ``'right'``, and ``'bottom'`` with int values. """ if isinstance(value, dict): required_keys = ['top', 'left', 'right', 'bottom'] for key in required_keys: if key not in value: error = ('Padding must have keys "{0}".'.format( '", "'.join(required_keys))) raise ValueError(error) _assert_is_type('padding: {0}'.format(key), value[key], int) if value[key] < 0: raise ValueError('Padding cannot be negative.') else: if value < 0: raise ValueError('Padding cannot be negative.')
def from_numpy(cls, np_obj, name, columns, index=None, index_key=None, **kwargs): """Load values from a numpy array Parameters ---------- np_obj : numpy.ndarray numpy array to load data from name : string ``name`` field for the data columns : iterable Sequence of column names, from left to right. Must have same length as the number of columns of ``np_obj``. index : iterable, default None Sequence of indices from top to bottom. If ``None`` (default), then the indices are integers starting at 0. Must have same length as the number of rows of ``np_obj``. index_key : string, default None Key to use for the index. If ``None`` (default), ``idx`` is used. **kwargs : dict Additional arguments passed to the :class:`Data` constructor Notes ----- The individual elements of ``np_obj``, ``columns``, and ``index`` must return valid values from :func:`Data.serialize`. """ if not np: raise LoadError('numpy could not be imported') _assert_is_type('numpy object', np_obj, np.ndarray) # Integer index if none is provided index = index or range(np_obj.shape[0]) # Explicitly map dict-keys to strings for JSON serializer. columns = map(str, columns) index_key = index_key or cls._default_index_key if len(index) != np_obj.shape[0]: raise LoadError( 'length of index must be equal to number of rows of array') elif len(columns) != np_obj.shape[1]: raise LoadError( 'length of columns must be equal to number of columns of ' 'array') data = cls(name=name, **kwargs) data.values = [ dict([(index_key, cls.serialize(idx))] + [(col, x) for col, x in zip(columns, row)]) for idx, row in zip(index, np_obj.tolist()) ] return data