Custom Charting in VB express

Posted on 2012-09-02
Medium Priority
Last Modified: 2012-10-14
Hi, I am trying to use the VB charts feature to plot custom x-y plots in VB charts...
I'd like to import data from an external *.dat file  which is basically a column vector of data (x, y1, y2....yn)..
I am guessing that it has to do something with the "datasource" option in chart properties.. However, it seems to work databases only... How about data from simple files like *.dat or excel etc...

Appreciate your suggestions...
Question by:VBlearner100
  • 2
LVL 35

Accepted Solution

Robert Schutt earned 2000 total points
ID: 38360766
If you're willing to take a little detour, this is how it could be done with a datatable taken from a .csv file (strangely, .dat extension seems to be impossible but maybe I did something else wrong):
Imports System.Data.Odbc

Public Class Form1

    Const C_DATADIR As String = "c:/tmp/datadir"

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable
            Using da As New OdbcDataAdapter("select * from [data.csv]", "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & C_DATADIR & ";Extensions=csv;")
            End Using
        Catch ex As Exception
        End Try
        Me.Chart1.DataSource = dt
        Me.Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.StackedColumn
        For c As Integer = 0 To dt.Columns.Count - 2
            If c > 0 Then
                Me.Chart1.Series.Add("Series" & (c + 1))
                Me.Chart1.Series(c).ChartType = Me.Chart1.Series(0).ChartType
            End If
            ' fields from csv are called F1, F2, ...
            Me.Chart1.Series(c).XValueMember = "F1"
            Me.Chart1.Series(c).YValueMembers = "F" & (c + 2)
    End Sub
End Class

Open in new window

This is assuming you put a datafile called data.csv in a folder "c:\tmp\datadir" together with a file called schema.ini:

Open in new window

example data.csv:

Open in new window

example output:example output capture

Author Comment

ID: 38372535
Thank you very much and for the clear example.. this is very insightful..
LVL 35

Expert Comment

by:Robert Schutt
ID: 38404990
Hi, any more info you need? Otherwise please close the question.

LVL 50

Expert Comment

by:Martin Liss
ID: 38494365
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Featured Post

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!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

840 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