简体   繁体   中英

How to populate point chart with with free separate lists?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM