
import pandas as pd
import plotly.offline as pyo
import plotly.express as px
import plotly.graph_objects as go
pd.options.plotting.backend = 'plotly'
sales1 = pd.read_csv('../input/sample-sales-data/sales_data_sample.csv', encoding = 'unicode_escape')
sales1.head(3)
"""
# How many quantity ordered of each product and compare to price
First of all sum the number of quantity ordered for each product
"""
compa1 = sales1.groupby(['PRODUCTLINE'])['QUANTITYORDERED'].sum().reset_index()
compa1
"""
To compare price of each product to the sum of number of quantity ordered, get price of each product.
"""
compa2 = sales1.groupby('PRODUCTLINE')['PRICEEACH'].mean().reset_index()
compa2
bar_data = go.Bar(
          x = compa1['PRODUCTLINE'],
          y = compa1['QUANTITYORDERED'],
          name = 'Quantity Ordered',
          text = compa1['QUANTITYORDERED'],
          texttemplate = '%{text:.2s}',
          textposition = 'inside',
          yaxis = 'y1',
          marker=dict(
                 color=compa1['QUANTITYORDERED'],
                 colorscale='phase',
                 showscale=False),
         customdata=compa1[['PRODUCTLINE', 'QUANTITYORDERED']],
         hovertemplate =
          '<br><b>Product</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>Q.Ordered</b>: %{customdata[1]:,0f}'
)

line_data = go.Scatter(
          x = compa2['PRODUCTLINE'],
          y = compa2['PRICEEACH'],
          name = 'Price of Product',
          text = compa2['PRICEEACH'],
          mode = 'markers + lines',
          yaxis = 'y2',
          marker=dict(color='#bd3786'),
          customdata=compa2[['PRODUCTLINE', 'PRICEEACH']],
          hovertemplate =
          '<br><b>Product</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>Price</b>: $%{customdata[1]:0f}'
    
)    



data = [bar_data, line_data]

layout = go.Layout(
             title={
                'text': 'Quantity ordered and price of each product ',
                'y': 0.93,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'},
             titlefont={'family': 'Oswald',
                        'color': 'rgb(230, 34, 144)',
                        'size': 25},

             hovermode='x',

             xaxis=dict(title='<b>Name of Product</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                            family='Arial',
                            size=12,
                            color='rgb(17, 37, 239)'
                        )

                ),

             yaxis=dict(title='<b>Quantity Ordered</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                        )

                ),
             yaxis2=dict(title='<b>Price of Each Product ($)</b>', overlaying='y', side='right',
                         color='rgb(230, 34, 144)',
                         showline=True,
                         showgrid=False,
                         showticklabels=True,
                         linecolor='rgb(104, 204, 104)',
                         linewidth=2,
                         ticks='outside',
                         tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                         )

                 ),

             legend=dict(title='',
                         x=0.3,
                         y=0.95,
                         orientation='h',
                         bgcolor='rgba(255, 255, 255, 0)',
                         traceorder="normal",
                         font=dict(
                              family="sans-serif",
                              size=12,
                              color='#000000')),

                         legend_title_font_color="green", #this code is available only for legend title
                         uniformtext_minsize=15,
                         uniformtext_mode='hide',

                 )



figure1 = go.Figure(data=data, layout=layout)

figure1.show()

"""
# Compare sales and quantity ordered of each product
"""
compa3 = sales1.groupby(['PRODUCTLINE'])[['QUANTITYORDERED', 'SALES']].sum().reset_index()
compa3
bar_data1 = go.Bar(
          x = compa3['PRODUCTLINE'],
          y = compa3['QUANTITYORDERED'],
          name = 'Quantity Ordered',
          text = compa3['QUANTITYORDERED'],
          texttemplate = '%{text:.2s}',
          textposition = 'inside',
          yaxis = 'y1',
          offsetgroup=1,
          marker=dict(color='#1E6B99'),
         customdata=compa3[['PRODUCTLINE', 'QUANTITYORDERED', 'SALES']],
         hovertemplate =
          '<br><b>Product</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>Q.Ordered</b>: %{customdata[1]:,.0f}'
)

