如何根据列值在xy散点图中着色点?

考虑下面的工作表:

ABCD 1 COMPANY XVALUE YVALUE GROUP 2 Apple 45 35 red 3 Xerox 45 38 red 4 KMart 63 50 orange 5 Exxon 53 59 green 

我在Excel中使用了散点图函数来创build下面的图表:

在这里输入图像说明

但是,图表中的每个点都有一个附加属性: GROUP 。 有四个组: redorangeblackgreen 。 我想相应地为每个点着色,以便我可以看到一个模式(例如,组的green几乎总是在图表的左侧)。 因为我的列表是500行,我不能手动执行此操作。 我怎样才能自动做到这一点?

我回答了一个非常类似的问题:

https://stackoverflow.com/a/15982217/1467082

您只需遍历该系列的.Points集合,然后您可以根据您需要的任何标准分配点的.Format.Fill.ForeColor.RGB值。

更新

下面的代码将按照屏幕截图为图表着色。 这只假定使用三种颜色。 您可以为其他颜色值添加额外的case语句,并将myColor的分配更新为适当的RGB值。

截图

 Option Explicit Sub ColorScatterPoints() Dim cht As Chart Dim srs As Series 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 = ActiveSheet.ChartObjects(1).Chart Set srs = cht.SeriesCollection(1) '## 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) For p = 1 To srs.Points.Count Set pt = srs.Points(p) Set cl = valRange(p).Offset(0, 1) '## assume color is in the next column. With pt.Format.Fill .Visible = msoTrue '.Solid 'I commented this out, but you can un-comment and it should still work '## Assign Long color value based on the cell value '## Add additional cases as needed. Select Case LCase(cl) Case "red" myColor = RGB(255, 0, 0) Case "orange" myColor = RGB(255, 192, 0) Case "green" myColor = RGB(0, 255, 0) End Select .ForeColor.RGB = myColor End With Next End Sub 

非VBA解决scheme:

您需要为每个颜色组创build一组额外的数据,以表示该特定组的Y值。 您可以使用这些组在graphics中创build多个数据集。

这是一个使用你的数据的例子:

  ABCDEFG ---------------------------------------------------------------------------------------------------------------------- 1| COMPANY XVALUE YVALUE GROUP Red Orange Green 2| Apple 45 35 red =IF($D2="red",$C2,NA()) =IF($D2="orange",$C2,NA()) =IF($D2="green",$C2,NA()) 3| Xerox 45 38 red =IF($D3="red",$C3,NA()) =IF($D3="orange",$C3,NA()) =IF($D3="green",$C3,NA()) 4| KMart 63 50 orange =IF($D4="red",$C4,NA()) =IF($D4="orange",$C4,NA()) =IF($D4="green",$C4,NA()) 5| Exxon 53 59 green =IF($D5="red",$C5,NA()) =IF($D5="orange",$C5,NA()) =IF($D5="green",$C5,NA()) 

应该看起来像这样:

  ABCDEFG --------------------------------------------------------------------- 1| COMPANY XVALUE YVALUE GROUP Red Orange Green 2| Apple 45 35 red 35 #N/A #N/A 3| Xerox 45 38 red 38 #N/A #N/A 4| KMart 63 50 orange #N/A 50 #N/A 5| Exxon 53 59 green #N/a #N/A 59 

现在,您可以使用不同的数据集生成图表。 下面是一张仅显示此示例数据的图片:

在这里输入图像说明

您可以将系列(X;Y)值更改为B:B ; E:E B:B ; E:EB:B ; F:F B:B ; F:FB:B ; G:G B:B ; G:G ,以便在添加更多数据时自动更新graphics。

尝试这个:

 Dim xrndom As Random Dim x As Integer xrndom = New Random Dim yrndom As Random Dim y As Integer yrndom = New Random 'chart creation Chart1.Series.Add("a") Chart1.Series("a").ChartType = DataVisualization.Charting.SeriesChartType.Point Chart1.Series("a").MarkerSize = 10 Chart1.Series.Add("b") Chart1.Series("b").ChartType = DataVisualization.Charting.SeriesChartType.Point Chart1.Series("b").MarkerSize = 10 Chart1.Series.Add("c") Chart1.Series("c").ChartType = DataVisualization.Charting.SeriesChartType.Point Chart1.Series("c").MarkerSize = 10 Chart1.Series.Add("d") Chart1.Series("d").ChartType = DataVisualization.Charting.SeriesChartType.Point Chart1.Series("d").MarkerSize = 10 'color Chart1.Series("a").Color = Color.Red Chart1.Series("b").Color = Color.Orange Chart1.Series("c").Color = Color.Black Chart1.Series("d").Color = Color.Green Chart1.Series("Chart 1").Color = Color.Blue For j = 0 To 70 x = xrndom.Next(0, 70) y = xrndom.Next(0, 70) 'Conditions If j < 10 Then Chart1.Series("a").Points.AddXY(x, y) ElseIf j < 30 Then Chart1.Series("b").Points.AddXY(x, y) ElseIf j < 50 Then Chart1.Series("c").Points.AddXY(x, y) ElseIf 50 < j Then Chart1.Series("d").Points.AddXY(x, y) Else Chart1.Series("Chart 1").Points.AddXY(x, y) End If Next 

最近我不得不做类似的事情,我解决了下面的代码。 希望能帮助到你!

 Sub ColorCode() Dim i As Integer Dim j As Integer i = 2 j = 1 Do While ActiveSheet.Cells(i, 1) <> "" If Cells(i, 5).Value = "RED" Then ActiveSheet.ChartObjects("YourChartName").Chart.FullSeriesCollection(1).Points(j).MarkerForegroundColor = RGB(255, 0, 0) Else If Cells(i, 5).Value = "GREEN" Then ActiveSheet.ChartObjects("YourChartName").Chart.FullSeriesCollection(1).Points(j).MarkerForegroundColor = RGB(0, 255, 0) Else If Cells(i, 5).Value = "GREY" Then ActiveSheet.ChartObjects("YourChartName").Chart.FullSeriesCollection(1).Points(j).MarkerForegroundColor = RGB(192, 192, 192) Else If Cells(i, 5).Value = "YELLOW" Then ActiveSheet.ChartObjects("YourChartName").Chart.FullSeriesCollection(1).Points(j).MarkerForegroundColor = RGB(255, 255, 0) End If End If End If End If i = i + 1 j = j + 1 Loop End Sub