2

我正在开发一个应用程序来绘制图表并在图表中显示图例。

这是我到目前为止所做的。没有图例部分图绘制正确。我正在尝试绘制像这个MSDN链接这样的图例。在运行时,我收到此错误。

尝试谷歌搜索。但我不知道确切的原因是什么。

           Chart gammaPlotChart = new Chart();
           ChartArea chart4Area = new ChartArea();
           chart4Area.Name = "Default";
           Series gammaValues = new Series();
           gammaValues.Name = "LogGamma";
           gammaValues.ChartType = SeriesChartType.Line;
           gammaValues.XValueType = ChartValueType.Double;
           gammaValues.YValueType = ChartValueType.Double;
           gammaPlotChart.ChartAreas[0].AxisX.Minimum = 1.4;
           gammaPlotChart.ChartAreas[0].AxisX.Interval = 0.2;
           gammaPlotChart.ChartAreas[0].AxisX.Maximum = 5.0;
           gammaPlotChart.ChartAreas[0].AxisY.Minimum = 0;
           gammaPlotChart.ChartAreas[0].AxisY.Interval = 0.5;
           gammaPlotChart.ChartAreas[0].AxisY.Maximum = 3.0;
           gammaPlotChart.Series.Add(gammaValues);
           gammaPlotChart.Series["LogGamma"].Points.DataBindXY(sRGBValues.greyScaleValues.LogV, sRGBValues.greyScaleValues.LogL);

           gammaPlotChart.Legends.Add(new Legend("Legend1"));
           gammaPlotChart.Legends["Legend1"].DockedToChartArea = "Default";
           gammaPlotChart.Series["LogGamma"].Legend = "Legend1";
           gammaPlotChart.Series["LogGamma"].IsVisibleInLegend = true;


           gammaPlotChart.Legends["Legend1"].CellColumns.Add(new LegendCellColumn("Name", LegendCellColumnType.Text, "LEGENDTEXT"));
           gammaPlotChart.Legends["Legend1"].CellColumns.Add(new LegendCellColumn("Sym", LegendCellColumnType.SeriesSymbol, "Mist"));
           Font font = new Font("Arial", 10);
           gammaPlotChart.Legends["Legend1"].CellColumns.Add(new LegendCellColumn("Avg", LegendCellColumnType.Text, "AVG{N2}"));
           gammaPlotChart.Legends["Legend1"].CellColumns[0].Font = new System.Drawing.Font("Arial", 10);
           gammaPlotChart.Legends["Legend1"].CellColumns[1].Font = font;
           gammaPlotChart.Legends["Legend1"].CellColumns[2].Font = font;

           Rectangle chart4Rect = new System.Drawing.Rectangle(locChart4, chartSize);
           gammaPlotChart.Printing.PrintPaint(e.Graphics, chart4Rect);

IDE如下所示: 在此处输入图像描述

桩迹:

System.ArgumentNullException was unhandled by user code
  Message=Value cannot be null.
Parameter name: font
  Source=System.Drawing
  ParamName=font
  StackTrace:
       at System.Drawing.Graphics.MeasureString(String text, Font font, SizeF layoutArea, StringFormat stringFormat)
       at System.Drawing.Graphics.MeasureString(String text, Font font)
       at System.Windows.Forms.DataVisualization.Charting.GdiGraphics.MeasureString(String text, Font font)
       at System.Windows.Forms.DataVisualization.Charting.ChartGraphics.MeasureStringAbs(String text, Font font)
       at System.Windows.Forms.DataVisualization.Charting.Legend.GetHeaderSize(ChartGraphics chartGraph, LegendCellColumn legendColumn)
       at System.Windows.Forms.DataVisualization.Charting.Legend.GetOptimalSize(ChartGraphics chartGraph, SizeF maxSizeRel)
       at System.Windows.Forms.DataVisualization.Charting.Legend.CalcLegendPosition(ChartGraphics chartGraph, RectangleF& chartAreasRectangle, Single elementSpacing)
       at System.Windows.Forms.DataVisualization.Charting.LegendCollection.CalcInsideLegendPosition(ChartGraphics chartGraph, Single elementSpacing)
       at System.Windows.Forms.DataVisualization.Charting.ChartPicture.Resize(ChartGraphics chartGraph, Boolean calcAreaPositionOnly)
       at System.Windows.Forms.DataVisualization.Charting.ChartPicture.Paint(Graphics graph, Boolean paintTopLevelElementOnly)
       at System.Windows.Forms.DataVisualization.Charting.PrintingManager.PrintPaint(Graphics graphics, Rectangle position)
       at ReportPrinter.PrintReport.DrawChart(PrintPageEventArgs e, InputData sRGBValues, InputData adobeRGBValues)
       at ReportPrinter.PrintReport.printDoc_PrintPage(Object sender, PrintPageEventArgs e)
       at System.Drawing.Printing.PrintDocument.OnPrintPage(PrintPageEventArgs e)
       at System.Drawing.Printing.PrintDocument._OnPrintPage(PrintPageEventArgs e)
       at System.Drawing.Printing.PrintController.PrintLoop(PrintDocument document)
       at System.Drawing.Printing.PrintController.Print(PrintDocument document)
       at System.Drawing.Printing.PrintDocument.Print()
       at System.Windows.Forms.PrintPreviewControl.ComputePreview()
       at System.Windows.Forms.PrintPreviewControl.CalculatePageInfo()
  InnerException: 
4

1 回答 1

7

我遇到了同样的问题:我正在使用您链接到的 MSDN 页面制作表格图例,并且遇到了与您收到的相同的错误。

我的解决方案是设置 LegendCellColumn.HeaderFont 属性。例如在 Zigma 的案例中:

LegendCellColumn lcc = new LegendCellColumn("Name", LegendCellColumnType.Text, "LEGENDTEXT");
lcc.HeaderFont = new System.Drawing.Font("Trebuchet MS", 12F, System.Drawing.FontStyle.Bold);
gammaPlotChart.Legends["Legend1"].CellColumns.Add(lcc);

解释:

问题是使用带参数的构造函数:

LegendCellColumn("Name", LegendCellColumnType.Text, "LEGENDTEXT")

因为在第一个参数中,您设置了 LegendCellColumn 的 HeaderText 属性,但 HeaderFont 属性默认为“null”。当图表尝试绘制图例时,它发现标题没有字体并引发错误。

于 2013-06-25T13:44:19.850 回答