3

enter image description hereI have a relatively simple problem, I want to display 2 simple line series in a Chart from a DataSet with 2 tables in it.

Right now I simply create a second series with the same ValueMembers but they are displayed on top of one another. The DataSet is filled correctly with different values.

      dataAdapter.Fill(dataSetChart);
      chartKunden.Series.Add("Kunden");
      chartKunden.Series.Add("Table1");
      chartKunden.Series["Kunden"].ChartType = SeriesChartType.Line;
      chartKunden.Series["Table1"].ChartType = SeriesChartType.Column;
      chartKunden.Series["Table1"].XValueMember = "Woche";  
      chartKunden.Series["Table1"].YValueMembers = "Stunden";     
      chartKunden.Series["Kunden"].XValueMember = "Woche";  
      chartKunden.Series["Kunden"].YValueMembers = "Stunden";
      chartKunden.DataSource = dataSetChart;

I basically just want to know how to seperate them so the second series gets the data from the second table of the DataSet.

Updated DataBind:

chartKunden.Series["Table2"].Points.DataBind(dataSetChart.Tables[1].Rows, "Woche", "Stunden", "");

chartKunden.Series["Table1"].Points.DataBind(dataSetChart.Tables[0].Rows, "Woche", "Stunden", "");
Tantem
  • 55
  • 5
  • please show us the code. Show us your attempt to access the "second table of the DataSet". So it seems that the real question here is: How do I access the data in the second table ? – Mong Zhu Sep 24 '19 at 13:24
  • I updated the question, but unfortunately there isn't much to see :( – Tantem Sep 24 '19 at 13:27
  • Do not bind the chart but Series.Points : `chartKunden.Series["Kunden"].DataSource = dataSetChart.Tables[someTable1];` etc.. – TaW Sep 24 '19 at 13:37
  • I don't know if I am doing something wrong but there is no DataSource property DataSource in `Series[Kunden]` – Tantem Sep 24 '19 at 13:45
  • 2
    Whoops, typo: `chartKunden.Series["Kunden"].Points.DataSource = dataSetChart.Tables[someTable1];` – TaW Sep 24 '19 at 13:50
  • Also no DataSource in Points, only DataBind and I don't know the syntax or if it does what I need :D – Tantem Sep 24 '19 at 13:56
  • 2
    Um, sorry you're right. There are [many ways to do databinding](https://msdn.microsoft.com/library/dd456766%28v=vs.100%29.aspx). Here use `yourSeries1.Points.DataBind(yourSource, "xvaluename", "yvaluename", "");)`. Note the empty string! (Here one could put a customproperty to bind; usually not working..) Also see [here](https://stackoverflow.com/questions/33588055/drawing-a-chart-from-a-datatable/33588458#33588458) – TaW Sep 24 '19 at 14:00
  • Okay this looks promising, I'm still having trouble with "yourSource", I tried `dataSetChart.Tables[0]`but it is not accepted by the enumerator? just `dataSetChart.Tables`is accepted but doesn't seem to work. Maybe you can help again and answer in an answer so I can give you the solution :) – Tantem Sep 24 '19 at 14:09
  • 1
    Sorry to have given you three hint with faults in them. The answer below is tested, though.. ;-) – TaW Sep 24 '19 at 14:40

2 Answers2

4

There are many ways to do databinding.

You can bind each Series to a separate data source for example like so:

s1.XValueMember = "col11";
s1.YValueMembers = "col12";
s2.XValueMember = "col21";
s2.YValueMembers = "col22";

s1.Points.DataBind(t1.Rows, "col11", "col12", "");
s2.Points.DataBind(t2.Rows, "col21", "col22", "");

This assumes a two DataTables t1 and t2 with Columns "col11", "col12" and "col21", "col22".

Note the empty string as last parameter. Here one can add a list of comma-separated custom properties to add to the binding. Example:

s1.Points.DataBind(t1.Rows, "col11", "col12", "Tooltip=colcom1");

See here for a duscussion of limitations for this!

Also note that this binding overload needs to find x- and y-values in the same data source. Check out the overview of bindings above for even more flexible ways!!

A simple example to bind x- and y-values to different sources could be written as:

s2.Points.DataBindXY(t2.Rows, "col21", t1.Rows, "col12");

Note that now we can't set extended properties!

TaW
  • 53,122
  • 8
  • 69
  • 111
  • So I finally got a chance to test your solution and it seems like I'm having problems with the fact that the Series have different lengths. It looks like the shorter Series just takes the value of the longer one. Is there an option to tell the shorter Series not to do that? – Tantem Sep 27 '19 at 06:25
  • 1
    _the shorter Series just takes the value of the longer one_ Not sure I understand but this really can't happen. Can you post some more, like a screenshot? What type are the x-values? (`wochen`) ? Numbers? – TaW Sep 27 '19 at 06:58
  • I added a screenshot, you can see the two lines in different shades of green on top of one another. Now the odd thing is that one table contains 3 rows and the other one contains only 2, yet they both display with 3 DataPoints in the Chart. Both Values are numbers, xValue would be Integers and the yValue allows commas – Tantem Sep 27 '19 at 07:08
  • Funny enough, when I look into the Points property of the Series the data is correct, and it shows the correct values i wish for, it is just drawn wrong somehow... – Tantem Sep 27 '19 at 07:50
  • 1
    Hm, that looks a lot as if both series bind to the very same data. Do use the debugger to test the number and content of the tables. Also: You seem to have changed the name of a series, right? It is often clearer to refer to elment by name instead of index.. – TaW Sep 27 '19 at 07:50
  • Yes I changed the name for better understanding, but both in the DataSet and in the Points property the data seems to be correct. Well the TableNames would be Table and Table1, I think that wouldn't be helpful :D – Tantem Sep 27 '19 at 07:52
  • 1
    The effect seems impossible, so there ought to be some typo somewhere. I would try to change some of the data to see how that takes effect ! – TaW Sep 27 '19 at 07:54
  • Okay, I tried adding only the Table with 2 rows and went through the entire process with the debugger, the Points.Count property is 0 before and 2 after the databind and stays at 2 from there on out. Yet it is still displayed with 3 DataPoints. This is really frustrating :D. – Tantem Sep 27 '19 at 08:05
  • I also changed a Value and the "extra" Value from the wrong table updated too, if it is a typo I can't find it. – Tantem Sep 27 '19 at 08:20
0

enter image description hereOkay I think I could narrow down the problem, I draw the Chart in 2 different ways depending on a CheckButton, usually I draw the other version first, but now I draw the version with the mistake first and the data is now displayed correctly. I thought I cleared the Chart and DataSet properly before adding the new Values but doesn't seem to be the case, I will further look into this.

Tantem
  • 55
  • 5
  • Not sure if this should be an answer. The points will always show the contents of the tables but unless you clear the table rows they will keep previous rows. – TaW Sep 27 '19 at 08:37