首页 文章

在导出的OxyPlot图中添加页脚

提问于
浏览
2

我正在使用OxyPlot导出图 .

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

这是有效的,但正如你可能想象的那样,这是非常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创建一个轴,它位于第一个轴下方,然后禁用除 Headers 之外的所有视觉效果 .
最后我将它添加到我的plotmodel pm.Axes.Add(xAxis) .

要导出我的plotmodel,我使用 PdfExporter ,如下所示:

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

问候
Chriz

1 回答

  • 0

    只需要对我的项目做同样的事情,并认为我将分享我如何为其他需要页脚的人管理它 .

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

    • 删除项目中对OxyPlot.Pdf的任何先前引用 .

    • 从以下位置下载OxyPlot.Pdf源代码:https://github.com/oxyplot/oxyplot

    • 在VS的项目中,右键单击'Solution Explorer'中的解决方案并添加现有项目 .

    • 导航到下载的源代码并添加 OxyPlot.Pdf.csproj

    • 右键单击您的项目并添加参考

    • 选择左侧的'Projects'并选中右侧的OxyPlot.Pdf复选框 . 点击OK .

    • 通过构建和运行项目来检查它是否正常工作 .

    • 转到PdfRenderContext.cs文件,找到顶部附近的PdfRenderContext方法 .

    • 添加以下代码,然后构建并运行您的项目 .

    此代码创建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

相关问题