Skip to content

Commit

Permalink
Fixing to Choropleth width and int type error (#395)
Browse files Browse the repository at this point in the history
* bugfix state column as int AttributeError

* customize larger width of Choropleth chart

* resolve JSON error for unmatched geo attributes
  • Loading branch information
dorisjlee authored Jun 30, 2021
1 parent 2b6e162 commit 17dd0ee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lux/vislib/altair/AltairChart.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def __init__(self, vis):
self.tooltip = True
# ----- START self.code modification -----
self.code = ""
self.width = 160
self.height = 150
self.chart = self.initialize_chart()
# self.add_tooltip()
self.encode_color()
Expand Down Expand Up @@ -71,17 +73,17 @@ def apply_default_config(self):
labelFont="Helvetica Neue",
)
plotting_scale = lux.config.plotting_scale
self.chart = self.chart.properties(width=160 * plotting_scale, height=150 * plotting_scale)
self.chart = self.chart.properties(
width=self.width * plotting_scale, height=self.height * plotting_scale
)
self.code += (
"\nchart = chart.configure_title(fontWeight=500,fontSize=13,font='Helvetica Neue')\n"
)
self.code += "chart = chart.configure_axis(titleFontWeight=500,titleFontSize=11,titleFont='Helvetica Neue',\n"
self.code += "\t\t\t\t\tlabelFontWeight=400,labelFontSize=8,labelFont='Helvetica Neue',labelColor='#505050')\n"
self.code += "chart = chart.configure_legend(titleFontWeight=500,titleFontSize=10,titleFont='Helvetica Neue',\n"
self.code += "\t\t\t\t\tlabelFontWeight=400,labelFontSize=8,labelFont='Helvetica Neue')\n"
self.code += (
f"chart = chart.properties(width={160 * plotting_scale},height={150 * plotting_scale})\n"
)
self.code += f"chart = chart.properties(width={self.width * plotting_scale},height={self.height * plotting_scale})\n"

def encode_color(self):
color_attr = self.vis.get_attr_by_channel("color")
Expand Down
9 changes: 6 additions & 3 deletions lux/vislib/altair/Choropleth.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def __repr__(self):
return f"Choropleth Map <{str(self.vis)}>"

def initialize_chart(self):
# Override default width and height
self.width = 200

x_attr = self.vis.get_attr_by_channel("x")[0]
y_attr = self.vis.get_attr_by_channel("y")[0]

Expand Down Expand Up @@ -148,6 +151,8 @@ def get_geomap(self, feature):

def get_us_fips_code(self, attribute):
"""Returns FIPS code given a US state"""
if not isinstance(attribute, str):
return attribute
usa = pd.DataFrame(
[
{"fips": 1, "state": "alabama", "abbrev": "al"},
Expand Down Expand Up @@ -204,16 +209,14 @@ def get_us_fips_code(self, attribute):
]
)
attribute = attribute.lower()
if not isinstance(attribute, str):
return attribute
match = usa[(usa.state == attribute) | (usa.abbrev == attribute)]
if len(match) == 1:
return match["fips"].values[0]
else:
if attribute in ["washington d.c.", "washington dc", "d.c.", "d.c"]:
return 11
else:
return attribute
return 0 # any unmatching value (e.g. nan)

def get_country_iso_code(self, attribute):
"""Returns country ISO code given a country"""
Expand Down

0 comments on commit 17dd0ee

Please sign in to comment.