如何将带有图例键的数据表添加到C#中的MS图表中?

时间:2016-03-04 09:06:04

标签: c# excel charts legend legend-properties

有两个列表名为 listversion &的 MIN_list 即可。使用这些列表的值我创建了一个折线图。一切都很好。但我想知道是否可以将带有图例键的数据表添加到图表中,如MS Excel。

chart.Series.Clear();
chart.ChartAreas[0].AxisX.Title = "Version";
chart.ChartAreas[0].AxisX.TitleFont = new System.Drawing.Font("Arial", 12, FontStyle.Regular);

chart.ChartAreas[0].AxisY.Title = "Time";
chart.ChartAreas[0].AxisY.TitleFont = new System.Drawing.Font("Arial", 12, FontStyle.Regular);

Series MIN = chart.Series.Add("Minimum");
MIN.Points.DataBindXY(listVersion, MIN_list[j]);
MIN.ChartType = SeriesChartType.Line;
MIN.Color = Color.Red;
MIN.BorderWidth = 3;

我期待这样的事情

enter image description here

如果有可能我该怎么办?

谢谢。

1 个答案:

答案 0 :(得分:1)

是的,你可以这样做:

enter image description here

以下是我采取的步骤:

首先我们禁用原始x[i+1]-x[i],因为它无法以我们需要的方式进行操作..:

Legend

现在我们创建一个新的快捷方式引用它:

chart1.Legends[0].Enabled = false;

接下来我们做一些定位:

chart1.Legends.Add(new Legend("customLegend"));
Legend L = chart1.Legends[1];

现在我们要为标题填写一行,每个系列填充一行。

我对两者都使用一个通用函数,并传入一个标志来指示是否应该填写标题(x值)或单元格数据(y值)。这是我如何调用函数:

L.DockedToChartArea = chart1.ChartAreas[0].Name;  // the ca it refers to 
L.Docking = Docking.Bottom;
L.IsDockedInsideChartArea = false;
L.Alignment = StringAlignment.Center; 

请注意,为了实现这一目标,我们需要在addValuesToLegend(L, chart1.Series[0], false); foreach (Series s in chart1.Series) addValuesToLegend(L, s, true); 中进行一些准备工作:

  • 我们需要明确设置Series ,否则我们无法引用它们。
  • 我在每个系列的Series.Colors添加了一个格式字符串;但也许你找到了一个更好的解决方案,避免对标题的格式进行硬编码..

所以这里是完成所有填充和一些样式的函数:

Tag

要在我们的图例表格的单元格周围绘制边框,我们需要对void addValuesToLegend(Legend L, Series S, bool addYValues) { // create a new row for the legend LegendItem newItem = new LegendItem(); // if the series has a markerstyle we show it: newItem.MarkerStyle = S.MarkerStyle ; newItem.MarkerColor = S.Color; newItem.MarkerSize *= 2; // bump up the size if (S.MarkerStyle == MarkerStyle.None) { // no markerstyle so we just show a colored rectangle // you could add code to show a line for other chart types.. newItem.ImageStyle = LegendImageStyle.Rectangle; newItem.BorderColor = Color.Transparent; newItem.Color = S.Color; } else newItem.ImageStyle = LegendImageStyle.Marker; // the rowheader shows the marker or the series color newItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleCenter); // add series name newItem.Cells.Add(LegendCellType.Text, addYValues ? S.Name : "", ContentAlignment.MiddleLeft); // combine the 1st two cells: newItem.Cells[1].CellSpan = 2; // we hide the first cell of the header row if (!addYValues) { newItem.ImageStyle = LegendImageStyle.Line; newItem.Color = Color.Transparent; newItem.Cells[0].Tag = "*"; // we mark the 1st cell for not painting it } // now we loop over the points: foreach (DataPoint dp in S.Points) { // we format the y-value string t = dp.YValues[0].ToString(S.Tag.ToString()); // or maybe the x-value. it is a datatime so we need to convert it! // note the escaping to work around my european locale! if (!addYValues) t = DateTime.FromOADate(dp.XValue).ToString("M\\/d\\/yyyy"); newItem.Cells.Add(LegendCellType.Text, t, ContentAlignment.MiddleCenter); } // we can create some white space around the data: foreach (var cell in newItem.Cells) cell.Margins = new Margins(25, 20, 25, 20); // finally add the row of cells: L.CustomItems.Add(newItem); } 事件进行编码:

PrePaint

您可以添加更多样式,但我不确定您是否可以完美匹配该示例..