简体   繁体   English

更改图表上系列的填充颜色

[英]Change Fill Color Of Series On Chart

I have two types of charts that will be populated into Excel by a BI tool.我有两种类型的图表,它们将通过 BI 工具填充到 Excel 中。 I need to colour the available series in them according to some rules.我需要根据一些规则为它们中的可用系列着色。

The first chart shows expenditure by year (year is the series), and there are varying degrees of history from a few months, up to 24 months.第一张图是按年(年是系列)支出的,从几个月到24个月都有不同程度的历史。 This means my 24 months of data is spread over years 2015, 2016, 2017. Next year this changes to 2016, 2017, 2018 as I'm keeping a rolling 24 months.这意味着我的数据为24个月延展到2015年多年,2016年,2017年。明年我保持滚动24个月这种变化,到2016年,2017年,2018。

Whatever the data set, I need the most recent year (eg 2017) in the bar chart data to be displayed in blue, the year before that (eg 2016) in orange, and then the year before that (eg 2015) in grey.无论数据集是什么,我都需要条形图数据中的最近一年(例如 2017 年)以蓝色显示,前一年(例如 2016 年)以橙色显示,然后前一年(例如 2015 年)以灰色显示。

It is possible I won't have 24 months (eg new clients).我可能没有 24 个月(例如新客户)。 If there are only six months, the same colouring logic applies, and that most recent year would need to display in blue.如果只有六个月,则应用相同的着色逻辑,并且最近的一年需要显示为蓝色。

Expenditure Chart支出图表

The second chart shows series values based on performance.第二个图表显示了基于性能的系列值。 These series are called 'on time', 'in tolerance' and 'late'.这些系列被称为“准时”、“容忍”和“迟到”。

Their colours need to be: 'on time' = mid green, 'in tolerance' = light green, 'late' = red.它们的颜色必须是:“准时”= 中绿色,“在容限内”= 浅绿色,“迟到”= 红色。

1 or 2 or all 3 of these series may be present in a given chart with no predictability.这些系列的 1 个2 个全部 3 个可能出现在给定的图表中,而没有可预测性。 I need the VBA to determine which series are available and colour accordingly.我需要 VBA 来确定可用的系列和相应的颜色。

Performance Chart业绩图表

I cobbled together code from other feeds, and isn't a base to build from.我从其他提要中拼凑了代码,并不是构建的基础。 I think I need to use ForEach type syntax, as I know I need to loop through each of the SeriesCollection objects.我想我需要使用 ForEach 类型语法,因为我知道我需要遍历每个 SeriesCollection 对象。

Here's a simple little routine for your first question, recoloring the series in your chart blue, orange, and gray but reverse the default order:对于您的第一个问题,这是一个简单的小程序,将图表中的系列重新着色为蓝色、橙色和灰色,但反转默认顺序:

Sub ReverseDefaultColors()
  Dim iSrs As Long, nsrs As Long

  If ActiveChart Is Nothing Then
    MsgBox "Select a chart and try again.", vbExclamation, "No Active Chart"
  Else
    With ActiveChart
      nsrs = .SeriesCollection.Count
      ' work backwards from last series
      For iSrs = nsrs To 1 Step -1
        Select Case nsrs - iSrs
          Case 0 ' last series
            .SeriesCollection(iSrs).Format.Fill.ForeColor.ObjectThemeColor = _
                msoThemeColorAccent1
          Case 1 ' next to last series
            .SeriesCollection(iSrs).Format.Fill.ForeColor.ObjectThemeColor = _
                msoThemeColorAccent2
          Case 2 ' etc.
            .SeriesCollection(iSrs).Format.Fill.ForeColor.ObjectThemeColor = _
                msoThemeColorAccent3
        End Select
      Next
    End With
  End If
End Sub

Here's another for your second question, coloring green, light green, and red based on series name (adjust RGB as required).这是您的第二个问题的另一个问题,根据系列名称为绿色、浅绿色和红色着色(根据需要调整 RGB)。 You should note that some people (about 8% of males, less than 1% of females) may have problems distinguishing between green and red.您应该注意到有些人(大约 8% 的男性,不到 1% 的女性)可能难以区分绿色和红色。 For this reason, blue and orange are often used as a preferred color scheme.出于这个原因,蓝色和橙色通常被用作首选配色方案。

Sub ColorGreenToRed()
  Dim iSrs As Long, nSrs As Long
  If ActiveChart Is Nothing Then
    MsgBox "Select a chart and try again.", vbExclamation, "No Active Chart"
  Else
    With ActiveChart
      nSrs = .SeriesCollection.Count
      For iSrs = 1 To nSrs
        ' only format series whose names are found
        Select Case LCase$(.SeriesCollection(iSrs).Name)
          Case "on time"
            .SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = _
                RGB(0, 176, 80) ' Green
          Case "in tolerance"
            .SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = _
                RGB(146, 208, 80) ' Light Green
          Case "late"
            .SeriesCollection(iSrs).Format.Fill.ForeColor.RGB = _
                RGB(255, 0, 0) ' Red
        End Select
      Next
    End With
  End If
End Sub

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

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