Как разместить большую легенду под диаграммой? - PullRequest
1 голос
/ 06 марта 2019

В моем графике много элементов, которые не помещаются в одну линию под графиком.Как я могу добиться разрыва строки, чтобы все легенды помещались под графиком?

Вот пример моего варианта использования, скопируйте его в локальный проект и используйте NuGet для настройки пакета MigraDoc.

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.DocumentObjectModel.Shapes.Charts;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
using System.Diagnostics;
using PdfSharp.Pdf;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main()
        {
            // Create a MigraDoc document
            Document document = CreateDocument();

            //string ddl = MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToString(document);
            //MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document, "MigraDoc.mdddl");

            PdfDocumentRenderer renderer = new PdfDocumentRenderer(unicode: true);
            renderer.Document = document;

            renderer.RenderDocument();

            // Save the document...

            //renderer.PdfDocument.Save(filename);
            renderer.PdfDocument.Save("example.pdf");
            // ...and start a viewer.
            //Process.Start(@"C:\pdf.pdf");
        }

        /// <summary>
        /// Creates a Document
        /// </summary>
        public static Document CreateDocument()
        {
            // Create a new MigraDoc document
            Document document = new Document();

            DefineStyles(document);
            DefineCover(document);
            DefineParagraphsTest(document);

            return document;
        }

        /// <summary>
        /// Defines the styles used in the document.
        /// </summary>
        public static void DefineStyles(Document document)
        {
            // Get the predefined style Normal.
            Style style = document.Styles["Normal"];
            // Because all styles are derived from Normal, the next line changes the 
            // font of the whole document. Or, more exactly, it changes the font of
            // all styles and paragraphs that do not redefine the font.
            style.Font.Name = "Times New Roman";

            // Heading1 to Heading9 are predefined styles with an outline level. An outline level
            // other than OutlineLevel.BodyText automatically creates the outline (or bookmarks) 
            // in PDF.

            style = document.Styles["Heading1"];
            style.Font.Name = "Arial";
            style.Font.Size = 18;
            style.Font.Bold = true;
            style.Font.Color = Colors.DarkBlue;
            style.ParagraphFormat.PageBreakBefore = true;
            style.ParagraphFormat.SpaceAfter = 8;

            style = document.Styles["Heading2"];
            style.Font.Size = 12;
            style.Font.Bold = true;
            style.ParagraphFormat.PageBreakBefore = false;
            style.ParagraphFormat.SpaceBefore = 6;
            style.ParagraphFormat.SpaceAfter = 6;

            style = document.Styles["Heading3"];
            style.Font.Size = 10;
            style.Font.Bold = true;
            style.Font.Italic = true;
            style.ParagraphFormat.SpaceBefore = 6;
            style.ParagraphFormat.SpaceAfter = 3;

            style = document.Styles[StyleNames.Header];
            style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right);

            style = document.Styles[StyleNames.Footer];
            style.ParagraphFormat.AddTabStop("8cm", TabAlignment.Center);

            // Create a new style called TextBox based on style Normal
            style = document.Styles.AddStyle("TextBox", "Normal");
            style.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
            style.ParagraphFormat.Borders.Width = 2.5;
            style.ParagraphFormat.Borders.Distance = "3pt";
            style.ParagraphFormat.Shading.Color = Colors.SkyBlue;

            // Create a new style called TOC based on style Normal
            style = document.Styles.AddStyle("TOC", "Normal");
            style.ParagraphFormat.AddTabStop("16cm", TabAlignment.Right, TabLeader.Dots);
            style.ParagraphFormat.Font.Color = Colors.Blue;

            //Create a new style called Title based on style Normal
            style = document.Styles.AddStyle("Title", "Normal");
            style.Font.Name = "Arial";
            style.Font.Size = 28;
            style.Font.Bold = true;
            style.Font.Color = Colors.Black;

            //Create a new style called PUID based on style Normal
            style = document.Styles.AddStyle("PUID", "Normal");
            style.Font.Name = "Arial";
            style.Font.Size = 22;
            style.Font.Bold = true;
            style.Font.Color = Colors.Black;

            //Create a new style called PASS based on style Normal
            style = document.Styles.AddStyle("PASS", "Normal");
            style.Font.Name = "Arial";
            style.Font.Size = 36;
            style.Font.Bold = false;
            style.Font.Color = Colors.Green;

            //Create a new style called FAIL based on style Normal
            style = document.Styles.AddStyle("FAIL", "Normal");
            style.Font.Name = "Arial";
            style.Font.Size = 36;
            style.Font.Bold = false;
            style.Font.Color = Colors.Red;

            //Create a new style called Text based on style Normal
            style = document.Styles.AddStyle("Text", "Normal");
            style.Font.Name = "Arial";
            style.Font.Size = 10;
            style.Font.Color = Colors.Black;
            style.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
        }

        /// <summary>
        /// Defines the cover page.
        /// </summary>
        public static void DefineCover(Document document)
        {
            Section section = document.AddSection();

            section.PageSetup.DifferentFirstPageHeaderFooter = true;
            HeaderFooter cover_footer = section.Footers.FirstPage;

            //DIN A4 = 21cm x 29,7cm
            Paragraph paragraph = section.AddParagraph("Title");
            paragraph.Style = "Title";
            paragraph.Format.SpaceBefore = "0cm";
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            paragraph = section.AddParagraph("Date: ");
            paragraph.Style = "Text";
            paragraph.AddDateField();

            paragraph = section.AddParagraph("Green");
            paragraph.Style = "PASS";
            paragraph.Format.Alignment = ParagraphAlignment.Center;

            paragraph = section.AddParagraph("Red");
            paragraph.Style = "FAIL";
            paragraph.Format.Alignment = ParagraphAlignment.Center;

        }

        public static void DefineParagraphsTest(Document document)
        {
            Paragraph paragraph = document.LastSection.AddParagraph("First", "Heading1");
            paragraph.AddBookmark("First");

            GenerateHistogram(document);
        }

        public static void GenerateHistogram(Document document)
        {
            document.LastSection.AddParagraph("Histogram", "Heading2");

            Chart chart = new Chart();
            chart.Left = 0;
            chart.Width = Unit.FromCentimeter(16);
            chart.Height = Unit.FromCentimeter(12);
            chart.Format.Alignment = ParagraphAlignment.Center;//has no effect
            chart.FooterArea.AddLegend();

            for (int loop = 0; loop < 15; loop++)
            {
                Series series = chart.SeriesCollection.AddSeries();
                series.ChartType = ChartType.Column2D;
                List<double> Yvalues = new List<double>();
                for (double i = 0.2; i <= 4.5; i += 0.1)
                {
                    Yvalues.Add(i);
                }
                series.Add(Yvalues.ToArray());
                series.HasDataLabel = false;
                series.Name = "legend" + loop;

            }

            XSeries xseries = chart.XValues.AddXSeries();
            //Xaxis contains every second value, to avoid overlapping text
            List<string> Xaxis = new List<string>();
            bool skip = false;
            for (double i = 0.2; i <= 4.5; i += 0.1)
            {
                if (skip)
                {
                    Xaxis.Add("");
                    skip = false;
                }
                else
                {
                    Xaxis.Add(i.ToString());
                    skip = true;
                }

            }
            xseries.Add(Xaxis.ToArray());

            chart.XAxis.MajorTickMark = TickMarkType.Outside;
            chart.XAxis.Title.Caption = "Time Interval [s]";

            chart.YAxis.MajorTickMark = TickMarkType.Outside;
            chart.YAxis.HasMajorGridlines = true;

            chart.PlotArea.LineFormat.Color = Colors.DarkGray;
            chart.PlotArea.LineFormat.Width = 1;

            document.LastSection.Add(chart);
        }
    }
}

Он генерирует следующий вывод: Screenshot of resulting pdf

Любое предложение о том, как я могу заставить перенос строки, чтобы все легенды помещались в две строки?

...