简体   繁体   English

值不能为null。 参数名称:字体

[英]value cannot be null. parameter name :font

I am developing a an apllication to draw a chart and show legend in the chart. 我正在开发一个应用程序来绘制图表并在图表中显示图例。

Here is what I have done so far. 这是我到目前为止所做的。 Without the legend part graph is drawing correctly. 没有图例的零件图将正确绘制。 I am trying to draw legend like this MSDN link. 我试图绘制像这个MSDN链接的传奇。 At run time I am getting this error. 在运行时,我收到此错误。

Tried googling. 尝试使用谷歌搜索。 But I wont get what the exact reason is. 但是我不知道确切的原因是什么。

           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);

The IDE is shown below : IDE如下所示: 在此输入图像描述

Stak trace : 痕迹

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: 

I had the same problem: I was using the MSDN page you linked to to make a table legend and i experienced the same error as you received. 我遇到了同样的问题:我正在使用您链接到的MSDN页面制作表格图例,我遇到了与您收到的相同的错误。

The solution for me was setting the LegendCellColumn.HeaderFont property. 我的解决方案是设置LegendCellColumn.HeaderFont属性。 For example in Zigma's case: 例如,在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);

Explanation: 说明:

The problem is using the constructor with parameters: 问题是使用带有参数的构造函数:

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

Because in the first parameter you set the HeaderText property of the LegendCellColumn but the HeaderFont property is "null" for default. 因为在第一个参数中设置了LegendCellColumn的HeaderText属性,但默认情况下HeaderFont属性为“null”。 When the chart tries to draw the legend it discovers it has no font for the header and throws an error. 当图表试图绘制图例时,它发现它没有标题的字体并引发错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM