返回列表 上一主題 發帖

[發問] 主座標軸與副座標軸群組間應如何正確設定

回復 1# c_c_lai
版本不圖 不易偵錯 ,寫個範例你參考一下
  1. Private Sub 製圖()
  2.     Dim xR As Range, Xi As Integer, i As Integer, 圖色()
  3.     圖色 = Array(4, 6, 8, 10)
  4.     ActiveSheet.ChartObjects.Delete         '刪除全部圖表
  5.     Set xR = Range("F1")    '利用xR的位置 設下圖表的位置
  6.         '加入新圖表  須指定 1右邊位置, 2高度位置, 3圖表的Width, 4圖表的Height
  7.         With ChartObjects.Add(xR.Left, xR.Top, xR.Resize(, 10).Width, xR.Resize(10).Height).Chart
  8.             .ChartType = 51  ' xlLineMarkers    '折線圖                             '圖表 式樣
  9.             With ActiveSheet.Range("A11").CurrentRegion
  10.                     Set xR = Union(.Columns(1), .Columns(3), .Columns(5))
  11.                     '資料 範圍的 1,3,5 欄為圖表資料  :第1欄 為x軸的數值
  12.             End With
  13.             .SetSourceData Source:=xR, PlotBy:=xlColumns
  14.             '圖表資料來源 : xR  資料式樣: 欄
  15.                           
  16.              .HasTitle = True                                              '顯示 圖表標題
  17.              .ChartTitle.Characters.Text = ActiveSheet.Name                'HasTitle = False 會有錯誤
  18.              .HasAxis(xlCategory, xlPrimary) = False                       '不顯示 X軸座標
  19.             
  20.             'HasLegend 圖例
  21.              .HasLegend = False                                         ' 取消 顯示 圖例
  22.            '  .Legend.Position = xlTop                                   '顯示 圖例位置
  23.            ' .Axes(xlCategory).TickLabels.NumberFormatLocal = "m/d;@"    ' Axes(xlCategory) X座標軸
  24.            ' ** 上二式 須  顯示圖例 ->  .HasLegend = True
  25.             
  26.             '.SeriesCollection(1).AxisGroup = 2                           '新增 Y座標軸 副座標軸
  27.           '  .SeriesCollection(3)).Delete                 '刪除第3數列資料
  28.             With .Axes(xlValue)          'y軸格線
  29.                 .HasMajorGridlines = 0   '取消 主格線
  30.                 .HasMinorGridlines = 0   '取消 副格線
  31.             End With
  32.             '以下一些程式碼 你可用錄製得知
  33.             With .ChartArea                                             '圖表的圖表區
  34.                 .Border.Weight = 1
  35.                 .Border.LineStyle = -1
  36.                 .Fill.OneColorGradient Style:=msoGradientHorizontal, Variant:=3, Degree:=0.231372549019608
  37.                 .Fill.Visible = True
  38.                 .Fill.ForeColor.SchemeColor = 圖色(0)
  39.             End With
  40.             With .PlotArea                                              '圖表的 繪圖區
  41.                 .Fill.OneColorGradient Style:=msoGradientHorizontal, Variant:=1, _
  42.                 Degree:=0.231372549019608
  43.                 .Fill.Visible = True
  44.                 .Fill.ForeColor.SchemeColor = 圖色(0)
  45.                 With .Border
  46.                     .ColorIndex = 16
  47.                     .Weight = xlThin
  48.                     .LineStyle = xlContinuous
  49.                 End With
  50.             End With
  51.             For i = 1 To .SeriesCollection.Count
  52.                 With .SeriesCollection(i)   '數列
  53.                   '  .XValues = ""           '<-為x軸的數值   可在此取消
  54.                     With .Border
  55.                         .ColorIndex = 圖色(i)
  56.                         .Weight = xlMedium
  57.                         .LineStyle = xlContinuous
  58.                     End With
  59.                     '以下屬性 只適合 折線圖
  60.                    ' .MarkerBackgroundColorIndex = xlAutomatic
  61.                     '.MarkerForegroundColorIndex = xlAutomatic
  62.                     '.MarkerStyle = xlNone
  63.                     '.Smooth = False
  64.                     '.MarkerSize = 7
  65.                     '.Shadow = False
  66.                 End With
  67.             Next
  68.         End With
  69. End Sub
