Spaces:
Running
Running
feat(chart): GPT-6 服务端组件 StockChart 渲染器——插件 chatgpt-dil 整包透传的股票行情出图(备忘录120)——①插件侧把 StockChart(服务端组件,componentResults[rid].state 21参数)按官方组件源码合成同构 DIL 树(头部/档位条/指标表走既有渲染层), <chart> 的 data/series/xAxis/yAxis 逐字透传到本构建器, payload 带 __xwxKind:'stock-chart' 标记; ②本文件内 buildChatGPTChartHtml 入口最前分流到新增 buildChatGPTStockChartHtml(必须在旧位置签名兼容之前判, 否则无 chartType 的 payload 被 legacy 归一化误吞—冒烟实证) → server.js 与桌面 Rust 调用点零改动, 桌面 pdf-render/chatgpt-chart-html.js 由 build-pdf-render-assets.mjs 逐字节同步(MD5 150de6f4); ③渲染契约按三重取证: Recharts 3.10.1 AreaChart, series[].color 语义色随涨跌方向(CSSOM 实测 --green-500:#00a240/--red-500:#e02e2a), Area strokeWidth2 + fill 线性渐变(stopOpacity 0.45→0) + fillOpacity0.6, xAxis ticks+labels 映射+preserveStartEnd+tickMargin8+padding.right10, yAxis 右轴 width40+domain/ticks+axisLine false, 水平虚线网格 #ccc '3 3', dot/activeDot false, 桌面档高340; ④验证: verify-stock-render.mjs 真机 payload(a8a911ef 的 5D 档72点) puppeteer 9/9 断言(线色/渐变两档/fill-opacity/网格5条/y刻度220-240/x刻度来自labels映射), pdf-test 容器重建后 /api/render_charts 出图; 端到端 29 页 PDF 逐页比对股票卡与网页一致. 生产 space/main 未推送(用户指示:会影响生产环境; 待推送后线上 StockChart 才出图, 未推期间该 payload 走旧入口返回 null 由上层回退 MD 表格)
Browse files- lib/chatgpt-chart-html.js +138 -1
lib/chatgpt-chart-html.js
CHANGED
|
@@ -63,6 +63,11 @@ const OFFICIAL = {
|
|
| 63 |
|
| 64 |
// ── 页内渲染脚本(与 Node 侧共享同一份契约常量,经 JSON 注入) ──
|
| 65 |
function buildChatGPTChartHtml(spec, opts) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
// 兼容旧的位置参数签名(桌面端 Rust 未重编窗口期,2026-10-13 部署约束)
|
| 67 |
if (typeof spec === 'string' || (spec && spec.chartType === undefined && arguments.length > 1)) {
|
| 68 |
var a = arguments;
|
|
@@ -398,4 +403,136 @@ window._widgetError = null;
|
|
| 398 |
<\/script></body></html>`;
|
| 399 |
}
|
| 400 |
|
| 401 |
-
module.exports = { buildChatGPTChartHtml, OFFICIAL_CONTRACT: OFFICIAL };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
// ── 页内渲染脚本(与 Node 侧共享同一份契约常量,经 JSON 注入) ──
|
| 65 |
function buildChatGPTChartHtml(spec, opts) {
|
| 66 |
+
// StockChart(服务端组件,插件 chatgpt-dil.js 整包下传):同引擎同壳,
|
| 67 |
+
// 渲染契约不同(area+渐变、右轴、时间刻度映射),走专用构建器。
|
| 68 |
+
// 必须在旧位置签名兼容**之前**判——stock payload 没有 chartType,
|
| 69 |
+
// 会被下面的 legacy 归一化误吞(冒烟实证)。
|
| 70 |
+
if (spec && spec.__xwxKind === 'stock-chart') return buildChatGPTStockChartHtml(spec, opts);
|
| 71 |
// 兼容旧的位置参数签名(桌面端 Rust 未重编窗口期,2026-10-13 部署约束)
|
| 72 |
if (typeof spec === 'string' || (spec && spec.chartType === undefined && arguments.length > 1)) {
|
| 73 |
var a = arguments;
|
|
|
|
| 403 |
<\/script></body></html>`;
|
| 404 |
}
|
| 405 |
|
| 406 |
+
module.exports = { buildChatGPTChartHtml, buildChatGPTStockChartHtml, OFFICIAL_CONTRACT: OFFICIAL };
|
| 407 |
+
|
| 408 |
+
/**
|
| 409 |
+
* buildChatGPTStockChartHtml — ChatGPT StockChart(服务端组件)→ 完整渲染 HTML
|
| 410 |
+
*
|
| 411 |
+
* 契约取证(2026-10-14,三重证据):
|
| 412 |
+
* · 官方组件源码 chunk 599788(StockChart.dil.tsx 编译产物):官方 <chart> 元素
|
| 413 |
+
* 收到 {width:'100%', height:isDesktop?340:240, data, series, xAxis, yAxis,
|
| 414 |
+
* showLegend:false, showYAxis:true}——本模块只画**图区**,头部/档位条/指标表
|
| 415 |
+
* 由插件 DIL 层的原语树渲染(官方组件本身就是 DIL 原语写的)。
|
| 416 |
+
* · 官方 DOM 真值(真机 temp/test/chatgpt/2026-10-14-dil-r14/out/official-dom3.json):
|
| 417 |
+
* Recharts AreaChart,svg 768×340;area path fill=url(#gradient) fill-opacity 0.6,
|
| 418 |
+
* gradient 两档 stop-opacity 0.45→0;line stroke=var(--green-500|--red-500)
|
| 419 |
+
* (语义色随涨跌方向,CSSOM 实测 green #00a240 / red #e02e2a)strokeWidth 2;
|
| 420 |
+
* clipPath 5,5,708×300 → margin 5 + 右轴宽 40 + 底部刻度带 35。
|
| 421 |
+
* · series[0] = {type:'area', dataKey:'close', color:'red'|'green', curveType:'linear'};
|
| 422 |
+
* xAxis = {dataKey:'timestamp', labels{ts→label}, tickMargin:8, padding:{right:10},
|
| 423 |
+
* ticks:[…], interval:'preserveStartEnd'};yAxis = {domain, ticks, width:40,
|
| 424 |
+
* axisLine:false, orientation:'right'}。
|
| 425 |
+
*/
|
| 426 |
+
function buildChatGPTStockChartHtml(spec, opts) {
|
| 427 |
+
opts = opts || {};
|
| 428 |
+
const C = spec.chart || {};
|
| 429 |
+
if (!Array.isArray(C.data) || !C.data.length) return null;
|
| 430 |
+
const engine = opts.engine === 'v2' ? 'v2' : 'v3';
|
| 431 |
+
const version = opts.version || (engine === 'v2' ? '2.12.7' : '3.10.1');
|
| 432 |
+
const width = opts.width || 719;
|
| 433 |
+
const height = opts.height || spec.height || 340;
|
| 434 |
+
const json = (v) => JSON.stringify(v === undefined ? null : v).replace(/<\/script/gi, '<\\/script');
|
| 435 |
+
return `<!DOCTYPE html>
|
| 436 |
+
<html><head><meta charset="UTF-8">
|
| 437 |
+
<style>
|
| 438 |
+
body { margin:0; padding:8px; background:#fff; color:#0d0d0d;
|
| 439 |
+
font-family:-apple-system-body, ui-sans-serif, -apple-system, system-ui, 'Segoe UI', Helvetica, 'Apple Color Emoji', Arial, sans-serif, 'Segoe UI Emoji', 'Segoe UI Symbol'; }
|
| 440 |
+
#widget-container { width: 735px; }
|
| 441 |
+
/* 官方刻度文字:12px / rgb(93,93,93) / 400(与 charts_widget_v2 同源覆盖) */
|
| 442 |
+
.recharts-wrapper text { font-size:12px; fill:#5d5d5d; font-weight:400;
|
| 443 |
+
font-family:-apple-system-body, ui-sans-serif, -apple-system, system-ui, 'Segoe UI', Helvetica, Arial, sans-serif; }
|
| 444 |
+
</style>
|
| 445 |
+
<script src="https://unpkg.com/react@18.2.0/umd/react.production.min.js"><\/script>
|
| 446 |
+
<script src="https://unpkg.com/react-dom@18.2.0/umd/react-dom.production.min.js"><\/script>
|
| 447 |
+
<script src="https://unpkg.com/react-is@18.2.0/umd/react-is.production.min.js"><\/script>
|
| 448 |
+
<script src="https://unpkg.com/prop-types@15.8.1/prop-types.min.js"><\/script>
|
| 449 |
+
<script src="https://unpkg.com/recharts@${version}/umd/Recharts.js"><\/script>
|
| 450 |
+
</head><body>
|
| 451 |
+
<div id="widget-container"></div>
|
| 452 |
+
<script>
|
| 453 |
+
window._widgetRendered = false;
|
| 454 |
+
window._widgetError = null;
|
| 455 |
+
(function () {
|
| 456 |
+
var SPEC = ${json(spec)};
|
| 457 |
+
var BOX = ${json({ width, height })};
|
| 458 |
+
try {
|
| 459 |
+
var rc = Recharts, h = React.createElement;
|
| 460 |
+
var C = SPEC.chart || {};
|
| 461 |
+
var data = C.data || [];
|
| 462 |
+
// 官方语义色(CSSOM 实测 --green-500:#00a240 / --red-500:#e02e2a);
|
| 463 |
+
// 其余取值按字面量透传(模型若直接发 hex 则原样生效)。
|
| 464 |
+
var TOKEN = { green: '#00a240', red: '#e02e2a' };
|
| 465 |
+
var colorOf = function (c) { return TOKEN[c] || c || '#00a240'; };
|
| 466 |
+
var xa = C.xAxis && typeof C.xAxis === 'object' ? C.xAxis : {};
|
| 467 |
+
var ya = C.yAxis && typeof C.yAxis === 'object' ? C.yAxis : {};
|
| 468 |
+
var series = (C.series || []).length ? C.series : [{ dataKey: 'close', type: 'area', color: 'green', curveType: 'linear' }];
|
| 469 |
+
var xLabels = xa.labels || {};
|
| 470 |
+
|
| 471 |
+
var children = [h(rc.CartesianGrid, {
|
| 472 |
+
key: 'grid', horizontal: true, vertical: false, stroke: '#ccc', strokeDasharray: '3 3',
|
| 473 |
+
})];
|
| 474 |
+
children.push(h(rc.XAxis, {
|
| 475 |
+
key: 'x',
|
| 476 |
+
dataKey: xa.dataKey || 'timestamp',
|
| 477 |
+
type: 'category',
|
| 478 |
+
scale: 'auto',
|
| 479 |
+
ticks: Array.isArray(xa.ticks) ? xa.ticks : undefined,
|
| 480 |
+
interval: xa.interval || 'preserveStartEnd',
|
| 481 |
+
tickMargin: xa.tickMargin != null ? xa.tickMargin : 8,
|
| 482 |
+
padding: xa.padding || { right: 10 },
|
| 483 |
+
axisLine: false, tickLine: false,
|
| 484 |
+
tickFormatter: function (v) {
|
| 485 |
+
var s = xLabels[String(v)];
|
| 486 |
+
return s != null ? s : (v == null ? '' : String(v));
|
| 487 |
+
},
|
| 488 |
+
}));
|
| 489 |
+
children.push(h(rc.YAxis, {
|
| 490 |
+
key: 'y',
|
| 491 |
+
orientation: ya.orientation || 'right',
|
| 492 |
+
width: ya.width != null ? ya.width : 40,
|
| 493 |
+
domain: Array.isArray(ya.domain) ? ya.domain : ['auto', 'auto'],
|
| 494 |
+
ticks: Array.isArray(ya.ticks) ? ya.ticks : undefined,
|
| 495 |
+
axisLine: ya.axisLine !== undefined ? ya.axisLine : false,
|
| 496 |
+
tickLine: false,
|
| 497 |
+
tickMargin: 4,
|
| 498 |
+
}));
|
| 499 |
+
series.forEach(function (s, i) {
|
| 500 |
+
var color = colorOf(s.color);
|
| 501 |
+
var gid = 'xwx-stock-grad-' + i;
|
| 502 |
+
children.push(h('defs', { key: 'def' + i },
|
| 503 |
+
h('linearGradient', { id: gid, x1: 0, y1: 0, x2: 0, y2: 1 },
|
| 504 |
+
h('stop', { offset: 0, stopColor: color, stopOpacity: 0.45 }),
|
| 505 |
+
h('stop', { offset: '100%', stopColor: color, stopOpacity: 0 }))));
|
| 506 |
+
children.push(h(rc.Area, {
|
| 507 |
+
key: 'a' + i,
|
| 508 |
+
dataKey: s.dataKey || 'close',
|
| 509 |
+
type: s.curveType || 'linear',
|
| 510 |
+
stroke: color,
|
| 511 |
+
strokeWidth: 2,
|
| 512 |
+
fill: 'url(#' + gid + ')',
|
| 513 |
+
fillOpacity: 0.6,
|
| 514 |
+
dot: false,
|
| 515 |
+
activeDot: false,
|
| 516 |
+
connectNulls: true,
|
| 517 |
+
isAnimationActive: false,
|
| 518 |
+
}));
|
| 519 |
+
});
|
| 520 |
+
|
| 521 |
+
var tree = h(rc.AreaChart, {
|
| 522 |
+
data: data,
|
| 523 |
+
width: BOX.width,
|
| 524 |
+
height: BOX.height,
|
| 525 |
+
margin: { top: 5, right: 5, bottom: 5, left: 5 },
|
| 526 |
+
}, children);
|
| 527 |
+
var mount = document.getElementById('widget-container');
|
| 528 |
+
if (ReactDOM.createRoot) ReactDOM.createRoot(mount).render(tree);
|
| 529 |
+
else ReactDOM.render(tree, mount);
|
| 530 |
+
window._widgetRendered = true;
|
| 531 |
+
} catch (err) {
|
| 532 |
+
window._widgetError = String((err && err.message) || err);
|
| 533 |
+
document.getElementById('widget-container').innerHTML = '<div style="padding:20px;color:#c00">Chart render error: ' + window._widgetError + '</div>';
|
| 534 |
+
window._widgetRendered = true;
|
| 535 |
+
}
|
| 536 |
+
})();
|
| 537 |
+
<\/script></body></html>`;
|
| 538 |
+
}
|