Spaces:
Runtime error
Runtime error
| 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 |