Ejemplo n.º 1
0
def transform_chart(
    chart: alt.Chart, extract_encoding_transforms: bool = True
) -> alt.Chart:
    """Return a chart with the transformed data

    Parameters
    ----------
    chart : alt.Chart
        The chart instance from which the data and transform
        will be extracted.
    extract_encoding_transforms : bool
        If True (default), then also extract transforms from encodings.

    Returns
    -------
    chart_out : alt.Chart
        A copy of the input chart with the transformed data.

    Example
    -------
    >>> import pandas as pd
    >>> data = pd.DataFrame({'x': range(5), 'y': list('ABCAB')})
    >>> chart = alt.Chart(data).mark_bar().encode(x='sum(x)', y='y')
    >>> new_chart = transform_chart(chart)
    >>> new_chart.data
       y  sum_x
    0  A      3
    1  B      5
    2  C      2
    >>> new_chart.encoding
    FacetedEncoding({
      x: PositionFieldDef({
        field: FieldName('sum_x'),
        title: 'Sum of x',
        type: StandardType('quantitative')
      }),
      y: PositionFieldDef({
        field: FieldName('y'),
        type: StandardType('nominal')
      })
    })
    """
    if extract_encoding_transforms:
        chart = extract_transform(chart)
    chart = chart.properties(data=extract_data(chart, apply_encoding_transforms=False))
    chart.transform = alt.Undefined
    return chart
Ejemplo n.º 2
0
def transform_chart(chart: alt.Chart) -> alt.Chart:
    """Return a chart with the transformed data

    Parameters
    ----------
    chart : alt.Chart
        The chart instance from which the data and transform
        will be extracted.

    Returns
    -------
    chart_out : alt.Chart
        A copy of the input chart with the transformed data.
    """
    chart = chart.properties(data=extract_data(chart))
    chart.transform = alt.Undefined
    return chart