- 帖子
- 2035
- 主題
- 24
- 精華
- 0
- 積分
- 2031
- 點名
- 0
- 作業系統
- Win7
- 軟體版本
- Office2010
- 閱讀權限
- 100
- 性別
- 男
- 註冊時間
- 2012-3-22
- 最後登錄
- 2024-2-1
|
回復 21# lin_6219 - Sub KChartWithVolume2() ' K線圖與成交量圖放在同一圖表
- Dim nRow As Integer, ChtObj As ChartObject
- Dim i As Integer, j As Integer, chartname As String
- Dim myMax, myMin, GapNr As Integer
-
- On Error Resume Next
-
- With Worksheets("主畫面")
- .ChartObjects.Delete
- .Select
- nRow = Worksheets("繪圖資料").Range("A65536").End(xlUp).Row
- ' Set ChtObj = Worksheets("主畫面").ChartObjects.Add(1, 1, 450, 250)
- ' With ChtObj.Chart ~ End With
- ' 此範例 改以 Worksheets("主畫面").Shapes.AddChart.Select 的方式處理;
- ' With ActiveChart
- ' . . . . . . . . . . . .
- ' With .ChartArea
- ' .Height = 250
- ' .Width = 450
- ' End With
- ' . . . . . . . . . . . .
- ' chartname = Trim(Replace(ActiveChart.Name, ActiveSheet.Name, ""))
- ' .Shapes(chartname).Left = Cells(1, 1).Left
- ' .Shapes(chartname).Top = Cells(1, 1).Top
- ' . . . . . . . . . . . .
- ' End With
- ' 同理,亦可以使用 Worksheets("主畫面").ChartObjects.Add() 的方式處理
- ' 差別只在於 With ActiveChart ~ End With 間,要另行宣告圖表高、寬度,以及座標位置。
- ' 換言之, Worksheets("主畫面").ChartObjects.Add() 是一次便宣告完成,宣告物件不同。
- .Shapes.AddChart.Select
-
- ' With ChtObj.Chart
- With ActiveChart
- ' .SetSourceData Worksheets("繪圖資料").Range("A2:E" & CStr(nRow))
- .SetSourceData Source:=Range("繪圖資料!$A$2:繪圖資料!$E$" & CStr(nRow))
- .ChartType = xlStockOHLC
- .HasTitle = True
- .ChartTitle.Characters.Text = "K線與成交量圖"
-
- With .ChartGroups(1)
- .AxisGroup = xlPrimary
- .HasUpDownBars = True
- .UpBars.Interior.ColorIndex = 3
- .DownBars.Interior.ColorIndex = 1
- .GapWidth = 10
- End With
-
- ' .SeriesCollection.Add Worksheets("繪圖資料").Range("G1:G" & CStr(nRow))
- .SeriesCollection.Add Source:=Range("繪圖資料!$G$2:繪圖資料!$G$" & CStr(nRow))
-
- With .SeriesCollection(5)
- .ChartType = xlXYScatterLinesNoMarkers
- ' .ChartType = xlLine
- .AxisGroup = xlPrimary
- ' 如果 主座標值為 xlStockOHLC,此處宣告會被忽視,視同 xlSecondary。
- .Border.ColorIndex = 7
- .Name = "=繪圖資料!$G$1"
- End With
-
- With Worksheets("繪圖資料") ' 設定主座標軸最大及最小值
- myMax = Application.Max(.Range("C2:C" & CStr(nRow)))
- myMin = Application.Min(.Range("D2:D" & CStr(nRow)))
- myMin = myMin - (myMax - myMin)
- End With
-
- With .Axes(xlValue)
- .MaximumScale = Round(myMax, 2)
- .MinimumScale = Round(myMin, 2)
- End With
-
- ' .SeriesCollection.NewSeries ' 新增成交量數列 (與下述宣告結果一致)
- .SeriesCollection.Add Source:=Range("繪圖資料!$F$2:繪圖資料!$F$" & CStr(nRow))
-
- With .SeriesCollection(6)
- ' 此處 .Values 存放值範圍必須是指向 "有實體數據資料" 範圍區域。
- ' 如果使用 .SeriesCollection.NewSeries 宣告,則必須指明 .Values。
- ' .Values = Worksheets("繪圖資料").Range("F2:F" & CStr(nRow))
- .ChartType = xlColumnClustered
- .Name = "成交量" ' "=繪圖資料!$F$1" (成交金額)
- .Interior.ColorIndex = 17
- .AxisGroup = xlSecondary ' 設為副座標軸
- End With
-
- With Worksheets("繪圖資料") ' 計算副座標軸最大及最小值
- myMax = Application.Max(.Range("F2:F" & CStr(nRow)))
- myMax = myMax * 2
- myMin = 0.01
- End With
-
- With .Axes(xlValue, xlSecondary) ' 設定副座標軸最大及最小值
- .MaximumScale = Round(myMax, 0)
- .MinimumScale = Round(myMin, 0)
- End With
-
- With .Axes(xlCategory) ' X座標軸 (時間軸)
- .CategoryType = xlCategoryScale
- .TickLabelSpacing = 4 ' 標示間距
- .TickLabels.NumberFormatLocal = "yyyy/m/d"
- .TickLabels.Font.ColorIndex = 5 ' Blue Color
- End With
-
- With .Legend ' 刪除不必要的圖例
- .LegendEntries(2).Delete ' 開盤價
- .LegendEntries(2).Delete ' 最高價
- .LegendEntries(2).Delete ' 最低價
- .LegendEntries(2).Delete ' 收盤價
- .Top = .Parent.ChartTitle.Top - 5
- End With
-
- With .ChartArea
- .Height = 250 ' 將原本設定之高度調至適度位置
- .Width = 450
-
- .Border.Weight = 2
- .Border.LineStyle = 0
- ' 圖表的繪圖區外之 X、Y 軸列示資料數據區塊部分予以填入圖表預設(系統)之底色、可增強視覺效果。
- .Fill.OneColorGradient Style:=msoGradientHorizontal, Variant:=1, Degree:=0.231372549019608
- ' .Fill.Visible = True ' 加入圖表底色變數內給于之色系。
- ' .Fill.ForeColor.SchemeColor = 圖表底色
- End With
-
- With .PlotArea ' 調整繪圖區域大小與位置
- .Top = .Top - 10
- .Height = .Height + 15
- .Width = .Width + 95
- End With
-
- .PlotArea.Select ' 將圖表的繪圖區格線灰黑顏色修改成淡青色、以及表格實線改以虛線表示
- .Axes(xlValue).MajorGridlines.Select
- With Selection.Format.Line
- .Visible = msoTrue
- .ForeColor.ObjectThemeColor = msoThemeColorAccent1
- .ForeColor.TintAndShade = 0
- .ForeColor.Brightness = 0.8000000119
- .Transparency = 0
- .Weight = 0.25
- .DashStyle = msoLineSysDash
- End With
- End With
-
- chartname = Trim(Replace(ActiveChart.Name, ActiveSheet.Name, ""))
- .Shapes(chartname).Left = Cells(1, 1).Left ' 設定此圖表實際擺放的 X、Y 座標位置。
- .Shapes(chartname).Top = Cells(1, 1).Top
- .[A1].Select
- End With
- End Sub
複製代碼 |
|