bar_data2 = go.Bar(
          x = compa3['PRODUCTLINE'],
          y = compa3['SALES'],
          name = 'Sales',
          text = compa3['SALES'],
          texttemplate = '%{text:.2s}',
          textposition = 'auto',
          yaxis = 'y2',
          offsetgroup=2,
          marker=dict(color='#B45513'),
          customdata=compa3[['PRODUCTLINE', 'QUANTITYORDERED', 'SALES']],
          hovertemplate =
          '<br><b>Product</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>Sales</b>: $%{customdata[2]:,.0f}'
    
)    



data = [bar_data1, bar_data2]

layout = go.Layout(
             title={
                'text': 'Sales of ordered quantity',
                'y': 0.93,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'},
             titlefont={'family': 'Oswald',
                        'color': 'rgb(230, 34, 144)',
                        'size': 25},

             hovermode='x',
             height=600,

             xaxis=dict(title='<b>Name of Product</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                            family='Arial',
                            size=12,
                            color='rgb(17, 37, 239)'
                        )

                ),

             yaxis=dict(title='<b>Quantity Ordered</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                        )

                ),
             yaxis2=dict(title='<b>Sales ($)</b>', overlaying='y', side='right',
                         color='rgb(230, 34, 144)',
                         showline=True,
                         showgrid=False,
                         showticklabels=True,
                         linecolor='rgb(104, 204, 104)',
                         linewidth=2,
                         ticks='outside',
                         tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                         )

                 ),

             legend=dict(title='',
                         x=0.3,
                         y=0.95,
                         orientation='h',
                         bgcolor='rgba(255, 255, 255, 0)',
                         traceorder="normal",
                         font=dict(
                              family="sans-serif",
                              size=12,
                              color='#000000')),

                         legend_title_font_color="green", #this code is available only for legend title
                         uniformtext_minsize=15,
                         uniformtext_mode='hide',

                 )



figure2 = go.Figure(data=data, layout=layout)

figure2.show()

"""
# Count each status of products
"""
compa4 = sales1.groupby('STATUS')['PRODUCTLINE'].count()
compa4
Cancelled = sales1.loc[sales1['STATUS'] == 'Cancelled'].count()[0]
Cancelled
Disputed = sales1.loc[sales1['STATUS'] == 'Disputed'].count()[0]
Disputed
In_Process = sales1.loc[sales1['STATUS'] == 'In Process'].count()[0]
In_Process
On_Hold = sales1.loc[sales1['STATUS'] == 'On Hold'].count()[0]
On_Hold
Resolved = sales1.loc[sales1['STATUS'] == 'Resolved'].count()[0]
Resolved
Shipped = sales1.loc[sales1['STATUS'] == 'Shipped'].count()[0]
Shipped
labels = ['Cancelled', 'Disputed', 'In Process', 'On Hold', 'Resolved', 'Shipped']
values = [Cancelled, Disputed, In_Process, On_Hold, Resolved, Shipped]


figure = go.Figure(data=[go.Pie(labels=labels, values=values,
                               
                
                hoverinfo='label+value+percent',
                textinfo='label+percent',
                textfont=dict(size=13),
                insidetextorientation='radial',
                           
)])

figure.show()
"""
# Calculate yearly sales value
"""
compa5 = sales1.groupby(['YEAR_ID'])[['SALES', 'QUANTITYORDERED']].sum().reset_index()
compa5.head()
bar_data1 = go.Bar(
          x = compa5['YEAR_ID'],
          y = compa5['SALES'],
          name = 'Sales',
          text = compa5['SALES'],
          texttemplate = '%{text:.2s}',
          textposition = 'inside',
          yaxis = 'y1',
          marker=dict(color=' #7F8C8D'),
          customdata=compa5[['YEAR_ID', 'SALES', 'QUANTITYORDERED']],
          hovertemplate =
          '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>Sales</b>: $%{customdata[1]:,.0f}'
)

line_data2 = go.Scatter(
          x = compa5['YEAR_ID'],
          y = compa5['QUANTITYORDERED'],
          name = 'Quantity Ordered',
          text = compa5['QUANTITYORDERED'],
          mode = 'markers + lines',
          yaxis = 'y2',
          marker=dict(color='#DC7633 '),
          customdata=compa5[['YEAR_ID', 'SALES', 'QUANTITYORDERED']],
          hovertemplate =
          '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>Quantity Ordered</b>: %{customdata[2]:,.0f}'
    
)    



data = [bar_data1, line_data2]

