Results 1 to 5 of 5

Thread: Excel Graphing

Threaded View

  1. #1

    Thread Starter
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Excel Graphing

    This is some code to aid in the production of Excel graphs. It is intended mainly as a foundation rather than a complete code for all possibilities, as the full range of Excel graphs and their associated requirements are immense. It can be used to display Line graphs, Scatter graphs and Column graphs, and will do much of the formatting (to your specifications).
    It is fairly easy to adapt for other graph types and other requirements, such as trendlines etc, can be easily inserted by the user.
    Just copy and paste the code to a VBA module.

    Coded in Excel XP (2002).

    There is also a handy Enum of all the colour codes used by Excel .

    Have fun.

    All comments / bugs / suggestions welcome.

    zaza

    -------------------------------------------------

    Usage:

    First, call creategraph to set the graph up. Then call setgraphdata to add the data series. Then define any fonts that you wish to use for Main title, axes titles and axes, then call setgraphstyle.

    VB Code:
    1. Call creategraph(ActiveSheet, "Bobbins", LineGraph, xlColumns, 200, 200, 400, 300)
    2.    
    3.     Call setgraphdata(ActiveSheet, "Bobbins", Range("B2:B6"), Range("C2:C6"), xlPrimary, "Line1", xlMarkerStyleCircle, 6, Blue, Black, xlContinuous, xlThin, Blue, True)
    4.     Call setgraphdata(ActiveSheet, "Bobbins", Range("B2:B6"), Range("E2:E6"), xlPrimary, "Line2", xlMarkerStyleCircle, 6, Green, Black, xlLineStyleNone, xlThin, Green, True)
    5.     Call setgraphdata(ActiveSheet, "Bobbins", Range("B2:B6"), Range("G2:G6"), xlPrimary, "Line3", xlMarkerStyleCircle, 6, Red, Black, xlContinuous, xlThin, Red, True)
    6.  
    7.     Dim graphfont As New NewFont
    8.     graphfont.Name = "Times New Roman"
    9.     graphfont.Size = 12
    10.     graphfont.Bold = True
    11.    
    12.     Dim axisfont As New NewFont
    13.     axisfont.Name = "Times New Roman"
    14.     axisfont.Size = 10
    15.     axisfont.Bold = True
    16.    
    17.     Dim datafont As New NewFont
    18.     datafont.Name = "Times New Roman"
    19.     datafont.Size = 9
    20.     datafont.Italic = True
    21.  
    22.     Call setgraphstyle(ActiveSheet, "Bobbins", "My Chart", "x-axis", "y-axis", , White, Black, True, False, graphfont, axisfont, datafont, 0, 6, 0, 25, , , False, False, xlTickMarkInside, xlTickMarkInside)

    -------------------------------------------------

    Code:

    VB Code:
    1. Public Enum Colour  'Colours list
    2.     None = -4142
    3.     Black = 1
    4.     White = 2
    5.     Red = 3
    6.     BrightGreen = 4
    7.     Blue = 5
    8.     Yellow = 6
    9.     Magenta = 7
    10.     Cyan = 8
    11.     DarkRed = 9
    12.     Green = 10
    13.     DarkBlue = 11
    14.     DarkYellow = 12
    15.     Violet = 13
    16.     Teal = 14
    17.     Grey25 = 15
    18.     Grey50 = 16
    19.     Lilac = 17
    20.     Cream = 19
    21.     DarkViolet = 21
    22.     Pink = 22
    23.     MidBlue = 23
    24.     PaleLavender = 24
    25.     SkyBlue = 33
    26.     LightCyan = 34
    27.     LightGreen = 35
    28.     LightYellow = 36
    29.     PaleBlue = 37
    30.     Rose = 38
    31.     Lavender = 39
    32.     Tan = 40
    33.     LightBlue = 41
    34.     Aqua = 42
    35.     Lime = 43
    36.     Gold = 44
    37.     LightOrange = 45
    38.     Orange = 46
    39.     BlueGrey = 47
    40.     Grey40 = 48
    41.     DarkTeal = 49
    42.     SeaGreen = 50
    43.     DarkGreen = 51
    44.     Olive = 52
    45.     Brown = 53
    46.     Plum = 54
    47.     Indigo = 55
    48.     Grey80 = 56
    49. End Enum
    50.  
    51. Public Enum chartstyle  'Chart styles
    52.     LineGraph = xlLine
    53.     ScatterGraph = xlXYScatter
    54.     ColumnGraph = xlColumnClustered
    55. End Enum
    56.  
    57. Sub creategraph(ByVal wksht As Worksheet, ByVal chtname As String, ByVal chttype As chartstyle, ByVal chtplotby As XlRowCol, ByVal chtleft As Double, ByVal chttop As Double, ByVal chtwidth As Double, ByVal chtheight As Double)
    58.    
    59.     '-------------------------------------------------
    60.     '   Parameters:
    61.     '
    62.     '   Worksheet
    63.     '   Your choice of chart name
    64.     '   The chart type
    65.     '   The left, top, width and height of the chart
    66.     '
    67.     '-------------------------------------------------
    68.    
    69.    
    70.     Dim newcht As ChartObject
    71.    
    72.     Set newcht = wksht.ChartObjects.Add(chtleft, chttop, chtwidth, chtheight)
    73.     newcht.Name = chtname
    74.     newcht.Chart.charttype = chttype
    75.    
    76.    
    77. End Sub
    78.  
    79.  
    80. Sub setgraphdata(ByVal wksht As Worksheet, ByVal chtname As String, ByVal chtxaxis As Range, ByVal chtyaxis As Range, ByVal chtPS As XlAxisGroup, ByVal chtlinename As String, ByVal chtmarkerstyle As XlMarkerStyle, ByVal chtmarkersize As Integer, ByVal chtmarkerbkcolor As Colour, ByVal chtmarkerfcolor As Colour, ByVal chtline As XlLineStyle, ByVal chtlinewt As XlBorderWeight, ByVal chtlinecol As Colour, ByVal chtlinesmooth As Boolean)
    81.  
    82.     '-------------------------------------------------
    83.     '   Parameters:
    84.     '
    85.     '   Worksheet
    86.     '   Chart name
    87.     '   x-axis range
    88.     '   y-axis range
    89.     '   y-axis group
    90.     '   Name of series for legend
    91.     '   Point style
    92.     '   Point size
    93.     '   Point back colour
    94.     '   Point fore colour
    95.     '   Line style
    96.     '   Line weight
    97.     '   Line colour
    98.     '   Smooth? (True/False)
    99.     '
    100.     '-------------------------------------------------
    101.    
    102.    
    103.    
    104.     Dim newcht As ChartObject
    105.     Dim ns As Series
    106.    
    107.     Set newcht = wksht.ChartObjects(chtname)
    108.     Set ns = newcht.Chart.SeriesCollection.NewSeries
    109.  
    110.  
    111.     'Set series parameters
    112.    
    113.     If newcht.Chart.charttype <> chartstyle.ColumnGraph Then
    114.    
    115.         With ns
    116.             .XValues = chtxaxis
    117.             .Values = chtyaxis
    118.             .AxisGroup = chtPS
    119.             .Name = chtlinename
    120.             If chtmarkerstyle <> xlMarkerStyleNone Then
    121.                 .MarkerBackgroundColorIndex = chtmarkerbkcolor
    122.                 .MarkerForegroundColorIndex = chtmarkerfcolor
    123.                 .MarkerStyle = chtmarkerstyle
    124.                 .MarkerSize = chtmarkersize
    125.             End If
    126.             .Smooth = chtlinesmooth
    127.         End With
    128.      
    129.    
    130.         'Set linestyle parameters
    131.         With ns.Border
    132.             .LineStyle = chtline
    133.             If chtline <> xlLineStyleNone Then
    134.                 .ColorIndex = chtlinecol
    135.                 .Weight = chtlinewt
    136.             End If
    137.         End With
    138.  
    139.     Else
    140.    
    141.         With ns
    142.             .XValues = chtxaxis
    143.             .Values = chtyaxis
    144.             .AxisGroup = chtPS
    145.             .Name = chtlinename
    146.             .Interior.ColorIndex = chtmarkerbkcolor
    147.         End With
    148.    
    149.     End If
    150.  
    151. End Sub
    Last edited by zaza; Sep 5th, 2006 at 03:46 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width