Solved

Using MS Chart in .NET

Posted on 2004-04-15
10
2,178 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:Volkan Vardar
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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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