每次在前端放大/缩小时,如何以编程方式检查Altair图表的放大/缩小功能

2024-09-26 18:05:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个Streamlight代码,其中我使用altair在前端显示图表。用户使该图表具有放大/缩小功能。我使用“.interactive()”来完成如下操作

chart = alt.Chart(embd_1).mark_circle(size=30).encode(
    x = 'dimention1:Q', 
    y = 'dimention2:Q',
    tooltip=['col1'] ,
    color=color).properties(width=600,height=600).add_selection(selected).interactive()

但是,当我在应用程序中放大/缩小时,我面临一个问题。更新要花很多时间。一旦我开始滚动鼠标,更新前端缩放的图表需要10秒以上的时间

我只是想知道这可能是因为streamlit正在运行altairs代码下面的所有代码,因为我不知道在使用放大/缩小功能时如何跳过或避免某些代码

因此,问题是如何通过编程定义用户是否正在放大/缩小

如下图所示:

if CheckZoom_SomeThingIDnotKnowYet == True:
   logic to execute code1
else:
   logic to execute code2

有关使用的数据和Altair代码的其他信息:

def altair_graph(embd_1):
    selected = alt.selection_single(on="click", empty="none")
    dom = ['Other IPs', 'Slected IP','Sel Dims']
    rng_clr = ['lightgrey', 'red','blue']

    color_point=alt.Color('color', scale=alt.Scale(domain=dom, range=rng_clr))

    color = alt.condition(selected, alt.value('red'), color_point,legend=None)

    chart = alt.Chart(embd_1).mark_circle(size=30).encode(
        x = 'dimention1:Q', 
        y = 'dimention2:Q',
        tooltip=['dimention1','dimention2'] ,
        color=color
    ).properties(width=600,height=600).add_selection(selected).interactive()

    return chart

上述函数的示例可按如下方式创建:

dimention1=np.random.rand(1,100000).squeeze()
dimention2=np.random.rand(1,100000).squeeze()
colr_values = ['Other Ids', 'Slected Id','Sel Dims']
color = np.random.choice(colr_values, 100000, p=[0.9, 0.05, 0.05])
sample = pd.DataFrame({'dimention1':dimention1,'dimention2':dimention2,'color':color})
altair_graph(sample)

如示例所示,我的实时数据超过150k个数据点

即使我在Streamlight之外执行了上述代码,这也需要花费大量时间来放大/缩小。请给我一个解决这个问题的方法


Tags: 数据代码npchart图表时间altinteractive
1条回答
网友
1楼 · 发布于 2024-09-26 18:05:14

Altair/VegaLite目前的表现不是很好,有那么多数据点(我认为它在某个地方会减慢20-40k左右)。您可以尝试alt.data_transformers.enable('data_server')以获得潜在的小改进,并在这里查看我的答案以获得更多详细信息,并在注释https://stackoverflow.com/a/67349827/2166823中进行讨论

相关问题 更多 >

    热门问题