I have a wep-app which generates a chart, and streams it on the response output to the client. This all works quite ok. But I want the chart to have a transparent background (so 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 (a simple console app to show the problem). 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);
}
Wow - easy answer, thanks a lot. I had tried setting the anti-aliasing to "high", which I though meant "best quality". But "system-default" (which I thought it would have been if I hadn't set it at all) definitely does the trick.
xdzgor
Member
6 Points
17 Posts
Chart - transparency and text quality
Feb 22, 2011 07:36 PM|LINK
Hi
I have a wep-app which generates a chart, and streams it on the response output to the client. This all works quite ok. But I want the chart to have a transparent background (so 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 (a simple console app to show the problem). 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); }Thanks,
</div> </div>Peter
SGWellens
All-Star
126031 Points
10310 Posts
Moderator
Re: Chart - transparency and text quality
Feb 23, 2011 01:46 AM|LINK
This seems to fix it:
c.TextAntiAliasingQuality = TextAntiAliasingQuality.SystemDefault;
My blog
xdzgor
Member
6 Points
17 Posts
Re: Chart - transparency and text quality
Feb 23, 2011 05:34 AM|LINK
Wow - easy answer, thanks a lot. I had tried setting the anti-aliasing to "high", which I though meant "best quality". But "system-default" (which I thought it would have been if I hadn't set it at all) definitely does the trick.
Thanks, Peter