在MS图表图例中显示值 [英] Show Values in MS Chart Legends
问题描述
我试图在WinForm上的图例中显示FastLine类型MS图表的最新系列值.图表中最多可以包含10个系列.我正在使用以下代码在图例中添加3列(符号,系列名称,值):
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.
推荐答案
我终于设法得到了想要的东西. Junaith指向使用CustomItem的指针是实现这一目标的关键.这是我的最终解决方案:
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:
创建系列时:
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);
添加实时XY值时:
_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);
这篇关于在MS图表图例中显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!