1

I want to create a line chart where in x axis I will have dates and in y axis I will have prices.

I have createad an list of all the dates and prices from the database.

var ListOfDates = DBaccess.GetOperationToList().Select(x =>x.Data.ToString());
var ListOfPrices = DBaccess.GetOperationToList().Select(x => x.Price);

for (int i = 0; i < ListOfDates.Count(); i++)
{
    chart2.Series["s1"].Points.AddXY(ListOfPrices, ListOfPrices);
}

When I want to create points I have the following mistake "Points od data doesn't work value type 'System.Linq.Enumerable+WhereSelectEnumerableIterator'. You can use Double, Decimal, Single, int, long, uint, ulong, String, Datetime, short, ushort."

But the Data in ListOfDates have Datetime type, and the Price have Double type. How do I change the type?

1
  • Maybe: AddXY(ListOfDates[i], ListOfPrices)[i]);? But onit the ToString from the DataTime so it will add the real type, not strings! Commented May 5, 2019 at 14:22

1 Answer 1

1

To tell the Series what the real data type is use this:

chart2.Series[0].XValueType = ChartValueType.DateTime;

You could add the DataPoints in a loop as in your code, but you will have to use the elements, not the whole list:

chart2.Series["s1"].Points.AddXY(ListOfDates[i], ListOfPrices[i]);

This assumes the the two lists are aligned with equal lengths.

But it is recommended to use DataBinding.

If your data source has named properties you can use this form of databinding:

chart2.Series[0].Points.DataBindXY(datalist, "date", datalist, "price");

If it hasn't, you can create a combined DataSource like so:

var ListOfData = DBaccess.GetOperationToList()
                         .Select(x => new { date = x.Data, price = x.Price});

and then bind it:

 chart2.Series[0].Points.DataBind(ListOfData, "date", "price", "");

Note that if you add the x-values as strings, as you did, they will be stored as 0s and you won't be able to use them for anything, neither formatting nor searching nor aligning points of series.

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.