* Extract legend position from chartSpace > chart > legend > legendPos.
(chartNode: SafeXmlNode, ctx: RenderContext)
| 790 | * Extract legend position from chartSpace > chart > legend > legendPos. |
| 791 | */ |
| 792 | function extractLegendInfo(chartNode: SafeXmlNode, ctx: RenderContext): LegendInfo | undefined { |
| 793 | const legend = chartNode.child('legend') |
| 794 | if (!legend.exists()) return undefined |
| 795 | |
| 796 | const legendPos = legend.child('legendPos') |
| 797 | // OOXML default legend position is 'r' (right) per the spec, not 'b' |
| 798 | const posVal = legendPos.exists() ? legendPos.attr('val') || 'r' : 'r' |
| 799 | |
| 800 | const overlay = legend.child('overlay').attr('val') === '1' |
| 801 | |
| 802 | // Map OOXML positions to ECharts; keep legend inside and below chart title (avoid overlap on slide 4, 6, etc.) |
| 803 | const base = { confine: true as const } |
| 804 | const topBelowTitle = '14%' // leave room for chart title so legend does not overlap |
| 805 | let option: echarts.EChartsOption['legend'] |
| 806 | switch (posVal) { |
| 807 | case 'b': |
| 808 | option = { ...base, bottom: '5%', orient: 'horizontal' as const } |
| 809 | break |
| 810 | case 't': |
| 811 | option = { ...base, top: topBelowTitle, orient: 'horizontal' as const } |
| 812 | break |
| 813 | case 'l': |
| 814 | option = { ...base, left: '2%', top: '44%', orient: 'vertical' as const } |
| 815 | break |
| 816 | case 'r': |
| 817 | option = { ...base, right: '2%', top: '44%', orient: 'vertical' as const } |
| 818 | break |
| 819 | case 'tr': |
| 820 | option = { ...base, top: topBelowTitle, right: '2%', orient: 'vertical' as const } |
| 821 | break |
| 822 | default: |
| 823 | option = { ...base, right: '2%', top: '44%', orient: 'vertical' as const } |
| 824 | break |
| 825 | } |
| 826 | return { |
| 827 | option, |
| 828 | overlay, |
| 829 | textStyle: (() => { |
| 830 | const s = extractTxPrStyle(legend, ctx) |
| 831 | if (!s) return undefined |
| 832 | return { |
| 833 | ...(s.color ? { color: s.color } : {}), |
| 834 | ...(s.fontSize !== undefined ? { fontSize: s.fontSize } : {}), |
| 835 | ...(s.bold === true ? { fontWeight: 'bold' } : {}), |
| 836 | ...(s.fontFamily ? { fontFamily: s.fontFamily } : {}), |
| 837 | } |
| 838 | })(), |
| 839 | manualLayout: extractLegendManualLayout(legend), |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | /** |
| 844 | * Parse legend/layout/manualLayout to ECharts legend position/size override. |
no test coverage detected