Spaces:
Runtime error
Runtime error
File size: 3,693 Bytes
bb3e610 |
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 |
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 |