返回列表 上一主題 發帖

[發問] K 線股票圖如何能與主力、散戶、及成交量線共存?

[發問] K 線股票圖如何能與主力、散戶、及成交量線共存?

本帖最後由 c_c_lai 於 2012-5-7 12:05 編輯

K 線股票如何能與主力、散戶、及成交量線共存? 這個議題前陣子
在線上也不斷地與 Hsieh、GBKEE、register313等諸位版主先進請益過,
同時 kobo、AlexLiou 、chen_cook 等同好前輩也參與論述,謝謝大家的熱心!
現在把完成的心得提供出來,以期能有更多的人來共修,同時亦希望能獲得
各位有更棒的修正與經驗提出分享,別人能寫的為什麼我不會呢?

股票圖表2.rar (24.02 KB)

對不起! 上附的檔案是 OK 的,只是我想解開我在處理上的一些疑惑,所以將此篇分享同時
改成發問, 一舉雙得 (一魚兩吃)。

   ActiveSheet.Shapes.AddChart.Select
    With ActiveChart
        .SetSourceData Source:=Range("工作表1!$B$1:工作表1!$B$" & totalRows & ", 工作表1!$C$1:工作表1!$F$" & totalRows)
        .ChartType = xlStockOHLC
        With .ChartGroups(1)
             .AxisGroup = 1
                   .
                   .
                   .
        End With

        .SeriesCollection.Add Source:=Range("工作表1!$G$2:工作表1!$I$" & totalRows)
        
        With .SeriesCollection(5)    ' 成交量
            .AxisGroup = 2
            .Name = "=工作表1!$G$1"
            .ChartType = xlColumnClustered
                   .
                   .
                   .
        End With

以上程式碼運作都 OK, 但是如果我把成交量的部分用下列方式表達,卻會出現錯誤訊息。
        With .ChartGroups(2)
             .AxisGroup = 2
                   .
                   .
        End With
難道 .SeriesCollection.Add Source:=Range("工作表1!$G$2:工作表1!$I$" & totalRows) 之宣告,
不就是第二組 (Group 2) 嗎?
而且如果   With .SeriesCollection(6) 以及 With .SeriesCollection(7) 內都一併加上  .AxisGroup = 2
也會有錯誤訊息,那會是甚麼情況? 語法不正確、或是有語法限制?
我發現只要是股票圖加上其它圖表的組合比較會有難度。

謝謝指導!

TOP

本帖最後由 c_c_lai 於 2012-5-7 14:42 編輯

