highcharts-for-python / highcharts-core

Python wrapper for the Highcharts Core JavaScript library
https://www.highcharts.com/integrations/python
Other
58 stars 13 forks source link

`CartesianData.from_json()` raises error when deserializing from `CartesianDataCollection.to_json()` #169

Closed hcpchris closed 6 months ago

hcpchris commented 6 months ago

Describe the bug Generate a simple chart using a CartesianDataCollection, serialize the chart using Chart.to_json(), and then attempt to round-trip it using Chart.from_json(). The code below shows a simple example:

    from highcharts_core import highcharts
    import pandas as pd
    import numpy as np

    dates = [
        "30/04/2024",
        "30/04/2024",
        "26/04/2024",
        "12/04/2024",
        "31/03/2024",
        "31/03/2024",
        "29/03/2024",
        "15/03/2024",
        "05/03/2024",
    ]
    amounts = 100 + np.random.normal(loc=100, scale=10, size=len(dates))

    df = pd.DataFrame([{"date": x, "amount": y} for x, y in zip(dates, amounts)])
    df["date"] = pd.to_datetime(df["date"], format="%d/%m/%Y")
    df["milliseconds"] = (df["date"] - pd.Timestamp("1970-01-01")) // pd.Timedelta("1ms")

    my_chart = highcharts.Chart.from_pandas(
        df, property_map={"x": "milliseconds", "y": "amount"}, series_type="line"
    )

    as_json = my_chart.to_json()

    my_new_chart = highcharts.Chart.from_json(as_json)

Expected behavior Should round-trip into a chart that renders the same, even if internally it deserializes CartesianDataCollection into a series of CartesianData instances.