layout = go.Layout(
             title={
                'text': 'Yearly Sales and Quantity Ordered',
                'y': 0.93,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'},
             titlefont={'family': 'Oswald',
                        'color': 'rgb(230, 34, 144)',
                        'size': 25},

             hovermode='x',
             height=550,

             xaxis=dict(title='<b>Year</b>',
                        tick0=0,
                        dtick=1,
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                            family='Arial',
                            size=12,
                            color='rgb(17, 37, 239)'
                        )

                ),

             yaxis=dict(title='<b>Total Sales ($)</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                        )

                ),
             yaxis2=dict(title='<b>Total Quantity Ordered</b>', overlaying='y', side='right',
                         color='rgb(230, 34, 144)',
                         showline=True,
                         showgrid=False,
                         showticklabels=True,
                         linecolor='rgb(104, 204, 104)',
                         linewidth=2,
                         ticks='outside',
                         tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                         )

                 ),

             legend=dict(title='Trend',
                         x=0.8,
                         y=0.95,
                         orientation='v',
                         bgcolor='rgba(255, 255, 255, 0)',
                         traceorder="normal",
                         font=dict(
                              family="sans-serif",
                              size=12,
                              color='#000000')),

                         legend_title_font_color="green", #this code is available only for legend title
                         uniformtext_minsize=15,
                         uniformtext_mode='hide',

                 )



figure2 = go.Figure(data=data, layout=layout)

figure2.show()

"""
# Calculate monthly sales
"""
monthly_sales = sales1.groupby(['YEAR_ID','MONTH_ID'])['SALES'].sum().reset_index()
monthly_sales.head(3)
year_2003 = monthly_sales[monthly_sales['YEAR_ID'] == 2003]
year_2003
year_2004 = monthly_sales[monthly_sales['YEAR_ID'] == 2004]
year_2005 = monthly_sales[monthly_sales['YEAR_ID'] == 2005]
data_2003 = go.Scatter(
                    x = year_2003['MONTH_ID'],
                    y = year_2003['SALES'],
                    name = '2003',
                    mode = 'markers + lines',
                    customdata=year_2003[['YEAR_ID', 'MONTH_ID', 'SALES']],
                    hovertemplate =
                    '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
                    '<b>Month</b>: %{customdata[1]}'+
                    '<br><b>Sales</b>: $%{customdata[2]:,.0f}</br>'
)

data_2004 = go.Scatter(
                    x = year_2004['MONTH_ID'],
                    y = year_2004['SALES'],
                    name = '2003',
                    mode = 'markers + lines',
                    customdata=year_2004[['YEAR_ID', 'MONTH_ID', 'SALES']],
                    hovertemplate =
                    '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
                    '<b>Month</b>: %{customdata[1]}'+
                    '<br><b>Sales</b>: $%{customdata[2]:,.0f}</br>'
)

data_2005 = go.Scatter(
                    x = year_2005['MONTH_ID'],
                    y = year_2005['SALES'],
                    name = '2003',
                    mode = 'markers + lines',
                    customdata=year_2005[['YEAR_ID', 'MONTH_ID', 'SALES']],
                    hovertemplate =
                    '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
                    '<b>Month</b>: %{customdata[1]}'+
                    '<br><b>Sales</b>: $%{customdata[2]:,.0f}</br>'
)


data = [data_2003, data_2004, data_2005]

layout = go.Layout(
             title={
                'text': 'Monthly Sales',
                'y': 0.93,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'},
             titlefont={'family': 'Oswald',
                        'color': 'rgb(230, 34, 144)',
                        'size': 25},

             hovermode='x',
             height=600,

             xaxis=dict(title='<b>Month</b>',
                        tick0=0,
                        dtick=1,
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                            family='Arial',
                            size=12,
                            color='rgb(17, 37, 239)'
                        )

                ),

             yaxis=dict(title='<b>Sales</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                        )

                ),

             legend=dict(title='',
                         x=0.3,
                         y=0.95,
                         orientation='h',
                         bgcolor='rgba(255, 255, 255, 0)',
                         traceorder="normal",
                         font=dict(
                              family="sans-serif",
                              size=12,
                              color='#000000')),

                         legend_title_font_color="green", #this code is available only for legend title
#                          uniformtext_minsize=15,
#                          uniformtext_mode='hide',

                 )



figure2 = go.Figure(data=data, layout=layout)

figure2.show()

