def make_bump_chart(self): self.plot_df['year'] = pd.to_datetime( self.plot_df['year'].astype(str) + '-01-01') brush = alt.selection(type='interval', encodings=['x']) a = alt.Chart(self.plot_df).mark_line( point=True, interpolate='monotone').encode( x='year:T', y='rank:Q', color=alt.Color('Topic:N', sort='descending'), tooltip=['year:T', 'rank:Q', 'Topic:N', 'rate']).properties( height=400, width=800).transform_filter(brush).interactive() b = alt.Chart(self.plot_df).mark_area().encode( y='sum(rate):Q', x='year:T', ).properties(width=800, height=100).add_selection(brush) if self.add_data_names != ['All']: add_charts = [ self.make_add_data_chart(i, brush) for i in self.add_data_names ] chart = alt.vconcat(a, *add_charts, b, padding=0, spacing=0).configure_view( stroke=None).configure_axis(grid=False) return chart else: chart = alt.vconcat(a, b, padding=0, spacing=0).configure_view( stroke=None).configure_axis(grid=False) return chart
def make_ridge_chart(self): self.plot_df['year'] = pd.to_datetime(self.plot_df['year'].astype(str) + '-01-01') brush = alt.selection(type='interval', encodings=['x']) step = 18 overlap = 4 a = alt.Chart(self.plot_df).mark_area(stroke='black', strokeWidth=0, fillOpacity=0.6).encode( x=alt.X('year:T'), y=alt.Y('rate:Q', scale=alt.Scale(range=[0, -overlap * (step + 1)]), axis=None), row=alt.Row('Topic:N', header=alt.Header(title=None, labelPadding=0, labelFontSize=0)), color='Topic:N' ).properties( width=800, height=step, bounds='flush', ).transform_filter( brush ) b = alt.Chart(self.plot_df).mark_area().encode( y='sum(rate):Q', x='year:T', ).properties( width=800, height=100 ).add_selection( brush ) if self.add_data_names != ['All']: add_charts = [self.make_add_data_chart(i, brush) for i in self.add_data_names] chart = alt.vconcat( a, *add_charts, b, padding=0, spacing=0 ).configure_view( stroke=None ).configure_axis( grid=False ) return chart else: chart = alt.vconcat( a, b, padding=0, spacing=0 ).configure_view( stroke=None ).configure_axis( grid=False ) return chart
def test_selection_to_dict(): brush = alt.selection(type='interval') # test some value selections # Note: X and Y cannot have conditions alt.Chart('path/to/data.json').mark_point().encode( color=alt.condition(brush, alt.ColorValue('red'), alt.ColorValue('blue')), opacity=alt.condition(brush, alt.value(0.5), alt.value(1.0)), text=alt.condition(brush, alt.TextValue('foo'), alt.value('bar'))).to_dict() # test some field selections # Note: X and Y cannot have conditions # Conditions cannot both be fields alt.Chart('path/to/data.json').mark_point().encode( color=alt.condition(brush, alt.Color('col1:N'), alt.value('blue')), opacity=alt.condition(brush, 'col1:N', alt.value(0.5)), text=alt.condition(brush, alt.value('abc'), alt.Text('col2:N')), size=alt.condition(brush, alt.value(20), 'col2:N')).to_dict()
def test_selection_to_dict(): brush = alt.selection(type="interval") # test some value selections # Note: X and Y cannot have conditions alt.Chart("path/to/data.json").mark_point().encode( color=alt.condition(brush, alt.ColorValue("red"), alt.ColorValue("blue")), opacity=alt.condition(brush, alt.value(0.5), alt.value(1.0)), text=alt.condition(brush, alt.TextValue("foo"), alt.value("bar")), ).to_dict() # test some field selections # Note: X and Y cannot have conditions # Conditions cannot both be fields alt.Chart("path/to/data.json").mark_point().encode( color=alt.condition(brush, alt.Color("col1:N"), alt.value("blue")), opacity=alt.condition(brush, "col1:N", alt.value(0.5)), text=alt.condition(brush, alt.value("abc"), alt.Text("col2:N")), size=alt.condition(brush, alt.value(20), "col2:N"), ).to_dict()