import pandas as pd
import krisk.plot as kk
# Use this when you want to nbconvert the notebook (used by nbviewer)
from krisk import init_notebook; init_notebook()

"""
Explanation: Krisk is created for building statistical interactive visualization with pandas+Jupyter integration on top of Echarts.
End of explanation
"""


df = pd.read_csv('http://www.stat.ubc.ca/~jenny/notOcto/STAT545A/'
                              'examples/gapminder/data/'
                              'gapminderDataFiveYear.txt', sep='\t')

df.head()

"""
Explanation: We will be using GapMinder data for examples below.
End of explanation
"""


kk.bar(df,'continent')

"""
Explanation: Let's start by small example. Using bar plot to count the data of category,
End of explanation
"""


kk.bar(df,'continent',y='gdpPercap',how='mean')

"""
Explanation: Note that by default, the plot already used a tooltip. You can hover the plot to see the y-value.
We also can plot bar by averaging GDP per capita for each continent,
End of explanation
"""


kk.bar(df,'year',y='gdpPercap',c='continent',how='mean')

"""
Explanation: We can change x as year, and use the grouping on continent,
End of explanation
"""


(kk.bar(df,'year',y='gdpPercap',c='continent',how='mean',stacked=True,annotate=True)
 .set_size(width=1000))

"""
Explanation: Stacked and annotate the chart,
End of explanation
"""


p = kk.line(df,'year',y='gdpPercap',c='continent',how='mean',
           stacked=True,annotate='all',area=True)
p.set_tooltip_style(trigger='axis',axis_pointer='shadow')
p.set_size(width=1000)

"""
Explanation: Next we can do the same thing with line chart, using area, annotate, and tooltip based on axis,
End of explanation
"""


p = (kk.hist(df,x='lifeExp',c='continent',stacked=True,bins=100))
p.set_tooltip_style(trigger='axis',axis_pointer='shadow')
p.set_theme('vintage')

"""
Explanation: We can also create a histogram and add theme into it,
End of explanation
"""


p = kk.scatter(df[df.year == 2007],'lifeExp','gdpPercap',s='pop',c='continent')
p.set_size(width=1000, height=500)
p.set_tooltip_format(['country','lifeExp','gdpPercap','pop','continent'])
p.set_theme('dark')
p.set_toolbox(save_format='png',restore=True,data_zoom=True)
p.set_legend(orient='vertical',x_pos='-1%',y_pos='-3%')
p.set_title('GapMinder of 2007',x_pos='center',y_pos='-5%')

"""
Explanation: Let's get a little bit advanced. We're going to create scatter points of GapMinder data in 2007. We use Life Expectancy, GDP per Capita, and Population as x,y,size respectively. We also want to add the information on the tooltip, add and reposition toolbox, legend, and title.
End of explanation
"""


