Quantcast
Channel: Open XML Format SDK forum
Viewing all articles
Browse latest Browse all 1288

How to create a combination pivot chart using open XML with C#

$
0
0

Step1:
We have created a bar chart using below code, we were able to get a pivot chart with stocked column.
But in one of the series , we need to add a line chart to this chart.
How can we achieve this?
Please let us know.
Thanks in advance.

private void GenerateChartPartContent(ChartPart chartPart,  string reportFileName, string sheetName, string tableName, string fileName)
        {
            //C.ChartSpace chartSpace1 = new C.ChartSpace();

            chartPart.ChartSpace = new C.ChartSpace();
            chartPart.ChartSpace.Append(new C.EditingLanguage() { Val = new StringValue("en-US") });
            DocumentFormat.OpenXml.Drawing.Charts.Chart chart = chartPart.ChartSpace.AppendChild<DocumentFormat.OpenXml.Drawing.Charts.Chart>(
                new DocumentFormat.OpenXml.Drawing.Charts.Chart());


            C.Date1904 date19041 = new C.Date1904() { Val = false };
            C.EditingLanguage editingLanguage1 = new C.EditingLanguage() { Val = "en-US" };
            C.RoundedCorners roundedCorners1 = new C.RoundedCorners() { Val = false };

            AlternateContent alternateContent2 = new AlternateContent();

            AlternateContentChoice alternateContentChoice2 = new AlternateContentChoice() { Requires = "c14" };
            alternateContentChoice2.AddNamespaceDeclaration("c14", "");
            C14.Style style1 = new C14.Style() { Val = 102 };

            alternateContentChoice2.Append(style1);

            AlternateContentFallback alternateContentFallback1 = new AlternateContentFallback();
            C.Style style2 = new C.Style() { Val = 2 };

            alternateContentFallback1.Append(style2);

            alternateContent2.Append(alternateContentChoice2);
            alternateContent2.Append(alternateContentFallback1);

            C.PivotSource pivotSource1 = new C.PivotSource();
            C.PivotTableName pivotTableName1 = new C.PivotTableName();
            pivotTableName1.Text = "[" + fileName + "]" + sheetName + "!" + tableName;
            C.FormatId formatId1 = new C.FormatId() { Val = (UInt32Value)14U };

            pivotSource1.Append(pivotTableName1);
            pivotSource1.Append(formatId1);

            chartPart.ChartSpace.Append(pivotSource1);

            C.Chart chart1 = new C.Chart();

            C.PivotFormats pivotFormats1 = new C.PivotFormats();

            // Create a new clustered column chart.
            C.PlotArea plotArea = chart.AppendChild<C.PlotArea>(new C.PlotArea());
            C.Layout layout = plotArea.AppendChild<C.Layout>(new C.Layout());

            C.BarChart barChart = plotArea.AppendChild<C.BarChart>(new C.BarChart(new C.BarDirection() { Val = new EnumValue<C.BarDirectionValues>(C.BarDirectionValues.Column) },
                new C.BarGrouping() { Val = new EnumValue<C.BarGroupingValues>(C.BarGroupingValues.Stacked) }));
            barChart.Append(new C.AxisId() { Val = new UInt32Value(48650112u) });
            barChart.Append(new C.AxisId() { Val = new UInt32Value(48672768u) });
            C.Overlap overlap1 = new C.Overlap() { Val = 100 };
            C.GapWidth gapWidth1 = new C.GapWidth() { Val = (UInt16Value)219U };

            C.DataLabels dataLabels1 = new C.DataLabels();
            C.ShowLegendKey showLegendKey1 = new C.ShowLegendKey() { Val = true };
            C.ShowValue showValue1 = new C.ShowValue() { Val = false };
            C.ShowCategoryName showCategoryName1 = new C.ShowCategoryName() { Val = false };
            C.ShowSeriesName showSeriesName1 = new C.ShowSeriesName() { Val = false };
            C.ShowPercent showPercent1 = new C.ShowPercent() { Val = false };
            C.ShowBubbleSize showBubbleSize1 = new C.ShowBubbleSize() { Val = false };
            C.AutoTitleDeleted autoTitleDeleted1 = new C.AutoTitleDeleted() { Val = false };
            C.NoMultiLevelLabels noMultiLevelLabels1 = new C.NoMultiLevelLabels { Val = true };
            dataLabels1.Append(showLegendKey1);
            dataLabels1.Append(showValue1);
            dataLabels1.Append(showCategoryName1);
            dataLabels1.Append(showSeriesName1);
            dataLabels1.Append(showPercent1);
            dataLabels1.Append(showBubbleSize1);
            //overlap column
            barChart.Append(overlap1);
            //gridlines width
            barChart.Append(gapWidth1);
            uint i = 0;
            barChart.Append(dataLabels1);

            ////////////////////////

            // Page filters code
            C.ChartSpaceExtensionList chartSpaceExtensionList1 = new C.ChartSpaceExtensionList();

            C.ChartSpaceExtension chartSpaceExtension1 = new C.ChartSpaceExtension() { Uri = "{E28EC0CA-F0BB-4C9C-879D-F8772B89E7AC}" };

            C.ChartSpaceExtension chartSpaceExtension2 = new C.ChartSpaceExtension() { Uri = "{781A3756-C4B2-4CAC-9D66-4F8BD8637D16}" };

            C14.PivotOptions pivotOptions1 = new C14.PivotOptions();
            C14.DropZoneFilter dropZoneFilter1 = new C14.DropZoneFilter() { Val = true };
            C14.DropZoneCategories dropZoneCategories1 = new C14.DropZoneCategories() { Val = true };
            C14.DropZoneData dropZoneData1 = new C14.DropZoneData() { Val = true };
            C14.DropZoneSeries dropZoneSeries1 = new C14.DropZoneSeries() { Val = true };
            C14.DropZonesVisible dropZonesVisible1 = new C14.DropZonesVisible() { Val = true };

            pivotOptions1.Append(dropZoneFilter1);
            pivotOptions1.Append(dropZoneCategories1);
            pivotOptions1.Append(dropZoneData1);
            pivotOptions1.Append(dropZoneSeries1);
            pivotOptions1.Append(dropZonesVisible1);

            chartSpaceExtension2.Append(pivotOptions1);

            chartSpaceExtensionList1.Append(chartSpaceExtension1);
            chartSpaceExtensionList1.Append(chartSpaceExtension2);
            chartPart.ChartSpace.Append(chartSpaceExtensionList1);
            /////////////////////////////////////


            // Add the Category Axis.
            C.CategoryAxis catAx = plotArea.AppendChild<C.CategoryAxis>(new C.CategoryAxis(new C.AxisId() { Val = new UInt32Value(48650112u) }, new C.Scaling(new C.Orientation()
            {
                Val = new EnumValue<DocumentFormat.
                    OpenXml.Drawing.Charts.OrientationValues>(DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax)
            }),
                new C.AxisPosition() { Val = new EnumValue<C.AxisPositionValues>(C.AxisPositionValues.Bottom) },

                new C.TickLabelPosition() { Val = new EnumValue<C.TickLabelPositionValues>(C.TickLabelPositionValues.NextTo) }, //NextTo KB00--Line
                new C.CrossingAxis() { Val = new UInt32Value(48672768U) },
                new C.Crosses() { Val = new EnumValue<C.CrossesValues>(C.CrossesValues.AutoZero) },
                new C.AutoLabeled() { Val = new BooleanValue(true) },
                new C.Delete() { Val = new BooleanValue(false) },//Category axis values
                new C.LabelAlignment() { Val = new EnumValue<C.LabelAlignmentValues>(C.LabelAlignmentValues.Center) },
                new C.LabelOffset() { Val = new UInt16Value((ushort)100) }));

            // Add the Value Axis.
            C.ValueAxis valAx = plotArea.AppendChild<C.ValueAxis>(new C.ValueAxis(new C.AxisId() { Val = new UInt32Value(48672768u) },
                new C.Scaling(new C.Orientation()
                {
                    Val = new EnumValue<DocumentFormat.OpenXml.Drawing.Charts.OrientationValues>(
                        DocumentFormat.OpenXml.Drawing.Charts.OrientationValues.MinMax)
                }),
                new C.AxisPosition() { Val = new EnumValue<C.AxisPositionValues>(C.AxisPositionValues.Left) },
                new C.MajorGridlines(),
                new DocumentFormat.OpenXml.Drawing.Charts.NumberingFormat()
                {
                    FormatCode = new StringValue("General"),
                    SourceLinked = new BooleanValue(true)
                }, new C.TickLabelPosition()
                {
                    Val = new EnumValue<C.TickLabelPositionValues>
                        (C.TickLabelPositionValues.NextTo)
                }, new C.CrossingAxis() { Val = new UInt32Value(48650112U) },
                 new C.AutoLabeled() { Val = new BooleanValue(true) },
                new C.Crosses() { Val = new EnumValue<C.CrossesValues>(C.CrossesValues.AutoZero) },
                 new C.Delete() { Val = new BooleanValue(false) },//value axis values
                   new C.LabelAlignment() { Val = new EnumValue<C.LabelAlignmentValues>(C.LabelAlignmentValues.Center) },
                 new C.CrossBetween() { Val = new EnumValue<C.CrossBetweenValues>(C.CrossBetweenValues.Between) }));
            // Add the chart Legend.
            C.Legend legend = chart.AppendChild<C.Legend>(new C.Legend(new C.LegendPosition() { Val = new EnumValue<C.LegendPositionValues>(C.LegendPositionValues.Right) },//This is for Right Legends position //NextTo KB00--Line
                new C.Layout()));

            chart.Append(new C.PlotVisibleOnly() { Val = new BooleanValue(true) });
            chart.Append(autoTitleDeleted1);
            // Save the chart part.
            chartPart.ChartSpace.Save();

            }


Viewing all articles
Browse latest Browse all 1288

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>