File size: 1,803 Bytes
b966bc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Project      : Python.
# @File         : 666_😝_TEST
# @Time         : 2023/3/9 上午10:28
# @Author       : yuanjie
# @WeChat       : meutils
# @Software     : PyCharm
# @Description  : https://github.com/streamlit/example-app-commenting/blob/main/streamlit_app.py


"""
不断刷新数据图表
https://blog.csdn.net/qq_42761569/article/details/123418493
http://cw.hubwiz.com/card/c/streamlit-manual/1/6/13/
"""
import altair as alt


def get_chart(data):
    # 鼠标悬停
    hover = alt.selection_single(
        fields=["date"],
        nearest=True,
        on="mouseover",
        empty="none",
    )

    lines = (
        alt.Chart(data, title="Evolution of stock prices")
        .mark_line()
        .encode(
            x="date",
            y="price",
            color="symbol",
            strokeDash="symbol",
        )
    )

    # Draw points on the line, and highlight based on selection
    points = lines.transform_filter(hover).mark_circle(size=65)

    # Draw a rule at the location of the selection
    tooltips = (
        alt.Chart(data)
        .mark_rule()
        .encode(
            x="date",
            y="price",
            opacity=alt.condition(hover, alt.value(0.3), alt.value(0)),
            tooltip=[
                alt.Tooltip("date", title="Date"),
                alt.Tooltip("price", title="Price (USD)"),
            ],
        )
        .add_selection(hover)
    )

    return (lines + points + tooltips).interactive()


# source = data.stocks()
# all_symbols = source.symbol.unique()
# symbols = st.multiselect("Choose stocks to visualize", all_symbols, all_symbols[:3])
#
#
# source = source[source.symbol.isin(symbols)]
# chart = get_chart(source)
# st.altair_chart(chart, use_container_width=True)