Search code examples
winformsc#-4.0teechart

Exporting TeeChart as Image


We are using TeeChart for .net (Version 4.1.2013.7302) in our forms application.

One of the charts in our product has Y Axis scrolling enabled. This makes some portion of chart visible at a given instance. To see other part of chart, user needs to use the scrollbar. A separate scrollbar is used instead of axis scrollbar, as there will be a adjoining grid control and; both chart & grid are expected to be scrolled using common scrollbar. Following is the sample form image depicting the scenario:

enter image description here

We are using TeeChart's export functionality to export this chart as an image. But since chart has scrolling enabled (i.e. minimum of chart is not visible by default); TeeChart is exporting only visible portion of Chart, and not the entire chart. Following is the image of chart exported: enter image description here

Please suggest if there exists any way to export the entire chart as an image, and not just the visible portion of it?

Thanks in Advance.


Solution

  • Here's a fuller version of Yeray's code that fills out the exported image to the size of the full, mainly non-visible chart:

    private void button11_Click(object sender, EventArgs e)
    {
      //get zoomed axis min maxes
      double xtmpMin = tChart1.Axes.Bottom.Minimum;
      double xtmpMax = tChart1.Axes.Bottom.Maximum;
      double ytmpMin = tChart1.Axes.Left.Minimum;
      double ytmpMax = tChart1.Axes.Left.Maximum;
    
      //how many pixels are plotted for the axes' ranges
      int yPixelRange = tChart1.Axes.Left.CalcPosValue(tChart1.Axes.Left.Minimum)-tChart1.Axes.Left.CalcPosValue(tChart1.Axes.Left.Maximum);
      int xPixelRange = tChart1.Axes.Bottom.CalcPosValue(tChart1.Axes.Bottom.Maximum) - tChart1.Axes.Bottom.CalcPosValue(tChart1.Axes.Bottom.Minimum);
    
      //get the chart header/footer space to re-apply to chart
      int yMargins = tChart1.Bounds.Height - yPixelRange;
      int xMargins = tChart1.Bounds.Width - xPixelRange;
    
      //how many pixels are we getting per axis scale
      double pixelsPerYAxisInt = yPixelRange / (ytmpMax - ytmpMin);
      double pixelsPerXAxisInt = xPixelRange / (xtmpMax - xtmpMin);
    
      //what increment are we at. Note. To get this back we may need to mod font size, min separation
      double yInc = tChart1.Axes.Left.CalcIncrement;
      double xInc = tChart1.Axes.Bottom.CalcIncrement;
    
      //now reset auto axes before plotting full chart. Could use other criteria here
      tChart1.Axes.Left.Automatic = true;
      tChart1.Axes.Bottom.Automatic = true;
    
      //Repaint full Chart (necessary for positioning calcs)
      tChart1.Draw();
    
      //set increments on full scales (note Chart will try to set them, 
      //but if it can't you have the last word with label separation, font size, etc)
      tChart1.Axes.Left.Increment = yInc;
      tChart1.Axes.Bottom.Increment = xInc;
    
      //dimension chart for export
      double fullYRange = tChart1.Axes.Left.Maximum - tChart1.Axes.Left.Minimum;
      double fullXRange = tChart1.Axes.Bottom.Maximum - tChart1.Axes.Bottom.Minimum;
    
      int fullYSize = (int)((pixelsPerYAxisInt * fullYRange) + yMargins);
      int fullXSize = (int)((pixelsPerXAxisInt * fullXRange) + xMargins);
    
      //setup and export image
      tChart1.Export.Image.PNG.Width = fullXSize;
      tChart1.Export.Image.PNG.Height = fullYSize;
    
      tChart1.Export.Image.PNG.Save(@"c:\mypath\chart.png");
    
      //reset screen chart to where it was      
      tChart1.Axes.Bottom.SetMinMax(xtmpMin, xtmpMax);
      tChart1.Axes.Left.SetMinMax(ytmpMin, ytmpMax);
    }
    

    There are many ways to optimise that code, Axis does have an iRange that I haven't tried, and some of the steps can be brought together but I hope they are clear and useful and give you something of what you're looking for.