回復 3# white5168
  1. Option Explicit

  2. Sub drawCharts()
  3.     Dim totalRows As Single
  4.     Dim xRow, yCol, cHeight, cWidth As Integer
  5.     Dim text As String
  6.     Dim chartname As String
  7.     Dim sRowHeight As Single
  8.    
  9.     xRow = 3
  10.     yCol = 1
  11.     cHeight = 31         'CHeight 定義為圖表所佔的列高
  12.     cWidth = 874
  13.         
  14.     Sheets("工作表1").Select
  15.    
  16.     ActiveSheet.ChartObjects.Delete
  17.         
  18.     totalRows = Sheets("工作表1").Range("B" & Rows.Count).End(xlUp).Row     ' 傳回 B 欄所使用儲存格之最後一格之列號
  19.     '先把列高找出來, 才好算出圖要佔幾列, 兩個圖才好連在一起
  20.     sRowHeight = Sheets("工作表1").Rows(3).RowHeight
  21.    
  22.     ActiveSheet.Shapes.AddChart.Select
  23.     With ActiveChart
  24.         .SetSourceData Source:=Range("工作表1!$B$1:工作表1!$B$" & totalRows & ", 工作表1!$C$1:工作表1!$F$" & totalRows)
  25.         .ChartType = xlStockOHLC
  26.         With .ChartGroups(1)
  27.              .AxisGroup = 1
  28.              ' .UpBars.Format.Fill.ForeColor.RGB = RGB(255, 0, 0)           ' 紅色
  29.              .UpBars.Format.Fill.ForeColor.RGB = RGB(255, 69, 0)          ' 橘紅色
  30.              ' .DownBars.Format.Fill.ForeColor.RGB = RGB(0, 32, 96)       ' 深藍色
  31.              .DownBars.Format.Fill.ForeColor.RGB = RGB(0, 250, 170)     ' 淺洋綠色
  32.         End With
  33.         
  34.         .SeriesCollection.Add Source:=Range("工作表1!$G$2:工作表1!$I$" & totalRows)
  35.         
  36.         With .SeriesCollection(5)
  37.             .AxisGroup = 2
  38.             .Name = "=工作表1!$G$1"
  39.             .ChartType = xlColumnClustered
  40.             
  41.             With .Format.Line
  42.                 .Visible = msoTrue          ' 成交量
  43.                 .ForeColor.RGB = RGB(147, 112, 219)        ' 成交量指數顯示之顏色    ---   紫色
  44.                 .Transparency = 0
  45.             End With
  46.         End With
  47.         
  48.         With .SeriesCollection(6)
  49.             .Name = "=工作表1!$H$1"
  50.             .ChartType = xlLine
  51.             With .Format.Line
  52.                 .Visible = msoTrue          ' 主力界入
  53.                 ' .ForeColor.RGB = RGB(105, 205, 170)      ' 主力界入指數顯示之顏色    ---   藍綠色
  54.                 .ForeColor.RGB = RGB(32, 178, 170)         ' 主力界入指數顯示之顏色    ---   海洋綠色
  55.                 .Transparency = 0
  56.             End With
  57.         End With
  58.         
  59.         With .SeriesCollection(7)
  60.             .Name = "=工作表1!$I$1"
  61.             .ChartType = xlLine
  62.             With .Format.Line
  63.                 .Visible = msoTrue          ' 散戶方向
  64.                 .ForeColor.RGB = RGB(65, 105, 225)         ' 散戶方向指數顯示之顏色    ---   天藍色
  65.                 .Transparency = 0
  66.             End With
  67.         End With
  68.         
  69.         .Axes(xlValue).TickLabels.NumberFormatLocal = "0_ "
  70.         
  71.         With .Axes(xlCategory)                            ' X座標軸 (時間軸)
  72.             .CategoryType = xlCategoryScale
  73.             .TickLabels.NumberFormatLocal = "hh:mm"
  74.             .MajorTickMark = xlNone
  75.             .Border.Weight = xlHairline
  76.             .Border.LineStyle = xlNone
  77.             .TickLabelPosition = xlLow
  78.             .TickLabels.Font.Size = 10
  79.         End With
  80.         
  81.         .ChartArea.Height = cHeight * sRowHeight                           ' 將原本設定之高度調至適度位置
  82.         .ChartArea.Width = cWidth
  83.         
  84.         text = "股票圖 K 線、主力、散戶、與成交量"
  85.         
  86.         .SetElement (msoElementChartTitleCenteredOverlay)
  87.         .ChartTitle.text = text
  88.         .ChartTitle.Format.TextFrame2.TextRange.Font.Size = 14
  89.         .Legend.Position = xlCorner                         ' 將圖表圖示從圖表最下方調整到圖表之右上角位置
  90.     End With
  91.    
  92.     chartname = Trim(Replace(ActiveChart.Name, ActiveSheet.Name, ""))
  93.     ActiveSheet.Shapes(chartname).Left = Cells(xRow, yCol).Left     ' 設定此圖表實際擺放的 X、Y 座標位置。
  94.     ActiveSheet.Shapes(chartname).Top = Cells(xRow, yCol).Top
  95.    
  96.     Range("A1").Select
  97. End Sub
複製代碼

TOP

回復 5# white5168
程式的模組化與註釋本來就很重要,但請注意註釋要標註到重點,
而非亂譜。打從八位元開始就已經從事軟體維護的我,真不知如何
向你解說看起來好亂的定義?

TOP

回復  c_c_lai
謝謝先進分享
值得借鏡使用
非常好的圖例
執行產生如下錯誤:
.SetElement (msoEleme ...
b9208 發表於 2012-10-7 09:21

Chart.SetElement 方法
會設定圖表上的圖表元素。可讀寫的 MsoChartElementType 物件。
版本資訊
新增的版本:  Excel 2007

TOP

謝謝說明
請教要在Excel 2003中執行如何修訂
謝謝
b9208 發表於 2012-10-7 21:57

我手頭上沒 2003 的版本,這個問題你可以請教 Hsien、GBKEE 兩位先進、或其他前輩。

TOP

回復 18# r2henry
這個版本(股票圖)只適用 2007 ~ 2010 的版本,
如要適用於 2003 須加以修改的。

TOP

回復 21# shan0948
這個版本(股票圖)只適用 2007 ~ 2010 的版本,
如要適用於 2003 須加以修改的。

TOP

回復 21# shan0948 [/b
Check your e-mail.

TOP

回復 23# barrykuo
股票圖表,收一下 Mail.

TOP

        靜思自在 : 好事要提得起,是非要放得下,成就別人即是成就自己。
返回列表 上一主題