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