Results 1 to 9 of 9

Thread: [RESOLVED] MSChart- Simple example or explaination needed.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Resolved [RESOLVED] MSChart- Simple example or explaination needed.

    Does anyone have a simple example of creating a chart in VB that I can try to follow? I don't know where to start. I've searched for this and found a few but they have been far more comprehensive than what I need. I just need something simple to replicate the attached image.

    Would the MSChart be the best control to do this?
    Attached Images Attached Images  

  2. #2
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    Re: MSChart- Simple example or explaination needed.

    Yes use MSChart for that. Place one on your charts on a form and inside the properties go to custom and click the ... you will be able to format it to match what you have above.

    set data like:
    VB Code:
    1. Form1.MSChart1.Data = 40

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: MSChart- Simple example or explaination needed.

    Thanks ggettings, rightclicking on the chart for properties opens up a number of options. Starting to play with them now

    If I have two arrays of X and Y values for the two different lines, how do I use them for input? Two lines, different colours, and possibly a legend as I have above.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    376

    Re: MSChart- Simple example or explaination needed.

    Think of it like a msflexgrid with rows and cols.
    Adding colums will automatically give your new line a different color.

    rowcount is the number of intervals
    colcount is the numner of lines

    try something like this:
    VB Code:
    1. dim i as integer
    2. i = 0
    3.  
    4. Do Until i > your max array size
    5.  
    6.     mschart1.column = 1
    7.     mschart1.row = i
    8.     mschart1.data = val(i)
    9.  
    10.     i = i + 1
    11. Loop

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: MSChart- Simple example or explaination needed.

    Thanks for the help but I'm still not quite following this. Why are the three variables? Is column the X coordinate and row the Y coordinate and then what is data?

    I tried this in an XY scatter chart but oddly when I run it it just shows as the demo control.

    VB Code:
    1. Private Sub Form_Load()
    2.     MSChart1.Column = 1
    3.     MSChart1.Row = 1
    4.     MSChart1.Data = 1
    5.     MSChart1.Column = 2
    6.     MSChart1.Row = 2
    7.     MSChart1.Data = 2
    8. End Sub
    Attached Images Attached Images  

  6. #6
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    Re: MSChart- Simple example or explaination needed.

    Sgrya1:

    The MSChart control has many visible parts, all of which can be programmed.

    Each of these parts has a corresponding object in the MSChart control which can be used to change the format of practically any element of your chart.

    For example, the code below dramatically changes the look of a chart by changing the colors of the Plot object.

    VB Code:
    1. Private Sub cmdFormat_Click()
    2.    ' First, change the chart type to a 3D chart to
    3.    ' see all the parts of the plot.
    4.    MSChart1.chartType = VtChChartType3dArea
    5.  
    6.    ' Color the backdrop light blue using
    7.    ' the Plot object.
    8.    With MSChart1.Plot.Backdrop
    9.       ' No color will appear unless you set the style
    10.       ' property to VtFillStyleBrush.
    11.       .Fill.Style = VtFillStyleBrush
    12.       .Fill.Brush.FillColor.Set 100, 255, 200
    13.       ' Add a border.
    14.       .Frame.Style = VtFrameStyleThickInner
    15.       ' Set style to show a shadow.
    16.       .Shadow.Style = VtShadowStyleDrop    
    17.    End With
    18.  
    19.    ' Color the wall of the plot yellow.
    20.    With MSChart1.Plot
    21.       ' Set the style to solid.
    22.       .Wall.Brush.Style = VtBrushStyleSolid
    23.       ' Set the color to yellow.
    24.       .Wall.Brush.FillColor.Set 255, 255, 0
    25.    End With
    26.    
    27.    With MSChart1.Plot ' Color the plot base blue.
    28.       .PlotBase.BaseHeight = 200
    29.       .PlotBase.Brush.Style = VtBrushStyleSolid
    30.       .PlotBase.Brush.FillColor.Set 0, 0, 255
    31.    End With
    32. End Sub

    A very basic task with fonts might be to set the text of the chart's title. In order to do this, use the Title object's Text property:

    VB Code:
    1. MSChart1.Title.Text = "Year End Summary"
    This is simple enough. The next question is how to change the font's attributes.

    In order to format any text attribute on the chart, you must use the VtFont object. For example, to format the title, the following code will work:

    VB Code:
    1. With MSChart1.Title.VtFont
    2.    .Name = "Algerian"
    3.    .Style = VtFontStyleBold
    4.    .Effect = VtFontEffectUnderline
    5.    .Size = 14
    6.    .VtColor.Set 255, 0, 255
    7. End With

    You can use the same technique with other parts of chart. The only difference lies in the object model. For example, to format the text attributes of the Legend area use the following code:

    VB Code:
    1. MSChart1.Legend.VtFont.Size = 18
    To change the scale of the plot, you must specify that the y axis of the chart is going to be changed (changing the x axis has no visible effect). A convenient way to change the scale is to use a ComboBox control, as shown in the following code:

    VB Code:
    1. Private Sub Form_Load()
    2.    ' Configure a ComboBox control named cmbScale.
    3.    With cmbScale
    4.       .AddItem "Log"
    5.       .AddItem "Percent"
    6.       .AddItem "Linear"
    7.       .ListIndex = 0
    8.    End With
    9. End Sub
    10.  
    11. Private Sub cmbScale_Click()
    12.    ' The ComboBox has three items: Log, Percent,
    13.    ' and Linear (the default scale).
    14.  
    15.    Select Case cmbScale.Text
    16.    Case "Log"
    17.       MSChart1.Plot.Axis(VtChAxisIdY) _
    18.       .AxisScale.Type = VtChScaleTypeLogarithmic
    19.  
    20.       ' You must specify a LogBase to be used when
    21.       ' switching the scale to Log. The base can be
    22.       ' set to any value between 2 and 200.
    23.       MSChart1.Plot.Axis(VtChAxisIdY).AxisScale _
    24.       .LogBase = 10    
    25.  
    26.    Case "Percent"
    27.       MSChart1.Plot.Axis(VtChAxisIdY).AxisScale _
    28.       .Type = VtChScaleTypePercent
    29.       ' Set the PercentBasis to one of six types. For
    30.       ' the sake of expediency, only one is shown.
    31.       MSChart1.Plot.Axis(VtChAxisIdY).AxisScale _
    32.       .PercentBasis = VtChPercentAxisBasisMaxChart
    33.  
    34.    Case "Linear"
    35.       MSChart1.Plot.Axis(VtChAxisIdY).AxisScale _
    36.       .Type = VtChScaleTypeLinear
    37.    End Select
    38. End Sub

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: MSChart- Simple example or explaination needed.

    AIS4U, Thanks for that. There's a lot there that I can access by right clicking the chart and selecting properties but there does seem to be some formatting properties that aren't directly accessible.
    I don't need to change too much of the chart format at runtime other than adding chart lines and changing the chart scales. Do you have any suggestions on how to do this?

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: MSChart- Simple example or explaination needed.

    I found this. Anyone know how to add a second line to a chart though? The second overrides the first when I try.

    VB Code:
    1. Private Sub Form_Load()
    2.          Dim Graph(1 To 10, 1 To 2) As Single
    3.          Dim x As Integer
    4.          For x = 1 To 10
    5.             Graph(x, 1) = x   ' value for X-axis
    6.             Graph(x, 2) = x * 10 ' value for Y-axis
    7.          Next x
    8.          MSChart1.chartType = VtChChartType2dXY  ' set to X Y Scatter chart
    9.          MSChart1 = Graph ' populate chart's data grid using Graph array
    10.          ' Leave the following line commented until step 6:
    11.          MSChart1.Plot.UniformAxis = False
    12.       End Sub

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: MSChart- Simple example or explaination needed.

    Found this very comprehensive code too. Problem solved.

    VB Code:
    1. Option Explicit
    2. '**************************************
    3. 'Windows API/Global Declarations for :Creating 2D XY scatter charts on an unbound MSChart Control
    4. '**************************************
    5.  
    6. 'This example shows how to plot multiple X-Y scatter graphs, also known as 2D XY, on an unbound MS Chart
    7. 'control. X-Y scatter graphs differ from other types because the associated DataGrid object needs
    8. '2 columns per series, rather than just one. The first column for each series stores the X values, and
    9. 'the second one stores the Y values. Another difference is that if the # of plot points differs
    10. 'between multiple series, you have to remove the null points of the shorter series.
    11. 'Instructions: Add the MSChart control to your toolbox, then add it to a form. For clarity, try to make
    12. 'it at least 8 inches wide, by 5 inches tall, on the form. then, paste this code into
    13. 'the code window, and run.
    14. 'written by W. Baldwin, 8/2001
    15.  
    16. 'OldRowCount keeps track of how many points have been plotted for the previous series, so we can remove
    17. 'null points from all the series that are shorter:
    18. Dim OldRowCount As Long
    19. 'PenColor determines whether we are drawing in color or Black & White
    20. 'Black & White is for black and white printers, and uses different line patterns to distinguish series.
    21. 'ShowMarker is a flag that determines whether each plot point has a marker or not.
    22. Dim PenColor As Boolean, ShowMarker As Boolean
    23. 'ChartPoints is the array that will hold the plot data
    24. Dim ChartPoints() As Double
    25. Dim lRow As Long, lRow2 As Long
    26. Dim i As Integer, MsgPrompt As String
    27. Dim XValue As Single, YValue As Single
    28.  
    29. Private Sub Form_Load()
    30.     With MSChart1
    31.         .chartType = VtChChartType2dXY
    32.         .ShowLegend = True
    33.  
    34.         With .Plot.Axis(VtChAxisIdY).AxisTitle
    35.             .VtFont.Size = 12
    36.             .Visible = True
    37.             .Text = "Y Axis text"
    38.         End With
    39.  
    40.         With .Plot.Axis(VtChAxisIdX).AxisTitle
    41.             .VtFont.Size = 12
    42.             .Visible = True
    43.             .Text = "X Axis text"
    44.         End With
    45.         .Title.VtFont.Size = 12
    46.         .Title = "Example 2D XY Scatter Graph"
    47.         .Legend.Location.LocationType = VtChLocationTypeBottom
    48.         .Plot.Axis(VtChAxisIdY).AxisScale.Type = VtChScaleTypeLinear
    49.         .Plot.Axis(VtChAxisIdX).AxisScale.Type = VtChScaleTypeLinear
    50.         'Tip from KB article Q194221:
    51.         .Plot.UniformAxis = False
    52.         .Footnote.Text = "Footnote goes here"
    53.     End With
    54.     PenColor = True 'Draw In color
    55.     ShowMarker = True 'Show plot points
    56.     ChartIt 1
    57.     PenColor = False 'Black and White
    58.     ShowMarker = False 'Don't show plot points
    59.     ChartIt 2
    60. End Sub
    61.  
    62.  
    63. Private Sub ChartIt(CurSeries As Integer)
    64.     MousePointer = 11
    65.     'Create a new array of plot points for this Series
    66.     'We will redim the first subscript differently, to show that each series can have a different # of plot points:
    67.  
    68.     If CurSeries = 1 Then
    69.         ReDim ChartPoints(1 To 15, 1 To 2)
    70.     Else
    71.         ReDim ChartPoints(1 To 10, 1 To 2)
    72.     End If
    73.     'Create the array data:
    74.  
    75.     For lRow = 1 To UBound(ChartPoints, 1)
    76.         'create the X and Y values:
    77.         XValue = lRow + Rnd * 2
    78.         YValue = lRow + Rnd * 2
    79.         'create negative values for the 2nd series:
    80.  
    81.         If CurSeries = 2 Then
    82.             XValue = XValue * -1
    83.             YValue = YValue * -1
    84.         End If
    85.         ChartPoints(lRow, 1) = XValue
    86.         ChartPoints(lRow, 2) = YValue
    87.     Next lRow
    88.     'We need to increase the ColumnCount. For X-Y Scatter graphs, we need 2 columns for each series.
    89.     MSChart1.ColumnCount = CurSeries * 2
    90.  
    91.     With MSChart1
    92.         With .Plot
    93.             .Wall.Brush.Style = VtBrushStyleSolid
    94.             'Normally, you might want the Wall background of the Chart
    95.             'to be in color, if you're using Color pens, and to be white
    96.             'if using B&W pens, but, since we're drawing both a color
    97.             'series *and* a B&W series on *one* chart, we'll just make
    98.             'the wall color, for now. If you want White, uncomment the
    99.             'line found about 10 lines down:
    100.             .Wall.Brush.FillColor.Set 255, 255, 225
    101.  
    102.             If PenColor Then
    103.                 .Wall.Brush.FillColor.Set 255, 255, 225
    104.                 'You can set the individual Pen colors here, or just use
    105.                 'the defaults.
    106.             Else 'Based on an article In the VB KB:
    107.                 'Uncomment the next line if you want the wall color to
    108.                 'be white:
    109.                 '.Wall.Brush.FillColor.Set 255, 255, 255
    110.                 '
    111.                 'Set the different patterns for Black and White plotting.
    112.                 'You need to set the Pen for only the 'X column:
    113.  
    114.                 Select Case CurSeries * 2 - 1
    115.                     Case 1
    116.                     .SeriesCollection(1).Pen.Style = VtPenStyleSolid
    117.                     .SeriesCollection(1).Pen.VtColor.Set 0, 0, 0
    118.                     Case 3
    119.                     .SeriesCollection(3).Pen.Style = VtPenStyleDashed
    120.                     .SeriesCollection(3).Pen.VtColor.Set 0, 0, 0
    121.                     Case 5
    122.                     .SeriesCollection(5).Pen.Style = VtPenStyleDotted
    123.                     .SeriesCollection(5).Pen.VtColor.Set 0, 0, 0
    124.                     Case 7
    125.                    
    126.                     .SeriesCollection(7).Pen.Style = VtPenStyleDitted
    127.                     .SeriesCollection(7).Pen.VtColor.Set 0, 0, 0
    128.                 End Select
    129.         End If
    130.     End With
    131.     .ColumnLabelCount = CurSeries * 2
    132.     'If the current series has more plot points that the previous
    133.     'one, we need to change .RowCount accordingly:
    134.  
    135.     If UBound(ChartPoints, 1) > OldRowCount& Then
    136.         .RowCount = UBound(ChartPoints, 1)
    137.     End If
    138.     'Both of the next 2 lines seem to do thesame thing:
    139.     .Plot.SeriesCollection(CurSeries * 2 - 1).SeriesMarker.Show = ShowMarker
    140.     .Plot.SeriesCollection.Item(CurSeries * 2 - 1).SeriesMarker.Show = ShowMarker
    141.     'Create the plot points for this series from the ChartPoints array:
    142.  
    143.     For lRow = 1 To UBound(ChartPoints, 1)
    144.         .DataGrid.SetData lRow, CurSeries * 2 - 1, ChartPoints(lRow, 1), False
    145.         .DataGrid.SetData lRow, CurSeries * 2, ChartPoints(lRow, 2), False
    146.     Next
    147.     'Remove null points from *this* series,if it has *fewer*
    148.     'points than the prior ones. If you don't remove null points,
    149.     'then the graph will add 0,0 points, erroneously. See MS
    150.     'Knowledge Base article Q177685 for more info:
    151.  
    152.     For lRow2 = lRow To OldRowCount&
    153.         .DataGrid.SetData lRow2, CurSeries * 2 - 1, 0, True
    154.         .DataGrid.SetData lRow2, CurSeries * 2, 0, True
    155.     Next
    156.     'Remove null points from *prior* series,if this series
    157.     'has *more* points than the prior ones:
    158.  
    159.     If CurSeries > 1 Then
    160.         For lRow = OldRowCount& + 1 To .RowCount
    161.             For lRow2 = 1 To CurSeries - 1
    162.                 .DataGrid.SetData lRow, lRow2 * 2 - 1, 0, True
    163.                 .DataGrid.SetData lRow, lRow2 * 2, 0, True
    164.             Next
    165.         Next
    166.     End If
    167.     'Store the current RowCount
    168.     OldRowCount& = .RowCount
    169.     .Column = CurSeries * 2 - 1
    170.     .ColumnLabel = "Series " & Str(CurSeries)
    171.     .Refresh
    172. End With
    173.  
    174. SubExit:
    175.     MousePointer = 0
    176. End Sub
    177.  
    178.  
    179. Private Sub Command1_Click()
    180.     'If you want to be able to print the chart, just
    181.     'add a Command1 button that calls this event.
    182.     MsgPrompt = "Make sure that device " & Printer.DeviceName & " is ready"
    183.     i = MsgBox(MsgPrompt, vbOKCancel, "Confirmation")
    184.  
    185.     If i = vbCancel Then
    186.         Exit Sub
    187.     End If
    188.     On Error GoTo ErrHandler
    189.     MSChart1.EditCopy
    190.     Printer.Print " "
    191.     Printer.PaintPicture Clipboard.GetData(), 0, 0
    192.     Printer.EndDoc
    193.     Exit Sub
    194. ErrHandler:
    195.     i = MsgBox("An Error has occurred. Make sure your selected printer can print graphics.", vbOKOnly, "Error")
    196.     Resume Next
    197. End Sub

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