/** * DualAxisChart - 双Y轴柱状+折线组合图类 * * 【业务用途】 * 用于展示财务指标对比分析,例如: * - 净利润(柱状图)+ 每股收益(折线图) * - 股东权益(柱状图)+ 未分配利润(折线图) * - 每股收益(柱状图)+ 净资产收益率(折线图) * 左侧Y轴对应柱状图,右侧Y轴对应折线图,实现不同量级指标的同图对比。 * * 【调用方式】 * new DualAxisChart('容器ID', { * title: '图表标题', * xAxisData: ['03-31', '06-30', '09-30', '12-31'], * series: [ * { name: '柱状图名称', type: 'bar', data: [...], color: '#fbd258' }, * { name: '折线图名称', type: 'line', data: [...], color: '#76a0eb' } * ] * }) * * 【处理流程】 * 1. 接收图表配置(标题、X轴标签、双系列数据、Y轴名称等) * 2. 根据 splitLineSide 参数决定分割线显示在左Y轴还是右Y轴 * 3. 为柱状图绑定左Y轴(yAxisIndex=0),折线图绑定右Y轴(yAxisIndex=1) * 4. 构建完整的ECharts option(tooltip + 图例 + 双Y轴 + grid + series) * 5. 注册窗口resize事件 * * @param {string} domId - HTML容器元素ID * @param {Object} config - 完整图表配置 * @param {string} config.title - 图表标题,居中显示 * @param {string[]} config.xAxisData - X轴标签数组 * @param {Array} config.series - 系列配置,固定2项 * series[0] = { name, type:'bar', data, color, barWidth? } * series[1] = { name, type:'line', data, color, smooth? } * @param {string[]}[config.yAxisNames] - 左右Y轴名称,默认 ['', ''] * @param {boolean} [config.showLegend=true] - 是否显示图例 * @param {string} [config.legendPosition='bottom'] - 图例位置 bottom | top * @param {string} [config.splitLineSide='left'] - 网格分割线显示在哪个Y轴 left | right * @param {boolean} [config.smooth=false] - 折线是否平滑曲线 * @param {Object} [config.grid] - 自定义grid覆盖默认边距 * @param {Object} [config.xAxis] - 自定义xAxis覆盖默认配置 * @param {Array} [config.yAxis] - 自定义yAxis覆盖(数组2项,对应左右Y轴) */ ;(function(global) { 'use strict' /* ===== 默认色板 ===== */ var DEFAULT_COLORS = ['#F2B84B', '#2F6FBF'] /* ===== 默认grid边距配置 ===== */ var DEFAULT_GRID = { left: '5%', right: '5%', top: '12%', bottom: '26%', containLabel: true /* 保证Y轴标签不被裁剪 */ } /** * 【构造函数】创建双Y轴柱折组合图实例 * * 执行流程: * 1. 查找DOM容器,不存在则警告退出 * 2. 解析config中的各项参数 * 3. 根据splitLineSide构建左右Y轴(分割线在左侧或右侧) * 4. 允许通过config.yAxis覆盖Y轴的min/max/interval等 * 5. 构建series:柱状图绑左Y轴,折线图绑右Y轴 * 6. 支持自定义grid、xAxis覆盖 * 7. 初始化ECharts实例并渲染 * 8. 注册窗口resize事件 */ function DualAxisChart(domId, config) { var self = this /* 第一步:查找DOM容器 */ var dom = document.getElementById(domId) if (!dom) { console.warn('DualAxisChart:未找到容器元素 #' + domId) return } /* 第二步:解析配置参数 */ config = config || {} var title = config.title || '' var xAxisData = config.xAxisData || [] var seriesData = config.series || [] var yAxisNames = config.yAxisNames || ['', ''] var showLegend = config.showLegend !== false var legendPosition = config.legendPosition || 'bottom' var splitLineSide = config.splitLineSide || 'left' var smooth = config.smooth !== undefined ? config.smooth : false var tooltipFormatter = config.tooltipFormatter || null var showLabel = config.showLabel || false var labelFormatter = config.labelFormatter || null var barMaxWidth = config.barMaxWidth || null var xAxisRotate = config.xAxisRotate || null var legendAlign = config.legendAlign || 'center' var legendRight = config.legendRight || null var legendTop = config.legendTop || null var legendBottom = config.legendBottom || null var singleAxis = config.singleAxis === true var negativeColor = config.negativeColor || null var zeroLine = config.zeroLine === true /* 第三步:构建双Y轴 */ /* hasSplitLeft=true 时分割线在左Y轴,=false 时在右Y轴 */ var hasSplitLeft = splitLineSide === 'left' var yAxis = [ { type: 'value', name: yAxisNames[0], axisLine: { show: false }, /* 隐藏Y轴线 */ axisTick: { show: false }, /* 隐藏刻度线 */ splitLine: hasSplitLeft || singleAxis ? { lineStyle: { color: '#EEF1F5', type: 'dashed' } } : { show: false }, /* 分割线在左Y轴或隐藏 */ axisLabel: { fontSize: 11, color: '#666' } } ] if (!singleAxis) { yAxis.push({ type: 'value', name: yAxisNames[1], axisLine: { show: false }, axisTick: { show: false }, splitLine: hasSplitLeft ? { show: false } : { show: true, lineStyle: { color: '#EEF1F5', type: 'dashed' } }, axisLabel: { fontSize: 11, color: '#666' } }) } /* 第四步:允许外部覆盖Y轴的min/max/interval等配置 */ if (config.yAxis) { if (config.yAxis[0]) { for (var key in config.yAxis[0]) { if (config.yAxis[0].hasOwnProperty(key)) yAxis[0][key] = config.yAxis[0][key] } } if (!singleAxis && config.yAxis[1]) { for (var key in config.yAxis[1]) { if (config.yAxis[1].hasOwnProperty(key)) yAxis[1][key] = config.yAxis[1][key] } } } /* 第五步:构建series */ /* 柱状图绑定左Y轴(默认),折线图绑定右Y轴(yAxisIndex=1) */ var series = [] for (var i = 0; i < seriesData.length; i++) { var s = seriesData[i] var item = { name: s.name, type: s.type, data: s.data } /* 未指定颜色时从默认色板取色 */ var color = s.color || DEFAULT_COLORS[i % DEFAULT_COLORS.length] var negColor = s.type === 'bar' ? (s.negativeColor || negativeColor) : null item.itemStyle = { color: (function (baseColor, negative) { return negative ? function (params) { return params.value < 0 ? negative : baseColor } : baseColor })(color, negColor) } if (s.type === 'line') { /* 折线图配置:绑定右Y轴 + 圆点标记 + 平滑曲线可选 */ item.smooth = s.smooth !== undefined ? s.smooth : smooth item.yAxisIndex = singleAxis ? 0 : 1 item.connectNulls = false item.lineStyle = { color: color, width: 2 } item.symbol = 'circle' item.symbolSize = 5 } /* 柱状图宽度,默认40% */ if (s.type === 'bar') { item.barWidth = s.barWidth || 24 item.itemStyle.borderRadius = [2, 2, 0, 0] if (barMaxWidth) item.barMaxWidth = barMaxWidth if (zeroLine) { item.markLine = { silent: true, symbol: 'none', label: { show: false }, emphasis: { label: { show: false } }, lineStyle: { color: '#c9ced6', width: 1, type: 'solid' }, data: [{ yAxis: 0, label: { show: false } }] } } } /* 数据标签:在柱顶/折线端显示数值 */ if (showLabel) { item.label = { show: true, position: 'top', fontSize: 11, color: '#333' } if (labelFormatter) item.label.formatter = labelFormatter } series.push(item) } /* 第六步:构建完整的ECharts option */ var option = { tooltip: { trigger: 'axis', axisPointer: { type: 'cross', crossStyle: { color: '#999' } }, formatter: tooltipFormatter || undefined }, /* grid:可通过config.grid覆盖默认边距 */ grid: config.grid || JSON.parse(JSON.stringify(DEFAULT_GRID)), /* xAxis:可通过config.xAxis完全覆盖默认配置 */ xAxis: config.xAxis || (function() { var ax = { type: 'category', data: xAxisData, axisPointer: { type: 'shadow' }, axisLine: { lineStyle: { color: '#d8d8d8' } }, axisLabel: { fontSize: 11, color: '#666' }, axisTick: { show: false } } if (xAxisRotate !== null) ax.axisLabel.rotate = xAxisRotate return [ax] })(), yAxis: yAxis, series: series } /* 标题:居中显示 */ if (title) { option.title = { text: title, left: 'center', top: 5, textStyle: { fontSize: 14, fontWeight: 'bold', color: '#333' } } } /* 图例:底部或顶部,显示系列名称 */ if (showLegend) { option.legend = { data: seriesData.map(function(s) { return s.name }), itemWidth: 20, itemHeight: 10, textStyle: { fontSize: 12, color: '#666' } } if (legendTop !== null) { option.legend.top = legendTop option.legend.left = 'center' } else if (legendBottom !== null) { option.legend.bottom = legendBottom option.legend.left = 'center' } else if (legendPosition === 'bottom') { option.legend.bottom = 2 if (legendAlign === 'right') { option.legend.right = 8 } else if (legendAlign === 'left') { option.legend.left = 8 } else { option.legend.left = 'center' } option.legend.itemGap = 12 } else { option.legend.top = 5 option.legend.left = 'center' } if (legendRight !== null) option.legend.right = legendRight } /* 第七步:初始化ECharts并渲染 */ var chart = echarts.init(dom) chart.setOption(option) /* 第八步:注册窗口resize事件 */ var resizeHandler = function() { if (chart) chart.resize() } window.addEventListener('resize', resizeHandler) /* 暴露内部引用给实例方法 */ self.chart = chart self._resizeHandler = resizeHandler } /** * 手动触发图表尺寸重新计算 */ DualAxisChart.prototype.resize = function() { if (this.chart) this.chart.resize() } /** * 销毁图表实例,释放资源 */ DualAxisChart.prototype.destroy = function() { if (this._resizeHandler) window.removeEventListener('resize', this._resizeHandler) if (this.chart) { this.chart.dispose(); this.chart = null } } /* 暴露到全局环境 */ global.DualAxisChart = DualAxisChart })(window)