Answered New to chart and trying out arraylist

  • Sunday, April 29, 2012 4:19 PM
     
      Has Code

    Dear All,

                 I am very new to microsoft charts. Below is my codes. The char pops up with just the word series1 at right top. What else am I missing to make it appear as a line chart?

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace win1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                //ArrayList listDataSource = new ArrayList();
            }
            public class Record
            {
                int id, age;
                string name;
                public Record(int id, string name, int age)
                {
                    this.id = id;
                    this.name = name;
                    this.age = age;
                }
                public int ID
                {
                    get { return id; }
                    set { id = value; }
                }
                public string Name
                {
                    get { return name; }
                    set { name = value; }
                }
                public int Age
                {
                    get { return age; }
                    set { age = value; }
                }
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // Create a list.
                ArrayList listDataSource = new ArrayList();
    
                // Populate the list with records.
                listDataSource.Add(new Record(1, "Jane", 19));
                listDataSource.Add(new Record(2, "Joe", 30));
                listDataSource.Add(new Record(3, "Bill", 15));
                listDataSource.Add(new Record(4, "Michael", 42));
    
                // Bind the chart to the list.
                //ChartControl myChart = chartControl1;
                chrtMain.DataSource = listDataSource;
                chrtMain.Series[0].XValueMember = "name";
                chrtMain.Series[0].YValueMembers = "age";
                chrtMain.DataBind();
               
            }
    
           
        }
    }
    

All Replies

  • Wednesday, May 02, 2012 10:08 AM
    Moderator
     
     Answered Has Code

    I doubt the chart knows how to use your class called "Record".

    If you want to use that, bind the data in a loop.

    // Bind the chart to the list
    
    Series s = new Series("People");
    chrtMain.Series.Add(s);
    
    foreach (Record r in listDataSource) {
    	s.Points.AddXY(r.Name, r.Age);
    }
    • Marked As Answer by siplaModerator Thursday, May 03, 2012 8:03 AM
    •  
  • Thursday, May 03, 2012 8:15 AM
     
      Has Code

    Dear Sipla,

                     I have move to the db method as below but I got few challenges here.I have time ticker which updates the time say now for 5 seconds. Then it keep calling a  function and in the function it search the sql based on the current time and shows the data. I got few things to confirm am I doing the right method on the refreshing of chart and secondly I would prefer to continuosly show the chart as I get new data from the db?

    public partial class Form1 : Form
        {
            static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
            
            static int alarmCounter = 1;
            static bool exitFlag = false;
    
            // This is the method to run when the timer is raised.
            private  void TimerEventProcessor(Object myObject,
                                                    EventArgs myEventArgs)
            {
                //myTimer.Stop();
    
                MessageBox.Show("Time now is : " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
    
                MySqlConnection localConnection1;
                MySqlCommand command;
    
                localConnection1 = new MySqlConnection("Address='******';Database='****';User Name='****';Password='*****';Pooling='false'");
                MySqlDataAdapter myLocalDataAdapter1 = new MySqlDataAdapter();
    
                String mySelectQuery1 = "Select  " +
                                        "sum(pc) as sumPC" +
                                        "timeStampID " +
                                        "From byteTransmit Where timeStampID='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'";
    
                MessageBox.Show(mySelectQuery1);
                MySqlCommand myCommand1 = new MySqlCommand(mySelectQuery1, localConnection1);
                DataTable myDataTable1 = new DataTable();
                myLocalDataAdapter1.SelectCommand = myCommand1;
                myLocalDataAdapter1.Fill(myDataTable1);
    
    
                chart1.DataSource = myDataTable1;
    
                chart1.DataBind();
    
                chart1.Series["SE"].XValueMember = "timeStampID";
    
                chart1.Series["SE"].YValueMembers = "sumPC";
    
    }
            
            public Form1()
            {
                InitializeComponent();
            }
            
            
            private void Form1_Load(object sender, EventArgs e)
            {
                myTimer.Tick += new EventHandler(TimerEventProcessor);
    
                // Sets the timer interval to 5 seconds.
                myTimer.Interval = 5000;
                myTimer.Start();
                
                
            }
        }
    }


  • Thursday, May 03, 2012 8:23 AM
    Moderator
     
     

    This question has nothing to do with the original question in this thread. Also, you already posted this same exact question in another thread. Please don't post the same question in different threads.

    Though it seems your orignal question is obsolete by now, as you've changed your approach, if my reply did not answer it, feel free to unmark it as an answer.

  • Thursday, May 03, 2012 8:28 AM
     
     

    Dear Sipla,

                    Sorry for my approach. Thank you for your help.