From 967ffa6c0155b2c1e2f37d23f624211a5a38c513 Mon Sep 17 00:00:00 2001 From: inter Date: Mon, 8 Sep 2025 16:35:28 +0800 Subject: [PATCH] Add File --- g2-ssr/charts/column.js | 118 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 g2-ssr/charts/column.js diff --git a/g2-ssr/charts/column.js b/g2-ssr/charts/column.js new file mode 100644 index 0000000..6cfbbc6 --- /dev/null +++ b/g2-ssr/charts/column.js @@ -0,0 +1,118 @@ +const {checkIsPercent} = require("./utils"); + +function getColumnOptions(baseOptions, axis, data) { + + const x = axis.filter((item) => item.type === 'x') + const y = axis.filter((item) => item.type === 'y') + const series = axis.filter((item) => item.type === 'series') + + if (x.length === 0 || y.length === 0) { + return + } + + const _data = checkIsPercent(y[0], data) + + const options = { + ...baseOptions, + type: 'interval', + data: _data.data, + encode: { + x: x[0].value, + y: y[0].value, + color: series.length > 0 ? series[0].value : undefined, + }, + style: { + radiusTopLeft: (d) => { + if (d[y[0].value] && d[y[0].value] > 0) { + return 4 + } + return 0 + }, + radiusTopRight: (d) => { + if (d[y[0].value] && d[y[0].value] > 0) { + return 4 + } + return 0 + }, + radiusBottomLeft: (d) => { + if (d[y[0].value] && d[y[0].value] < 0) { + return 4 + } + return 0 + }, + radiusBottomRight: (d) => { + if (d[y[0].value] && d[y[0].value] < 0) { + return 4 + } + return 0 + }, + }, + axis: { + x: { + title: x[0].name, + labelFontSize: 12, + labelAutoHide: { + type: 'hide', + keepHeader: true, + keepTail: true, + }, + labelAutoRotate: false, + labelAutoWrap: true, + labelAutoEllipsis: true, + }, + y: {title: y[0].name}, + }, + scale: { + x: { + nice: true, + }, + y: { + nice: true, + }, + }, + interaction: { + elementHighlight: {background: true}, + }, + tooltip: (data) => { + if (series.length > 0) { + return { + name: data[series[0].value], + value: `${data[y[0].value]}${_data.isPercent ? '%' : ''}`, + } + } else { + return {name: y[0].name, value: `${data[y[0].value]}${_data.isPercent ? '%' : ''}`} + } + }, + labels: [ + { + text: (data) => { + const value = data[y[0].value] + if (value === undefined || value === null) { + return '' + } + return `${value}${_data.isPercent ? '%' : ''}` + }, + position: (data) => { + if (data[y[0].value] < 0) { + return 'bottom' + } + return 'top' + }, + dy: -25, + transform: [ + {type: 'contrastReverse'}, + {type: 'exceedAdjust'}, + {type: 'overlapHide'}, + ], + }, + ], + } + + if (series.length > 0) { + options.transform = [{type: 'stackY'}] + } + + return options +} + +module.exports = {getColumnOptions} \ No newline at end of file