ExcelTip.Net留存知识帖 ---【注:附件之前被网盘供应商清空后,现已修复-现已修复-现已修复为本地下载!】
现在位置:首页 > E问E答 > 图表图形 > 动态复合图表

动态复合图表

作者:绿色风 分类: 时间:2022-08-17 浏览:135
楼主
芐雨


一个动态的复合图表,通过复选框来控制
要求:
1.不同的系列设置不同的颜色
2.不同的系列设置不同的图表类型
3.占比,转化率等系列在次坐标轴,如全部都是占比的系列设置在主次标轴
(详情设置可见下图)

 



思路
根据要求列出不同系列的设置----遍历CheckBoxes找出勾选的系列----根据系列的设置做出图表

代码如下:
  1. Sub 动态图表_芐雨()
  2.     Dim arr, brr, Dic As Object
  3.     Dim n As Integer, j As Integer, k As Integer, x As Integer
  4.     Application.ScreenUpdating = False
  5.     Set Dic = CreateObject("scripting.dictionary")
  6.     k = 1
  7.     With ActiveSheet
  8.         arr = .Range("X3").CurrentRegion  '把格式设置导入数组arr
  9.         For n = 2 To UBound(arr)
  10.             Dic(arr(n, 1)) = n          '放出字典Dic,n为数组arr的位置
  11.         Next
  12.         ReDim brr(1 To n - 1, 0)
  13.         .Shapes("图表 17").Select
  14.         For j = 1 To .CheckBoxes.Count  '求出√选的项,记入数组brr
  15.             If .CheckBoxes(j).Value = 1 Then x = x + 1: brr(x, 0) = .CheckBoxes(j).Text
  16.         Next
  17.     End With
  18.     With ActiveChart
  19.         For Z = .SeriesCollection.Count To 1 Step -1
  20.             .SeriesCollection(Z).Delete   '删除所有系列
  21.         Next
  22.         For i = 1 To x
  23.             tt = Dic(brr(i, 0))
  24.             .SeriesCollection.NewSeries
  25.             .SeriesCollection(i).Name = arr(tt, 1)        '系列名
  26.             .SeriesCollection(i).Values = arr(tt, 2)      '系列值
  27.             .SeriesCollection(i).XValues = arr(tt, 3)     '水平轴值
  28.             If arr(tt, 4) = "折线" Then
  29.                 .SeriesCollection(i).ChartType = xlLine     '图形类型: xlLine折线图
  30.                 .SeriesCollection(i).Format.Line.ForeColor.RGB = RGB(arr(tt, 6), arr(tt, 7), arr(tt, 8))    '设置线颜色
  31.                 .SeriesCollection(i).Format.Line.Weight = arr(tt, 5)       '设置线大小'
  32.             Else
  33.                 .SeriesCollection(i).ChartType = xlColumnClustered   '图形类型:xlColumnClustered 柱形
  34.                 .SeriesCollection(i).Format.Fill.ForeColor.RGB = RGB(arr(tt, 6), arr(tt, 7), arr(tt, 8))   '设置填充颜色
  35.             End If
  36.             If .SeriesCollection(i).Name = "PV" Then k = 2
  37.             If .SeriesCollection(i).Name = "UV" Then k = 2
  38.             If .SeriesCollection(i).Name = "进货数" Then k = 2
  39.             If .SeriesCollection(i).Name = "销售数" Then k = 2
  40.             If .SeriesCollection(i).Name = "进货货值" Then k = 2
  41.             If .SeriesCollection(i).Name = "销售金额" Then k = 2
  42.             If .SeriesCollection(i).Name = "购买人数" Then k = 2
  43.             If .SeriesCollection(i).Name = "平均客单价" Then k = 2

  44.         Next
  45.         For Z = .SeriesCollection.Count To 1 Step -1     '根据k值设置为次坐标轴
  46.             If .SeriesCollection(Z).Name = "销量售**" Then .SeriesCollection(Z).AxisGroup = k
  47.             If .SeriesCollection(Z).Name = "销量占比" Then .SeriesCollection(Z).AxisGroup = k
  48.             If .SeriesCollection(Z).Name = "销售额售**" Then .SeriesCollection(Z).AxisGroup = k
  49.             If .SeriesCollection(Z).Name = "平均转化率" Then .SeriesCollection(Z).AxisGroup = k
  50.             If .SeriesCollection(Z).Name = "平均退货率" Then .SeriesCollection(Z).AxisGroup = k
  51.             If k = 1 Then  'k=1时,放入主坐标轴
  52.                 .Axes(xlValue).TickLabels.NumberFormatLocal = "0.00%"   'Y轴数字格式
  53.             Else
  54.                 .Axes(xlValue).TickLabels.NumberFormatLocal = "0"
  55.             End If
  56.         Next
  57.     End With
  58.     Application.ScreenUpdating = True
  59. End Sub




动态复合图表_芐雨.zip


2楼
滴水穿石
这种动态图,如果不用VBA可以做出来吗?
3楼
芐雨
可以做出接近的,但部分效果不能实现。(如:占比,转化率会自动设置为次坐标轴。)
4楼
滴水穿石
好吧...
感觉VBA好难...
5楼
老糊涂
学习图表VBA
6楼
13641096715
下载学习,感谢分享。
7楼
yeminqiang
果断收藏!留给后代学习。相信下一代更有智慧!
8楼
yeminqiang



免责声明

有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素, 经与ExcelTip.Net站长Apolloh商议并征得其同意, 现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示, 供有需要的人士查询使用,也慰缅曾经的论坛时代。 所示各个帖子的原作者如对版权有异议, 可与本人沟通提出,或于本站点留言,我们会尽快处理。 在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一

评论列表
sitemap