import altair as alt

# remove when running on Colab or JupyterLab / add when running in a jupyter notebook
alt.renderers.enable('notebook')

"""
Explanation: Declarative Visualizations using Altair
My take on it: Fast explorative visualizations if you do not need to control the details
https://altair-viz.github.io/
Based on the idea that there is a grammar for graphics
https://www.amazon.com/Grammar-Graphics-Statistics-Computing/dp/0387245448
This notebook on Colab
https://colab.research.google.com/github/DJCordhose/ai/blob/master/notebooks/altair-sandbox/car-insurance.ipynb
End of explanation
"""


# Choose one of the two following data sets, the larger one gives better results, but might clutter the visualization depending on resolution

!curl -O https://raw.githubusercontent.com/DJCordhose/ai/master/notebooks/scipy/data/insurance-customers-1500.csv
!curl -O https://raw.githubusercontent.com/DJCordhose/ai/master/notebooks/scipy/data/insurance-customers-300.csv

import pandas as pd
# customers = pd.read_csv('./insurance-customers-300.csv', sep=';')
customers = pd.read_csv('./insurance-customers-1500.csv', sep=';')

customers.head()

"""
Explanation: Car insurance customer data
End of explanation
"""


alt.Chart(customers).mark_point().encode(x='age', y='max speed', color='group')

"""
Explanation: A quick interactive overview
End of explanation
"""


# https://altair-viz.github.io/user_guide/encoding.html
# https://altair-viz.github.io/user_guide/customization.html

alt.Chart(customers).mark_point().encode(
    x='age',
    y='max speed',
    shape='group',
    color=alt.Color('group',
          type='nominal',
          legend=alt.Legend(title="Type of Driver by Color"),
          scale=alt.Scale(
                      range=['red', 'green', 'yellow'])),
).interactive()

"""
Explanation: A bit more elaborateed
End of explanation
"""


# https://altair-viz.github.io/gallery/interactive_layered_crossfilter.html

import altair as alt

brush = alt.selection(type='interval', encodings=['x'], resolve='intersect', empty='none')

# Define the base chart, with the common parts of the
# background and highlights
base = alt.Chart().mark_bar().encode(
    x=alt.X(alt.repeat('column'), type='quantitative'),
    y='count()'
).properties(
    width=250,
    height=150
)

# blue background with selection
background = base.properties(
    selection=brush
)

# yellow highlights on the transformed data
highlight = base.encode(
    color=alt.value('goldenrod')
).transform_filter(
    brush
)

# layer the two charts & repeat
alt.layer(
    background, highlight,
    data=customers
).repeat(
    column=["age", "max speed", "thousand km per year"]
)

"""
Explanation: Interactive filtering to explore the data
End of explanation
"""


