.collections


class: DataPointCollection

class DataPointCollection(**kwargs)[source]

Collection of data points.

This class stores numerical values that Highcharts can interpret from a primitive array in a numpy.ndarray (in the .ndarray property) and non-numerical data point properties as Highcharts for Python DataBase-descended objects (in the .data_points property).

Note

When serializing to JS literals, if possible, the collection is serialized to a primitive array to boost performance within Python and JavaScript. However, this may not always be possible if data points have non-array-compliant properties configured (e.g. adjusting their style, names, identifiers, etc.). If serializing to a primitive array is not possible, the results are serialized as JS literal objects.

Class Inheritance
Inheritance diagram of DataPointCollection

copy(other=None, overwrite=True, **kwargs)

Copy the configuration settings from this instance to the other instance.

Parameters:
  • other (HighchartsMeta) – The target instance to which the properties of this instance should be copied. If None, will create a new instance and populate it with properties copied from self. Defaults to None.

  • overwrite (bool) – if True, properties in other that are already set will be overwritten by their counterparts in self. Defaults to True.

  • kwargs – Additional keyword arguments. Some special descendents of HighchartsMeta may have special implementations of this method which rely on additional keyword arguments.

Returns:

A mutated version of other with new property values

classmethod from_array(value)[source]

Creates a DataPointCollection instance from an array of values.

Parameters:

value (iterable or numpy.ndarray) – The value that should contain the data which will be converted into data point instances.

Returns:

A single-object collection of data points.

Return type:

DataPointCollection or None

Raises:

HighchartsDependencyError – if NumPy is not installed

classmethod from_dict(as_dict: dict, allow_snake_case: bool = True)

Construct an instance of the class from a dict object.

Parameters:
  • as_dict (dict) – A dict representation of the object.

  • allow_snake_case (bool) – If True, interprets snake_case keys as equivalent to camelCase keys. Defaults to True.

Returns:

A Python object representation of as_dict.

Return type:

HighchartsMeta

classmethod from_js_literal(as_str_or_file, allow_snake_case: bool = True, _break_loop_on_failure: bool = False)

Return a Python object representation of a Highcharts JavaScript object literal.

Parameters:
  • as_str_or_file (str) – The JavaScript object literal, represented either as a str or as a filename which contains the JS object literal.

  • allow_snake_case (bool) – If True, interprets snake_case keys as equivalent to camelCase keys. Defaults to True.

  • _break_loop_on_failure (bool) – If True, will break any looping operations in the event of a failure. Otherwise, will attempt to repair the failure. Defaults to False.

Returns:

A Python object representation of the Highcharts JavaScript object literal.

Return type:

HighchartsMeta

classmethod from_json(as_json_or_file, allow_snake_case: bool = True)

Construct an instance of the class from a JSON string.

Parameters:
  • as_json_or_file – The JSON string for the object or the filename of a file that contains the JSON string.

  • allow_snake_case (bool) – If True, interprets snake_case keys as equivalent to camelCase keys. Defaults to True.

Returns:

A Python objcet representation of as_json.

Return type:

HighchartsMeta

classmethod from_ndarray(value)[source]

Creates a DataPointCollection instance from an array of values.

Parameters:

value (numpy.ndarray) – The value that should contain the data which will be converted into data point instances.

Returns:

A single-object collection of data points.

Return type:

DataPointCollection or None

Raises:

HighchartsDependencyError – if NumPy is not installed

get_required_modules(include_extension=False) List[str]

Return the list of URLs from which the Highcharts JavaScript modules needed to render the chart can be retrieved.

Parameters:

include_extension (bool) – if True, will return script names with the '.js' extension included. Defaults to False.

Return type:

list of str

to_array(force_object=False, force_ndarray=False) List[source]

Generate the array representation of the data points (the inversion of .from_array()).

Warning

If any data points cannot be serialized to a JavaScript array, this method will instead return the untrimmed dict representation of the data points as a fallback.

