Angular中SMA与价格成交量Highcharts按缩放级别调整pointWidth
实现方案
1. 定义缩放级别与pointWidth的对应关系
先创建一个映射对象,把每个缩放按钮对应的列宽值存好,方便后续直接取用:
const rangePointWidthMap: {[key: string]: number} = { '1m': 9, '3m': 7, '6m': 5, 'ytd': 6, '1y': 2, 'all': 3 // ALL的默认列宽,可按需修改 };
2. 为图表添加缩放按钮监听事件
在图表配置的chart选项里加入事件监听,每次点击缩放按钮时,找到成交量系列并更新它的pointWidth:
chart: { events: { // 监听缩放按钮点击动作 rangeSelectorButtonClick: function(e: any) { // 通过ID定位成交量系列 const volumeSeries = this.series.find(s => s.id === 'volume'); if (volumeSeries) { // 从映射表取对应列宽,找不到则用默认值3 const targetWidth = rangePointWidthMap[e.button.type] || 3; // 更新列宽并重新渲染图表 volumeSeries.update({ pointWidth: targetWidth }, true); } }, // 页面加载时初始化对应列宽 load: function() { // 获取当前默认选中的缩放按钮 const activeButton = this.rangeSelector.buttonOptions.find(btn => btn.selected); if (activeButton) { const volumeSeries = this.series.find(s => s.id === 'volume'); if (volumeSeries) { const targetWidth = rangePointWidthMap[activeButton.type] || 3; volumeSeries.update({ pointWidth: targetWidth }, false); } } } } },
3. 整合到现有代码的完整配置
把上述内容合并到你现有的图表配置中,最终代码片段如下:
// 定义缩放级别与列宽的映射关系 const rangePointWidthMap: {[key: string]: number} = { '1m': 9, '3m': 7, '6m': 5, 'ytd': 6, '1y': 2, 'all': 3 }; // 图表完整配置 const chartOptions: Highcharts.Options = { chart: { events: { load: function() { const activeButton = this.rangeSelector.buttonOptions.find(btn => btn.selected); if (activeButton) { const volumeSeries = this.series.find(s => s.id === 'volume'); if (volumeSeries) { const targetWidth = rangePointWidthMap[activeButton.type] || 3; volumeSeries.update({ pointWidth: targetWidth }, false); } } }, rangeSelectorButtonClick: function(e: any) { const volumeSeries = this.series.find(s => s.id === 'volume'); if (volumeSeries) { const targetWidth = rangePointWidthMap[e.button.type] || 3; volumeSeries.update({ pointWidth: targetWidth }, true); } } } }, plotOptions: { series: { dataGrouping: { enabled: false, } }, column: { pointPadding: 0, groupPadding: 0, borderWidth: 0, shadow: false, pointWidth: 3, }, }, series: [{ type: 'candlestick', name: 'CAST', id: 'cast', zIndex: 2, data: ohlc }, { type: 'column', name: 'Volume', id: 'volume', data: volume, yAxis: 1, threshold: null , pointRange: 1, pointWidth: 3 // 默认值,后续会被事件覆盖 }] };
注意事项
- 借助Highcharts的
rangeSelectorButtonClick事件捕捉缩放切换动作,通过系列ID精准定位成交量列修改宽度。 load事件是为了保证页面初始化时,默认选中的缩放级别能对应正确的列宽。- 映射表可随时调整各缩放级别对应的列宽,ALL的数值可根据实际显示效果修改。
内容的提问来源于stack exchange,提问作者Usha
相关产品推荐
相关产品推荐

