?
Solved

Using MS Chart in .NET

Posted on 2004-04-15
10
Medium Priority
?
2,230 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
[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
  • 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
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 

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

Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

718 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