Parameters:
  • force_object (bool) –

    if True, forces the return of the instance’s untrimmed dict representation. Defaults to False.

    Warning

    Values in .ndarray are ignored within this operation in favor of data points stored in .data_points.

    However, if there are no data points in .data_points then data point objects will be assembled based on .ndarray.

  • force_ndarray (bool) –

    if True, forces the return of the instance’s data points as a numpy.ndarray. Defaults to False.

    Warning

    Properties of any .data_points are ignored within this operation if .ndarray is populated.

    However, if .ndarray is not populated, then a numpy.ndarray will be assembled from values in .data_points (ignoring properties that Highcharts (JS) cannot interpret as a primitive array).

Raises:

HighchartsValueError – if both force_object and force_ndarray are True

Returns:

The array representation of the data point collection.

Return type:

list

to_dict() dict

Generate a dict representation of the object compatible with the Highcharts JavaScript library.

Note

The dict representation has a property structure and naming convention that is intentionally consistent with the Highcharts JavaScript library. This is not Pythonic, but it makes managing the interplay between the two languages much, much simpler.

Returns:

A dict representation of the object.

Return type:

dict

to_js_literal(filename=None, encoding='utf-8', careful_validation=False) str | None[source]

Return the object represented as a str containing the JavaScript object literal.

Parameters:
  • filename (Path-like) – The name of a file to which the JavaScript object literal should be persisted. Defaults to None

  • encoding (str) – The character encoding to apply to the resulting object. Defaults to 'utf-8'.

  • careful_validation – if True, will carefully validate JavaScript values

along the way using the esprima-python library. Defaults to False.

Warning

Setting this value to True will significantly degrade serialization performance, though it may prove useful for debugging purposes.

Return type:

str or None

to_json(filename=None, encoding='utf-8')

Generate a JSON string/byte string representation of the object compatible with the Highcharts JavaScript library.

Note

This method will either return a standard str or a bytes object depending on the JSON serialization library you are using. For example, if your environment has orjson, the result will be a bytes representation of the string.

Parameters:
  • filename (Path-like) – The name of a file to which the JSON string should be persisted. Defaults to None

  • encoding (str) – The character encoding to apply to the resulting object. Defaults to 'utf-8'.

Returns:

A JSON representation of the object compatible with the Highcharts library.

Return type:

str or bytes

static trim_dict(untrimmed: dict, to_json: bool = False, context: str = None) dict

Remove keys from untrimmed whose values are None and convert values that have .to_dict() methods.

Parameters:
  • untrimmed (dict) – The dict whose values may still be None or Python objects.

  • to_json (bool) – If True, will remove all keys from untrimmed that are not serializable to JSON. Defaults to False.

  • context (str or None) – If provided, will inform the method of the context in which it is being run which may inform special handling cases (e.g. where empty strings may be important / allowable). Defaults to None.

Returns:

Trimmed dict

Return type:

dict

static trim_iterable(untrimmed, to_json=False, context: str = None)

Convert any EnforcedNullType values in untrimmed to 'null'.

Parameters:
  • untrimmed (iterable) – The iterable whose members may still be None or Python objects.

  • to_json (bool) – If True, will remove all members from untrimmed that are not serializable to JSON. Defaults to False.

  • context (str or None) – If provided, will inform the method of the context in which it is being run which may inform special handling cases (e.g. where empty strings may be important / allowable). Defaults to None.

Return type:

iterable

property array: List | None

Primitive collection of values for data points in the collection. Used if NumPy is not available. Defaults to None.

Note

If NumPy is availalbe, will instead behave as an alias for .ndarray

Return type:

list or None

property data_points: List[DataBase] | None

The collection of data points for the series. Defaults to None.

Return type:

list of DataBase or None

property ndarray

A dict whose keys correspond to data point properties, and whose values are numpy.ndarray instances that contain the data point collection’s values.

Return type:

dict or None

property ndarray_length: int

The length of the array stored in .ndarray.

Return type:

int

property requires_js_object: bool

Indicates whether or not the data point must be serialized to a JS literal object or whether it can be serialized to a primitive array.

Returns:

True if the data point must be serialized to a JS literal object. False if it can be serialized to an array.

Return type:

bool