在根据列值对 xy 散点图中的点着色后,数据点的颜色错误?
Getting the wrong color for data points after I color dots in a xy scatterplot according to column value?
我已经在我的代码基础上解决了这个问题:How can I color dots in a xy scatterplot according to column value?
但是我逐个上色后,得到的颜色并不完全准确。
编辑:我想我已经找到了问题所在。我有切片器来过滤 Sheet 1 上的散点图,其中数据位于 Sheet 3.
例如,我未过滤数据的前6个数据点是“B / L / B / L / B”,对应的是“Blue / Green / Blue / Green / Blue”。但是,在我申请过滤器仅显示应该只显示绿色的“L”之后,前 6 个数据点仍然显示“蓝色/绿色/蓝色/绿色/蓝色”。我应该如何解决这个问题?
Sub ColorScatterPoints()
Dim cht As Chart
Dim srs As Series
Dim srs_end As Long
Dim pt As Point
Dim p As Long
Dim Vals$, lTrim#, rTrim#
Dim valRange As Range, cl As Range
Dim myColor As Long
Set cht = ThisWorkbook.Worksheets(1).ChartObjects("Speed/Cons Chart").Chart
Set srs = cht.SeriesCollection("with Full/Eco")
'## Get the series Y-Values range address:
lTrim = InStrRev(srs.Formula, ",", InStrRev(srs.Formula, ",") - 1, vbBinaryCompare) + 1
rTrim = InStrRev(srs.Formula, ",")
Vals = Mid(srs.Formula, lTrim, rTrim - lTrim)
Set valRange = Range(Vals) '#Column G / 7
srs_end = ThisWorkbook.Worksheets(3).Cells(Rows.Count, 7).End(xlUp).Row - 1
For p = 1 To srs_end
Set pt = srs.Points(p)
Set cl = valRange(p).Offset(0, -5) '## color is 5 columns to the left.
With pt.Format.Fill
.Visible = msoTrue
Select Case cl
Case "L"
myColor = RGB(112, 173, 71) 'should appear as Green
Case "B"
myColor = RGB(68, 114, 196) 'should appear as Blue
End Select
.ForeColor.RGB = myColor
End With
Next
End Sub
在 运行 代码之后,我注意到我的一些标记为“L”的数据点也是蓝色的。对于我标记为“B”的数据点,反之亦然。
谁能告诉我代码错在哪里?
如果您有可以过滤掉的行,那么您需要循环输入范围而不是系列点:
Sub ColorScatterPoints()
Dim cht As Chart
Dim srs As Series
Dim valRange As Range, c As Range, i As Long
Dim myColor As Long
Set cht = ThisWorkbook.Worksheets(1).ChartObjects("Speed/Cons Chart").Chart
Set srs = cht.SeriesCollection("with Full/Eco")
'## Get the series Y-Values range:
Set valRange = Range(Split(srs.Formula, ",")(2)) '#Column G / 7
'loop over the series Y-value range
For Each c In valRange.Cells
If Not c.EntireRow.Hidden Then 'check row is not filtered out
i = i + 1 'increment datapoint index
Select Case c.Offset(0, -5).Value
Case "L": myColor = RGB(112, 173, 71)
Case "B": myColor = RGB(68, 114, 196)
Case Else: myColor = RGB(150, 150, 150) '<< add a default
End Select
With srs.Points(i).Format.Fill 'format the i'th point
.Visible = msoTrue
.ForeColor.RGB = myColor
End With
End If
Next c
End Sub
我已经在我的代码基础上解决了这个问题:How can I color dots in a xy scatterplot according to column value?
但是我逐个上色后,得到的颜色并不完全准确。
编辑:我想我已经找到了问题所在。我有切片器来过滤 Sheet 1 上的散点图,其中数据位于 Sheet 3.
例如,我未过滤数据的前6个数据点是“B / L / B / L / B”,对应的是“Blue / Green / Blue / Green / Blue”。但是,在我申请过滤器仅显示应该只显示绿色的“L”之后,前 6 个数据点仍然显示“蓝色/绿色/蓝色/绿色/蓝色”。我应该如何解决这个问题?
Sub ColorScatterPoints()
Dim cht As Chart
Dim srs As Series
Dim srs_end As Long
Dim pt As Point
Dim p As Long
Dim Vals$, lTrim#, rTrim#
Dim valRange As Range, cl As Range
Dim myColor As Long
Set cht = ThisWorkbook.Worksheets(1).ChartObjects("Speed/Cons Chart").Chart
Set srs = cht.SeriesCollection("with Full/Eco")
'## Get the series Y-Values range address:
lTrim = InStrRev(srs.Formula, ",", InStrRev(srs.Formula, ",") - 1, vbBinaryCompare) + 1
rTrim = InStrRev(srs.Formula, ",")
Vals = Mid(srs.Formula, lTrim, rTrim - lTrim)
Set valRange = Range(Vals) '#Column G / 7
srs_end = ThisWorkbook.Worksheets(3).Cells(Rows.Count, 7).End(xlUp).Row - 1
For p = 1 To srs_end
Set pt = srs.Points(p)
Set cl = valRange(p).Offset(0, -5) '## color is 5 columns to the left.
With pt.Format.Fill
.Visible = msoTrue
Select Case cl
Case "L"
myColor = RGB(112, 173, 71) 'should appear as Green
Case "B"
myColor = RGB(68, 114, 196) 'should appear as Blue
End Select
.ForeColor.RGB = myColor
End With
Next
End Sub
在 运行 代码之后,我注意到我的一些标记为“L”的数据点也是蓝色的。对于我标记为“B”的数据点,反之亦然。
谁能告诉我代码错在哪里?
如果您有可以过滤掉的行,那么您需要循环输入范围而不是系列点:
Sub ColorScatterPoints()
Dim cht As Chart
Dim srs As Series
Dim valRange As Range, c As Range, i As Long
Dim myColor As Long
Set cht = ThisWorkbook.Worksheets(1).ChartObjects("Speed/Cons Chart").Chart
Set srs = cht.SeriesCollection("with Full/Eco")
'## Get the series Y-Values range:
Set valRange = Range(Split(srs.Formula, ",")(2)) '#Column G / 7
'loop over the series Y-value range
For Each c In valRange.Cells
If Not c.EntireRow.Hidden Then 'check row is not filtered out
i = i + 1 'increment datapoint index
Select Case c.Offset(0, -5).Value
Case "L": myColor = RGB(112, 173, 71)
Case "B": myColor = RGB(68, 114, 196)
Case Else: myColor = RGB(150, 150, 150) '<< add a default
End Select
With srs.Points(i).Format.Fill 'format the i'th point
.Visible = msoTrue
.ForeColor.RGB = myColor
End With
End If
Next c
End Sub