Skip to content

Commit

Permalink
Merge pull request #16 from jcunninghame/jcunni/pop-chart
Browse files Browse the repository at this point in the history
move chart into components file, grouped bar for pop comparison
  • Loading branch information
jcunninghame authored Jun 5, 2023
2 parents 0c801b1 + a42ff6d commit 1dc5c7f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 57 deletions.
72 changes: 72 additions & 0 deletions components.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import streamlit as st
from streamlit_echarts import st_echarts
import util


Expand Down Expand Up @@ -49,3 +50,74 @@ def year_slider(year_values):
year_values.index(start_year) : year_values.index(end_year) + 1
]
return selected_range


def claim_type_line_chart(df, animated=True):
if animated:
t = st.session_state["iteration"]
month_list = sorted(list(set(df["year_month"])))
anim_data = df.loc[df["year_month"] <= month_list[t], :]
list_data = [anim_data.columns.to_list()] + anim_data.values.tolist()
else:
list_data = [df.columns.to_list()] + df.values.tolist()
series = list(set(df["claim_type"]))
datasetWithFilters = [
{
"id": f"dataset_{s}",
"fromDatasetId": "dataset_raw",
"transform": {
"type": "filter",
"config": {
"and": [
{"dimension": "claim_type", "=": s},
]
},
},
}
for s in series
]
seriesList = [
{
"type": "line",
"datasetId": f"dataset_{s}",
"showSymbol": False,
"name": s,
"labelLayout": {"moveOverlap": "shiftY"},
"emphasis": {"focus": "series"},
"encode": {
"x": "year_month",
"y": "paid_amount_pmpm",
"label": ["claim_type", "paid_amount_pmpm"],
"itemName": "year_month",
"tooltip": ["paid_amount_pmpm"],
},
}
for s in series
]
option = {
"color": ["#06405C", "#FFCC05", "#66B1E2"],
"dataset": [{"id": "dataset_raw", "source": list_data}] + datasetWithFilters,
"title": {"text": "Paid Amount PMPM by Claim Type"},
"tooltip": {"order": "valueDesc", "trigger": "axis"},
"xAxis": {"type": "category", "nameLocation": "middle"},
"yAxis": {"name": "PMPM"},
"grid": {"right": 140},
"series": seriesList,
}
st_echarts(options=option, height="400px", key="chart")


def pop_grouped_bar(df):
pivoted_df = df.pivot(
index="category", columns="year", values="current_period_pmpm"
).reset_index()
list_data = [pivoted_df.columns.to_list()] + pivoted_df.values.tolist()
option = {
"legend": {},
"tooltip": {},
"dataset": {"source": list_data},
"xAxis": {"type": "category", "data": sorted(list(set(df["category"])))},
"yAxis": {"type": "value"},
"series": [{"type": "bar"} for x in list(set(df["year"]))],
}
st_echarts(options=option)
61 changes: 4 additions & 57 deletions pages/02_financial_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,8 @@
import pandas as pd


def claim_type_line_chart(df, animated=True):
if animated:
t = st.session_state["iteration"]
month_list = sorted(list(set(pmpm_claim_type_data["year_month"])))
anim_data = df.loc[df["year_month"] <= month_list[t], :]
list_data = [anim_data.columns.to_list()] + anim_data.values.tolist()
else:
list_data = [df.columns.to_list()] + df.values.tolist()
series = list(set(df["claim_type"]))
datasetWithFilters = [
{
"id": f"dataset_{s}",
"fromDatasetId": "dataset_raw",
"transform": {
"type": "filter",
"config": {
"and": [
{"dimension": "claim_type", "=": s},
]
},
},
}
for s in series
]
seriesList = [
{
"type": "line",
"datasetId": f"dataset_{s}",
"showSymbol": False,
"name": s,
"labelLayout": {"moveOverlap": "shiftY"},
"emphasis": {"focus": "series"},
"encode": {
"x": "year_month",
"y": "paid_amount_pmpm",
"label": ["claim_type", "paid_amount_pmpm"],
"itemName": "year_month",
"tooltip": ["paid_amount_pmpm"],
},
}
for s in series
]
option = {
"color": ["#06405C", "#FFCC05", "#66B1E2"],
"dataset": [{"id": "dataset_raw", "source": list_data}] + datasetWithFilters,
"title": {"text": "Paid Amount PMPM by Claim Type"},
"tooltip": {"order": "valueDesc", "trigger": "axis"},
"xAxis": {"type": "category", "nameLocation": "middle"},
"yAxis": {"name": "PMPM"},
"grid": {"right": 140},
"series": seriesList,
}
st_echarts(options=option, height="400px", key="chart")


year_month_values = sorted(list(set(data.year_months()["year_month"])))

year_values = sorted(list(set([x[:4] for x in year_month_values])))
## --------------------------------- ##
## --- --- ##
Expand Down Expand Up @@ -116,14 +62,14 @@ def claim_type_line_chart(df, animated=True):
month_list = sorted(list(set(pmpm_claim_type_data["year_month"])))
if animate:
while st.session_state["iteration"] < len(month_list):
claim_type_line_chart(pmpm_claim_type_data, True)
comp.claim_type_line_chart(pmpm_claim_type_data, True)
time.sleep(0.05)
st.session_state["iteration"] += 1

if st.session_state["iteration"] < len(month_list) and animate:
st.experimental_rerun()
else:
claim_type_line_chart(pmpm_claim_type_data, False)
comp.claim_type_line_chart(pmpm_claim_type_data, False)


## --------------------------------- ##
Expand Down Expand Up @@ -190,6 +136,7 @@ def claim_type_line_chart(df, animated=True):
test = test.loc[test.year != year_values[0]]
st.table(util.format_df(test))

comp.pop_grouped_bar(test)

## --------------------------------- ##
## Service Category 1
Expand Down

0 comments on commit 1dc5c7f

Please sign in to comment.