"""
In the above chart, there are three legends, 2003, 2004, 2005.
"""
"""
# Compare growth rate of sales to actual sales
Create new column 'GROWTH RATE' inside data frame 'monthly_sales'
"""
monthly_sales['GROWTH RATE'] = monthly_sales['SALES'].pct_change()
monthly_sales.head(3)
year_2003_g = monthly_sales[monthly_sales['YEAR_ID'] == 2003]
year_2003_g.head(3)
year_2004_g = monthly_sales[monthly_sales['YEAR_ID'] == 2004]
year_2005_g = monthly_sales[monthly_sales['YEAR_ID'] == 2005]
bar_2003 = go.Bar(
          x = year_2003['MONTH_ID'],
          y = year_2003['SALES'],
          name = '2003',
          marker = dict(color='rgb(240, 128, 128)'),
          yaxis = 'y1',
          customdata=year_2003[['YEAR_ID', 'MONTH_ID', 'SALES']],
                    hovertemplate =
                    '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
                    '<b>Month</b>: %{customdata[1]}'+
                    '<br><b>Sales</b>: $%{customdata[2]:,.0f}</br>'
)

bar_2004 = go.Bar(
          x = year_2004['MONTH_ID'],
          y = year_2004['SALES'],
          name = '2004',
           marker = dict(color='rgb(239, 11, 173)'),
          yaxis = 'y1',
          customdata=year_2004[['YEAR_ID', 'MONTH_ID', 'SALES']],
                    hovertemplate =
                    '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
                    '<b>Month</b>: %{customdata[1]}'+
                    '<br><b>Sales</b>: $%{customdata[2]:,.0f}</br>'
)

bar_2005 = go.Bar(
          x = year_2005['MONTH_ID'],
          y = year_2005['SALES'],
          name = '2005',
          marker = dict(color='rgb(11, 180, 239)'),
          yaxis = 'y1',
          customdata=year_2005[['YEAR_ID', 'MONTH_ID', 'SALES']],
                    hovertemplate =
                    '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
                    '<b>Month</b>: %{customdata[1]}'+
                    '<br><b>Sales</b>: $%{customdata[2]:,.0f}</br>'
)

data_g_2003 = go.Scatter(
                    x = year_2003_g['MONTH_ID'],
                    y = year_2003_g['GROWTH RATE'],
                    name = '2003',
                    mode = 'markers + lines',
                    customdata=year_2003_g[['YEAR_ID', 'MONTH_ID', 'GROWTH RATE']],
                    hovertemplate =
                    '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
                    '<b>Month</b>: %{customdata[1]}'+
                    '<br><b>Growth Rate</b>: %{customdata[2]:,.2f}</br>',
                    yaxis = 'y2',
                    marker = dict(color='rgb(33, 47, 61)'),
                    line=dict(dash='dash')
)

data_g_2004 = go.Scatter(
                    x = year_2004_g['MONTH_ID'],
                    y = year_2004_g['GROWTH RATE'],
                    name = '2004',
                    mode = 'markers + lines',
                    customdata=year_2004_g[['YEAR_ID', 'MONTH_ID', 'GROWTH RATE']],
                    hovertemplate =
                    '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
                    '<b>Month</b>: %{customdata[1]}'+
                    '<br><b>Growth Rate</b>: %{customdata[2]:,.2f}</br>',
                    yaxis = 'y2',
                    marker = dict(color='rgb(11, 239, 204)'),
                    line=dict(dash='dash')
)

data_g_2005 = go.Scatter(
                    x = year_2005_g['MONTH_ID'],
                    y = year_2005_g['GROWTH RATE'],
                    name = '2005',
                    mode = 'markers + lines',
                    customdata=year_2005_g[['YEAR_ID', 'MONTH_ID', 'GROWTH RATE']],
                    hovertemplate =
                    '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
                    '<b>Month</b>: %{customdata[1]}'+
                    '<br><b>Growth Rate</b>: %{customdata[2]:,.2f}</br>',
                     yaxis = 'y2',
                     marker = dict(color='rgb(11, 87, 239)'),
                     line=dict(dash='dash')
)

data = [bar_2003, bar_2004, bar_2005, data_g_2003, data_g_2004, data_g_2005]

