Page 1 of 1

How to export full legend to png/jpeg/bmp etc..

Posted: Mon Jan 14, 2013 4:27 pm
by 9637806
We are using v2.0.1992.14012 to create various charts in our winforms app.
We provide the UI to save the chart into several of the provided image formats, e.g. JPG, PNG, BMP etc..

The problem is that this exports exactly what is shown on the chart control including the legend which could include more values than can fit hence we use the legend scrollbar.

I could save with different height/width but the problem is how to know how many legend items there are.
I searched these forums and came up with http://www.teechart.net/support/viewtop ... end#p42329 but a TChart objects Legend property does not have an Items property as detailed in the code.

The code that does the saving is in a custom usercontrol which is used in the different areas of the app to show charts. Therefore I cannot hardcode something specific to 1 particular chart.

Any ideas of how I can save the entire legend in the output picture (or even a separate picture if needs be). The above link is not really suitable because obviously given enough data that code would decrease the font size into negative sizes which is bound to fail.

Re: How to export full legend to png/jpeg/bmp etc..

Posted: Tue Jan 15, 2013 4:10 pm
by 10050769
Hello occ,

I have made a simple code where I have export the Chart in jpeg, png and bmp format:

Code: Select all

  public Form1()
        {
            InitializeComponent();
            InitializeChart();
        }
        Steema.TeeChart.Tools.LegendScrollBar legendScrollBar;
        private void InitializeChart()
        {
            tChart1.Dock = DockStyle.Fill;
            tChart1.Aspect.View3D = false;

            Steema.TeeChart.Styles.Bar bar1 = new Steema.TeeChart.Styles.Bar(tChart1.Chart);
            bar1.FillSampleValues(1000);
            // tChart1.Legend.DrawBehind = true;
            tChart1.Legend.LegendStyle = Steema.TeeChart.LegendStyles.Values;
            legendScrollBar = new Steema.TeeChart.Tools.LegendScrollBar(tChart1.Chart);
        }
        private int index = 0;
        private void button1_Click(object sender, EventArgs e)
        {
           
                tChart1.Export.Image.JPEG.Save(@"C:\tChart1.jpeg");
                tChart1.Export.Image.PNG.Save(@"C:\tChart1.png");
                tChart1.Export.Image.Bitmap.Save(@"C:\tChart1.bmp");  

        }
Could you tell us if previous code works in your end?

Thanks,

Re: How to export full legend to png/jpeg/bmp etc..

Posted: Thu Jan 17, 2013 11:19 am
by 9637806
I'm not sure what the purpose of your example code is. All it does is export a chart that has 3 different formats.

As my original post already stated our app already provides the user interface for the chart to be exported into a number of formats.
The problem is that when there are a lot of items in the legend the exported chart only shows the legend items that were visible at the time the export was done.

What I'm looking for is some way to export all the legend items, even if it is in a separate image to the chart itself.
The code at the link in my original post from your forums (a) doesn't work with v2.0.1992.14012 and (b) simply did a loop reducing the legend font size by one until all items fit on, which is pretty useless except in trivial cases where there is only 1 or 2 items that won't fit on.

I noticed that the export class has Height and Width properties so I tried exporting with the export height/width double the charts height/width. However this had the pretty useless effect of just exporting the same thing as before except filling the space available. What would have been much preferable would have been if it actually redrew the chart into the export size specified, i.e. I would expect that when exporting by changing the size it would have had the same effect as if I had doubled the chart controls height/width. I did try this also but it has the downsides of (a) making the UI look stupid and (b) there seems to be no way to know how big you'd need to make it any way.

Re: How to export full legend to png/jpeg/bmp etc..

Posted: Fri Jan 18, 2013 1:07 pm
by 10050769
Hello occ,

I have arranged for you two simple codes, one in last version 4 and other in last version 2 of TeeChartFor.Net that allows achieve as you want, but with some differences:

Code Version 4:
The code allows you control the position of LegendScrollBar for legend visible items and export the images with automatic way independently of the Chart size, using LegendItems and AutoSize to achieve a good results:

Code: Select all

       public Form1()
        {
            InitializeComponent();
            InitializeChart();
        }
        Steema.TeeChart.Tools.LegendScrollBar legendScrollBar;
        int LegendItemCount;
        private void InitializeChart()
        {
            tChart1.Dock = DockStyle.Fill;
            tChart1.Aspect.View3D = false;
            Steema.TeeChart.Styles.Bar bar1 = new Steema.TeeChart.Styles.Bar(tChart1.Chart);
            legendScrollBar = new Steema.TeeChart.Tools.LegendScrollBar(tChart1.Chart);
            bar1.FillSampleValues(100);
            bar1.Marks.Visible = false;
            tChart1.Draw();
            //Caluclate the legend items; 
            tChart1.Legend.AutoSize = false;
            int sizew = tChart1.Legend.Width;
            int sizeh = tChart1.Legend.Height;
            tChart1.Legend.Height = sizeh;
            tChart1.Legend.Width = sizew + legendScrollBar.Size;
            LegendItemCount = tChart1.Legend.Items.Count;//save the defualt legend items count visible. 
            button1.Click += new EventHandler(button1_Click);
        }
        private int index = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            if (legendScrollBar.Position <= (tChart1[0].Count - LegendItemCount))
            {
                if (index != 0)
                {
                    legendScrollBar.Position = legendScrollBar.Position + LegendItemCount;
                    tChart1.Export.Image.JPEG.Save(@"tChart" + index.ToString() + ".jpeg");
                    index++;
                }
                else
                {
                    legendScrollBar.Position = 0;
                    tChart1.Export.Image.JPEG.Save(@"tChart" + index.ToString() + ".jpeg");
                    index++;
                }
                this.Text = legendScrollBar.Position.ToString();
            }
            else
            {
                MessageBox.Show("Exportation finished");
                index = 0;
                legendScrollBar.Position = 0;
            }

        }
Code Version 2:
The code allows you control the position of LegendScrollBar for legend visible items and export the images as the code I have used in Version 4, but you can not calculate automaticaly the values, because you need know previously the number of legend items are visible and depending the size of legend you get a value or other. So, you need count the number of items appear in the legend to use the number when you calculate the position of LegendScrollBar. Therefore, you must adapt the code by your requeriments.

Code: Select all

         public Form1()
        {
            InitializeComponent();
            InitializeChart();
        }
        Steema.TeeChart.Tools.LegendScrollBar legendScrollBar;
        int  LegendItemCount; 
        private void InitializeChart()
        {
            tChart1.Dock = DockStyle.Fill;
            tChart1.Aspect.View3D = false; 
            Steema.TeeChart.Styles.Bar bar1 = new Steema.TeeChart.Styles.Bar(tChart1.Chart);
            legendScrollBar = new Steema.TeeChart.Tools.LegendScrollBar(tChart1.Chart);
            bar1.FillSampleValues(100);
            bar1.Marks.Visible = false;
            //Caluclate the legend items; 
            LegendItemCount = 28;
            button1.Click += new EventHandler(button1_Click);
        }
        private int index = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            if (legendScrollBar.Position <= (tChart1[0].Count-LegendItemCount))
            {
               
               // tChart1.Header.Text = tChart1.Legend.Items.Count.ToString();
                if (index != 0)
                {
                    legendScrollBar.Position = legendScrollBar.Position + LegendItemCount;
                    tChart1.Export.Image.JPEG.Save(@"tChart"+index.ToString()+".jpeg");
                    index++;
                }
                else
                {
                    legendScrollBar.Position = 0; 
                    tChart1.Export.Image.JPEG.Save(@"tChart" + index.ToString() + ".jpeg");
                    index++;
                }
                this.Text = legendScrollBar.Position.ToString();
            }
            else
            {
                MessageBox.Show("Exportation finished");
                index = 0;
                legendScrollBar.Position = 0;
            }
          
        }
Could you tell us if previous code works in your end? On the other hand, I recommend you try my code using the Evaluation version of TeeChart 4, you can download this here and check the new functionalities and improvements as you can get with latest versions of TeeChartFor.Net.

I hope will helps.

Thanks,