Add a footer to an exported OxyPlot plot

797 views Asked by At

I am using OxyPlot to export plots.

When I export them, I want to add a footer to these plots with information like the path it is saved, a time-stamp, and so on...
Right now I am doing this by creating an extra X-axis on a different position-tier and then setting the sizes of all ticks and labels to zero except for the title font-size.

This works, but as you might imagine, this is quite hacky and does not look that good (as you cannot set for example the aligning).

So my question is, is there a possibility to add such a footer to the exported plot?

EDIT:

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

This is the workaround I mentioned.
I create an axis at position-tier 1, which is below the first one and then disable all visuals of it except the title.
And in the end I add it to my plotmodel pm.Axes.Add(xAxis).

To export my plotmodel I use PdfExporter like this:

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

Greetings
Chriz

1

There are 1 answers

0
Automate This On BEST ANSWER

Just had to do the same thing with my project and thought I'd share how I managed it for anyone else in need of a footer.

I couldn't find any built in OxyPlot methods to add a header or footer but if you use OxyPlot.PDF it's built on top of PDFSharp and you have more options to customize your PDF export.

  1. Remove any previous reference to OxyPlot.Pdf in your project.
  2. Download OxyPlot.Pdf source code from: https://github.com/oxyplot/oxyplot
  3. In your project in VS, right click your solution in 'Solution Explorer' and Add Existing Project.
  4. Navigate to the downloaded source code and add OxyPlot.Pdf.csproj
  5. Right click your project and Add Reference
  6. Select 'Projects' on the left and check the box for OxyPlot.Pdf on the right. Hit OK.
  7. Check that it's working by building and running project.
  8. Go to PdfRenderContext.cs file and find the PdfRenderContext method near the top.
  9. Add the code below then build and run your project.

This code creates a MigraDoc Document and then merges it with the 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);
    }

When you export the PDF a date stamp is now added to the lower right corner of the PDF. Note that I was working with landscape 8.5x11 inch paper so you may need to change position if you don't see it on the plot. Upper left corner is 0,0. Once it's working, build the OxyPlot.Pdf project to create the dll and then you can add it as a reference to your project and remove the source code.

Result:

enter image description here