layout = go.Layout(
             title={
                'text': 'Actual Sales and Growth Rate',
                'y': 0.93,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'},
             titlefont={'family': 'Oswald',
                        'color': 'rgb(230, 34, 144)',
                        'size': 25},

             hovermode='x',
             height=600,

             xaxis=dict(title='<b>Month</b>',
                        tick0=0,
                        dtick=1,
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                            family='Arial',
                            size=12,
                            color='rgb(17, 37, 239)'
                        )

                ),

             yaxis=dict(title='<b>Actual Sales</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                        )

                ),
             yaxis2=dict(title='<b>Growth Rate</b>', overlaying='y', side='right',
                         color='rgb(230, 34, 144)',
                         showline=True,
                         showgrid=False,
                         showticklabels=True,
                         linecolor='rgb(104, 204, 104)',
                         linewidth=2,
                         ticks='outside',
                         tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                         )

                 ),

             legend=dict(title='',
                         x=0.3,
                         y=0.95,
                         orientation='h',
                         bgcolor='rgba(255, 255, 255, 0)',
                         traceorder="normal",
                         font=dict(
                              family="sans-serif",
                              size=12,
                              color='#000000')),

                         legend_title_font_color="green", #this code is available only for legend title
                         uniformtext_minsize=15,
                         uniformtext_mode='hide',

                 )



figure2 = go.Figure(data=data, layout=layout)

figure2.show()



          
"""
# Count size of deal
"""
deal = sales1.groupby(['COUNTRY', 'DEALSIZE'])['SALES'].count().reset_index()
deal.head()
small = sales1.loc[sales1['DEALSIZE'] == 'Small'].count()[0]
small
medium = sales1.loc[sales1['DEALSIZE'] == 'Medium'].count()[0]
large = sales1.loc[sales1['DEALSIZE'] == 'Large'].count()[0]
labels = ['Small', 'Medium', 'Large']
values = [small, medium, large]
colors = ['#E67E22 ','#FF00FF', '#CD5C5C']

figure = go.Figure(data=[go.Pie(labels=labels, values=values,
                               
                marker=dict(colors=colors),
                hoverinfo='label+value+percent',
                textinfo='label+value',
                textfont=dict(size=13),
                insidetextorientation='radial',
                           
)])

figure.show()
"""
# Calculate sales from top 10 countries
"""
top_countries = sales1.groupby(['COUNTRY'])[['SALES','QUANTITYORDERED']].sum().sort_values(by=['SALES'], ascending=False).nlargest(10, columns=['SALES']).reset_index()
top_countries
bar_data = go.Bar(
          x = top_countries['SALES'],
          y = top_countries['COUNTRY'],
          text = top_countries['SALES'],
          texttemplate = '%{text:.2s}',
          textposition = 'inside',
          marker=dict(
                 color=top_countries['SALES'],
                 colorscale='portland',
                 showscale=False),
          orientation='h',
          customdata=top_countries[['QUANTITYORDERED', 'COUNTRY', 'SALES']],
          hovertemplate =
          '<br><b>Country</b>: %{y}<br><extra></extra>'+
          '<b>Sales</b>: $%{x:,.0f}'+
          '<br><b>Quantity Ordered</b>: %{customdata[0]:,.0f}<br>'
          
          
)

layout = go.Layout(
    title={
                'text': 'Top 10 Countries',
                'y': 0.93,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'},
             titlefont={'family': 'Oswald',
                        'color': 'rgb(230, 34, 144)',
                        'size': 25},

             hovermode='closest',
             height=600,

             xaxis=dict(title='<b>Sales</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                            family='Arial',
                            size=12,
                            color='rgb(17, 37, 239)'
                        )

                ),

             yaxis=dict(title='<b>Country</b>',
                        autorange='reversed',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=False,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                        )

                ),
    
            uniformtext_minsize=12,
            uniformtext_mode='hide'

                  

)

figure2 = go.Figure(data=bar_data, layout=layout)

figure2.show()



"""
# Calculate number of monthly active customer
"""
monthly_customer = sales1.groupby(['YEAR_ID','MONTH_ID'])['CUSTOMERNAME'].nunique().reset_index()
monthly_customer.head(3)
year_2003_cus = monthly_customer[monthly_customer['YEAR_ID'] == 2003]
year_2003_cus.head(3)
year_2004_cus = monthly_customer[monthly_customer['YEAR_ID'] == 2004]
year_2005_cus = monthly_customer[monthly_customer['YEAR_ID'] == 2005]
cus_data1 = go.Bar(
          x = year_2003_cus['MONTH_ID'],
          y = year_2003_cus['CUSTOMERNAME'],
          name = '2003',
          text = year_2003_cus['CUSTOMERNAME'],
          texttemplate = '%{text:,.0f}',
          textposition = 'outside',
          marker = dict(color='rgb(108, 34, 230)'),
          customdata=year_2003_cus['YEAR_ID'],
          hovertemplate =
          '<br><b>Year</b>: %{customdata}<br><extra></extra>'+
          '<b>Month</b>: %{x:,.0f}'+
          '<br><b>Customer</b>: %{y:,.0f}<br>',
          
          
)

