test_ebc / custom /visual.py
piaspace's picture
[first]
bb3e610
import altair as alt
import pandas as pd
from typing import Tuple, Literal, Union
# Heatmap
def make_heatmap(input_df, input_y, input_x, input_color, input_color_theme):
heatmap = alt.Chart(input_df).mark_rect().encode(
y=alt.Y(f'{input_y}:O', axis=alt.Axis(title="Month", titleFontSize=18, titlePadding=15, titleFontWeight=900, labelAngle=0)),
x=alt.X(f'{input_x}:O', axis=alt.Axis(title="", titleFontSize=18, titlePadding=15, titleFontWeight=900, labelAngle=0)),
color=alt.Color(f'max({input_color}):Q',
legend=None,
scale=alt.Scale(scheme=input_color_theme)),
stroke=alt.value('black'),
strokeWidth=alt.value(0.25),
).properties(width=900
).configure_axis(
labelFontSize=12,
titleFontSize=12
)
# height=300
return heatmap
# Donut chart
def make_donut(
input_response: float,
input_text: str,
input_color: Literal['blue', 'green', 'orange', 'red']
) -> alt.LayerChart:
"""
Altair๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง€์ •๋œ ํผ์„ผํŠธ, ๋ ˆ์ด๋ธ”, ์ƒ‰์ƒ ์Šคํ‚ค๋งˆ๋กœ ๋„๋„› ์ฐจํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
ํ•จ์ˆ˜ ๊ตฌ์กฐ:
1. ์ž…๋ ฅ ์ƒ‰์ƒ์— ๋”ฐ๋ฅธ ์ƒ‰์ƒ ์Šคํ‚ค๋งˆ ์ •์˜
2. ๋‘ ๊ฐœ์˜ DataFrame ์ƒ์„ฑ:
- ํผ์„ผํŠธ ํ‘œ์‹œ๋ฅผ ์œ„ํ•œ ๋ฉ”์ธ ๋ฐ์ดํ„ฐ
- ์ „์ฒด ์›์„ ์œ„ํ•œ ๋ฐฐ๊ฒฝ ๋ฐ์ดํ„ฐ
3. ์„ธ ๊ฐœ์˜ ๋ ˆ์ด์–ด ์ƒ์„ฑ:
- ๋ฐฐ๊ฒฝ ์› (plot_bg)
- ํผ์„ผํŠธ ํ˜ธ (plot)
- ์ค‘์•™ ํ…์ŠคํŠธ ํ‘œ์‹œ
๋งค๊ฐœ๋ณ€์ˆ˜:
----------
input_response : float
ํ‘œ์‹œํ•  ํผ์„ผํŠธ ๊ฐ’ (0-100 ์‚ฌ์ด)
input_text : str
์ฐจํŠธ์— ํ‘œ์‹œํ•  ๋ ˆ์ด๋ธ” ํ…์ŠคํŠธ
input_color : str
์‚ฌ์šฉํ•  ์ƒ‰์ƒ ์Šคํ‚ค๋งˆ ('blue', 'green', 'orange', 'red' ์ค‘ ํ•˜๋‚˜)
๋ฐ˜ํ™˜๊ฐ’:
-------
alt.LayerChart
๋ฐฐ๊ฒฝ, ํผ์„ผํŠธ ํ˜ธ, ์ค‘์•™ ํ…์ŠคํŠธ๊ฐ€ ๊ฒฐํ•ฉ๋œ Altair ๋ ˆ์ด์–ด ์ฐจํŠธ
์‚ฌ์šฉ ์˜ˆ์‹œ:
---------
>>> chart = make_donut(75, "์™„๋ฃŒ", "blue")
>>> chart.save('donut.html')
"""
if input_color == 'blue':
chart_color = ['#29b5e8', '#155F7A']
if input_color == 'green':
chart_color = ['#27AE60', '#12783D']
if input_color == 'orange':
chart_color = ['#F39C12', '#875A12']
if input_color == 'red':
chart_color = ['#E74C3C', '#781F16']
source = pd.DataFrame({
"Topic": ['', input_text],
"% value": [100-input_response, input_response]
})
source_bg = pd.DataFrame({
"Topic": ['', input_text],
"% value": [100, 0]
})
plot = alt.Chart(source).mark_arc(innerRadius=45, cornerRadius=25).encode(
theta="% value",
color= alt.Color("Topic:N",
scale=alt.Scale(
#domain=['A', 'B'],
domain=[input_text, ''],
# range=['#29b5e8', '#155F7A']), # 31333F
range=chart_color),
legend=None),
).properties(width=130, height=130)
text = plot.mark_text(align='center', color="#29b5e8", font="Lato", fontSize=32, fontWeight=700, fontStyle="italic").encode(text=alt.value(f'{input_response} %'))
plot_bg = alt.Chart(source_bg).mark_arc(innerRadius=45, cornerRadius=20).encode(
theta="% value",
color= alt.Color("Topic:N",
scale=alt.Scale(
# domain=['A', 'B'],
domain=[input_text, ''],
range=chart_color), # 31333F
legend=None),
).properties(width=130, height=130)
return plot_bg + plot + text