チャートを描画し、チャートに凡例を表示するアプリケーションを開発しています。
これが私がこれまでに行ったことです。凡例部分がなければ、グラフは正しく描画されています。この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: