Solved

Using MS Chart in .NET

Posted on 2004-04-15
10
2,151 Views
Last Modified: 2012-05-05
Hi everybody,
I have a simple question.  I am using visual studio .net and I want to chart a dataset using an MS chart.  The problem is I don't know how to pass the dataset to the MS chart.

My dataset has 2 columns and looks like this:
City               Quantity
-----              ---------
Austin              120
Houston            40      
Paris                145
London            125
...                     ....

I want to chart the City as the x-axis and the Quantity as the y-axis.  And, I want to use Chart.

Thanks.
     
0
Comment
Question by:tanrisev1
  • 5
  • 4
10 Comments
 

Author Comment

by:tanrisev1
ID: 10838068
I just want to add that I am using visual basic for my application.
0
 
LVL 6

Expert Comment

by:vardium
ID: 10840177
I never tried, just suggesting but it should work.

suggest that your dataset is a matrix, when you post the transpose of it MS Chart will give you what u want.

To do this you can convert your vertical set to horizontal. Like that :

Austin              Houston                  Paris                London                ...
120                 40                          145                  125                     ...
   
If it works I can help u on converting the dataset to that format

0
 
LVL 9

Expert Comment

by:malharone
ID: 10845388
       Private Sub redrawGraph()
            Try
                With Me.AxMSChart1
                    .ColumnCount = '-- # of columns
                    .RowCount = '-- # of rows
                    Dim x As Integer
                    With .DataGrid
                                .SetData(1, YOUR_ROW, YOUR_COLUMN, VALUE, 0)
                                .ColumnLabel(YOUR_ROW, YOUR_COLUMN) = "TITLE"
                            End If
                    End With
                End With
            Catch ex As Exception
                MessageBox.Show("The following error occured: " & vbCrLf & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
0
 

Author Comment

by:tanrisev1
ID: 10845726
malharone,

The following part of your code is not clear to me:
     ".SetData(1, YOUR_ROW, YOUR_COLUMN, VALUE, 0)
      .ColumnLabel(YOUR_ROW, YOUR_COLUMN) = "TITLE" "

I didn't get how to set the parameters inside the parenthesis: 1 ?, YOUR_ROW ? ...

Could you explain it with my dataset?
The name of my dataset is "Dataset11" and it has two columns of data, the names of the fields are "City" and "Quantity"

Thanks.
0
 
LVL 9

Expert Comment

by:malharone
ID: 10846500
here's a working example

Public Class Form10
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents AxMSChart1 As AxMSChart20Lib.AxMSChart
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form10))
        Me.AxMSChart1 = New AxMSChart20Lib.AxMSChart
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.Button1 = New System.Windows.Forms.Button
        CType(Me.AxMSChart1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'AxMSChart1
        '
        Me.AxMSChart1.DataSource = Nothing
        Me.AxMSChart1.Location = New System.Drawing.Point(8, 8)
        Me.AxMSChart1.Name = "AxMSChart1"
        Me.AxMSChart1.OcxState = CType(resources.GetObject("AxMSChart1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxMSChart1.Size = New System.Drawing.Size(256, 152)
        Me.AxMSChart1.TabIndex = 0
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(16, 168)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(160, 80)
        Me.DataGrid1.TabIndex = 1
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(200, 176)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Button1"
        '
        'Form10
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.DataGrid1)
        Me.Controls.Add(Me.AxMSChart1)
        Me.Name = "Form10"
        Me.Text = "Form10"
        CType(Me.AxMSChart1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dt As New DataTable("Stats")
        dt.Columns.Add("City", "".GetType)
        dt.Columns.Add("Value", (0.0).GetType)
        Dim dr As DataRow
        dr = dt.NewRow()
        dr("City") = "LA"
        dr("Value") = 100
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("City") = "NY"
        dr("Value") = 150
        dt.Rows.Add(dr)


        dr = dt.NewRow()
        dr("City") = "XY"
        dr("Value") = 50
        dt.Rows.Add(dr)

        Me.DataGrid1.DataSource = dt
        drawGraph(dt)
    End Sub


    Private Sub drawGraph(ByVal dt As DataTable)
        Dim dr As DataRow
        Try
            With Me.AxMSChart1
                .ColumnCount = CShort(dt.Rows.Count)
                .RowCount = 1
                Dim x As Integer
                With .DataGrid
                    For Each dr In dt.Rows
                        x += 1
                        .SetData(1, CShort(x), CDbl(dr("value")), 0)
                        .ColumnLabel(CShort(x), 1) = dr("city")
                    Next
                End With
            End With
        Catch ex As Exception
            MessageBox.Show("The following error occured: " & vbCrLf & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Expert Comment

by:malharone
ID: 10846505
and secondly. . it's not the dataset that has two columns .. it's the datatable INSIDE the dataset11 that has two columns.
in your case, instrad of calling DrawGraph(dt) ... call DrawGraph(dataset11.tables(0))
0
 

Author Comment

by:tanrisev1
ID: 10847268
malharone,
Your code seems to work for me.
Could you also tell me how may I modify your code to include multiple series.  I also want to add another column named "price" to the chart.

How may I modify the following code to include this ?

                   For Each dr In dt.Rows
                        x += 1
                        .SetData(1, CShort(x), CDbl(dr("value")), 0)
                        .ColumnLabel(CShort(x), 1) = dr("city")
                    Next
Thanks.
0
 
LVL 9

Accepted Solution

by:
malharone earned 240 total points
ID: 10847326
Public Class Form10
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents AxMSChart1 As AxMSChart20Lib.AxMSChart
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form10))
        Me.AxMSChart1 = New AxMSChart20Lib.AxMSChart
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        Me.Button1 = New System.Windows.Forms.Button
        CType(Me.AxMSChart1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'AxMSChart1
        '
        Me.AxMSChart1.DataSource = Nothing
        Me.AxMSChart1.Location = New System.Drawing.Point(184, 16)
        Me.AxMSChart1.Name = "AxMSChart1"
        Me.AxMSChart1.OcxState = CType(resources.GetObject("AxMSChart1.OcxState"), System.Windows.Forms.AxHost.State)
        Me.AxMSChart1.Size = New System.Drawing.Size(264, 224)
        Me.AxMSChart1.TabIndex = 0
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(16, 8)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(160, 224)
        Me.DataGrid1.TabIndex = 1
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(32, 248)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Button1"
        '
        'Form10
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(464, 273)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.DataGrid1)
        Me.Controls.Add(Me.AxMSChart1)
        Me.Name = "Form10"
        Me.Text = "Form10"
        CType(Me.AxMSChart1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dt As New DataTable("Stats")
        Dim dr As DataRow
        dt.Columns.Add("City", "".GetType)
        dt.Columns.Add("Age", (0.0).GetType)
        dt.Columns.Add("Price", (0.0).GetType)
        dr = dt.NewRow()
        dr("City") = "LA"
        dr("Age") = 50
        dr("Price") = 100
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("City") = "NY"
        dr("Age") = 60
        dr("Price") = 90
        dt.Rows.Add(dr)


        dr = dt.NewRow()
        dr("City") = "XY"
        dr("Age") = 70
        dr("Price") = 70
        dt.Rows.Add(dr)

        Me.DataGrid1.DataSource = dt
        drawGraph(dt)
    End Sub

    Private Sub drawGraph(ByVal dt As DataTable)
        Dim dr As DataRow
        Try
            With Me.AxMSChart1
                .ColumnCount = 2
                .RowCount = CShort(dt.Rows.Count)
                Dim x As Integer
                With .DataGrid
                    For Each dr In dt.Rows
                        x += 1
                        .SetData(CShort(x), 1, CDbl(dr("age")), 0)
                        .SetData(CShort(x), 2, CDbl(dr("price")), 0)
                        .RowLabel(CShort(x), 1) = dr("city")
                    Next

                    .ColumnLabel(1, 1) = "Age"
                    .ColumnLabel(2, 1) = "Price"
                End With

                .ShowLegend = True
            End With
        Catch ex As Exception
            MessageBox.Show("The following error occured: " & vbCrLf & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class
0
 

Author Comment

by:tanrisev1
ID: 10847409
malharone,

Your code gives me two seperate charts one for city vs price and the other for city vs age.

I need a single chart.  I want the city to be the x-axis and, for each city I want to have two columns one for price and the other for age.  

Thanks.
0
 

Author Comment

by:tanrisev1
ID: 10847571
malharone,

I got it, thanks for your help.

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now