File size: 8,493 Bytes
9183c57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import streamlit as st
from util import developer_info_static
from src.plot import list_all, distribution_histogram, distribution_boxplot, count_Y, box_plot, violin_plot, strip_plot, density_plot ,multi_plot_heatmap, multi_plot_scatter, multi_plot_line, word_cloud_plot, world_map, scatter_3d

def display_word_cloud(text):
    _, word_cloud_col, _ = st.columns([1, 3, 1])
    with word_cloud_col:
        word_fig = word_cloud_plot(text)
        if word_fig == -1:
            st.error('Data not supported')
        else:
            st.pyplot(word_cloud_plot(text))

def data_visualization(DF):
    st.divider()
    st.subheader('Data Visualization')
    attributes = DF.columns.tolist()

    # Three tabs for three kinds of visualization
    single_tab, multiple_tab, advanced_tab = st.tabs(['Single Attribute Visualization', 'Multiple Attributes Visualization', 'Advanced Visualization'])
    
    # Single attribute visualization
    with single_tab:
        _, col_mid, _ = st.columns([1, 5, 1])
        with col_mid:
            plot_area = st.empty()
            
        col1, col2 = st.columns(2)
        with col1:
            att = st.selectbox(
                label = 'Select an attribute to visualize:',
                options = attributes,
                index = len(attributes)-1
            )
            st.write(f'Attribute selected: :green[{att}]')
            
        with col2:
            plot_types = ['Donut chart', 'Violin plot', 'Distribution histogram', 'Boxplot', 'Density plot', 'Strip plot', 'Distribution boxplot']
            plot_type = st.selectbox(
                key = 'plot_type1',
                label = 'Select a plot type:',
                options = plot_types,
                index = 0
            )
            st.write(f'Plot type selected: :green[{plot_type}]')

        if plot_type == 'Distribution histogram':
            fig = distribution_histogram(DF, att)
            plot_area.pyplot(fig)
        elif plot_type == 'Distribution boxplot':
            fig = distribution_boxplot(DF, att)
            if fig == -1:
                plot_area.error('The attribute is not numeric')
            else:
                plot_area.pyplot(fig)
        elif plot_type == 'Donut chart':
            fig = count_Y(DF, att)
            plot_area.plotly_chart(fig)
        elif plot_type == 'Boxplot':
            fig = box_plot(DF, [att])
            plot_area.plotly_chart(fig)
        elif plot_type == 'Violin plot':
            fig = violin_plot(DF, [att])
            plot_area.plotly_chart(fig)
        elif plot_type == 'Strip plot':
            fig = strip_plot(DF, [att])
            plot_area.plotly_chart(fig)
        elif plot_type == 'Density plot':
            fig = density_plot(DF, att)
            plot_area.plotly_chart(fig)

    # Multiple attribute visualization
    with multiple_tab:
        col1, col2 = st.columns([6, 4])
        with col1:
            options = st.multiselect(
                label = 'Select multiple attributes to visualize:',
                options = attributes,
                default = []
            )
        with col2:
            plot_types = ["Violin plot", "Boxplot", "Heatmap", "Strip plot", "Line plot", "Scatter plot"]
            plot_type = st.selectbox(
                key = 'plot_type2',
                label = 'Select a plot type:',
                options = plot_types,
                index = 0
            )
        _, col_mid, _ = st.columns([1, 5, 1])
        with col_mid:
            plot_area = st.empty()

        if options:
            if plot_type == 'Scatter plot':
                fig = multi_plot_scatter(DF, options)
                if fig == -1:
                    plot_area.error('Scatter plot requires two attributes')
                else:
                    plot_area.pyplot(fig)
            elif plot_type == 'Heatmap':
                fig = multi_plot_heatmap(DF, options)
                if fig == -1:
                    plot_area.error('The attributes are not numeric')
                else:
                    plot_area.pyplot(fig)
            elif plot_type == 'Boxplot':
                fig = box_plot(DF, options)
                if fig == -1:
                    plot_area.error('The attributes are not numeric')
                else:
                    plot_area.plotly_chart(fig)
            elif plot_type == 'Violin plot':
                fig = violin_plot(DF, options)
                if fig == -1:
                    plot_area.error('The attributes are not numeric')
                else:
                    plot_area.plotly_chart(fig)
            elif plot_type == 'Strip plot':
                fig = strip_plot(DF, options)
                if fig == -1:
                    plot_area.error('The attributes are not numeric')
                else:
                    plot_area.plotly_chart(fig)
            elif plot_type == 'Line plot':
                fig = multi_plot_line(DF, options)
                if fig == -1:
                    plot_area.error('The attributes are not numeric')
                elif fig == -2:
                    plot_area.error('Line plot requires two attributes')
                else:
                    plot_area.pyplot(fig)

    # Advanced visualization
    with advanced_tab:
        st.subheader("3D Scatter Plot")
        column_1, column_2, column_3 = st.columns(3)
        with column_1:
            x = st.selectbox(
                key = 'x',
                label = 'Select the x attribute:',
                options = attributes,
                index = 0
            )
        with column_2:
            y = st.selectbox(
                key = 'y',
                label = 'Select the y attribute:',
                options = attributes,
                index = 1 if len(attributes) > 1 else 0
            )
        with column_3:
            z = st.selectbox(
                key = 'z',
                label = 'Select the z attribute:',
                options = attributes,
                index = 2 if len(attributes) > 2 else 0
            )
        if st.button('Generate 3D Plot'):
            _, fig_3d_col, _ = st.columns([1, 3, 1])
            with fig_3d_col:
                fig_3d_1 = scatter_3d(DF, x, y, z)
                if fig_3d_1 == -1:
                    st.error('Data not supported')
                else:
                    st.plotly_chart(fig_3d_1)
        st.divider()

        st.subheader('World Cloud')
        upload_txt_checkbox = st.checkbox('Upload a new text file instead')
        if upload_txt_checkbox:
            uploaded_txt = st.file_uploader("Choose a text file", accept_multiple_files=False, type="txt")
            if uploaded_txt: 
                text = uploaded_txt.read().decode("utf-8")
                display_word_cloud(text)
        else:
            text_attr = st.selectbox(
                label = 'Select the text attribute:',
                options = attributes,
                index = 0)
            if st.button('Generate Word Cloud'):
                text = DF[text_attr].astype(str).str.cat(sep=' ')
                display_word_cloud(text)
        st.divider()

        st.subheader('World Heat Map')
        col_1, col_2 = st.columns(2)
        with col_1:
            country_col = st.selectbox(
                key = 'country_col',
                label = 'Select the country attribute:',
                options = attributes,
                index = 0
            )
        with col_2:
            heat_attribute = st.selectbox(
                key = 'heat_attribute',
                label = 'Select the attribute to display in heat map:',
                options = attributes,
                index = len(attributes) - 1
            )
        if st.button("Show Heatmap"):
            _, map_col, _ = st.columns([1, 3, 1])
            with map_col:
                world_fig = world_map(DF, country_col, heat_attribute)
                if world_fig == -1:
                    st.error('Data not supported')
                else:
                    st.plotly_chart(world_fig)
    st.divider()

    # Data Overview
    st.subheader('Data Overview')
    if 'data_origin' not in st.session_state:
        st.session_state.data_origin = DF
    st.dataframe(st.session_state.data_origin.describe(), width=1200)
    if 'overall_plot' not in st.session_state:
        st.session_state.overall_plot = list_all(st.session_state.data_origin)
    st.pyplot(st.session_state.overall_plot)

    st.divider()
    developer_info_static()