ExcelのVBAを活用して、簡単にグラフを虹色にするための記事です。
エクセルマクロの使い方は検索してください。ちなみに、エクセルでのショートカットキーはalt+F11です。
VBAの入力
時間がない人やとりあえず動けばいいという人は,以下のコードを貼り付け、グラフを選択した状態で実行すれば虹色のグラフになります。
Graph_rainbowcolor.bas
Option Explicit
Sub グラフレインボーカラー()
'グラフを選択した状態で使う。'
'変数定義'
Dim i As Long, j As Long, rate As Single, red As Long, green As Long, blue As Long
i = ActiveChart.FullSeriesCollection.Count
If i = 1 Then
MsgBox "ファイルを開けません", vbExclamation
ActiveChart.FullSeriesCollection(i).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 0, 0)
.Transparency = 0
End With
Else
For j = 1 To i
rate = (j - 1) / (i - 1)
' 赤場合分け'
If rate < 1 / 3 Then
red = 255
ElseIf rate < 1 / 2 Then
red = -6 * 255 * rate + 3 * 255
ElseIf rate < 5 / 6 Then
red = 0
Else
red = 6 * 127 * rate - 5 * 127
End If
' 緑場合分け'
If rate < 1 / 6 Then
green = 6 * 165 * rate
ElseIf rate < 1 / 3 Then
green = 6 * 90 * rate + 75
ElseIf rate < 1 / 2 Then
green = -6 * 127 * rate + 4 * 127
ElseIf rate < 2 / 3 Then
green = 6 * 127 * rate - 2 * 127
ElseIf rate < 5 / 6 Then
green = -6 * 255 * rate + 5 * 255
Else
green = 0
End If
' 青場合分け'
If rate < 1 / 2 Then
blue = 0
ElseIf rate < 2 / 3 Then
blue = 6 * 255 * rate + 3 * 255
ElseIf rate < 5 / 6 Then
blue = 255
Else
blue = -6 * 127 * rate + 7 * 127
End If
ActiveChart.FullSeriesCollection(j).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(red, green, blue)
.Transparency = 0
End With
Next j
End If
End Sub
Graph_rainbowcolor_0black.bas
Option Explicit
Sub グラフレインボーカラー_0black()
Dim i As Long, j As Long, rate As Single, red As Long, green As Long, blue As Long
On Error GoTo myError
For i = 1 To 100
ActiveChart.FullSeriesCollection(i).Select
Next i
myError: 'この行にジャンプします'
If i = 2 Then
MsgBox "ファイルを開けません", vbExclamation
ActiveChart.FullSeriesCollection(i).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 0, 0)
.Transparency = 0
End With
Else
ActiveChart.FullSeriesCollection(1).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 0, 0)
.Transparency = 0
End With
For j = 2 To i - 1
rate = (j - 2) / (i - 3)
' 赤場合分け'
If rate < 1 / 3 Then
red = 255
ElseIf rate < 1 / 2 Then
red = -6 * 255 * rate + 3 * 255
ElseIf rate < 5 / 6 Then
red = 0
Else
red = 6 * 127 * rate - 5 * 127
End If
' 緑場合分け'
If rate < 1 / 6 Then
green = 6 * 165 * rate
ElseIf rate < 1 / 3 Then
green = 6 * 90 * rate + 75
ElseIf rate < 1 / 2 Then
green = -6 * 127 * rate + 4 * 127
ElseIf rate < 2 / 3 Then
green = 6 * 127 * rate - 2 * 127
ElseIf rate < 5 / 6 Then
green = -6 * 255 * rate + 5 * 255
Else
green = 0
End If
' 青場合分け'
If rate < 1 / 2 Then
blue = 0
ElseIf rate < 2 / 3 Then
blue = 6 * 255 * rate + 3 * 255
ElseIf rate < 5 / 6 Then
blue = 255
Else
blue = -6 * 127 * rate + 7 * 127
End If
ActiveChart.FullSeriesCollection(j).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.RGB = RGB(red, green, blue)
.Transparency = 0
End With
Next j
End If
End Sub
ちょい解説
このサイトの虹色のRGBを線形で分割しています。
具体的には以下のグラフをx軸に沿ってn等分しています(nはグラフの系列数)。
終わりに
参考
https://effichem.blogspot.com/ 私のブログ
http://qwertyfk.blog16.fc2.com/blog-entry-151.html プログラミングメモ日記 虹の7色のRGB値