I am 1 week old on plotly and trying to create a line chart that shows solid lines for the period that has passed and a dotted line for period that is in the future and has a rangeslider to move across years.
Business case: (show power capacity by various fuel types (LNG/COAL/HYDRO/NUCLEAR) past years and future years should have a distinction in fill properties(solid/dash)
Though i manage to get the solid and dotted lines as per condition ,the chart displays very odd (the beginning and end year data is shown and rest is blank (screenshot attached) and when i use the range slider it renders even worse(Zooms into monthly granularity). )
the KEY_DATE Field i am using is fetched from the db and has a âYYYY-MM-DD hh:mm:ssâ considered as a date field.
code and output
import pandas as pd
import dash_html_components as html
from dash import Dash
from dash import dcc
from dash import html
import plotly.express as px
import plotly.graph_objects as go
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output
from ipywidgets import widgets
import plotly.io as pio
#read dataset
df = pd.read_csv(âC:\Users\asnikhm\Downloads\MASTER_Userinput.csvâ)
df =df[(df[âCOUNTRYâ]==âSouth Koreaâ) & (df[âMARKER_2â]==âPOWERGEN CAPACITYâ) & (df[âYEARâ]<2025)]
df =df.groupby([âYEARâ,âKEY_DATEâ,âMARKERâ,âREPORT_VERSIONâ],as_index=False)[âVALUEâ].sum()
OPT Define Styles
main_style = {
âbackgroundâ: â#FFFFFFâ,
âfont-familyâ: âArialâ
}
title_style = {
âbackgroundâ: â#5473FFâ,
âtextâ: â#FFFFFFâ
}
desc_style = {
âbackgroundâ: â#FFFFFFâ,
âtextâ: â5473FFâ
}
dropdown_style = {
âbackgroundâ: â#DDE5EDâ,
âtextâ: â#221C35â
}
app = Dash(name)
Define App Layout
app.layout = html.Div(
## Step 1: Define App Style
style={
âbackgroundColorâ: main_style[âbackgroundâ],
âfont-familyâ: main_style[âfont-familyâ]
},
create necessary sub components of layout
children=[
html.H1(
children=âKOREA POWER CAPACITYâ,
style={
âbackgroundColorâ: title_style[âbackgroundâ],
âcolorâ: title_style[âtextâ],
âtextAlignâ: âcenterâ}
),
html.Div(
children=âPOWERGEN , POWER_IMPORTS, POWER_EXPORTSâ,
style={
âbackgroundColorâ: desc_style[âbackgroundâ],
âcolorâ: desc_style[âtextâ],
âtextAlignâ: âcenterâ}
),
dcc.Dropdown(
id=âreportversion-dropdownâ,
style={âcolorâ: dropdown_style[âtextâ]},
options=[{âlabelâ: i, âvalueâ: i} for i in sorted(df.REPORT_VERSION.unique())],
value=â2022 Decâ,
multi=False
),
dcc.RangeSlider(id=âyear-slideâ,
min=2018,
max=2025,
value=[2020,2022],
marks={2018: â2018â,
2019: â2019â,
2020: â2020â,
2021: â2021â,
2022: â2022â,
2023: â2023â,
2024: â2024â,
2025: â2025â},
step =1,
dots=True,
pushable =1
),
dcc.Graph(
id=âgraph-outputâ,
figure = {}
)
]
)
DEFINE Callback for interactivity between Components
@app.callback(Output(component_id=âgraph-outputâ, component_property=âfigureâ),
[Input(component_id=âreportversion-dropdownâ, component_property=âvalueâ),
Input(component_id=âyear-slideâ, component_property=âvalueâ)],
prevent_initial_call=False
)
Step 2: Define Function to Create and Update Graph
def update_graph(dropdown_value,year_sl):
print(âcallback startingâ)
print(dropdown_value)
selected_val =dropdown_value
if len(selected_val) > 0:
updated_df = df[(df[âREPORT_VERSIONâ]==dropdown_value)]
upd_df1 = updated_df[(updated_df[âYEARâ]>=year_sl[0])&(updated_df[âYEARâ]<=year_sl[1])]
upd_df11 = upd_df1[(upd_df1[âYEARâ]<=2023)]
upd_df22 = upd_df1[(upd_df1[âYEARâ]>=2023)]
print(upd_df11)
print(upd_df22)
mygraph = go.FigureWidget(data=[
go.Scatter(x=upd_df11[âKEY_DATEâ],
y=upd_df11[upd_df11.MARKER==âHYDROâ][âVALUEâ],
mode=âlinesâ,
line={âdashâ:âsolidâ,âcolorâ:âblueâ}),
go.Scatter(x=upd_df22[âKEY_DATEâ],
y=upd_df22[upd_df22.MARKER==âHYDROâ][âVALUEâ],
mode=âlinesâ,
line={âdashâ:âdashâ,âcolorâ:âblueâ}),
go.Scatter(x=upd_df11[âKEY_DATEâ],
y=upd_df11[upd_df11.MARKER==âCOALâ][âVALUEâ],
mode=âlinesâ,
line={âdashâ:âsolidâ,âcolorâ:âyellowâ}),
go.Scatter(x=upd_df22[âKEY_DATEâ],
y=upd_df22[upd_df22.MARKER==âCOALâ][âVALUEâ],
mode=âlinesâ,
line={âdashâ:âdashâ,âcolorâ:âyellowâ}),
go.Scatter(x=upd_df11[âKEY_DATEâ],
y=upd_df11[upd_df11.MARKER==âLNGâ][âVALUEâ],
mode=âlinesâ,
line={âdashâ:âsolidâ,âcolorâ:âorangeâ}),
go.Scatter(x=upd_df22[âKEY_DATEâ],
y=upd_df22[upd_df22.MARKER==âLNGâ][âVALUEâ],
mode=âlinesâ,
line={âdashâ:âdashâ,âcolorâ:âorangeâ})
])
mygraph.update_layout(
height=700,
width=1300,
showlegend=False)
mygraph.update_xaxes(
dtick=âM1â,
automargin=True,
tickangle=80,
tickfont_size=12,
fixedrange=False
)
return mygraph
elif len(selected_val) == 0:
raise Dash.exceptions.PreventUpdate
# Run app
if name==âmainâ:
app.run_server(port=8052)