zijian2022 commited on
Commit
895ca8a
·
verified ·
1 Parent(s): 8220774

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -65
app.py CHANGED
@@ -332,22 +332,7 @@ app.layout = html.Div([
332
  }
333
  ),
334
 
335
- # 添加客户端JavaScript优化
336
- html.Script("""
337
- // 优化hover响应速度
338
- window.addEventListener('load', function() {
339
- // 减少hover事件的触发频率
340
- let hoverTimeout;
341
- document.addEventListener('mouseover', function(e) {
342
- if (e.target.closest('.js-plotly-plot')) {
343
- clearTimeout(hoverTimeout);
344
- hoverTimeout = setTimeout(function() {
345
- // 延迟处理hover事件,减少网络请求
346
- }, 50);
347
- }
348
- });
349
- });
350
- """)
351
  ], style={
352
  "fontFamily": "'Segoe UI', Tahoma, Geneva, Verdana, sans-serif",
353
  "backgroundColor": "#f5f7fa",
@@ -506,20 +491,9 @@ def get_shadow_info(joint_name, action_df, delta_t, time_for_plot):
506
  })
507
  return shadows
508
 
509
- def is_hover_in_shadow(hover_time, shadows):
510
- for shadow in shadows:
511
- if shadow['start_time'] <= hover_time <= shadow['end_time']:
512
- return True
513
- return False
514
 
515
- def find_shadows_in_range(shadows, start_time, end_time):
516
- shadows_in_range = []
517
- for shadow in shadows:
518
- if not (shadow['end_time'] < start_time or shadow['start_time'] > end_time):
519
- shadows_in_range.append(shadow)
520
- return shadows_in_range
521
 
522
- def generate_joint_graph(joint_name, idx, action_df, delta_t, time_for_plot, all_shadows, highlighted_shadows=None):
523
  angles = action_df[joint_name].values
524
  velocity = np.diff(angles) / delta_t
525
  smoothed_velocity = gaussian_filter1d(velocity, sigma=1)
@@ -527,15 +501,6 @@ def generate_joint_graph(joint_name, idx, action_df, delta_t, time_for_plot, all
527
  shapes = []
528
  current_shadows = all_shadows[joint_name]
529
  for shadow in current_shadows:
530
- is_highlighted = False
531
- if highlighted_shadows:
532
- for h_shadow in highlighted_shadows:
533
- if (shadow['start_time'] == h_shadow['start_time'] and
534
- shadow['end_time'] == h_shadow['end_time']):
535
- is_highlighted = True
536
- break
537
- color = "#3b82f6" if is_highlighted else "#ef4444" # Blue for highlighted, red for normal
538
- opacity = 0.7 if is_highlighted else 0.4
539
  shapes.append({
540
  "type": "rect",
541
  "xref": "x",
@@ -544,8 +509,8 @@ def generate_joint_graph(joint_name, idx, action_df, delta_t, time_for_plot, all
544
  "x1": shadow['end_time'],
545
  "y0": 0,
546
  "y1": 1,
547
- "fillcolor": color,
548
- "opacity": opacity,
549
  "line": {"width": 0}
550
  })
551
  return {
@@ -593,13 +558,13 @@ def generate_joint_graph(joint_name, idx, action_df, delta_t, time_for_plot, all
593
  )
594
  }
595
 
596
- # ------------------ 联动高亮回调 ------------------
597
  @app.callback(
598
  [Output(f"graph-{i}", "figure") for i in range(6)],
599
- [Input("store-data", "data")] + [Input(f"graph-{i}", "hoverData") for i in range(6)],
600
  prevent_initial_call=True
601
  )
602
- def update_all_graphs(data, *hover_datas):
603
  if not data or "data_df" not in data or len(data["data_df"]) == 0:
604
  return [no_update] * 6
605
 
@@ -613,29 +578,7 @@ def update_all_graphs(data, *hover_datas):
613
  for joint in columns:
614
  all_shadows[joint] = get_shadow_info(joint, action_df, delta_t, time_for_plot)
615
 
616
- # 查找是否有任何一个hover落在阴影内
617
- for idx, hover_data in enumerate(hover_datas):
618
- if hover_data and "points" in hover_data and len(hover_data["points"]) > 0:
619
- try:
620
- hover_time = float(hover_data["points"][0]["x"])
621
- triggered_joint = columns[idx]
622
-
623
- if is_hover_in_shadow(hover_time, all_shadows[triggered_joint]):
624
- hover_idx = np.searchsorted(time_for_plot, hover_time)
625
- start_idx = max(0, hover_idx - 20)
626
- end_idx = min(len(time_for_plot) - 1, hover_idx + 20)
627
- start_time = time_for_plot[start_idx]
628
- end_time = time_for_plot[end_idx]
629
- figures = []
630
- for i, joint in enumerate(columns):
631
- shadows_in_range = find_shadows_in_range(all_shadows[joint], start_time, end_time)
632
- fig = generate_joint_graph(joint, i, action_df, delta_t, time_for_plot, all_shadows, shadows_in_range)
633
- figures.append(fig)
634
- return figures
635
- except Exception as e:
636
- print(f"Error processing hover data: {e}")
637
-
638
- # 没有hover或不在阴影内,全部正常显示
639
  return [
640
  generate_joint_graph(joint, i, action_df, delta_t, time_for_plot, all_shadows)
641
  for i, joint in enumerate(columns)
 
332
  }
333
  ),
334
 
335
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336
  ], style={
337
  "fontFamily": "'Segoe UI', Tahoma, Geneva, Verdana, sans-serif",
338
  "backgroundColor": "#f5f7fa",
 
491
  })
492
  return shadows
493
 
 
 
 
 
 
494
 
 
 
 
 
 
 
495
 
496
+ def generate_joint_graph(joint_name, idx, action_df, delta_t, time_for_plot, all_shadows):
497
  angles = action_df[joint_name].values
498
  velocity = np.diff(angles) / delta_t
499
  smoothed_velocity = gaussian_filter1d(velocity, sigma=1)
 
501
  shapes = []
502
  current_shadows = all_shadows[joint_name]
503
  for shadow in current_shadows:
 
 
 
 
 
 
 
 
 
504
  shapes.append({
505
  "type": "rect",
506
  "xref": "x",
 
509
  "x1": shadow['end_time'],
510
  "y0": 0,
511
  "y1": 1,
512
+ "fillcolor": "#ef4444", # 固定红色
513
+ "opacity": 0.4,
514
  "line": {"width": 0}
515
  })
516
  return {
 
558
  )
559
  }
560
 
561
+ # ------------------ 图表更新回调 ------------------
562
  @app.callback(
563
  [Output(f"graph-{i}", "figure") for i in range(6)],
564
+ [Input("store-data", "data")],
565
  prevent_initial_call=True
566
  )
567
+ def update_all_graphs(data):
568
  if not data or "data_df" not in data or len(data["data_df"]) == 0:
569
  return [no_update] * 6
570
 
 
578
  for joint in columns:
579
  all_shadows[joint] = get_shadow_info(joint, action_df, delta_t, time_for_plot)
580
 
581
+ # 生成所有图表,无高亮逻辑
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
582
  return [
583
  generate_joint_graph(joint, i, action_df, delta_t, time_for_plot, all_shadows)
584
  for i, joint in enumerate(columns)