cus_data2 = go.Bar(
          x = year_2004_cus['MONTH_ID'],
          y = year_2004_cus['CUSTOMERNAME'],
          name = '2004',
          text = year_2004_cus['CUSTOMERNAME'],
          texttemplate = '%{text}',
          textposition = 'outside',
          marker = dict(color='rgb(34, 150, 230)'),
          customdata=year_2004_cus['YEAR_ID'],
          hovertemplate =
          '<br><b>Year</b>: %{customdata}<br><extra></extra>'+
          '<b>Month</b>: %{x:,.0f}'+
          '<br><b>Customer</b>: %{y:,.0f}<br>'
          
          
          
)

cus_data3 = go.Bar(
          x = year_2005_cus['MONTH_ID'],
          y = year_2005_cus['CUSTOMERNAME'],
          name = '2005',
          text = year_2005_cus['CUSTOMERNAME'],
          texttemplate = '%{text:,.0f}',
          textposition = 'outside',
          marker = dict(color='rgb(34, 230, 117)'),
          customdata=year_2005_cus['YEAR_ID'],
          hovertemplate =
          '<br><b>Year</b>: %{customdata}<br><extra></extra>'+
          '<b>Month</b>: %{x:,.0f}'+
          '<br><b>Customer</b>: %{y:,.0f}<br>'
          
          
)

data = [cus_data1, cus_data2, cus_data3]

layout = go.Layout(
             barmode='group',
             title={
                'text': 'Monthly Active Customer',
                'y': 0.93,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'},
             titlefont={'family': 'Oswald',
                        'color': 'rgb(230, 34, 144)',
                        'size': 25},

             hovermode='x',
    
             xaxis=dict(title='<b>Month</b>',
                        tick0=0,
                        dtick=1,
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                            family='Arial',
                            size=12,
                            color='rgb(17, 37, 239)'
                        )

                ),

             yaxis=dict(title='<b>Customer</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                        )

                ),
             

             legend=dict(title='',
                         x=0.25,
                         y=0.95,
                         orientation='h',
                         bgcolor='rgba(255, 255, 255, 0)',
                         traceorder="normal",
                         font=dict(
                              family="sans-serif",
                              size=12,
                              color='#000000')),

                         legend_title_font_color='green', #this code is available only for legend title
                         uniformtext_minsize=1,
                         uniformtext_mode='hide',

                 )



figure2 = go.Figure(data=data, layout=layout)

figure2.show()

"""
# Calculate average sales in each month
"""
average_sales = sales1.groupby(['YEAR_ID','MONTH_ID'])[['SALES', 'QUANTITYORDERED']].mean().reset_index()
average_sales.head(3)
ave_2003 = average_sales[average_sales['YEAR_ID'] == 2003]
ave_2003.head(3)
ave_2004 = average_sales[average_sales['YEAR_ID'] == 2004]
ave_2005 = average_sales[average_sales['YEAR_ID'] == 2005]
data_2003 = go.Scatter(
   x = ave_2003['MONTH_ID'],
   y = ave_2003['SALES'],
   name = '2003',
   mode = 'markers + lines',
   customdata=ave_2003[['YEAR_ID', 'QUANTITYORDERED']],
   hovertemplate =
          '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>Month</b>: %{x}'+
          '<br><b>Av.Sales</b>: %{y:.1f}<br>'+
          '<b>Av.Quantity Ordered</b>: %{customdata[1]:,.2f}',
            line=dict(dash='dash')
   
)

data_2004 = go.Scatter(
   x = ave_2004['MONTH_ID'],
   y = ave_2004['SALES'],
   name = '2004',
   mode = 'markers + lines',
   customdata=ave_2004[['YEAR_ID', 'QUANTITYORDERED']],
   hovertemplate =
          '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>Month</b>: %{x}'+
          '<br><b>Av.Sales</b>: %{y:.1f}<br>'+
          '<b>Av.Quantity Ordered</b>: %{customdata[1]:,.2f}',
            line=dict(dash='dash')
   
)

data_2005 = go.Scatter(
   x = ave_2005['MONTH_ID'],
   y = ave_2005['SALES'],
   name = '2005',
   mode = 'markers + lines',
   customdata=ave_2005[['YEAR_ID', 'QUANTITYORDERED']],
   hovertemplate =
          '<br><b>Year</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>Month</b>: %{x}'+
          '<br><b>Av.Sales</b>: %{y:.1f}<br>'+
          '<b>Av.Quantity Ordered</b>: %{customdata[1]:,.2f}'
            
)

data = [data_2003, data_2004, data_2005]

layout = go.Layout(title={
                'text': 'Average Sales per Month',
                'y': 0.93,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'},
             titlefont={'family': 'Oswald',
                        'color': 'rgb(230, 34, 144)',
                        'size': 25},

             hovermode='x',
    
             xaxis=dict(title='<b>Month</b>',
                        tick0=0,
                        dtick=1,
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                            family='Arial',
                            size=12,
                            color='rgb(17, 37, 239)'
                        )

                ),

             yaxis=dict(title='<b>Average Sales</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                        )

                ),
             

             legend=dict(title='',
                         x=0.35,
                         y=0.95,
                         orientation='h',
                         bgcolor='rgba(255, 255, 255, 0)',
                         traceorder="normal",
                         font=dict(
                              family="sans-serif",
                              size=12,
                              color='#000000')),

                         legend_title_font_color='green', #this code is available only for legend title
#                          uniformtext_minsize=1,
#                          uniformtext_mode='hide',

                 )



figure2 = go.Figure(data=data, layout=layout)

figure2.show()

"""
# Compare sales of each country on scatter chart
"""
product_sales1 = sales1.groupby(['COUNTRY','PRODUCTLINE'])[['QUANTITYORDERED', 'SALES']].sum().reset_index()
product_sales1
data = go.Scatter(
  x = product_sales1['QUANTITYORDERED'],
  y = product_sales1['SALES'],
  mode = 'markers',
  customdata=product_sales1[['COUNTRY', 'PRODUCTLINE']],
  hovertemplate =
          '<br><b>Country</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>Product</b>: %{customdata[1]}'+
          '<br><b>Quantity Ordered</b>: %{x:,.0f}<br>'+
          '<b>Sales</b>: $%{y:,.0f}',
    marker=dict(
        size=20,
        color=product_sales1['QUANTITYORDERED'],
        colorscale='mrybm', 
        showscale=False)

)

layout = go.Layout(title={
                'text': 'Sales of Ordered Quantity',
                'y': 0.93,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'},
             titlefont={'family': 'Oswald',
                        'color': 'rgb(230, 34, 144)',
                        'size': 25},

             hovermode='closest',
    
             xaxis=dict(title='<b>Quantity Ordered</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                            family='Arial',
                            size=12,
                            color='rgb(17, 37, 239)'
                        )

                ),

             yaxis=dict(title='<b>Sales</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                        )

                )
             
    )       

figure2 = go.Figure(data=data, layout=layout)

figure2.show()                   
             
"""
# Compare sales of each country on bubble chart
"""
product_sales2 = sales1.groupby(['COUNTRY', 'STATE', 'CITY', 'PRODUCTLINE', 'DEALSIZE'])[['QUANTITYORDERED', 'SALES']].sum().reset_index()
product_sales2
product_sales2['QUANTITYORDERED'][product_sales2['DEALSIZE'] == 'Small']
data1 = go.Scatter(
  x = product_sales2['QUANTITYORDERED'][product_sales2['DEALSIZE'] == 'Small'],
  y = product_sales2['SALES'][product_sales2['DEALSIZE'] == 'Small'],
  mode = 'markers',
  customdata=product_sales2[['COUNTRY', 'STATE',  'CITY', 'PRODUCTLINE', 'QUANTITYORDERED', 'SALES']],
  hovertemplate =
          '<br><b>Country</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>State</b>: %{customdata[1]}'+
          '<br><b>City</b>: %{customdata[2]}</br>'+
          '<b>Product</b>: %{customdata[3]}'+
          '<br><b>Q.Ordered</b>: %{customdata[4]:,.0f}</br>'+
          '<b>Sales</b>: $%{customdata[5]:,.0f}',
    marker=dict(
        size=2000*product_sales2['QUANTITYORDERED'][product_sales2['DEALSIZE'] == 'Small']/product_sales2['SALES'][product_sales2['DEALSIZE'] == 'Small'],
        color=product_sales2['QUANTITYORDERED'][product_sales2['DEALSIZE'] == 'Small'],
        colorscale='mrybm', 
        showscale=False),
    name='Small'

)

