def encode_color(self): AltairChart.encode_color(self) self.add_text() # Setting tooltip as non-null self.chart = self.chart.configure_mark( tooltip=alt.TooltipContent("encoding")) self.code += f"""chart = chart.configure_mark(tooltip=alt.TooltipContent('encoding'))"""
def encode_color( self ): # override encode_color in AltairChart to enforce add_text occurs afterwards AltairChart.encode_color(self) self.add_text() self.chart = self.chart.configure_mark(tooltip=alt.TooltipContent( 'encoding')) # Setting tooltip as non-null self.code += f'''chart = chart.configure_mark(tooltip=alt.TooltipContent('encoding'))'''
def initialize_chart(self): x_attr = self.vis.get_attr_by_channel("x")[0] y_attr = self.vis.get_attr_by_channel("y")[0] x_attr_abv = str(x_attr.attribute) y_attr_abv = str(y_attr.attribute) background, background_str = self.get_background(x_attr_abv.lower()) geographical_name = self.get_geographical_name(x_attr_abv.lower()) geo_map, geo_map_str, map_type, map_translation = self.get_geomap( x_attr_abv.lower()) self.data[x_attr_abv] = self.data[x_attr_abv].apply(map_translation) if len(x_attr_abv) > 25: x_attr_abv = x_attr.attribute[:15] + "..." + x_attr.attribute[-10:] if len(y_attr_abv) > 25: y_attr_abv = y_attr.attribute[:15] + "..." + y_attr.attribute[-10:] if isinstance(x_attr.attribute, str): x_attr.attribute = x_attr.attribute.replace(".", "") if isinstance(y_attr.attribute, str): y_attr.attribute = y_attr.attribute.replace(".", "") self.data = AltairChart.sanitize_dataframe(self.data) points = (alt.Chart(geo_map).mark_geoshape().encode( color=f"{str(y_attr.attribute)}:Q", ).transform_lookup( lookup="id", from_=alt.LookupData(self.data, str(x_attr.attribute), [str(y_attr.attribute)]), ).project(type=map_type).properties( title=f"Mean of {y_attr_abv} across {geographical_name}")) chart = background + points ###################################### ## Constructing Altair Code String ## ##################################### self.code += "import altair as alt\n" dfname = "placeholder_variable" self.code += f"""nan=float('nan') df = pd.DataFrame({str(self.data.to_dict())}) background = {background_str} points = alt.Chart({geo_map_str}).mark_geoshape().encode( color='{str(y_attr.attribute)}:Q', ).transform_lookup( lookup='id', from_=alt.LookupData({dfname}, "{str(x_attr.attribute)}", ["{str(y_attr.attribute)}"]) ).project( type="{map_type}" ).properties( title="Mean of {y_attr_abv} across {geographical_name}" ) chart = background + points """ return chart
def initialize_chart(self): x_attr = self.vis.get_attr_by_channel("x")[0] y_attr = self.vis.get_attr_by_channel("y")[0] x_attr_abv = str(x_attr.attribute) y_attr_abv = str(y_attr.attribute) if len(x_attr_abv) > 25: x_attr_abv = x_attr.attribute[:15] + "..." + x_attr.attribute[-10:] if len(y_attr_abv) > 25: y_attr_abv = y_attr.attribute[:15] + "..." + y_attr.attribute[-10:] x_min = self.vis.min_max[x_attr.attribute][0] x_max = self.vis.min_max[x_attr.attribute][1] y_min = self.vis.min_max[y_attr.attribute][0] y_max = self.vis.min_max[y_attr.attribute][1] if isinstance(x_attr.attribute, str): x_attr.attribute = x_attr.attribute.replace(".", "") if isinstance(y_attr.attribute, str): y_attr.attribute = y_attr.attribute.replace(".", "") self.data = AltairChart.sanitize_dataframe(self.data) chart = (alt.Chart(self.data).mark_circle().encode( x=alt.X( str(x_attr.attribute), scale=alt.Scale(domain=(x_min, x_max)), type=x_attr.data_type, axis=alt.Axis(title=x_attr_abv), ), y=alt.Y( str(y_attr.attribute), scale=alt.Scale(domain=(y_min, y_max)), type=y_attr.data_type, axis=alt.Axis(title=y_attr_abv), ), )) # Setting tooltip as non-null chart = chart.configure_mark(tooltip=alt.TooltipContent("encoding")) chart = chart.interactive() # Enable Zooming and Panning ##################################### ## Constructing Altair Code String ## ##################################### self.code += "import altair as alt\n" dfname = "placeholder_variable" self.code += f""" chart = alt.Chart({dfname}).mark_circle().encode( x=alt.X('{x_attr.attribute}',scale=alt.Scale(domain=({x_min}, {x_max})),type='{x_attr.data_type}', axis=alt.Axis(title='{x_attr_abv}')), y=alt.Y('{y_attr.attribute}',scale=alt.Scale(domain=({y_min}, {y_max})),type='{y_attr.data_type}', axis=alt.Axis(title='{y_attr_abv}')) ) chart = chart.configure_mark(tooltip=alt.TooltipContent('encoding')) # Setting tooltip as non-null chart = chart.interactive() # Enable Zooming and Panning """ return chart
def initialize_chart(self): self.tooltip = False x_attr = self.vis.get_attr_by_channel("x")[0] y_attr = self.vis.get_attr_by_channel("y")[0] x_attr_abv = str(x_attr.attribute) y_attr_abv = str(y_attr.attribute) if len(x_attr_abv) > 25: x_attr_abv = x_attr.attribute[:15] + "..." + x_attr.attribute[-10:] if len(y_attr_abv) > 25: y_attr_abv = y_attr.attribute[:15] + "..." + y_attr.attribute[-10:] if isinstance(x_attr.attribute, str): x_attr.attribute = x_attr.attribute.replace(".", "") if isinstance(y_attr.attribute, str): y_attr.attribute = y_attr.attribute.replace(".", "") # To get datetime to display correctly on bar charts if x_attr.data_type == "temporal": x_attr.data_type = "nominal" self.code += "from pandas import Timestamp\n" if y_attr.data_type == "temporal": y_attr.data_type = "nominal" self.code += "from pandas import Timestamp\n" if x_attr.data_model == "measure": agg_title = get_agg_title(x_attr) measure_attr = x_attr.attribute y_attr_field = alt.Y( str(y_attr.attribute), type=y_attr.data_type, axis=alt.Axis(labelOverlap=True, title=y_attr_abv), ) x_attr_field = alt.X( str(x_attr.attribute), type=x_attr.data_type, title=agg_title, axis=alt.Axis(title=agg_title), ) y_attr_field_code = f"alt.Y('{y_attr.attribute}', type= '{y_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{y_attr_abv}'))" x_attr_field_code = f"alt.X('{x_attr.attribute}', type= '{x_attr.data_type}', title='{agg_title}', axis=alt.Axis(title='{agg_title}'))" if y_attr.sort == "ascending": y_attr_field.sort = "-x" y_attr_field_code = f"alt.Y('{y_attr.attribute}', type= '{y_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{y_attr_abv}'), sort ='-x')" else: agg_title = get_agg_title(y_attr) measure_attr = y_attr.attribute x_attr_field = alt.X( str(x_attr.attribute), type=x_attr.data_type, axis=alt.Axis(labelOverlap=True, title=x_attr_abv), ) x_attr_field_code = f"alt.X('{x_attr.attribute}', type= '{x_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{x_attr_abv}'))" y_attr_field = alt.Y( str(y_attr.attribute), type=y_attr.data_type, title=agg_title, axis=alt.Axis(title=agg_title), ) y_attr_field_code = f"alt.Y('{y_attr.attribute}', type= '{y_attr.data_type}', title='{agg_title}', axis=alt.Axis(title='{agg_title}'))" if x_attr.sort == "ascending": x_attr_field.sort = "-y" x_attr_field_code = f"alt.X('{x_attr.attribute}', type= '{x_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{x_attr_abv}'),sort='-y')" k = 10 self._topkcode = "" n_bars = len(self.data.iloc[:, 0].unique()) plotting_scale = lux.config.plotting_scale if n_bars > k: # Truncating to only top k remaining_bars = n_bars - k self.data = self.data.nlargest(k, columns=measure_attr) self.data = AltairChart.sanitize_dataframe(self.data) self.text = alt.Chart(self.data).mark_text( x=155 * plotting_scale, y=142 * plotting_scale, align="right", color="#ff8e04", fontSize=11, text=f"+ {remaining_bars} more ...", ) self._topkcode = f"""text = alt.Chart(visData).mark_text( x={155 * plotting_scale}, y={142 * plotting_scale}, align="right", color = "#ff8e04", fontSize = 11, text=f"+ {remaining_bars} more ..." ) chart = chart + text\n""" self.data = AltairChart.sanitize_dataframe(self.data) chart = alt.Chart(self.data).mark_bar().encode(y=y_attr_field, x=x_attr_field) # TODO: tooltip messes up the count() bar charts # Can not do interactive whenever you have default count measure otherwise output strange error (Javascript Error: Cannot read property 'length' of undefined) # chart = chart.interactive() # If you want to enable Zooming and Panning self.code += "import altair as alt\n" # self.code += f"visData = pd.DataFrame({str(self.data.to_dict(orient='records'))})\n" self.code += f"visData = pd.DataFrame({str(self.data.to_dict())})\n" self.code += f""" chart = alt.Chart(visData).mark_bar().encode( y = {y_attr_field_code}, x = {x_attr_field_code}, )\n""" return chart
def initialize_chart(self): self.tooltip = False measure = self.vis.get_attr_by_data_model("measure", exclude_record=True)[0] msr_attr = self.vis.get_attr_by_channel(measure.channel)[0] msr_attr_abv = str(msr_attr.attribute) if len(msr_attr_abv) > 17: msr_attr_abv = msr_attr_abv[:10] + "..." + msr_attr_abv[-7:] x_min = self.vis.min_max[msr_attr.attribute][0] x_max = self.vis.min_max[msr_attr.attribute][1] if isinstance(msr_attr.attribute, str): msr_attr.attribute = msr_attr.attribute.replace(".", "") colval = self.vis.data[msr_attr.attribute] x_range = abs(max(colval) - min(colval)) plot_range = abs(x_max - x_min) markbar = x_range / plot_range * 12 self.data = AltairChart.sanitize_dataframe(self.data) axis_title = f"{msr_attr_abv} (binned)" if msr_attr.attribute == " ": axis_title = "Series (binned)" if measure.channel == "x": chart = (alt.Chart(self.data).mark_bar(size=markbar).encode( alt.X( str(msr_attr.attribute), title=axis_title, bin=alt.Bin(binned=True), type=msr_attr.data_type, axis=alt.Axis(labelOverlap=True, title=axis_title), scale=alt.Scale(domain=(x_min, x_max)), ), alt.Y("Number of Records", type="quantitative"), )) elif measure.channel == "y": chart = (alt.Chart(self.data).mark_bar(size=markbar).encode( x=alt.X("Number of Records", type="quantitative"), y=alt.Y( str(msr_attr.attribute), title=axis_title, bin=alt.Bin(binned=True), axis=alt.Axis(labelOverlap=True, title=axis_title), scale=alt.Scale(domain=(x_min, x_max)), ), )) ##################################### ## Constructing Altair Code String ## ##################################### self.code += "import altair as alt\n" # self.code += f"visData = pd.DataFrame({str(self.data.to_dict(orient='records'))})\n" self.code += f"visData = pd.DataFrame({str(self.data.to_dict())})\n" if measure.channel == "x": self.code += f""" chart = alt.Chart(visData).mark_bar(size={markbar}).encode( alt.X('{msr_attr.attribute}', title='{axis_title}',bin=alt.Bin(binned=True), type='{msr_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{axis_title}'), scale=alt.Scale(domain=({x_min}, {x_max}))), alt.Y("Number of Records", type="quantitative") ) """ elif measure.channel == "y": self.code += f""" chart = alt.Chart(visData).mark_bar(size={markbar}).encode( alt.Y('{msr_attr.attribute}', title='{axis_title}',bin=alt.Bin(binned=True), type='{msr_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{axis_title}'), scale=alt.Scale(domain=({x_min}, {x_max}))), alt.X("Number of Records", type="quantitative") ) """ return chart
def initialize_chart(self): self.tooltip = False measure = self.vis.get_attr_by_data_model("measure", exclude_record=True)[0] msr_attr = self.vis.get_attr_by_channel(measure.channel)[0] msr_attr_abv = str(msr_attr.attribute) if len(msr_attr_abv) > 17: msr_attr_abv = msr_attr_abv[:10] + "..." + msr_attr_abv[-7:] x_min, x_max = self.vis.min_max[msr_attr.attribute] x_range = abs(x_max - x_min) if isinstance(msr_attr.attribute, str): msr_attr.attribute = msr_attr.attribute.replace(".", "") markbar = compute_bin_width(self.data[msr_attr.attribute]) step = abs(self.data[msr_attr.attribute][1] - self.data[msr_attr.attribute][0]) # Default when bin too small if markbar < (x_range / 24): markbar = (x_max - x_min) / 12 self.data = AltairChart.sanitize_dataframe(self.data) end_attr_abv = str(msr_attr.attribute) + "_end" self.data[end_attr_abv] = self.data[str(msr_attr.attribute)] + markbar axis_title = f"{msr_attr_abv} (binned)" if msr_attr.attribute == " ": axis_title = "Series (binned)" if measure.channel == "x": chart = (alt.Chart(self.data).mark_bar().encode( x=alt.X( str(msr_attr.attribute), title=axis_title, bin=alt.Bin(binned=True, step=step), type=msr_attr.data_type, axis=alt.Axis(title=axis_title), scale=alt.Scale(domain=[x_min, x_max]), ), x2=end_attr_abv, y=alt.Y("Number of Records", type="quantitative"), )) elif measure.channel == "y": chart = (alt.Chart(self.data).mark_bar().encode( x=alt.X("Number of Records", type="quantitative"), y=alt.Y( str(msr_attr.attribute), title=axis_title, bin=alt.Bin(binned=True, step=step), type=msr_attr.data_type, axis=alt.Axis(title=axis_title), ), y2=end_attr_abv, )) ##################################### ## Constructing Altair Code String ## ##################################### self.code += "import altair as alt\n" self.code += f"visData = pd.DataFrame({str(self.data.to_dict())})\n" if measure.channel == "x": self.code += f""" chart = alt.Chart(visData).mark_bar().encode( x=alt.X('{msr_attr.attribute}', title='{axis_title}',bin=alt.Bin(binned=True, step={step}), type='{msr_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{axis_title}'), scale=alt.Scale(domain=({x_min}, {x_max}))), x2='{end_attr_abv}', y=alt.Y("Number of Records", type="quantitative") ) """ elif measure.channel == "y": self.code += f""" chart = alt.Chart(visData).mark_bar().encode( y=alt.Y('{msr_attr.attribute}', title='{axis_title}', bin=alt.Bin(binned=True, step={step}), type='{msr_attr.data_type}', axis=alt.Axis(labelOverlap=True, title='{axis_title}')), y2='{end_attr_abv}', x=alt.X("Number of Records", type="quantitative") ) """ return chart
def initialize_chart(self): self.tooltip = False # tooltip looks weird for line chart x_attr = self.vis.get_attr_by_channel("x")[0] y_attr = self.vis.get_attr_by_channel("y")[0] x_attr_abv = str(x_attr.attribute) y_attr_abv = str(y_attr.attribute) if len(x_attr_abv) > 25: x_attr_abv = x_attr.attribute[:15] + "..." + x_attr.attribute[-10:] if len(y_attr_abv) > 25: y_attr_abv = y_attr.attribute[:15] + "..." + y_attr.attribute[-10:] if isinstance(x_attr.attribute, str): x_attr.attribute = x_attr.attribute.replace(".", "") if isinstance(y_attr.attribute, str): y_attr.attribute = y_attr.attribute.replace(".", "") # Remove NaNs only for Line Charts (offsets axis range) self.data = self.data.dropna( subset=[x_attr.attribute, y_attr.attribute]) self.code += "import altair as alt\n" self.code += "import pandas._libs.tslibs.timestamps\n" self.code += "from pandas._libs.tslibs.timestamps import Timestamp\n" self.code += f"visData = pd.DataFrame({str(self.data.to_dict())})\n" if y_attr.data_model == "measure": agg_title = get_agg_title(y_attr) x_attr_spec = alt.X(str(x_attr.attribute), type=x_attr.data_type, axis=alt.Axis(title=x_attr_abv)) y_attr_spec = alt.Y( str(y_attr.attribute), type=y_attr.data_type, title=agg_title, axis=alt.Axis(title=y_attr_abv), ) x_attr_field_code = f"alt.X('{x_attr.attribute}', type = '{x_attr.data_type}', axis=alt.Axis(title='{x_attr_abv}'))" y_attr_fieldCode = f"alt.Y('{y_attr.attribute}', type= '{y_attr.data_type}', title='{agg_title}', axis=alt.Axis(title='{y_attr_abv}')" else: agg_title = get_agg_title(x_attr) x_attr_spec = alt.X( str(x_attr.attribute), type=x_attr.data_type, title=agg_title, axis=alt.Axis(title=x_attr_abv), ) y_attr_spec = alt.Y(str(y_attr.attribute), type=y_attr.data_type, axis=alt.Axis(title=y_attr_abv)) x_attr_field_code = f"alt.X('{x_attr.attribute}', type = '{x_attr.data_type}', title='{agg_title}', axis=alt.Axis(title='{x_attr_abv}')" y_attr_fieldCode = f"alt.Y('{y_attr.attribute}', type= '{y_attr.data_type}', axis=alt.Axis(title='{y_attr_abv}')" self.data = AltairChart.sanitize_dataframe(self.data) chart = alt.Chart(self.data).mark_line().encode(x=x_attr_spec, y=y_attr_spec) chart = chart.interactive() # Enable Zooming and Panning self.code += f""" chart = alt.Chart(visData).mark_line().encode( y = {y_attr_fieldCode}, x = {x_attr_field_code}, ) chart = chart.interactive() # Enable Zooming and Panning """ return chart