複製代碼

TOP

回復 3# c_c_lai
  1. Sub setRowColumn(sDraw As String)
  2.     Dim oShape As Shape
  3.     Dim xRow, yCol, cHeight, cWidth, inLeft, inTop, inWidth As Integer
  4.     Dim totalRows As Single
  5.     Dim chartname As String
  6.     'totalRows = Sheets("統計圖表").Range("B" & Rows.Count).End(xlUp).Row     ' 傳回 B 欄所使用儲存格之最後一格之列號
  7.     '如作用中物件 不是工作表 會有錯誤 如是圖表等
  8.     totalRows = Sheets("統計圖表").Range("B" & Sheets("統計圖表").Rows.Count).End(xlUp).Row     ' 傳回 B 欄所使用儲存格之最後一格之列號
  9.     Sheets(sDraw).Select
  10.     For Each oShape In ActiveSheet.Shapes
  11.         If oShape.Type = 3 Then
  12.             ActiveSheet.ChartObjects(oShape.Name).Activate                      ' 滑鼠點在 "圖表 #" 上 (例如:主力界入)。
  13.             '*************
  14.             '你上式程式碼  使得作用中物件變為 圖表 後面的程式如有 取的工作表的位址 皆須加上  Sheets((工作表).[A1] 等...
  15.             ActiveChart.SetSourceData Source:=Range("統計圖表!$B$1:統計圖表!$B$" & totalRows & ", 統計圖表!$F$1:統計圖表!$F$" & totalRows & _
  16.             ", 統計圖表!$I$1:統計圖表!$J$" & totalRows & ", 統計圖表!$S$1" & totalRows)  ' 圖示會分別顯示出 成交價、主力界入、以及散戶方向。
  17.             
  18.             'ActiveChart.SeriesCollection.Add Source:=Range("統計圖表!$S$1"& totalRows)
  19.             'ActiveChart.SeriesCollection(4).Name = "=統計圖表!$S$1"  '*** 位址要用R1C1 方式表示
  20.             'ActiveChart.SeriesCollection(4).Name = "=" & [統計圖表!$S$1].Address(, , xlR1C1, 1)
  21.             'ActiveChart.SeriesCollection(4).ChartType = xlColumnClustered          ' 堆疊直條圖
  22.             xRow = 2
  23.             yCol = 1
  24.             cHeight = 488
  25.             cWidth = 450
  26.             inLeft = 30
  27.             inTop = 30
  28.             inWidth = 378
  29.             chartname = Trim(Replace(ActiveChart.Name, ActiveSheet.Name, ""))
  30.             'ActiveChart.ChartArea.Height = cHeight                           ' 將原本設定之高度調至適度位置
  31.             'ActiveChart.ChartArea.Width = cWidth
  32.             'ActiveSheet.Shapes(chartname).Left = Cells(xRow, yCol).Left     ' 設定此圖表實際擺放的 X、Y 座標位置。
  33.             'ActiveSheet.Shapes(chartname).Top = Cells(xRow, yCol).Top
  34.             With ActiveChart.Parent
  35.                 .Width = cWidth                                       'ChartArea.-改成 父層  Parent
  36.                 .Height = cHeight
  37.                 .Left = ActiveSheet.Cells(xRow, yCol).Left
  38.                 .Top = ActiveSheet.Cells(xRow, yCol).Top
  39.                 With .Chart.PlotArea                  ' 實際繪圖區塊的範圍設定
  40.                   Debug.Print .InsideLeft             '  傳回: 繪圖區塊範圍設定
  41.                   Debug.Print .InsideTop              'Debug.Print: 印列在->   在檢視指令->即時運算視窗
  42.                   Debug.Print .InsideWidth
  43.                     .Left = inLeft                    '  設定: 繪圖區塊範圍
  44.                     .Top = inTop
  45.                     .Width = inWidth
  46.                 End With
  47.             End With
  48.         End If
  49.     Next
  50. End Sub
複製代碼

TOP

        靜思自在 : 道德是提昇自我的明燈,不該是呵斥別人的鞭子。
返回列表 上一主題