Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create sales_visualizer_with_region.py #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions data/sales_visualizer_with_region.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
import pandas as pd

# Assuming you have the sales data stored in a pandas DataFrame called sales_data
# with columns 'Date', 'Region', and 'Sales'

# Replace this with your actual data
sales_data = pd.DataFrame({
'Date': pd.date_range(start='2021-01-01', end='2021-01-31'),
'Region': ['North', 'East', 'South', 'West'] * 8,
'Sales': [100, 110, 120, 115, 130, 125, 135, 140, 145, 150, 155, 160, 165, 170,
180, 175, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240,
245, 250]
})

# Initialize the Dash app
app = dash.Dash(__name__)

# Define the layout
app.layout = html.Div([
html.H1("Soul Foods Sales Visualizer", style={'textAlign': 'center', 'color': '#0074D9'}),
html.Div("Filter by Region:", style={'textAlign': 'center', 'color': '#7FDBFF'}),
dcc.RadioItems(
id='region-filter',
options=[
{'label': 'North', 'value': 'North'},
{'label': 'East', 'value': 'East'},
{'label': 'South', 'value': 'South'},
{'label': 'West', 'value': 'West'},
{'label': 'All', 'value': 'All'}
],
value='All',
labelStyle={'display': 'block', 'margin': '10px'},
style={'textAlign': 'center', 'color': '#7FDBFF'}
),
dcc.Graph(id='sales-chart'),
])

# Define callback to update the chart based on region selection
@app.callback(
Output('sales-chart', 'figure'),
[Input('region-filter', 'value')]
)
def update_chart(selected_region):
if selected_region == 'All':
filtered_sales_data = sales_data
else:
filtered_sales_data = sales_data[sales_data['Region'] == selected_region]

traces = []
for region, region_sales_data in filtered_sales_data.groupby('Region'):
traces.append({
'x': region_sales_data['Date'],
'y': region_sales_data['Sales'],
'type': 'scatter',
'name': region
})

return {
'data': traces,
'layout': {
'title': 'Sales Data by Region',
'xaxis': {'title': 'Date'},
'yaxis': {'title': 'Sales'}
}
}

# Run the app
if __name__ == '__main__':
app.run_server(debug=True)