5

I want a chart with transparent background, and therefore PNG seems a good choice. But when I set transparent background, the quality of the axis labels falls dramatically. How do I fix this? See the following code. As it stands, the chart has a transparent background, as I want, but the text quality is atrocious. If I comment out the two "Color.Transparent" settings, then the text quality is nice, but the background is not transparent.

How do I get transparency and nice text?

public static void Main(string[] args)
{
  Chart c = new Chart();
  c.TextAntiAliasingQuality = TextAntiAliasingQuality.High;

  Series s = new Series("Series1");
  c.Series.Clear();
  c.Series.Add(s);
  s.ChartType = SeriesChartType.Line;

  s.Color = Color.Black;

  ChartArea chartArea = new ChartArea("ChartArea1");
  c.ChartAreas.Clear();
  c.ChartAreas.Add(chartArea);

  chartArea.BackColor = Color.FromArgb(255, 255, 255);
  chartArea.BackSecondaryColor = Color.FromArgb(220, 220, 220);
  chartArea.BackGradientStyle = GradientStyle.TopBottom;

  chartArea.AxisX.LineColor = Color.Gray;
  chartArea.AxisX.LineWidth = 2;
  chartArea.AxisX.LineDashStyle = ChartDashStyle.Solid;

  chartArea.AxisY.LineColor = Color.Gray;
  chartArea.AxisY.LineWidth = 2;
  chartArea.AxisY.LineDashStyle = ChartDashStyle.Solid;

  chartArea.AxisX.MajorGrid.LineColor = Color.LightGray;
  chartArea.AxisX.MajorGrid.LineDashStyle = ChartDashStyle.Dash;

  chartArea.AxisY.MajorGrid.LineColor = Color.LightGray;
  chartArea.AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;

  c.BackColor = Color.Transparent;
  chartArea.BackColor = Color.Transparent;


  double[] x = new double[] { 1999, 2005 };
  double[] y = new double[] { 3210, 13456 };

  Axis ay = chartArea.AxisY;
  ay.Maximum = 13456;
  ay.Minimum = 3210;

  Axis ax = chartArea.AxisX;
  ax.Maximum = 2005;
  ax.Minimum = 1999;

  for (int i = 0; i < x.Length; i++)
  {
    double xvalue = x[i];
    double yvalue = y[i];
    s.Points.AddXY(xvalue, yvalue);
   }

   // Save chart-image to disk:
   c.SaveImage("chartimage.png", ChartImageFormat.Png);
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
xdzgor
  • 51
  • 1
  • 2
  • Try setting c.AntiAliasing = Text. By the way, in the future could you post an image as well? I think that would make it easier for people to find your problem. – Brandon Bohrer Feb 21 '11 at 19:39

3 Answers3

6

Set chart's AntiAliasing to AntiAliasingStyles.Graphics to disable the antialiasing on text.

Taken from this thread.

Martin Capodici
  • 1,486
  • 23
  • 27
  • While this cleans up the text a bit, it's still unacceptably jagged... this is more a problem with the chart control itself though. – Edyn Jun 27 '13 at 15:26
0

Maybe this help you

in your .aspx file where your chart code is, look for the asp:ChartArea tag. then add BackColor = "Transparent".

<asp:ChartArea Name="ChartArea1" BackColor="Transparent" 
            </asp:ChartArea>

Hope this help.

Pablo Johnson
  • 1,044
  • 15
  • 21
0
chart.TextAntiAliasingQuality = TextAntiAliasingQuality.SystemDefault;

I read this from here: http://forums.asp.net/p/1656335/4315304.aspx?Re%20Chart%20transparency%20and%20text%20quality

cheny
  • 2,545
  • 1
  • 24
  • 30