edgeStyle.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // 边样式统一配置
  2. // 所有边类型及颜色
  3. export const edgeTypeColors = {
  4. '属于': '#9b59b6',
  5. '包含': '#3498db',
  6. '标签共现': '#2ecc71',
  7. '分类共现': '#f39c12',
  8. '匹配': '#e94560',
  9. '推导': '#00bcd4', // 青色 - 推导关系
  10. '组成': '#8bc34a', // 浅绿色 - 组合成员
  11. '支撑': '#ff9800', // 橙色 - 支撑关系
  12. '关联': '#9c27b0' // 紫色 - 关联关系
  13. }
  14. // 获取边样式(统一入口)
  15. // edge: { type, score, ... }
  16. export function getEdgeStyle(edge) {
  17. const type = edge.type || ''
  18. const score = edge.score
  19. const color = edgeTypeColors[type] || '#666'
  20. // 匹配边:>=0.8 实线,否则虚线
  21. let strokeDasharray = 'none'
  22. if (type === '匹配') {
  23. strokeDasharray = (score >= 0.8) ? 'none' : '4,2'
  24. }
  25. // 推导边:使用箭头,根据分数调整透明度
  26. // 组成边:实线样式(和推导边一样)
  27. // 不同边类型的透明度计算
  28. let opacity = 0.3
  29. if (type === '匹配') {
  30. opacity = Math.max(0.3, score * 0.7)
  31. } else if (type === '推导') {
  32. opacity = Math.max(0.4, score * 0.8)
  33. } else if (type === '组成') {
  34. opacity = 0.6
  35. } else if (type === '支撑') {
  36. opacity = 0.7
  37. } else if (type === '关联') {
  38. opacity = 0.6
  39. }
  40. // 关联边使用虚线
  41. if (type === '关联') {
  42. strokeDasharray = '4,2'
  43. }
  44. // 箭头配置
  45. const hasArrow = ['推导', '支撑'].includes(type)
  46. const arrowSize = type === '推导' ? 8 : 6
  47. const arrowRefX = type === '推导' ? 20 : 15
  48. return {
  49. color,
  50. strokeWidth: type === '推导' ? 2 : 1.5,
  51. strokeDasharray,
  52. opacity,
  53. hasArrow,
  54. arrowSize,
  55. arrowRefX,
  56. markerId: hasArrow ? `arrow-${type}` : null,
  57. scoreText: score !== undefined ? score.toFixed(2) : ''
  58. }
  59. }
  60. // 创建箭头标记定义(供 SVG defs 使用)
  61. export function createArrowMarkers(defs) {
  62. const arrowTypes = ['推导', '支撑']
  63. arrowTypes.forEach(type => {
  64. const style = getEdgeStyle({ type, score: 1 })
  65. // 正常箭头
  66. defs.append('marker')
  67. .attr('id', `arrow-${type}`)
  68. .attr('viewBox', '0 -5 10 10')
  69. .attr('refX', style.arrowRefX)
  70. .attr('refY', 0)
  71. .attr('markerWidth', style.arrowSize)
  72. .attr('markerHeight', style.arrowSize)
  73. .attr('orient', 'auto')
  74. .append('path')
  75. .attr('d', 'M0,-5L10,0L0,5')
  76. .attr('fill', style.color)
  77. // 置灰箭头(用于 hover 时其他边)
  78. defs.append('marker')
  79. .attr('id', `arrow-${type}-dimmed`)
  80. .attr('viewBox', '0 -5 10 10')
  81. .attr('refX', style.arrowRefX)
  82. .attr('refY', 0)
  83. .attr('markerWidth', style.arrowSize)
  84. .attr('markerHeight', style.arrowSize)
  85. .attr('orient', 'auto')
  86. .append('path')
  87. .attr('d', 'M0,-5L10,0L0,5')
  88. .attr('fill', style.color)
  89. .attr('fill-opacity', 0.15)
  90. })
  91. }