Timetraveller Timetraveller - 2 months ago 15
C# Question

Binding a chart using a csv file produces errors

I am coding in Visual Studio 12, ASP.net using C#. I have data in my csv that looks like this.

ID, Engine Type, Car,
111,vtec, 1,
131,vtec, 1,
157,boxer,1,
148,boxer,1,
167,vtec,1,
158,,0,
107,,0,


I have done this to make it into a list.

var testingobject = from line in File.ReadAllLines("testingparttwo.csv").Skip(1)
let parts = line.Split(',')
select new CarClass()
{
Id = Int32.Parse(parts[0]),
Engine_Type = parts[1],
Car = Int32.Parse(parts[2])
};
var mynewlist = testingobject.ToList();


Then I bind a drop down list with ID,Engine Type and Car as a selection using just a list that I manually created.

Then after a user clicks Car, I would like to bind the appropriate data to a chart (column or pie, doesn't matter as long as I see a visual). 1's and 0's represent whether a customer has a car (1) or not (0). I would like to see how many different types of engine types are there and get a visual on it. According to the data, there are 3 vtecs and 2 boxers.

This is my drop down list upon selection changed code is:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

string tempy = DropDownList1.Text;

var testingobject = from line in File.ReadAllLines("testingparttwo.csv").Skip(1)
let parts = line.Split(',')
select new CarClass()
{
Id = Int32.Parse(parts[0]),
Engine_Type = parts[1],
Car = Int32.Parse(parts[2])
};
var myseconedlist = testingobject.ToList();

if (tempy == "Car")
{
var myfavitems = myseconedlist.Where(a => a.Car == 1).ToList();
foreach (var t in myfavitems.GroupBy(a => a.Engine_Type))
{
Series Series1 = new Series();
Chart1.Series.Add(Series1);
Chart1.DataSource = myfavitems.AsEnumerable();
Chart1.Series[0].Points.ADDXY(t.key,t.count()); //this doesnt work!!
Chart1.Series[0].XValueMember = "Engine Type";
Chart1.Series[0].YValueMembers = "Car";
Chart1.DataBind();


So it keeps producing an error whenever I bind it regarding the t.key and t.count. .ADDXY require double values not t.key, t.count().

How do I bind this?

Answer

The way i solved this problem, for those who still want to know how I did it was: Since the group by statement produces a Key that differentiates the vtecs and boxers. I used that differentiation and posted my list as a datasource of my datagrid.

GridView1.DataSource = myfavitems;
        GridView1.DataBind();

Now the table will only show the group by items of your list in my case the "myfavitems" list. Now that all the remaining values in your csv file are excluded and you can see only the ones that have been filtered by the Groupby clause, it becomes easy to bind the datagrid to a chart.

Series Series1 = new Series();
Chart1.Series.Add(Series1);
Chart1.Series[0].XValueMember = "Engine Type";
Chart1.Series[0].YValueMembers = "Car";
Chart1.DataSource = GridView1.DataSource;
Chart1.DataBind();

You can add another statement after the Chart1.Databind to differentiate between the keys which is.

Chart1.Series[0].Points.AddXY("" + myword + "", t.Count());
Chart1.Series[0].PostBackValue = "#VALX, #VAL";

myword is a variable that stores this:

string myword = t.Key.AsEnumerable().ToString();

t is the variable that iterates through the myfavitems.groupby clause.

Also i disabled the datagrid so I don't have a visual on the data. Hope this can help!!