1

I am getting values from Listarray and want to create a chart from those values. Right now i have two Listarrays and they both have data. My requirement is that the Data in first Arraylist should be Xaxis values and Data in second arraylist should be yaxis values.Please help me out in this regards. My code is given below.

**These are the List arrays.**
List<string> lines = new List<string>();
List<string> lines1 = new List<string>();



    **This is my code but the values are hard code**
        DataSet dataSet = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Counter", typeof(int));

        DataRow r1 = dt.NewRow();
        r1[0] = "Demo"; //code should get r1[0] values from listarray 1

        r1[1] = 8;     //code should get r1[1] values from listarray 2 

        dt.Rows.Add(r1);
        DataRow r2 = dt.NewRow();
        r2[0] = "Second";
        r2[1] = 15;
        dt.Rows.Add(r2);
        dataSet.Tables.Add(dt);

        Chart chart1 = new Chart();
        chart1.DataSource = dataSet.Tables[0];

i did some changes in my code but it shows only x axis values not y axis.

        DataSet dataSet = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Counter", typeof(int));


        foreach (string str in lines1)
        {
            DataRow r1 = dt.NewRow();   
            // Here you will get an actual instance of a DataRow
            r1["Name"] = str;   // Assign values 
            dt.Rows.Add(r1);            
            //Console.WriteLine(str);
            //Console.ReadKey();
        }

        foreach (string str1 in lines)
        {
            DataRow r2 = dt.NewRow();  
            r2["Counter"] = str1;   // Assign values 
            dt.Rows.Add(r2);             
            //Console.WriteLine(str1);
            //Console.ReadKey();
        }

        //DataRow r1 = dt.NewRow();
        //r1[0] = "Demo";
        //r1[1] = 8;
        //dt.Rows.Add(r1);
        //DataRow r2 = dt.NewRow();
        //r2[0] = "Second";
        //r2[1] = 15;
        //dt.Rows.Add(r2);
        dataSet.Tables.Add(dt);


        Chart chart1 = new Chart();
        chart1.DataSource = dataSet.Tables[0];
1
  • i did some changes in my code but it shows only x axis values not y axis. Commented Sep 5, 2016 at 14:21

1 Answer 1

1

See here for a good overview of the many ways of binding data to a Chart.

Your code is using the second way, but it is missing a few things.

  • Your Chart needs a ChartArea.
  • You also need a Series to display the data.
  • You need to tell the Series which of the Columns is X- and which is Y-Value

With these addition data-binding will work:

chart1.ChartAreas.Add("CA1");
Series s = chart1.Series.Add("S1");

s.XValueMember = "Name";
s.YValueMembers = "Counter";

Now it should show the chart. When you change the table data you should re-bind:

 chart1.DataBind();

Note the various differences, advantages and draw-backs of the many data-binding methods!!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.