Programmatic Chart Creation and Saving to Disk

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
jzarech
Newbie
Newbie
Posts: 50
Joined: Mon Jul 07, 2008 12:00 am

Programmatic Chart Creation and Saving to Disk

Post by jzarech » Sun May 05, 2024 3:26 am

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

Edu
Newbie
Newbie
Posts: 9
Joined: Tue Nov 28, 2023 12:00 am

Re: Programmatic Chart Creation and Saving to Disk

Post by Edu » Tue May 07, 2024 10:46 am

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
Edu
Steema Support

jzarech
Newbie
Newbie
Posts: 50
Joined: Mon Jul 07, 2008 12:00 am

Re: Programmatic Chart Creation and Saving to Disk

Post by jzarech » Tue May 07, 2024 2:04 pm

Thanks so much!

jzarech
Newbie
Newbie
Posts: 50
Joined: Mon Jul 07, 2008 12:00 am

Re: Programmatic Chart Creation and Saving to Disk

Post by jzarech » Sun May 12, 2024 4:13 pm

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:

Image

jzarech
Newbie
Newbie
Posts: 50
Joined: Mon Jul 07, 2008 12:00 am

Re: Programmatic Chart Creation and Saving to Disk

Post by jzarech » Sun May 12, 2024 4:27 pm

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!

Image

Post Reply