【问题标题】:Add a footer to an exported OxyPlot plot为导出的 OxyPlot 绘图添加页脚
【发布时间】:2015-06-23 14:38:18
【问题描述】:

我正在使用 OxyPlot 导出绘图。

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

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

所以我的问题是,是否有可能在导出的图中添加这样的页脚?

编辑:

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

问候
克里斯

【问题讨论】:

  • 如果您可以添加尽可能小的代码示例来演示问题,那么其他用户会更容易帮助您。有些人甚至可能会采用您的示例并将其复制/粘贴到他们的 IDE 中以找出解决方案,但如果您给他们一个坚实的起点,这会容易得多。
  • 对不起,你是对的。我添加了一个代码示例,以便有人可以重现该问题。由于我是 StackOverflow 的新手,如果我遗漏了问题中的任何内容,请随时告诉我。

标签: oxyplot


【解决方案1】:

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

我找不到任何内置的 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,然后您可以将其添加为项目的引用并删除源代码。

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多