0

I have BindingLists that store some objects I want to visualise on chart.

BindingList<Place> places;
BindingList<Person> people;
BindingList<Animal> animals;

All of them implement following interface:

interface IMapObject
    {
        int getPositionX();
        int getPositionY();
    }

How can I easily bind them all to single WinForms Point Chart? Every List need to be in separate Series. Chart should update automatically when I add new object to one of those lists.

Piotrek
  • 10,919
  • 18
  • 73
  • 136

1 Answers1

0

I have never worked with the Chart control before but I understand how binding works so I am pretty sure this will will work.

First change your interface to this:

interface IMapObject
{
    int PositionX { get; set; }
    int PositionY { get; set; }
}

Implement that and then bind it like this:

ChartOne.Series.First().XValueMember = "PositionX";
ChartOne.Series.First().YValueMembers = "PositionY";

ChartOne.DataSource = places;

As you can see strings are used to indicate the XValueMember and the YValueMembers, and therefore does not really take advantage of your interface and do Strongly Typed binding. The question you need to ask is if the effort is really worth it or to just go with strings. If you want the strongly typed binding, please see this thread for how it can be done (not directly related to charts but same idea). If I were you, I would just go with strings.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • Your answer describes how to bind one BindingList to one Chart. What if I wanted to assign three BindingLists to Data Source? – Piotrek Nov 11 '17 at 15:47
  • @Piotrek would that not be a different series? – CodingYoshi Nov 11 '17 at 15:49
  • yes, each of those lists will be in different series. But in your answer you take only `places` as DataSource. Moreover, I can't find any way to bind List directy to Series instead of entire chart. – Piotrek Nov 11 '17 at 15:52
  • @Piotrek Not sure what you mean. For another series you would write `ChartOne.Series[1].XValueMember = "PositionX2"; ChartOne.Series[1].YValueMembers = "PositionY2"`. So your datasource will contain all the points for each series not just places. – CodingYoshi Nov 11 '17 at 16:02
  • I'm not sure what you mean too ^^ `places` is only one of three sets of data which I need to display on single chart. I also need to display `people` and `animals` (and points representing them need to look differently) – Piotrek Nov 11 '17 at 17:01
  • Yes so you need have `places`, `people`, and `animals` in your datasource. Then do what i said in my previous comment. – CodingYoshi Nov 11 '17 at 19:24