Flask + SQLite 后端,免构建 React + ECharts 前端,含合成样例数据。 测试参考数据(FOR-TEST-ONLY)与运行时数据库不入库。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
63 lines
3.1 KiB
JavaScript
63 lines
3.1 KiB
JavaScript
/*
|
||
* ECharts 图表组件。
|
||
* html`<${EChart} option=${{xAxis: ..., series: [...]}} height=${320}/>`
|
||
* - 坐标轴、提示框、文字颜色由下面两套 Apple 风格主题统一提供,页面里只写数据相关的配置。
|
||
* - 跟随系统深浅色:主题变化时重建图表。
|
||
* - echarts.js 只在打开带图表的页面时才下载。
|
||
*/
|
||
import echarts from 'echarts';
|
||
import {html, useEffect, useGlassPolicy, useRef} from 'vendor';
|
||
import {PALETTE} from '../format.js';
|
||
|
||
// 柱子默认用中性灰(systemGray4 / 深色 systemGray3),需要强调的那一项由页面用 accentMax() 标成蓝色
|
||
function appleTheme({label, secondary, grid, line, tooltip, bar}) {
|
||
const axis = {
|
||
axisLine: {lineStyle: {color: line}}, axisTick: {show: false},
|
||
axisLabel: {color: secondary}, splitLine: {lineStyle: {color: grid}}, nameTextStyle: {color: secondary},
|
||
};
|
||
return {
|
||
color: PALETTE,
|
||
backgroundColor: 'transparent',
|
||
textStyle: {fontFamily: 'inherit', color: secondary},
|
||
categoryAxis: axis, valueAxis: axis,
|
||
legend: {textStyle: {color: secondary}},
|
||
tooltip: {backgroundColor: tooltip, borderWidth: 0, textStyle: {color: label},
|
||
extraCssText: 'border-radius:12px;box-shadow:0 8px 24px rgba(0,0,0,.16)'},
|
||
// 柱顶数字用次要文字色、去掉描边(默认描边在深色模式下会糊成一团)
|
||
bar: {barMaxWidth: 28, itemStyle: {borderRadius: 6, color: bar},
|
||
label: {color: secondary, fontSize: 11, textBorderWidth: 0}},
|
||
pie: {label: {color: secondary, textBorderWidth: 0}},
|
||
line: {smooth: true, symbolSize: 6, lineStyle: {width: 2.5}},
|
||
};
|
||
}
|
||
|
||
// 颜色取自 Apple 人机界面指南的系统色(label / secondaryLabel / separator / systemGray)
|
||
echarts.registerTheme('light', appleTheme({label: '#1d1d1f', secondary: '#6e6e73', grid: 'rgba(0,0,0,.06)',
|
||
line: '#d2d2d7', tooltip: 'rgba(255,255,255,.96)', bar: '#c7c7cc'}));
|
||
echarts.registerTheme('dark', appleTheme({label: '#f5f5f7', secondary: '#98989d', grid: 'rgba(255,255,255,.08)',
|
||
line: '#424245', tooltip: 'rgba(44,44,46,.96)', bar: '#48484a'}));
|
||
|
||
const GRID = {left: 12, right: 18, top: 30, bottom: 10, containLabel: true};
|
||
|
||
export function EChart({option, height = 320}) {
|
||
const box = useRef(null);
|
||
const chart = useRef(null);
|
||
const {resolvedTheme, reduceMotion} = useGlassPolicy();
|
||
|
||
// 创建 / 主题切换时重建;容器尺寸变化时自适应
|
||
useEffect(() => {
|
||
chart.current = echarts.init(box.current, resolvedTheme);
|
||
const ro = new ResizeObserver(() => chart.current.resize());
|
||
ro.observe(box.current);
|
||
return () => { ro.disconnect(); chart.current.dispose(); };
|
||
}, [resolvedTheme]);
|
||
|
||
// 页面每次渲染都会传入新的 option 对象;内容没变时跳过重绘(例如打开弹窗不会让图表重播动画)
|
||
const key = JSON.stringify(option);
|
||
useEffect(() => {
|
||
chart.current.setOption({animation: !reduceMotion, grid: GRID, ...option}, true);
|
||
}, [key, resolvedTheme, reduceMotion]);
|
||
|
||
return html`<div ref=${box} style=${{height}}></div>`;
|
||
}
|