Show Values in MS Chart Legends

2019-08-31 18:44发布

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.

2条回答
Juvenile、少年°
2楼-- · 2019-08-31 19:28

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.

chartSel.Legends.Add(ySeries.Name);

To access the legend columns from the series,

string legendName = chart1.Series[SeriesName].Legend;
LegendCellColumnCollection cols = chart1.Legends[legendName].CellColumns;
查看更多
Fickle 薄情
3楼-- · 2019-08-31 19:30

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:

LegendItem newItem = new LegendItem();
newItem.ImageStyle = LegendImageStyle.Line;
newItem.Color = lineColor;
newItem.BorderWidth = 2;
newItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleLeft); // Symbol
newItem.Cells.Add(LegendCellType.Text, seriesName, ContentAlignment.MiddleLeft); // Series Name
newItem.Cells.Add(LegendCellType.Text, "", ContentAlignment.MiddleLeft); // Value
...
...
series.IsVisibleInLegend = false;
_chart.Series.Add(series);

At the time of adding a real-time XY value:

_chart.Legends[0].CustomItems[seriesIndex].Cells[2].Text = YValue.ToString("00.00");

That gives me the view I wanted:

----  Series1   45.21 
----  Series2   100 
----  Series3   1.123
----  Series4   250.145

If you are deleting any of your series at runtime, then make sure you delete the corresponding custom item using:

_chart.Legends[0].CustomItems.RemoveAt(i);
查看更多
登录 后发表回答