data2 = go.Scatter(
  x = product_sales2['QUANTITYORDERED'][product_sales2['DEALSIZE'] == 'Medium'],
  y = product_sales2['SALES'][product_sales2['DEALSIZE'] == 'Medium'],
  mode = 'markers',
  customdata=product_sales2[['COUNTRY', 'STATE',  'CITY', 'PRODUCTLINE', 'QUANTITYORDERED', 'SALES']],
  hovertemplate =
          '<br><b>Country</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>State</b>: %{customdata[1]}'+
          '<br><b>City</b>: %{customdata[2]}</br>'+
          '<b>Product</b>: %{customdata[3]}'+
          '<br><b>Q.Ordered</b>: %{customdata[4]:,.0f}</br>'+
          '<b>Sales</b>: $%{customdata[5]:,.0f}',
    marker=dict(
        size=2000*product_sales2['QUANTITYORDERED'][product_sales2['DEALSIZE'] == 'Medium']/product_sales2['SALES'][product_sales2['DEALSIZE'] == 'Medium'],
        color=product_sales2['QUANTITYORDERED'][product_sales2['DEALSIZE'] == 'Medium'],
        colorscale='mrybm', 
        showscale=False),
    name='Medium'

)

data3 = go.Scatter(
  x = product_sales2['QUANTITYORDERED'][product_sales2['DEALSIZE'] == 'Large'],
  y = product_sales2['SALES'][product_sales2['DEALSIZE'] == 'Large'],
  mode = 'markers',
  customdata=product_sales2[['COUNTRY', 'STATE',  'CITY', 'PRODUCTLINE', 'QUANTITYORDERED', 'SALES']],
  hovertemplate =
          '<br><b>Country</b>: %{customdata[0]}<br><extra></extra>'+
          '<b>State</b>: %{customdata[1]}'+
          '<br><b>City</b>: %{customdata[2]}</br>'+
          '<b>Product</b>: %{customdata[3]}'+
          '<br><b>Q.Ordered</b>: %{customdata[4]:,.0f}</br>'+
          '<b>Sales</b>: $%{customdata[5]:,.0f}',
    marker=dict(
        size=4000*product_sales2['QUANTITYORDERED'][product_sales2['DEALSIZE'] == 'Large']/product_sales2['SALES'][product_sales2['DEALSIZE'] == 'Large'],
        color=product_sales2['QUANTITYORDERED'][product_sales2['DEALSIZE'] == 'Large'],
        colorscale='mrybm', 
        showscale=False),
    name='Large'

)

data = [data1, data2, data3]

layout = go.Layout(title={
                'text': 'Sales of Ordered Quantity',
                'y': 0.93,
                'x': 0.5,
                'xanchor': 'center',
                'yanchor': 'top'},
             titlefont={'family': 'Oswald',
                        'color': 'rgb(230, 34, 144)',
                        'size': 25},

             hovermode='closest',
             height=600,      
    
             xaxis=dict(title='<b>Quantity Ordered</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                            family='Arial',
                            size=12,
                            color='rgb(17, 37, 239)'
                        )

                ),

             yaxis=dict(title='<b>Sales</b>',
                        color='rgb(230, 34, 144)',
                        showline=True,
                        showgrid=True,
                        showticklabels=True,
                        linecolor='rgb(104, 204, 104)',
                        linewidth=2,
                        ticks='outside',
                        tickfont=dict(
                           family='Arial',
                           size=12,
                           color='rgb(17, 37, 239)'
                        )

                ),
              
            legend=dict(title='',
                         x=0.25,
                         y=0.95,
                         orientation='h',
                         bgcolor='rgba(255, 255, 255, 0)',
                         traceorder="normal",
                         font=dict(
                              family="sans-serif",
                              size=12,
                              color='#000000')),

                         legend_title_font_color='green', #this code is available only for legend title       
             
    )       

figure2 = go.Figure(data=data, layout=layout)

figure2.show()             