Page 1 of 1
Programmatic Chart Creation and Saving to Disk
Posted: Sun May 05, 2024 3:26 am
by 13049545
Hi Fellow TChart Devs,
I was curious if there's an example of programmatically generating charts using TChart.Net then saving chart pic to a disk file.
In other words to use in an API without WinForms.
I searched here and on YouTube and didn't find anything.
It would save me a lot of time.
Thanks,
ja
Re: Programmatic Chart Creation and Saving to Disk
Posted: Tue May 07, 2024 10:46 am
by edu
Hello,
here's a quick example of what you're requesting, made with a Console App and .NET 6.0.
Code: Select all
using Steema;
using Steema.TeeChart;
using Steema.TeeChart.Styles;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
//Create chart
TChart myChart = new TChart();
//Create your series and populate them and add Series to Chart
Line myLine = new Line();
myChart.Series.Add(myLine);
for (int i = 0; i<5; i++)
{
myLine.Add(i, i*2);
}
// (Optional) Customization
myLine.Marks.Visible = true;
myLine.ColorEach = true;
myChart.Export.Image.PNG.Width = 1500;
myChart.Export.Image.PNG.Height = 1500;
//Finally, export.
string myPath = "myChart.png";
myChart.Export.Image.PNG.Save(myPath);
}
}
}
Regards,
Edu
Re: Programmatic Chart Creation and Saving to Disk
Posted: Tue May 07, 2024 2:04 pm
by 13049545
Thanks so much!
Re: Programmatic Chart Creation and Saving to Disk
Posted: Sun May 12, 2024 4:13 pm
by 13049545
So I tried the code and it works but I'm having trouble writing custom text out to "graphics" object - normally this text is written in the "BeforeDraw" event when using WinForms - but in API style no events are thrown so I thought I could access the "g" graphics object passed in the DrawEvents and draw but I get an error -- see picture below - System.NullReferenceException on the g.TextOut line of code:
Re: Programmatic Chart Creation and Saving to Disk
Posted: Sun May 12, 2024 4:27 pm
by 13049545
Nevermind - doyyy -
I just wired up the API Version of TChart to its AfterDraw event handler and did the drawing the same as the WinForms Version and it works -- thanks for your help!