Asp.net

ASP.NET 圖表:設置 X 和 Y 軸上的字型

  • January 25, 2016

考慮建構 ASP.NET 圖表圖像的 ASP.NET MVC 控制器方法。

public FileStreamResult MakeImg(IEnumerable<MyObj> stats)
   {
       Chart barchart = BarChart(400, 300);

       Series series1 = new Series("Series1");
       series1.ChartArea = "ca1";            
       series1.ChartType = SeriesChartType.Column;
       series1.IsValueShownAsLabel = true;    
       series1.Font = new Font("Verdana", 9f, FontStyle.Regular);

       barchart.Series.Add(series1);            

       // Set chart data source
       barchart.DataSource = stats;

       // Set series members names for the X and Y values
       barchart.Series["Series1"].XValueMember = "FriendlyDate";
       barchart.Series["Series1"].YValueMembers = "NumRecords";

       // Data bind to the selected data source
       barchart.DataBind();

        MemoryStream ms = new MemoryStream();
        barchart.SaveImage(ms, ChartImageFormat.Png);
        ms.Seek(0, SeekOrigin.Begin);

        return new FileStreamResult(ms, "image/png");
   }

圖像以不吸引人的方式呈現:

醜陋的 http://www.imagechicken.com/uploads/1253830647005451400.png

問題:如何以程式方式設置字型:

  • X 和 Y 軸標籤 - 即 Y 上的 0 到 35,以及 X 上的日期
  • 數據 - 即 12、0、0、3、6?
chart.ChartAreas[0].AxisX.LabelStyle.Font
chart.ChartAreas[0].AxisY.LabelStyle.Font

是您需要為軸設置字型的屬性。

Chart1.ChartAreas

$$ 0 $$.AxisX.LabelStyle.Font = new System.Drawing.Font(“Verdana”, 8f); Chart1.ChartAreas$$ 0 $$.AxisY.LabelStyle.ForeColor = System.Drawing.Color.Red;

引用自:https://stackoverflow.com/questions/1474299