我正在开发一个应用程序来绘制图表并在图表中显示图例。
这是我到目前为止所做的。没有图例部分图绘制正确。我正在尝试绘制像这个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: