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

有2个列表名为listversionMIN_list 。 使用这些列表的值我创build了一个折线图。 一切都很好。 但我想知道是否有可能添加一个数据表与图例如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; 

我期待着这样的事情

在这里输入图像说明

如果可能我该怎么做?

谢谢。

是的,你可以这么做:

在这里输入图像说明

以下是我采取的步骤:

首先,我们禁用原来的Legend因为它不能被我们需要的方式操纵..:

 chart1.Legends[0].Enabled = false; 

现在我们创build一个新的和一个快捷方式参考:

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

接下来我们做一下定位:

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

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

我使用一个通用的函数,并通过一个标志来指示是否应该填写标题(x值)或单元格数据(y值)。以下是我如何调用该函数:

 addValuesToLegend(L, chart1.Series[0], false); foreach (Series s in chart1.Series) addValuesToLegend(L, s, true); 

请注意,为了这个工作,我们需要在我们的Series做一些准备工作:

  • 我们需要明确地设置Series.Colors ,否则我们不能引用它们。
  • 我已经为每个系列的Tag添加了一个格式string; 但也许你find一个更好的解决scheme,避免硬编docker的格式。

所以这里是所有的填充和一些样式的function:

 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事件:

 private void chart1_PrePaint(object sender, ChartPaintEventArgs e) { LegendCell cell = e.ChartElement as LegendCell; if (cell != null && cell.Tag == null) { RectangleF r = e.ChartGraphics.GetAbsoluteRectangle(e.Position.ToRectangleF()); e.ChartGraphics.Graphics.DrawRectangle(Pens.DimGray,Rectangle.Round(r)); // Let's hide the left border when there is a cell span! if (cell.CellSpan != 1) e.ChartGraphics.Graphics.DrawLine(Pens.White, r.Left, r.Top+1, r.Left, r.Bottom-1); } } 

你可以添加更多的样式,虽然我不知道你是否可以完美地匹配这个例子。

Interesting Posts