Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb6 chart graphics.

Posted on 2012-12-28
8
Medium Priority
?
838 Views
Last Modified: 2013-01-06
Dear Experts, I would ike to use the vb6 chart control to show a bar and line chart of data from a csv file, I'm not sure if it can be done but is it possible to get the chart to accept data from a csv file and plot a bar and line chart from the data in the file. I would also like to find the equation of the line chart and display this in a textbox. Thank you for any help you can give. The csv file is sorted by frequency in ascending order.

The csv file data is in the form as shown below:

Number     Frequency
39             34
26             76
15             120
etc            etc
0
Comment
Question by:AWaterfalls
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 9

Expert Comment

by:shorvath
ID: 38730919
any sort order for x or y values?
0
 

Author Comment

by:AWaterfalls
ID: 38731373
Sort  Ascending Frequency (y)
0
 

Author Comment

by:AWaterfalls
ID: 38731840
No Further Comment.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:eemit
ID: 38734419
Try this:

Insert a Microsoft Chart Control into your project.
(On the Project menu, click Components)
Microsoft Chart Control 6.0 (SP4) (OLEDB)
      (on Windows XP: c:\windows\system32\MSCHRT20.OCX)

Private Sub Form_Load()
  MSChart1.Visible = False
End Sub

Private Sub Command1_Click()

  Dim i As Long
  Dim j As Long
  Dim sHeader As String
  Dim sLine As String
  Dim aItems() As String
  
  Dim sFileName As String
  Dim nFileNum As Long
  Dim aData() As String

  Screen.MousePointer = vbHourglass
  
  nFileNum = FreeFile
  
  Open App.Path & "\" & "yourcsv.csv" For Input As #nFileNum
  
  '/ set headers
  Line Input #nFileNum, sHeader
  aItems = Split(sHeader, ",")
  
  ' When you create a multi-dimensioned array, the first series can be assigned a string;
  ' when the array is assigned to the ChartData property, the strings become the labels for the rows.
  ReDim Preserve aData(LBound(aItems) To UBound(aItems), i)

  For j = 0 To UBound(aItems)
      aData(j, i) = aItems(j)
  Next j
  '/

  ' loop through file and populate rest of the multi-dimensioned array
  i = 1
  Do While Not EOF(nFileNum)
      ' read line
      Line Input #nFileNum, sLine
      If Not sLine = sHeader Then
          ' split values on comma
          aItems = Split(sLine, ",")
          
          ' When you're using ReDim Preserve on a multidimensional array,
          ' you can resize only its last dimension:
          ReDim Preserve aData(UBound(aItems), i)

          For j = 0 To UBound(aItems)
              aData(j, i) = aItems(j)
          Next j

          i = i + 1
      End If
  Loop
  
  Close #nFileNum

  '/ create a multi-dimensioned array for our chart
  Dim n As Long
  ReDim aDataForChart(i - 1, UBound(aItems))
  For n = 0 To i - 1
      For j = 0 To UBound(aItems)
          aDataForChart(n, j) = aData(j, n)
      Next j
  Next n
  '/

  With MSChart1
  
      .Visible = True
      
      '/ set the chart type
      ' set the chart type to a 2d Line
      .chartType = VtChChartType2dLine
      
      ' set the chart type to a 2d Bar
      '.chartType = VtChChartType2dBar
      '/
      
      ' Plot Data Using Array and the ChartData Property
      .ChartData = aDataForChart
  
      .Height = 3400
    
      ' add chart title
      '.TitleText = "Frequency of Numbers"
      With .Title
          .Text = "Frequency of Numbers"
          .VtFont.Size = 12
          .VtFont.VtColor.Set 255, 0, 0
      End With
    
      ' set chart Labels
      ' (aDataForChart(0,0) : "Number")
      ' (aDataForChart(0,1) : " Frequency")
      For n = 1 To i - 1
          .Row = n
          .RowLabel = aDataForChart(n, 0)
      Next n
    
      Dim serX As Series
      For Each serX In MSChart1.Plot.SeriesCollection
          If serX.LegendText = "Number" Then
              ' Hide "Number" Serie
              serX.Position.Hidden = True
              
              ' Set XAxis Title
              'Dim XAxis As MSChart20Lib.Axis
              'Set XAxis = .Plot.Axis(VtChAxisIdX, 1)
              'XAxis.AxisTitle = serX.LegendText
              
              .Plot.Axis(VtChAxisIdX, 1).AxisTitle = serX.LegendText
              
          ElseIf serX.LegendText = "Frequency" Then
  
              With serX.StatLine
                  ' VtChStatsMean - Shows the mathematical mean of the values in the series. 
                  .Flag = VtChStatsMean
                  .Style(VtChStatsMean) = VtPenStyleDitted  'VtPenStyleDashed  'VtPenStyleDotted
                  .VtColor.Set 255, 0, 0
                  .Width = 1
              End With
              
              ' Set YAxis Title
              ' VtChAxisIdY2 Identifiziert die sekundäre Y-Achse
              'Dim YAxis As MSChart20Lib.Axis
              'Set YAxis = .Plot.Axis(VtChAxisIdY2, 1)
              'YAxis.AxisTitle = serX.LegendText
              
              .Plot.Axis(VtChAxisIdY2, 1).AxisTitle = serX.LegendText
              
          End If
      Next
      
      ' Set Chart footnote
      With .Footnote
          .Location.Visible = True
          .Location.LocationType = VtChLocationTypeBottomLeft
          .Text = "sorted by frequency in ascending order"
          .VtFont.VtColor.Set 128, 128, 128
      End With
      
  End With
  
  Screen.MousePointer = vbDefault

End Sub

Open in new window


The code above produces this chart:
screenshot of the chart
for this csv:
Number, Frequency
39,12
26,27
44,46
5,54
10,68
19,77
30,120
0
 

Author Comment

by:AWaterfalls
ID: 38734730
Thank you,for your answer. However I am using a microsoft graph chart, not an MSCHART.

As the commands are different for these two charts, and I am unable to download the MSChart. because i have windows 7 x64 bits not xp. As before I am  using vb6.

Could you please rewrite the example using the microsoft graph chart. Thank you.
0
 
LVL 15

Accepted Solution

by:
eemit earned 2000 total points
ID: 38734793
Insert a Microsoft Chart Control into your project.
(On the Project menu, click Components)
Microsoft Chart Control 6.0 (SP4) (OLEDB)

- on Windows XP:
    c:\windows\system32\MSCHRT20.OCX

- on Windows 7 (64-bit):
    C:Windows\SysWOW64\MSCHRT20.OCX

Please consider that a 64-bit Windows has:
- two different versions of the system directory:
      System32 - Windows System folder for 64-bit files
      SysWOW64 - Windows System folder for 32-bit files
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Make the most of your online learning experience.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question