Beispiel #1
0
    def __init__(self, data, type, width=0, height=0, **kwargs):
        """Construct a chart object.

        Parameters
        ----------
        data : pandas.DataFrame, numpy.ndarray, Iterable, or dict
            Data to be plotted. Series are referenced by column name.

        type : str
            A string with the snake-case chart type. Example: 'area_chart',
            'bar_chart', etc...

        width : int
            The chart's width. Defaults to 0, which means "the default width"
            rather than actually 0px.

        height : int
            The chart's height. Defaults to 0, which means "the default height"
            rather than actually 0px.

        kwargs : anything
            Keyword arguments containing properties to be added to the
            ReChart's top-level element.

        """
        import pandas as pd

        assert type in chart_config.CHART_TYPES_SNAKE, (
            'Did not recognize "%s" type.' % type)
        self._data = data_frame_proto.convert_anything_to_df(data)
        self._type = type
        self._width = width
        self._height = height
        self._components = list()
        self._props = [(str(k), str(v)) for (k, v) in kwargs.items()]
Beispiel #2
0
 def test_styler(self):
     """Test that a DataFrame can be constructed from a pandas.Styler"""
     d = {"a": [1], "b": [2], "c": [3]}
     styler = pd.DataFrame(d).style.format("{:.2%}")
     df = convert_anything_to_df(styler)
     self.assertEqual(type(df), pd.DataFrame)
     self.assertEqual(df.shape, (1, 3))
Beispiel #3
0
def generate_chart(chart_type, data):
    if data is None:
        # Use an empty-ish dict because if we use None the x axis labels rotate
        # 90 degrees. No idea why. Need to debug.
        data = {"": []}

    if not isinstance(data, pd.DataFrame):
        data = convert_anything_to_df(data)

    n_cols = len(data.columns)
    data = pd.melt(data.reset_index(), id_vars=["index"])

    if chart_type == "area":
        opacity = {"value": 0.7}
    else:
        opacity = {"value": 1.0}

    chart = (
        getattr(alt.Chart(data), "mark_" + chart_type)()
        .encode(
            alt.X("index", title=""),
            alt.Y("value", title=""),
            alt.Color("variable", title="", type="nominal"),
            alt.Tooltip(["index", "value", "variable"]),
            opacity=opacity,
        )
        .interactive()
    )
    return chart
Beispiel #4
0
 def test_empty_numpy_array(self):
     """Test that a single-column empty DataFrame can be constructed
     from an empty numpy array.
     """
     arr = np.array([])
     df = convert_anything_to_df(arr)
     self.assertEqual(type(df), pd.DataFrame)
     self.assertEqual(df.shape, (0, 1))
Beispiel #5
0
 def test_dict_of_lists(self):
     """Test that a DataFrame can be constructed from a dict
     of equal-length lists
     """
     d = {"a": [1], "b": [2], "c": [3]}
     df = convert_anything_to_df(d)
     self.assertEqual(type(df), pd.DataFrame)
     self.assertEqual(df.shape, (1, 3))
Beispiel #6
0
def generate_chart(chart_type, data):
    if data is None:
        # Use an empty-ish dict because if we use None the x axis labels rotate
        # 90 degrees. No idea why. Need to debug.
        data = {"": []}

    if not isinstance(data, pd.DataFrame):
        data = convert_anything_to_df(data)

    n_cols = len(data.columns)
    data = pd.melt(data.reset_index(), id_vars=['index'])

    if chart_type == 'area':
        opacity = {'value': 0.7}
    else:
        opacity = {'value': 1.0}

    chart = getattr(alt.Chart(data), 'mark_' + chart_type)().encode(
        alt.X('index', title=''),
        alt.Y('value', title=''),
        alt.Color('variable', title='', type='nominal'),
        alt.Tooltip(['index', 'value', 'variable']),
        opacity=opacity).interactive()
    return chart