I am trying to show the latest series value in a Legend for a FastLine type MS chart on a WinForm. I can have up to 10 series in a chart. I am using following code to add 3 columns (Symbol, Series Name, Value) in the Legend:
// Add Color column
LegendCellColumn firstColumn = new LegendCellColumn();
firstColumn.ColumnType = LegendCellColumnType.SeriesSymbol;
firstColumn.HeaderBackColor = System.Drawing.Color.WhiteSmoke;
chart1.Legends[0].CellColumns.Add(firstColumn);
// Add Legend Text column
LegendCellColumn secondColumn = new LegendCellColumn();
secondColumn.ColumnType = LegendCellColumnType.Text;
secondColumn.Text = "#LEGENDTEXT";
secondColumn.HeaderBackColor = System.Drawing.Color.WhiteSmoke;
chart1.Legends[0].CellColumns.Add(secondColumn);
// Add Total cell column
// I will set text for this column at runtime.
LegendCellColumn total = new LegendCellColumn();
total.Name = "Value";
total.HeaderBackColor = System.Drawing.Color.WhiteSmoke;
chart1.Legends[0].CellColumns.Add(total);
My question is - How do I access the legend for a specific series to show the runtime values? While adding new points to the Series if I do something like -
// Add new point to this series here
......
......
_chart.Legends[0].CellColumns[2].Text = runtimeValue.ToString();
then, a same value gets shown for all the series. This value is the latest for one of the series. It looks like -
---- Series1 45.21
---- Series2 45.21
---- Series3 45.21
---- Series4 45.21
How do I access a legend for each series so that I can set the individual value to show something like -
---- Series1 45.21
---- Series2 100
---- Series3 1.123
---- Series4 250.145
Here on SO, I have seen that some posts have mentioned that they can access the legend items by series such as -
myChartName.Legends["mySeriesName"]
I get an invalid argument exception If I try to do that which meant I can't access the legend by a series name. Am I missing something here?
Thanks for any help offered.
In the post you have linked, the legend name and series name are same. That's why OP is accessing the legend using the series name. Check the below line in the second answer by OP.
To access the legend columns from the series,
I finally managed to get what I was looking for. Junaith's pointer towards using CustomItem is the key thing in achieving that. Here is my final solution:
At the time of creating a series:
At the time of adding a real-time XY value:
That gives me the view I wanted:
If you are deleting any of your series at runtime, then make sure you delete the corresponding custom item using: