Невозможно сгенерировать LineChart с использованием OpenXML - PullRequest
0 голосов
/ 13 апреля 2020

Ниже приведен мой код для генерации LineChart. Это генерирует только символы. Пожалуйста, помогите мне исправить этот код. Я новичок в области Open XML.

publi c stati c void CreateLineChart (документ WordprocessingDocument) {string title = "Title";

        Dictionary<string, int> data = new Dictionary<string, int>();
        data.Add("Jan", 2);
        data.Add("Feb", 2);
        data.Add("March", 12);
        data.Add("April", 2);
        data.Add("May", 40);
        data.Add("June", 20);

        // Get MainDocumentPart of Document
        MainDocumentPart mainPart = document.AddMainDocumentPart();
        mainPart.Document = new wp.Document(new wp.Body());

        wp.Paragraph paragraph1 = new wp.Paragraph();
        wp.ParagraphProperties paragraphProperties1 = new wp.ParagraphProperties();
        wp.ParagraphStyleId paragraphStyleId1 = new wp.ParagraphStyleId() { Val = "Normal" };
        wp.Justification justification1 = new wp.Justification() { Val = wp.JustificationValues.Center };
        wp.ParagraphMarkRunProperties paragraphMarkRunProperties1 = new wp.ParagraphMarkRunProperties();

        paragraphProperties1.Append(paragraphStyleId1);
        paragraphProperties1.Append(justification1);
        paragraphProperties1.Append(paragraphMarkRunProperties1);

        wp.Run run = new wp.Run();
        wp.Text text = new wp.Text();
        text.Text = "POC : Microsoft-Graph";
        run.Append(text);

        paragraph1.Append(paragraphProperties1);
        paragraph1.Append(run);
        mainPart.Document.Body.Append(paragraph1);

        /* Text of Description*/
        wp.Run run2 = new wp.Run();
        wp.Text text2 = new wp.Text();
        StringBuilder desc = new StringBuilder();
        desc.Append("Test paragraph");
        text2.Text = desc.ToString();
        wp.Paragraph paragraph2 = new wp.Paragraph();
        wp.ParagraphProperties paragraphProperties2 = new wp.ParagraphProperties();
        wp.ParagraphStyleId paragraphStyleId2 = new wp.ParagraphStyleId() { Val = "Normal" };
        wp.Justification justification2 = new wp.Justification() { Val = wp.JustificationValues.Left };
        wp.ParagraphMarkRunProperties paragraphMarkRunProperties2 = new wp.ParagraphMarkRunProperties();

        paragraphProperties2.Append(paragraphStyleId2);
        paragraphProperties2.Append(justification2);
        paragraphProperties2.Append(paragraphMarkRunProperties2);

        run2.Append(text2);
        paragraph2.Append(paragraphProperties2);
        paragraph2.Append(run2);
        mainPart.Document.Body.Append(paragraph2);
        /*End*/

        // Create ChartPart object in Word Document
        ChartPart chartPart = mainPart.AddNewPart<ChartPart>("rId110");

        // the root element of chartPart 
        ChartSpace chartSpace = new ChartSpace();
        chartSpace.Append(new EditingLanguage() { Val = "en-us" });

        // Create Chart 
        DocumentFormat.OpenXml.Drawing.Charts.Chart chart = new DocumentFormat.OpenXml.Drawing.Charts.Chart();
        chart.Append(new AutoTitleDeleted() { Val = true });

        // Define the 3D view
        View3D view3D = new View3D();
        view3D.Append(new RotateX() { Val = 45 });
        view3D.Append(new RotateY() { Val = 0 });

        // Intiliazes a new instance of the PlotArea class
        PlotArea plotArea = new PlotArea();
        LineChart lineChart = plotArea.AppendChild<LineChart>(
            new LineChart { Grouping = new Grouping { Val = GroupingValues.Standard } }
         );

        plotArea.Append(new Layout());
        ChartShapeProperties chartShapePros = new ChartShapeProperties();

        uint i = 0;
        // Iterate through each key in the Dictionary collection and add the key to the chart Series
        // and add the corresponding value to the chart Values.

        foreach (string key in data.Keys)
        {
            LineChartSeries lineChartSeries = lineChart.AppendChild<LineChartSeries>(
                new LineChartSeries(
                    new Index() { Val = new UInt32Value(i) },
                    new Order() { Val = new UInt32Value(i) },
                    new SeriesText(new NumericValue() { Text = key })
                    )
                );

            StringLiteral strLit = lineChartSeries.AppendChild<CategoryAxisData>(new CategoryAxisData()).AppendChild<StringLiteral>(new StringLiteral());
            strLit.Append(new PointCount() { Val = new UInt32Value(1U) });
            strLit.AppendChild<StringPoint>(new StringPoint() { Index = new UInt32Value(0U) }).Append(new NumericValue(title));

            NumberLiteral numLit = lineChartSeries.AppendChild<DocumentFormat.OpenXml.Drawing.Charts.Values>(
                new DocumentFormat.OpenXml.Drawing.Charts.Values()).AppendChild<NumberLiteral>(new NumberLiteral());
            numLit.Append(new FormatCode("General"));
            numLit.Append(new PointCount() { Val = new UInt32Value(1U) });
            numLit.AppendChild<NumericPoint>(new NumericPoint() { Index = new UInt32Value(0U) }).Append(new NumericValue((data[key]).ToString()));

            lineChartSeries.ChartShapeProperties = new ChartShapeProperties();
            var outline1 = lineChartSeries.ChartShapeProperties.AppendChild<DocumentFormat.OpenXml.Drawing.Outline>(new DocumentFormat.OpenXml.Drawing.Outline());
            var solid = outline1.AppendChild<DocumentFormat.OpenXml.Drawing.SolidFill>(new DocumentFormat.OpenXml.Drawing.SolidFill());
            solid.AppendChild<DocumentFormat.OpenXml.Drawing.RgbColorModelHex>(new DocumentFormat.OpenXml.Drawing.RgbColorModelHex() { Val = new HexBinaryValue() { Value = "ff0000" } });

            LineSerExtensionList lineSerExtensionList1 = new LineSerExtensionList();

            LineSerExtension lineSerExtension1 = new LineSerExtension() { Uri = "{C3380CC4-5D6E-409C-BE32-E72D297353CC}" };
            lineSerExtension1.AddNamespaceDeclaration("c16", "http://schemas.microsoft.com/office/drawing/2014/chart");

            OpenXmlUnknownElement openXmlUnknownElement1 = OpenXmlUnknownElement.CreateOpenXmlUnknownElement("<c16:uniqueId val=\"{00000000-502B-4C73-944E-734E08E94F96}\" xmlns:c16=\"http://schemas.microsoft.com/office/drawing/2014/chart\" />");

            lineSerExtension1.Append(openXmlUnknownElement1);

            lineSerExtensionList1.Append(lineSerExtension1);
            lineChartSeries.Append(lineSerExtensionList1);
            i++;
        }

        lineChart.Append(new AxisId() { Val = new UInt32Value(48650112U) });
        lineChart.Append(new AxisId() { Val = new UInt32Value(48672768U) });



        // Add the Category Axis.
        CategoryAxis catAx = plotArea.AppendChild<CategoryAxis>(
            new CategoryAxis(new AxisId() { Val = new UInt32Value(48650112u) },

            new Scaling(new Orientation()
            {
                Val = new EnumValue<DocumentFormat.
                OpenXml.Drawing.Charts.OrientationValues>(DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax)
            }),
            //START NEW CODE
            new Delete() { Val = false },
           //Rotate label text of horizonal axis.
           //new TextProperties(new BodyProperties { Rotation = -5400000, Vertical = TextVerticalValues.Horizontal },
           //    new ListStyle(),
           //    new Paragraph(new ParagraphProperties(new DefaultRunProperties(), new EndParagraphRunProperties { Language = "en-US" }))
           //    ),
           //END NEW CODE

           new AxisPosition() { Val = new EnumValue<AxisPositionValues>(AxisPositionValues.Bottom) },

           //START NEW CODE
           new NumberingFormat() { FormatCode = "General", SourceLinked = false },
           new MajorTickMark() { Val = TickMarkValues.Outside },
           new MinorTickMark() { Val = TickMarkValues.Cross },
           //END NEW CODE

           new TickLabelPosition() { Val = new EnumValue<TickLabelPositionValues>(TickLabelPositionValues.NextTo) },
           new CrossingAxis() { Val = new UInt32Value(48672768U) },
           new Crosses() { Val = new EnumValue<CrossesValues>(CrossesValues.AutoZero) },
           new AutoLabeled() { Val = new BooleanValue(true) },
           new LabelAlignment() { Val = new EnumValue<LabelAlignmentValues>(LabelAlignmentValues.Center) },
           new LabelOffset() { Val = new UInt16Value((ushort)100) },
            //START NEW CODE
            new NoMultiLevelLabels() { Val = true }
            //END NEW CODE
           ));

        // Add the Value Axis.
        ValueAxis valAx = plotArea.AppendChild<ValueAxis>(new ValueAxis(new AxisId() { Val = new UInt32Value(48672768U) },
        new Scaling(new Orientation()
        {
            Val = new EnumValue<DocumentFormat.OpenXml.Drawing.Charts.OrientationValues>(
            DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax)
        }),

        //START NEW CODE
        new Delete() { Val = false },
        //END NEW CODE

        new AxisPosition() { Val = new EnumValue<AxisPositionValues>(AxisPositionValues.Left) },
        new MajorGridlines(),
        new DocumentFormat.OpenXml.Drawing.Charts.NumberingFormat()
        {
            FormatCode = new StringValue("General"),
            SourceLinked = new BooleanValue(true)
        },
            //START NEW CODE
            new MajorTickMark() { Val = TickMarkValues.Outside },
            new MinorTickMark() { Val = TickMarkValues.Cross },
        //END NEW CODE

        new TickLabelPosition()
        {
            Val = new EnumValue<TickLabelPositionValues>
            (TickLabelPositionValues.NextTo)
        }, new CrossingAxis() { Val = new UInt32Value(48650112U) },
        new Crosses() { Val = new EnumValue<CrossesValues>(CrossesValues.AutoZero) },
        new CrossBetween() { Val = new EnumValue<CrossBetweenValues>(CrossBetweenValues.Between) }
        ));

        // create child elements of the c:legend element
        Legend legend = new Legend();
        legend.Append(new LegendPosition() { Val = LegendPositionValues.Right });
        Overlay overlay = new Overlay() { Val = false };
        legend.Append(overlay);

        //TextProperties textPros = new TextProperties();
        //textPros.Append(new BodyProperties());
        //textPros.Append(new ListStyle());

        wp.Paragraph paragraph = new wp.Paragraph();
        ParagraphProperties paraPros = new ParagraphProperties();
        DefaultParagraphProperties defaultParaPros = new DefaultParagraphProperties();
        defaultParaPros.Append(new LatinFont() { Typeface = "Arial", PitchFamily = 34, CharacterSet = 0 });
        defaultParaPros.Append(new ComplexScriptFont() { Typeface = "Arial", PitchFamily = 34, CharacterSet = 0 });
        paraPros.Append(defaultParaPros);
        paragraph.Append(paraPros);
        paragraph.Append(new EndParagraphRunProperties() { Language = "en-Us" });

        //textPros.Append(paragraph);
        //legend.Append(textPros);

        // Append c:view3D, c:plotArea and c:legend elements to the end of c:chart element
        chart.Append(view3D);
        chart.Append(plotArea);
        chart.Append(legend);

        // Append the c:chart element to the end of c:chartSpace element
        chartSpace.Append(chart);

        // Create c:spPr Elements and fill the child elements of it
        chartShapePros = new ChartShapeProperties();
        Outline outline = new Outline();
        outline.Append(new NoFill());
        chartShapePros.Append(outline);

        // Append c:spPr element to the end of c:chartSpace element
        chartSpace.Append(chartShapePros);

        chartPart.ChartSpace = chartSpace;

        // Generate content of the MainDocumentPart
        GeneratePartContent(mainPart);

    }

publi c stati c void GeneratePartContent (mainDocumentPart mainPart) {wp.Paragraph абзац = новый wp.Paragraph ();

        // Create a new run that has an inline drawing object
        wp.Run run = new wp.Run();
        wp.Drawing drawing = new wp.Drawing();

        Inline inline = new Inline();
        inline.Append(new Extent() { Cx = 5274310L, Cy = 3076575L });
        DocProperties docPros = new DocProperties() { Id = (UInt32Value)1U, Name = "Patient-Count-Chart" };
        inline.Append(docPros);

        Graphic g = new Graphic();
        GraphicData graphicData = new GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/chart" };
        ChartReference chartReference = new ChartReference() { Id = "rId110" };
        graphicData.Append(chartReference);
        g.Append(graphicData);
        inline.Append(g);
        drawing.Append(inline);
        run.Append(drawing);
        paragraph.Append(run);

        mainPart.Document.Body.Append(paragraph);
    }
...