gpt4 book ai didi

oxyplot - 为导出的 OxyPlot 图添加页脚

转载 作者:行者123 更新时间:2023-12-05 06:44:06 36 4
gpt4 key购买 nike

我正在使用 OxyPlot 导出绘图。

当我导出它们时,我想为这些图添加一个页脚,其中包含保存路径、时间戳等信息...
现在,我通过在不同的位置层上创建一个额外的 X 轴,然后将所有刻度和标签的大小设置为零来实现这一点,除了标题字体大小。

这行得通,但正如您可能想象的那样,这很老套而且看起来不太好(因为您无法设置例如对齐)。

所以我的问题是,是否可以将这样的页脚添加到导出的绘图中?

编辑:

var xAxis = new OxyPlot.Axes.LinearAxis
{
Position = AxisPosition.Bottom,
PositionTier = 1,
Title = "Footer: i.e. path to my file",
MinorTickSize = 0.0,
MajorTickSize = 0.0,
FontSize = 0.0,
TitleFontSize = 12,
AxisDistance = 10
};

这就是我提到的解决方法。
我在第 1 层位置创建了一个轴,它位于第一个轴的下方,然后禁用除标题之外的所有视觉效果。
最后我将它添加到我的 plotmodel pm.Axes.Add(xAxis)

要导出我的绘图模型,我使用 PdfExporter,如下所示:

using (var stream = File.Create(testFile.pdf))
{
PdfExporter.Export(pm, stream, 800, 500);
}

问候
克里斯

最佳答案

我不得不对我的项目做同样的事情,我想我会为其他需要页脚的人分享我是如何管理它的。

我找不到任何内置的 OxyPlot 方法来添加页眉或页脚,但如果您使用 OxyPlot.PDF,它是在 PDFSharp 之上构建的,您有更多选项来自定义您的 PDF 导出。

  1. 删除项目中之前对 OxyPlot.Pdf 的任何引用。
  2. 从以下地址下载 OxyPlot.Pdf 源代码:https://github.com/oxyplot/oxyplot
  3. 在您的 VS 项目中,在“解决方案资源管理器”中右键单击您的解决方案并添加现有项目。
  4. 导航到下载的源代码并添加OxyPlot.Pdf.csproj
  5. 右键单击您的项目并添加引用
  6. 选择左侧的“项目”并选中右侧的 OxyPlot.Pdf 复选框。点击确定。
  7. 通过构建和运行项目来检查它是否正常工作。
  8. 转到 PdfRenderContext.cs 文件并在顶部附近找到 PdfRenderContext 方法。
  9. 添加以下代码,然后构建并运行您的项目。

此代码创建一个 MigraDoc 文档,然后将其与 OxyPlot PdfDocument 合并。

    public PdfRenderContext(double width, double height, OxyColor background)
{
//*** Original Code - Don't change **//
this.RendersToScreen = false;
this.doc = new PdfDocument();
var page = new PdfPage { Width = new XUnit(width), Height = new XUnit(height) };
this.doc.AddPage(page);
this.g = XGraphics.FromPdfPage(page);
if (background.IsVisible())
{
this.g.DrawRectangle(ToBrush(background), 0, 0, width, height);
}

//*** New Code to add footer **//
Document myDoc = new Document();
Section mySection = myDoc.AddSection();
Paragraph footerParagraph = mySection.Footers.Primary.AddParagraph();
footerParagraph.AddText(DateTime.Now.ToShortDateString());
footerParagraph.Format.Alignment = ParagraphAlignment.Right;

MigraDoc.Rendering.DocumentRenderer docRenderer = new MigraDoc.Rendering.DocumentRenderer(myDoc);
docRenderer.PrepareDocument();
docRenderer.RenderObject(g, XUnit.FromInch(9.5), XUnit.FromInch(8), "1in", footerParagraph);
}

当您导出 PDF 时,日期戳现在会添加到 PDF 的右下角。请注意,我使用的是横向 8.5x11 英寸纸,因此如果您在绘图上看不到它,您可能需要更改位置。左上角为 0,0。运行后,构建 OxyPlot.Pdf 项目以创建 dll,然后您可以将其添加为对项目的引用并删除源代码。

结果:

enter image description here

关于oxyplot - 为导出的 OxyPlot 图添加页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31005850/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com