


Build sales dashboard in python using interactive plotly data visualization library and dash web app library.
There are six graphs and one drop down list in this Sales Dashboard in Python by Plotly Dash with plotly data visualization library and dash library for web app.Selecting any country name from the drop down list, all graphs are dynamically changed.In some graphs,there are secondary y-axis.Custom hover effect is created for each graph to get information related to each product.Yearly sales graph shows sales for each year and it is compared to total number of quantity ordered in each year.Monthly sales graph shows sales for each month for selected country from the drop down list.
Import Library
Lines 1-6
First of all import necessary library for this dashboard. For this type of dashboard, I have used dash core components, dash html components, plotly graph objects and pandas for creating data frame.
Data
Line 9
Read CSV file using pandas read function. Don’t forget to insert encoding argument for escape unicode in CSV file inside read function.
Title
Lines 11-26
First of all create layout of dash. Using html component of dash give title of this dashboard as you want. Style title of dashboard using html style library as you want and adjust it according to your computer screen space. Similarly, give author name of this dashboard and place it right side of dashboard title using float right html library.
Drop Down List
Lines 28-38
Using dash core component, create a drop down list and place it on the middle of the page. Use many parameters of drop down list. For example, type any country name for value parameter. In options parameter, use pandas unique function to retrieve unique country name from the column of country to the data frame.
Charts
Lines 41-89
Using dash core components, create layout of six charts. Give unique id to each chart. Select display mode bar False or hover in configuration parameter. Adjust position and width of each chart using html style library according to your system screen resolution.
Data and Layout for each chart
Lines 92-811
After important component of dash that is callback, first of all define each chart and create specific data frame for each chart. Then display data on the chart using the newly created data frame. Format data on the chart using many parameters of chart. Format each chart layout. In layout of chart, we can format x-axis, y-axis and legend values. we can also increase and decrease width of chart. I have used many parameters to format these charts. You can see below these parameters in the code template. Please read the code below step by step, you will find there many useful parameters that are required in plotly chart in data and layout sections.
Download the csv data file using the below link.
To see full code,hover on the code area and use horizontal scrollbar if needed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 |
import dash import dash_core_components as dcc import dash_html_components as html from dash.dependencies import Input, Output import plotly.graph_objs as go import pandas as pd sales1 = pd.read_csv("sales_data_sample.csv", encoding='unicode_escape') app = dash.Dash(__name__,) app.layout = html.Div([ html.Div([ html.Br(), html.Br(), html.H1('Sample Sales Data Dashboard')], style={'margin-left': '5%','color':'#808000','width': '50%', 'display': 'inline-block' }), html.Div([ html.Br(), html.Br(), html.H4('Prepared by: Mubeen Ali')], style={'color':'#17202A','width': '30%', 'display': 'inline-block', 'float': 'right' }), html.Div([ html.Label('Select a Country:'), dcc.Dropdown(id='w_countries', multi=False, clearable=True, value='Australia', placeholder='Select Countries', options=[{'label': c, 'value': c} for c in (sales1['COUNTRY'].unique())]) ], style={'width': '10%','margin-left': '45%'}), # Create combination of bar chart and line chart (Compare quantity ordered to each price of product) html.Div([ html.Br(), dcc.Graph(id='bar_line_1', config={'displayModeBar': False}), ],style={'margin-left': '1.4%','width': '50%', 'display': 'inline-block'}), # Create combination of bar chart and line chart (Compare sales to each price of product) html.Div([ html.Br(), dcc.Graph(id='bar_line_2', config={'displayModeBar': False}), ],style={'width': '48.6%', 'display': 'inline-block', 'float': 'right'}), # Create group bar chart (Compare sales and quantity ordered for each product) html.Div([ html.Br(), dcc.Graph(id='bar_bar_3', config={'displayModeBar': False}), ],style={'margin-left': '1.4%','width': '50%', 'display': 'inline-block'}), # Create combination of bar chart and line chart (Compare each year sales and q. ordered for each product) html.Div([ html.Br(), dcc.Graph(id='bar_line_4', config={'displayModeBar': False}), ],style={'width': '48.6%', 'display': 'inline-block', 'float': 'right'}), # Create line chart (each month sales) html.Div([ html.Br(), dcc.Graph(id='line_line_5', config={'displayModeBar': False}), ],style={'margin-left': '1.4%','width': '50%', 'display': 'inline-block', 'margin-bottom':'3%'}), # Create scatter chart (Compare sales and q. ordered) html.Div([ html.Br(), dcc.Graph(id='scatter_6', config={'displayModeBar': False}), ], style={'width': '48.6%', 'display': 'inline-block', 'float': 'right', 'margin-bottom':'3%'}), ], style={'background-color': '#e6e6e6'}) # Create combination of bar chart and line chart (Compare quantity ordered to each price of product) @app.callback(Output('bar_line_1', 'figure'), [Input('w_countries', 'value')]) def update_graph(w_countries): # Data for bar product_sales1 = sales1.groupby(['PRODUCTLINE', 'COUNTRY'])['QUANTITYORDERED'].sum().reset_index() # Data for line product_sales2 = sales1.groupby(['PRODUCTLINE', 'COUNTRY'])['PRICEEACH'].mean().reset_index() return { 'data': [go.Bar(x=product_sales1[product_sales1['COUNTRY'] == w_countries]['PRODUCTLINE'], y=product_sales1[product_sales1['COUNTRY'] == w_countries]['QUANTITYORDERED'], text=product_sales1[product_sales1['COUNTRY'] == w_countries]['QUANTITYORDERED'], name='Quantity Ordered', texttemplate='%{text:.2s}', textposition='auto', marker=dict( color=product_sales1[product_sales1['COUNTRY'] == w_countries]['QUANTITYORDERED'], colorscale='phase', showscale=False), yaxis='y1', hoverinfo='text', hovertext= '<b>Country</b>: ' + product_sales1[product_sales1['COUNTRY'] == w_countries]['COUNTRY'].astype(str) + '<br>'+ '<b>Q.Ordered</b>: ' + [f'{x:,.0f}' for x in product_sales1[product_sales1['COUNTRY'] == w_countries]['QUANTITYORDERED']] + '<br>'+ '<b>Product</b>: ' + product_sales1[product_sales1['COUNTRY'] == w_countries]['PRODUCTLINE'].astype(str) + '<br>' ), go.Scatter( x=product_sales2[product_sales2['COUNTRY'] == w_countries]['PRODUCTLINE'], y=product_sales2[product_sales2['COUNTRY'] == w_countries]['PRICEEACH'], name='Price of Product', text=product_sales2[product_sales2['COUNTRY'] == w_countries]['PRICEEACH'], mode='markers + lines', marker=dict(color='#bd3786'), yaxis='y2', hoverinfo='text', hovertext= '<b>Country</b>: ' + product_sales2[product_sales2['COUNTRY'] == w_countries]['COUNTRY'].astype(str) + '<br>'+ '<b>Price</b>: $' + [f'{x:,.0f}' for x in product_sales2[product_sales2['COUNTRY'] == w_countries]['PRICEEACH']] + '<br>'+ '<b>Product</b>: ' + product_sales2[product_sales2['COUNTRY'] == w_countries]['PRODUCTLINE'].astype(str) + '<br>' )], 'layout': go.Layout( width=780, height=520, title={ 'text': 'Quantity ordered and price of each product : ' + (w_countries), 'y': 0.93, 'x': 0.43, '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.25, y=1.08, orientation='h', bgcolor='rgba(255, 255, 255, 0)', traceorder="normal", font=dict( family="sans-serif", size=12, color='#000000')), legend_title_font_color="green", uniformtext_minsize=12, uniformtext_mode='hide', ) } # Create combination of bar chart and line chart (Compare sales to each price of product) @app.callback(Output('bar_line_2', 'figure'), [Input('w_countries', 'value')]) def update_graph(w_countries): # Data for bar product_sales3 = sales1.groupby(['PRODUCTLINE', 'COUNTRY'])['SALES'].sum().reset_index() # Data for line product_sales4 = sales1.groupby(['PRODUCTLINE', 'COUNTRY'])['PRICEEACH'].mean().reset_index() return { 'data': [go.Bar(x=product_sales3[product_sales3['COUNTRY'] == w_countries]['PRODUCTLINE'], y=product_sales3[product_sales3['COUNTRY'] == w_countries]['SALES'], text=product_sales3[product_sales3['COUNTRY'] == w_countries]['SALES'], name='Total Sales', texttemplate='%{text:.2s}', textposition='auto', marker=dict( color=product_sales3[product_sales3['COUNTRY'] == w_countries]['SALES'], colorscale='portland', showscale=False), yaxis='y1', hoverinfo='text', hovertext= '<b>Country</b>: ' + product_sales3[product_sales3['COUNTRY'] == w_countries]['COUNTRY'].astype(str) + '<br>'+ '<b>Sales</b>: $' + [f'{x:,.0f}' for x in product_sales3[product_sales3['COUNTRY'] == w_countries]['SALES']] + '<br>'+ '<b>Product</b>: ' + product_sales3[product_sales3['COUNTRY'] == w_countries]['PRODUCTLINE'].astype(str) + '<br>' ), go.Scatter( x=product_sales4[product_sales4['COUNTRY'] == w_countries]['PRODUCTLINE'], y=product_sales4[product_sales4['COUNTRY'] == w_countries]['PRICEEACH'], name='Price of Product', text=product_sales4[product_sales4['COUNTRY'] == w_countries]['PRICEEACH'], mode='markers + lines', marker=dict(color='#bd3786'), yaxis='y2', hoverinfo='text', hovertext= '<b>Country</b>: ' + product_sales4[product_sales4['COUNTRY'] == w_countries]['COUNTRY'].astype(str) + '<br>'+ '<b>Product</b>: ' + product_sales4[product_sales4['COUNTRY'] == w_countries]['PRODUCTLINE'].astype(str) + '<br>'+ '<b>Price</b>: $' + [f'{x:,.0f}' for x in product_sales4[product_sales4['COUNTRY'] == w_countries]['PRICEEACH']] + '<br>' )], 'layout': go.Layout( width=780, height=520, title={ 'text': 'Total Sales and price of each product : ' + (w_countries), 'y': 0.93, 'x': 0.43, '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>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>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.25, y=1.08, orientation='h', bgcolor='rgba(255, 255, 255, 0)', traceorder="normal", font=dict( family="sans-serif", size=12, color='#000000')), legend_title_font_color="green", uniformtext_minsize=12, uniformtext_mode='hide', ) } # Create group bar chart (Compare sales and quantity ordered for each product) @app.callback(Output('bar_bar_3', 'figure'), [Input('w_countries', 'value')]) def update_graph(w_countries): # Data for bar product_sales5 = sales1.groupby(['PRODUCTLINE', 'COUNTRY'])['SALES'].sum().reset_index() # Data for line product_sales6 = sales1.groupby(['PRODUCTLINE', 'COUNTRY'])['QUANTITYORDERED'].sum().reset_index() return { 'data': [go.Bar(x=product_sales5[product_sales5['COUNTRY'] == w_countries]['PRODUCTLINE'], y=product_sales5[product_sales5['COUNTRY'] == w_countries]['SALES'], text=product_sales5[product_sales5['COUNTRY'] == w_countries]['SALES'], name='Total Sales', texttemplate='%{text:.2s}', textposition='auto', # marker=dict( # color=product_sales5[product_sales5['COUNTRY'] == w_countries]['SALES'], # colorscale='portland', # showscale=False), marker = dict(color='rgb(214, 137, 16)'), yaxis='y1', offsetgroup=1, hoverinfo='text', hovertext= '<b>Country</b>: ' + product_sales5[product_sales5['COUNTRY'] == w_countries]['COUNTRY'].astype(str) + '<br>'+ '<b>Product</b>: ' + product_sales5[product_sales5['COUNTRY'] == w_countries]['PRODUCTLINE'].astype(str) + '<br>'+ '<b>Total Sales</b>: $' + [f'{x:,.0f}' for x in product_sales5[product_sales5['COUNTRY'] == w_countries]['SALES']] + '<br>' ), go.Bar( x=product_sales6[product_sales6['COUNTRY'] == w_countries]['PRODUCTLINE'], y=product_sales6[product_sales6['COUNTRY'] == w_countries]['QUANTITYORDERED'], name='Total Q. Ordered', text=product_sales6[product_sales6['COUNTRY'] == w_countries]['QUANTITYORDERED'], texttemplate='%{text:.2s}', textposition='auto', marker=dict(color='rgb(112, 123, 124)'), yaxis='y2', offsetgroup=2, hoverinfo='text', hovertext= '<b>Country</b>: ' + product_sales6[product_sales6['COUNTRY'] == w_countries]['COUNTRY'].astype(str) + '<br>'+ '<b>Product</b>: ' + product_sales6[product_sales6['COUNTRY'] == w_countries]['PRODUCTLINE'].astype(str) + '<br>'+ '<b>Total Q. Ordered</b>: ' + [f'{x:,.0f}' for x in product_sales6[product_sales6['COUNTRY'] == w_countries]['QUANTITYORDERED']] + '<br>' )], 'layout': go.Layout( width=780, height=520, title={ 'text': 'Total Sales and Quantity ordered of each product : ' + (w_countries), 'y': 0.93, 'x': 0.43, '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>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='', x=0.25, y=1.08, orientation='h', bgcolor='rgba(255, 255, 255, 0)', traceorder="normal", font=dict( family="sans-serif", size=12, color='#000000')), legend_title_font_color="green", uniformtext_minsize=12, uniformtext_mode='hide', ) } # Create combination of bar chart and line chart (Compare each year sales and q. ordered for each product) @app.callback(Output('bar_line_4', 'figure'), [Input('w_countries', 'value')]) def update_graph(w_countries): # Data for bar product_sales7 = sales1.groupby(['COUNTRY', 'YEAR_ID'])['SALES'].sum().reset_index() # Data for line product_sales8 = sales1.groupby(['COUNTRY', 'YEAR_ID'])['QUANTITYORDERED'].sum().reset_index() return { 'data': [go.Bar(x=product_sales7[product_sales7['COUNTRY'] == w_countries]['YEAR_ID'], y=product_sales7[product_sales7['COUNTRY'] == w_countries]['SALES'], text=product_sales7[product_sales7['COUNTRY'] == w_countries]['SALES'], name='Total Sales', texttemplate='%{text:.2s}', textposition='auto', # marker=dict( # color=product_sales1[product_sales1['COUNTRY'] == w_countries]['QUANTITYORDERED'], # colorscale='phase', # showscale=False), marker = dict(color='rgb(11, 220, 239)'), yaxis='y1', hoverinfo='text', hovertext= '<b>Country</b>: ' + product_sales7[product_sales7['COUNTRY'] == w_countries]['COUNTRY'].astype(str) + '<br>'+ '<b>Total Sales</b>: $' + [f'{x:,.0f}' for x in product_sales7[product_sales7['COUNTRY'] == w_countries]['SALES']] + '<br>'+ '<b>Year</b>: ' + product_sales7[product_sales7['COUNTRY'] == w_countries]['YEAR_ID'].astype(str) + '<br>' ), go.Scatter( x=product_sales8[product_sales8['COUNTRY'] == w_countries]['YEAR_ID'], y=product_sales8[product_sales8['COUNTRY'] == w_countries]['QUANTITYORDERED'], name='Total Q. Ordered', text=product_sales8[product_sales8['COUNTRY'] == w_countries]['QUANTITYORDERED'], mode='markers + lines', marker=dict(color='#bd3786'), yaxis='y2', hoverinfo='text', hovertext= '<b>Country</b>: ' + product_sales8[product_sales8['COUNTRY'] == w_countries]['COUNTRY'].astype(str) + '<br>'+ '<b>Total Q. Ordered</b>: ' + [f'{x:,.0f}' for x in product_sales8[product_sales8['COUNTRY'] == w_countries]['QUANTITYORDERED']] + '<br>'+ '<b>Year</b>: ' + product_sales8[product_sales8['COUNTRY'] == w_countries]['YEAR_ID'].astype(str) + '<br>' )], 'layout': go.Layout( width=780, height=520, title={ 'text': 'Yearly sales and quantity ordered for each product : ' + (w_countries), 'y': 0.93, 'x': 0.43, 'xanchor': 'center', 'yanchor': 'top'}, titlefont={'family': 'Oswald', 'color': 'rgb(230, 34, 144)', 'size': 25}, hovermode='x', xaxis=dict(title='<b>Name of Product</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 Q. 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='', x=0.25, y=1.08, orientation='h', bgcolor='rgba(255, 255, 255, 0)', traceorder="normal", font=dict( family="sans-serif", size=12, color='#000000')), legend_title_font_color="green", uniformtext_minsize=12, uniformtext_mode='hide', ) } # Create line chart (each month sales) @app.callback(Output('line_line_5', 'figure'), [Input('w_countries', 'value')]) def update_graph(w_countries): # Data for line monthly_sales = sales1.groupby(['COUNTRY','YEAR_ID','MONTH_ID'])['SALES'].sum().reset_index() return { 'data': [go.Scatter(x=monthly_sales[(monthly_sales['YEAR_ID'] == 2003) & (monthly_sales['COUNTRY'] == w_countries)]['MONTH_ID'], y=monthly_sales[(monthly_sales['YEAR_ID'] == 2003) & (monthly_sales['COUNTRY'] == w_countries)]['SALES'], text=monthly_sales[(monthly_sales['YEAR_ID'] == 2003) & (monthly_sales['COUNTRY'] == w_countries)]['SALES'], name='2003', mode='markers+lines', hoverinfo='text', hovertext= '<b>Country</b>: ' + monthly_sales[(monthly_sales['YEAR_ID'] == 2003) & (monthly_sales['COUNTRY'] == w_countries)]['COUNTRY'].astype(str) + '<br>'+ '<b>Year</b>: ' + monthly_sales[(monthly_sales['YEAR_ID'] == 2003) & (monthly_sales['COUNTRY'] == w_countries)]['YEAR_ID'].astype(str) + '<br>'+ '<b>Month</b>: ' + monthly_sales[(monthly_sales['YEAR_ID'] == 2003) & (monthly_sales['COUNTRY'] == w_countries)]['MONTH_ID'].astype(str) + '<br>'+ '<b>Sales</b>: $' + [f'{x:,.0f}' for x in monthly_sales[(monthly_sales['YEAR_ID'] == 2003) & (monthly_sales['COUNTRY'] == w_countries)]['SALES']] + '<br>' ), go.Scatter(x=monthly_sales[(monthly_sales['YEAR_ID'] == 2004) & (monthly_sales['COUNTRY'] == w_countries)]['MONTH_ID'], y=monthly_sales[(monthly_sales['YEAR_ID'] == 2004) & (monthly_sales['COUNTRY'] == w_countries)]['SALES'], text=monthly_sales[(monthly_sales['YEAR_ID'] == 2004) & (monthly_sales['COUNTRY'] == w_countries)]['SALES'], name='2004', mode='markers+lines', hoverinfo='text', hovertext= '<b>Country</b>: ' + monthly_sales[(monthly_sales['YEAR_ID'] == 2004) & (monthly_sales['COUNTRY'] == w_countries)]['COUNTRY'].astype(str) + '<br>'+ '<b>Year</b>: ' + monthly_sales[(monthly_sales['YEAR_ID'] == 2004) & (monthly_sales['COUNTRY'] == w_countries)]['YEAR_ID'].astype(str) + '<br>'+ '<b>Month</b>: ' + monthly_sales[(monthly_sales['YEAR_ID'] == 2004) & (monthly_sales['COUNTRY'] == w_countries)]['MONTH_ID'].astype(str) + '<br>'+ '<b>Sales</b>: $' + [f'{x:,.0f}' for x in monthly_sales[(monthly_sales['YEAR_ID'] == 2004) & (monthly_sales['COUNTRY'] == w_countries)]['SALES']] + '<br>' ), go.Scatter(x=monthly_sales[(monthly_sales['YEAR_ID'] == 2005) & (monthly_sales['COUNTRY'] == w_countries)]['MONTH_ID'], y=monthly_sales[(monthly_sales['YEAR_ID'] == 2005) & (monthly_sales['COUNTRY'] == w_countries)]['SALES'], text=monthly_sales[(monthly_sales['YEAR_ID'] == 2005) & (monthly_sales['COUNTRY'] == w_countries)]['SALES'], name='2005', mode='markers+lines', hoverinfo='text', hovertext= '<b>Country</b>: ' + monthly_sales[(monthly_sales['YEAR_ID'] == 2005) & (monthly_sales['COUNTRY'] == w_countries)]['COUNTRY'].astype(str) + '<br>'+ '<b>Year</b>: ' + monthly_sales[(monthly_sales['YEAR_ID'] == 2005) & (monthly_sales['COUNTRY'] == w_countries)]['YEAR_ID'].astype(str) + '<br>'+ '<b>Month</b>: ' + monthly_sales[(monthly_sales['YEAR_ID'] == 2005) & (monthly_sales['COUNTRY'] == w_countries)]['MONTH_ID'].astype(str) + '<br>'+ '<b>Sales</b>: $' + [f'{x:,.0f}' for x in monthly_sales[(monthly_sales['YEAR_ID'] == 2005) & (monthly_sales['COUNTRY'] == w_countries)]['SALES']] + '<br>' )], 'layout': go.Layout( width=780, height=520, title={ 'text': 'Monthly sales : ' + (w_countries), 'y': 0.93, 'x': 0.43, '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>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)' ) ), legend=dict(title='', x=0.25, y=1.08, orientation='h', bgcolor='rgba(255, 255, 255, 0)', traceorder="normal", font=dict( family="sans-serif", size=12, color='#000000')), legend_title_font_color="green", uniformtext_minsize=12, uniformtext_mode='hide', ) } # Create scatter chart (Compare sales and q. ordered) @app.callback(Output('scatter_6', 'figure'), [Input('w_countries', 'value')]) def update_graph(w_countries): scatter = sales1.groupby(['COUNTRY','PRODUCTLINE'])[['QUANTITYORDERED', 'SALES']].sum().reset_index() return { 'data': [go.Scatter(x=scatter[scatter['COUNTRY'] == w_countries]['QUANTITYORDERED'], y=scatter[scatter['COUNTRY'] == w_countries]['SALES'], text=scatter[scatter['COUNTRY'] == w_countries]['SALES'], mode='markers', hoverinfo='text', hovertext= '<b>Country</b>: ' + scatter[scatter['COUNTRY'] == w_countries]['COUNTRY'].astype(str) + '<br>'+ '<b>Product</b>: ' + scatter[scatter['COUNTRY'] == w_countries]['PRODUCTLINE'].astype(str) + '<br>'+ '<b>Q.Ordered</b>: ' + [f'{x:,.0f}' for x in scatter[scatter['COUNTRY'] == w_countries]['QUANTITYORDERED']] + '<br>'+ '<b>Sales</b>: $' + [f'{x:,.0f}' for x in scatter[scatter['COUNTRY'] == w_countries]['SALES']] + '<br>', marker=dict( size=20, color=scatter[scatter['COUNTRY'] == w_countries]['QUANTITYORDERED'], colorscale='mrybm', showscale=False ) )], 'layout': go.Layout( width=780, height=520, title={ 'text': 'Sales of ordered quantity : ' + (w_countries), 'y': 0.93, 'x': 0.43, 'xanchor': 'center', 'yanchor': 'top'}, titlefont={'family': 'Oswald', 'color': 'rgb(230, 34, 144)', 'size': 25}, hovermode='x', 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)' ) ), ) } if __name__ == '__main__': app.run_server(debug=True) |
How to create dependant drop down list in dash?
You will soon see here dependent drop down list and other input components.