Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing DataVisualization.Chart size without using WinForms

I have an Web Service that generates different kinds of charts. The charts are generated programatically using System.Windows.Forms.DataVisualization.Charting and they are saved to .png files. But whatever I do, the charts have the dimension 300x300 pixels.

On the internet I have found many solutions on changing the size of the chart, but they only apply to situations where the chart is put into a WinForm and then saved to a file.

How can I change the size of the chart if I don't have WinForms in my application?

This is a dummy example of what I do in my code

int[] yVal = { 1, 1, 1, 1, 1, 1, 1 };
string[] xName = { "a", "b", "b", "b", "b", "b", "b" };

System.Windows.Forms.DataVisualization.Charting.Chart Chart1 = new Chart();
Chart1.Titles.Add("Title");
Chart1.Series.Add(new Series());

Chart1.Series[0].XValueType = ChartValueType.String;
Chart1.Series[0].YValueType = ChartValueType.Int32;
Chart1.Series[0].Points.DataBindXY(xName, yVal);

Chart1.Palette = ChartColorPalette.EarthTones;

Chart1.Legends.Add(new Legend());
Chart1.Legends[0].Enabled = false;

ChartArea chartArea = new ChartArea();
chartArea.AxisX.Title = "X";
chartArea.AxisY.Title = "Y";
Chart1.ChartAreas.Add(chartArea);


Chart1.SaveImage("chart.png", ChartImageFormat.Png);
like image 284
Coral Doe Avatar asked Jul 12 '26 02:07

Coral Doe


1 Answers

To get a 1000px x 1000px large image, add the following line after the chart initialization:

Chart1.Size = new Size(1000, 1000);
like image 76
Anders Gustafsson Avatar answered Jul 14 '26 16:07

Anders Gustafsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!