Link to home
Start Free TrialLog in
Avatar of tanrisev1
tanrisev1

asked on

Using MS Chart in .NET

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.
     
Avatar of tanrisev1
tanrisev1

ASKER

I just want to add that I am using visual basic for my application.
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

       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
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.
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
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))
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.
ASKER CERTIFIED SOLUTION
Avatar of malharone
malharone

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
malharone,

I got it, thanks for your help.