Search code examples
c#winformsgraphmschart

How to give two different dictionary value to stack-column chart


I have two dictionaries, from which want to show stacked-column chart. I tried but don't understand where I am going wrong:

chartname.DataSource = EventcategoryopenCount;
chartname.Series["OPEN"].XValueMember = "Key";
chartname.Series["OPEN"].YValueMembers = "Value";
chartname.DataSource = EventcategoryreductCount;
chartname.Series["Close"].XValueMember = "Key";
chartname.Series["Close"].YValueMembers = "Value";
chartname.DataBind();

If I only use one dictionary it creates output but when I am using both dictionaries nothing is showing in graph.


Solution

  • You must use a different way to do data binding if your two series shall have different datasources.

    There are several.. (Here is another post about a similar topic.)

    You should not bind to the chart but to the respective Series.Points!

    Change the code to:

    chartname.Series["OPEN"].Points.DataBind(EventcategoryopenCount, "Key", "Value", "");
    chartname.Series["Close"].Points.DataBind(EventcategoryreductCount, "Key", "Value", "");
    

    Btw: It is good practice to first set the members and then the datasource but this method will do it both in one call..

    Your data still need to be aligned.