将 TeeChart 导出为图像

Exporting TeeChart as Image

我们在表单应用程序中使用 TeeChart for .net(版本 4.1.2013.7302)。

我们产品中的一个图表启用了 Y 轴滚动。这使得图表的某些部分在给定实例中可见。要查看图表的其他部分,用户需要使用滚动条。使用单独的滚动条代替轴滚动条,因为将有一个相邻的网格控件;图表和网格都应使用通用滚动条滚动。以下是描述该场景的示例表单图像:

我们正在使用 TeeChart 的导出功能将此图表导出为图像。但是由于图表启用了滚动(即默认情况下图表的最小部分不可见); TeeChart 仅导出图表的可见部分,而不是整个图表。以下是导出的图表图片:

请建议是否有任何方法可以将整个图表导出为图像,而不仅仅是它的可见部分?

提前致谢。

您可以手动调整坐标轴刻度,导出图表,然后恢复坐标轴。即(如果底轴中的 0 - 4.25 是 "entire chart"):

double tmpMin = tChart1.Axes.Bottom.Minimum;
double tmpMax = tChart1.Axes.Bottom.Maximum;
tChart1.Axes.Bottom.SetMinMax(0, 4.25);
tChart1.Export.Image.JPEG.Save(myFileName);
tChart1.Axes.Bottom.SetMinMax(tmpMin, tmpMax);

这是 Yeray 代码的更完整版本,可将导出的图像填充到完整的(主要是不可见的)图表的大小:

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);
}

有很多方法可以优化该代码,Axis 确实有一个我没有尝试过的 iRange,并且可以将一些步骤放在一起,但我希望它们清晰有用,并能为您提供一些您想要的东西正在寻找。