简体   繁体   English

使用 Excel VBA 修改标记透明度

[英]Modify Marker Transparency with Excel VBA

Lets say I have the table below and I want to make a scatter plot of it.假设我有下表,我想制作它的散点图。

X    Y      Weight

1    0.5    12
2    0.1    23
3    0.2    36
4    0.5    27
1    0.2    4

I am looking for a way to set the transparency of marker based on values in a column so that each point's marker has its individual transparency level which might not be the same as the other points.我正在寻找一种方法来根据列中的值设置标记的透明度,以便每个点的标记具有其单独的透明度级别,这可能与其他点不同。 The reason I want this is that the weight of the points is not equal.我想要这个的原因是点的权重不相等。

One solution would be to create further number of points (Weight - 1) manually and plot them all with an appropriate transparency level.一种解决方案是手动创建更多点数(权重 - 1),并以适当的透明度级别绘制所有点。 Another solution would be to set the marker transparency level for each point manually.另一种解决方案是手动设置每个点的标记透明度级别。 However, none of these are logical as the number of the points is really large!然而,这些都不合逻辑,因为点数真的很大!

I have tried the below with VBA but no luck with it.我已经用 VBA 尝试了下面的方法,但没有成功。

Sub TransparencyModifier()
'
' TransparencyModifier Macro
'

'

Dim i as Integer
For i = 1 to 5
    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).Points(i).Select
    With Selection.Format.Fill
        .Transparency = Cells(i+1,3).Value/100
    End With
Next i
End Sub

Any help is appreciated任何帮助表示赞赏

There is a bug in Excel (I'm using Excel 2013). Excel 中有一个错误(我使用的是 Excel 2013)。 When you set the Fill transparency, the line style goes to auto, and you don't see the transparency.当您设置填充透明度时,线条样式将变为自动,并且您看不到透明度。 If you via VBA set the style back to solid, transparency gets set to 0.如果您通过 VBA 将样式重新设置为纯色,则透明度将设置为 0。

I was able to use the following workaround:我能够使用以下解决方法:

  1. MANUALLY Set your chart's Fill color to white (or same as your chart background).手动将图表的填充颜色设置为白色(或与图表背景相同)。 If you set it in code, it will REVERT...如果您在代码中设置它,它将 REVERT ...

  2. Add before the With lineWith行之前添加

    a.一种。 Selection.MarkerSize = 2 and Selection.MarkerSize = 2
    b.Selection.Border.LineStyle = msoLineFileNone

and

  1. Change your With line to With Selection.format.Line将您的With行更改为With Selection.format.Line

  2. Add before the .transparency =.transparency =之前添加

    a.一种。 .Visible = msoTrue and .Visible = msoTrue
    b..ForColor.ObjectThemeColor = msoThemeColorAccent1

Resulting code block:结果代码块:

Sub TransparencyModifier()
Dim i As Integer
For i = 1 To 5
    ActiveChart.SeriesCollection(1).Select
    ActiveChart.SeriesCollection(1).Points(i).Select
    Selection.MarkerSize = 2
    Selection.Border.LineStyle = msoLineFillNone
    With Selection.format.Fill
        .Visible = msoFalse
    End With
    With Selection.format.Line
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorAccent1
        .transparency = Cells(i + 1, 3).Value / 100
    End With
    Selection.Border.LineStyle = msoLineFillNone
Next i
End Sub

I made another solution to this writing a small python script which creates more points based on the weight of each point eg if the weight of a point is 12 then 12 identical points with the specifications of this point are created.我为此编写了另一个解决方案,它编写了一个小的 python 脚本,它根据每个点的权重创建更多的点,例如,如果一个点的权重为 12,那么将创建 12 个与该点的规格相同的点。 Then saved all these in a csv and plotted it and changed opacity for all markers manually (this is easy) to a number that gave me satisfying result.然后将所有这些保存在一个 csv 中并绘制它并手动将所有标记的不透明度(这很容易)更改为一个给我满意结果的数字。 As the extra points created overlay each other, the final product will show the weight of each point as well.由于创建的额外点相互叠加,最终产品也会显示每个点的权重。

Old post but if people have the same problem this worked for me, found it after some googling.旧帖子,但如果人们有同样的问题,这对我有用,请在谷歌搜索后找到。

Dim chtTemp As Chart
Dim serTemp As Series

Set chtTemp = ActiveSheet.ChartObjects(1).Chart ' your chart
Set serTemp = chtTemp.SeriesCollection(1) ' the series that you want to make transparent

With serTemp.Format.Fill
    .Solid
    .Visible = True
    .BackColor.RGB = RGB(255, 0, 0)
    .ForeColor.RGB = RGB(255, 0, 0)
    .Transparency = 0.3
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM