【问题标题】:Exporting TeeChart as Image将 TeeChart 导出为图像
【发布时间】:2016-08-25 11:45:54
【问题描述】:

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

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

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

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

提前致谢。

【问题讨论】:

    标签: winforms c#-4.0 teechart


    【解决方案1】:

    这是 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,其中一些步骤可以整合在一起,但我希望它们清晰且有用,并为您提供一些您想要的东西正在寻找。

    【讨论】:

      【解决方案2】:

      您可以手动调整坐标轴比例、导出图表然后恢复坐标轴。即(如果底部轴中的 0 - 4.25 是“整个图表”):

      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 的回复。但这并不能解决我们的问题,因为导出的图表将失去原始外观(如系列之间的垂直间距)。简单来说,如果 1/4 的图表在滚动条中可见,我们希望以当前图像大小导出它,即在这种情况下,导出的图像的垂直尺寸应该是当前可见图表高度的 4 倍。然后它将保留系列之间的间距。有没有办法做到这一点,请建议。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      • 2013-02-26
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多