Aligator Aligator - 1 year ago 99
C# Question

C# saving chart into image file with axis

can you tell me what is wrong with my code? I want to generate chart image in console application over System.Windows.Forms.DataVisualization.Charting library... Following code generate me chart only with columns, but I need chart with axis. Any ideas?

Chart chart = new Chart();
chart.Size = new System.Drawing.Size(2000, 500);

ChartArea area = new ChartArea();

chart.BackColor = System.Drawing.Color.Transparent;
chart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
chart.ChartAreas[0].AxisY.MajorGrid.Enabled = false;

chart.ChartAreas[0].AxisX.Title = "sasdasdasd";

Series series = new Series()
Name = "series2",
IsVisibleInLegend = false,
ChartType = SeriesChartType.Column


foreach (CnbItem item in items)
DataPoint p1 = new DataPoint(0, Double.Parse(item.Kurz));
p1.Color = System.Drawing.Color.LightBlue;
p1.AxisLabel = item.Kod;
p1.LegendText = item.Kod;
p1.Label = item.Kurz;



string filename = "D:\\Chart.png";
chart.SaveImage(filename, ChartImageFormat.Png);

Answer Source

Update: Setting the the Chart's Backcolor to Transparent actually works just fine. However some display programs do not display transparency; I use Irfanview as my default viewer and it is one of those which can't. I suspect yours also misses transparency..

Instead all transparency is rendered as Black, so unless you have a non-black line color.. your axes and labels etc.. seem to be gone. Paint (on W10) is another one but renders transparency to white, so the black pixels are visible.

The below image is from Photoshop, which, of course, doesn't have that problem..

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download