ASP.NET Chart: setting the fonts on X and Y axis
Asked Answered
G

3

9

Consider an ASP.NET MVC controller method that builds an ASP.NET Chart image.

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

The image is rendered in an unattractive manner:

fugly http://www.imagechicken.com/uploads/1253830647005451400.png

Question: How can I set the font programmatically for the:

  • X and Y axis labels - i.e. 0 through 35 on Y, and the dates on X
  • data - i.e. 12, 0, 0, 3, 6 ?
Giorgi answered 24/9, 2009 at 21:39 Comment(0)
S
13
chart.ChartAreas[0].AxisX.LabelStyle.Font
chart.ChartAreas[0].AxisY.LabelStyle.Font

is the property you need to set the font for the Axes.

Standardbearer answered 10/10, 2009 at 16:9 Comment(0)
M
1

Chart1.ChartAreas[0].AxisX.LabelStyle.Font = new System.Drawing.Font("Verdana", 8f); Chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = System.Drawing.Color.Red;

Malaise answered 25/1, 2016 at 14:37 Comment(0)
G
0

Another problem I faced was the jaggies on the text. Changing from .png to .jpg did the trick!

Giorgi answered 13/10, 2009 at 15:36 Comment(1)
Equally this will smooth everything out for you: chart1.AntiAliasing = AntiAliasingStyles.All;Freiburg

© 2022 - 2024 — McMap. All rights reserved.