# import the class
from chorogrid import Colorbin

# read the docs
help(Colorbin)

"""
Explanation: chorogrid tutorial, part 1:
Colorbin class
This class can be used independently of the Chorogrid class shown in part 2, and vice-versa, but they work well together.
This allows you to input a parallel list of ids (e.g. state abbreviations) and quantities (e.g. unemployment rate), and it will output a list of colors
End of explanation
"""


mycolors = ['#b35806', '#f1a340', '#fee0b6', '#d8daeb', '#998ec3', '#542788']

"""
Explanation: You'll need to supply a list of colors in hex format; I recommend the library colorlover. For now, I'll just enter a diverging list of six colors, ranging from dark brown to light brown/blue to dark blue.
With colorlover installed, they were produced with the following code:
import colorlover as cl
colors = cl.scales['6']['div']['PuOr']
def rgb2hex(list_of_tuples):
    output = []
    for r, g, b in list_of_tuples:
        output.append("#{0:02x}{1:02x}{2:02x}".format(int(r), int(g), int(b)))
    return output
colors = rgb2hex(cl.to_numeric(colors))
End of explanation
"""


import pandas as pd
df = pd.read_csv('chorogrid/sample_data/sample_state_data.csv')
df.tail()

"""
Explanation: We also need some data. I've prepared one column from the U.S. Census
End of explanation
"""


# I've stated the proportional and decimals args, but left them at their defaults for now.
mybin = Colorbin(df['Percent_living_in_same_home_as_one_year_ago'], mycolors, proportional=True, decimals=None)

"""
Explanation: Now we can instantiate the Colorbin class with the quantity column in the dataframe, plus our list of six colors. Note that you don't have to use pandas, you could pass two lists instead.
End of explanation
"""


mybin.fenceposts

"""
Explanation: Colorbin has automatically chosen six 'bins' representing the range of quantities. You can retrieve them with the .fenceposts attribute. Note that because there are six bins in this case, there are seven fenceposts: bin #1 is fencepost 0-1, bin #2 is 1-2 ... bin #6 is 6-7.
End of explanation
"""


mybin.fencepostlabels

"""
Explanation: Note that these are floats. To see what the class will output as text that can be used in a map legend, use .fencepostlabels
End of explanation
"""


print(mybin.colors_out[:10]+ ['...'])

"""
Explanation: We also have a list of colors the same length as the quantities we passed to the constructor, corresponding to the fenceposts.
End of explanation
"""


mybin.set_decimals(1)
mybin.recalc(fenceposts=True)
mybin.fencepostlabels

"""
Explanation: Back to the fenceposts. That's a lot of decimals, let's change it.
After changing an attribute, the output fenceposts/labels and/or colors will not be changed until the .recalc() method is called. Calling .recalc(fenceposts=False) just recalculates the colors (which would have little or no effect in this case, since we are just changing the decimals, so only colors right near a fencepost might change); calling .recalc(True) recalculates the fenceposts, and then the colors.
End of explanation
"""


mybin.count_bins()

"""
Explanation: You can see how many ids fall in each bin with the .count_bins() method.
End of explanation
"""


mybin.fenceposts = [77,80,82,84,86,88,91]
mybin.recalc(fenceposts=False) # if this were true, the fenceposts would be changed back to what they were originally
mybin.count_bins()

"""
Explanation: If you want the fenceposts to be easier-to-read whole numbers, or any specific numbers, just reassign the .fenceposts attribute.
End of explanation
"""


mybin = Colorbin(df['Percent_living_in_same_home_as_one_year_ago'], mycolors, proportional=False, decimals=None)

mybin.count_bins()

"""
Explanation: If you want each bin to contain(approximately) the same number of ids, you can pass proportional=False to the constructor. Note that having a choropleth with unequally-sized bins can be misleading to the viewer.
End of explanation
"""


mybin.calc_complements(0.5, '#ffffff', '#000000')
for i, (color, complement) in enumerate(zip(mybin.colors_out, mybin.complements)):
    if i<10:
        print(color, complement)

"""
Explanation: Finally, there is the function calc_complements that can be used to ensure text that overlays a choropleth is visible. Choose a level of 'darkness' of the greyscale version of the colors (0 is black, 1 is white), a font color for dark backgrounds and a font color for light backgrounds.
End of explanation
"""


