Can you please help me. I want to add chart title using documentFormat.openxml.drawing.chart namespace.
I am not able to add chart title in chart area printing on word document.
Please help me in this regard.
Can you please help me. I want to add chart title using documentFormat.openxml.drawing.chart namespace.
I am not able to add chart title in chart area printing on word document.
Please help me in this regard.
Hi Arijit,
Thanks for your inquiry. You can insert chart with title using Aspose.Words into document. Please check the code example shared in following documentation link.
Hi,
I have checked this thing, but here it is not showing chart.title.text.
I am getting Chart.title.chartText and i am not able to add the string Title into Charttext.
Hi Arijit,
Thanks for your inquiry. We have not found any issue with Chart.Title.Text property. Could you please share some more detail about your query along with input, output and expected output documents? We will investigate the issue on our side and provide you more information.
Hi Tahir,
I am very glad for your reply. Please find the below code which I am using in my MVC Application for printing chart on word file dynamically.
------------------------------------- Code -------------------------------------------------------
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Drawing.Charts;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using DocumentFormat.OpenXml.Drawing;
using dc = DocumentFormat.OpenXml.Drawing.Charts;
using d = DocumentFormat.OpenXml.Drawing;
using C14 = DocumentFormat.OpenXml.Office2010.Drawing.Charts;
using dw = DocumentFormat.OpenXml.Drawing.Wordprocessing;
using wp = DocumentFormat.OpenXml.Wordprocessing;
string barChartAxisLabel = string.Empty;
string barstackedChartAxisLabel = string.Empty;
string barChartFormatCode = string.Empty;
string barstackedChartFormatCode = string.Empty;
// xAxisValue
UInt32 axisValue1 = 172466944U;
UInt32 axisValue2 = 172468480U;
//yAxisValue
UInt32 axisValue3 = 172653184U;
UInt32 axisValue4 = 172651264U;
List lstChartSubArea = objChartArea.ChartSubAreaList;
List chartList = null;
//declare and open a Word document object
objWordDocx = WordprocessingDocument.Open(sourceFile, true);
// Get MainDocumentPart of Document
MainDocumentPart mainPart = objWordDocx.MainDocumentPart;
// Create ChartPart object in Word Document
ChartPart chartPart = mainPart.AddNewPart(chartPartRId);
dc.ChartSpace chartSpace1 = new dc.ChartSpace();
chartSpace1.AddNamespaceDeclaration("c", "http://schemas.openxmlformats.org/drawingml/2006/chart");
chartSpace1.AddNamespaceDeclaration("a", "http://schemas.openxmlformats.org/drawingml/2006/main");
chartSpace1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
dc.Date1904 date19041 = new dc.Date1904() { Val = false };
dc.EditingLanguage editingLanguage1 = new dc.EditingLanguage() { Val = "en-US" };
dc.RoundedCorners roundedCorners1 = new dc.RoundedCorners() { Val = false };
AlternateContent alternateContent1 = new AlternateContent();
alternateContent1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
AlternateContentChoice alternateContentChoice1 = new AlternateContentChoice() { Requires = "c14" };
alternateContentChoice1.AddNamespaceDeclaration("c14", "http://schemas.microsoft.com/office/drawing/2007/8/2/chart");
C14.Style style1 = new C14.Style() { Val = 102 };
alternateContentChoice1.Append(style1);
AlternateContentFallback alternateContentFallback1 = new AlternateContentFallback();
dc.Style style2 = new dc.Style() { Val = 2 };
alternateContentFallback1.Append(style2);
alternateContent1.Append(alternateContentChoice1);
alternateContent1.Append(alternateContentFallback1);
dc.Chart chart1 = new dc.Chart();
dc.AutoTitleDeleted autoTitleDeleted1 = new dc.AutoTitleDeleted() { Val = true };
#region plotArea layout
dc.PlotArea plotArea1 = new dc.PlotArea();
dc.Layout layout1 = new dc.Layout();
dc.ManualLayout manualLayout1 = new dc.ManualLayout();
dc.LayoutTarget layoutTarget1 = new dc.LayoutTarget() { Val = dc.LayoutTargetValues.Inner };
dc.LeftMode leftMode1 = new dc.LeftMode() { Val = dc.LayoutModeValues.Edge };
dc.TopMode topMode1 = new dc.TopMode() { Val = dc.LayoutModeValues.Edge };
dc.Left left1 = new dc.Left() { Val = 0.30841840890578336D };
dc.Top top1 = new dc.Top() { Val = 0.5665266908490111E-2D };
dc.Width width1 = new dc.Width() { Val = 0.50075300932211062D };
dc.Height height1 = new dc.Height() { Val = 0.50075300932211062D };
if (objChartArea.ChartType.Contains("Bar"))
{
left1.Val = 0.26841840890578336D;
width1.Val = 0.60075300932211062D;
height1.Val = 0.50546639435052352D;
}
manualLayout1.Append(layoutTarget1);
manualLayout1.Append(leftMode1);
manualLayout1.Append(topMode1);
manualLayout1.Append(left1);
manualLayout1.Append(top1);
manualLayout1.Append(width1);
manualLayout1.Append(height1);
layout1.Append(manualLayout1);
dc.BarChart barChart1 = null;
if (objChartArea.ChartType.Contains("Bar") || objChartArea.ChartType.Contains("Column"))
{
barChart1 = new dc.BarChart();
}
string[] chartTypes = objChartArea.ChartType.Split(’+’);
foreach (string chartType in chartTypes)
{
switch (chartType)
{
case "Bar":
barDirection1 = new dc.BarDirection() { Val = dc.BarDirectionValues.Bar };
barGrouping1 = new dc.BarGrouping() { Val = dc.BarGroupingValues.Clustered };
break;
default:
break;
}
if (chartType.Contains("Bar") || chartType.Contains("Column"))
{
dc.VaryColors varyColors1 = new dc.VaryColors() { Val = false };
if (IsLabel)
{
varyColors1.Val = true;
}
barChart1.Append(barDirection1);
barChart1.Append(barGrouping1);
barChart1.Append(varyColors1);
}
}
#region ChartSeries
uint c = 0;
uint xAxisLabelCount = default(uint);
foreach (ChartSeries csa in lstChartSubArea)
{
chartList = csa.ChartDataList;
if (xAxisLabelCount < chartList.Count)
{
xAxisLabelCount = (uint)chartList.Count;
}
if (csa.SeriesType.Contains("Bar") || csa.SeriesType.Contains("Column"))
{
#region barChartSeries
dc.BarChartSeries barChartSeries = new dc.BarChartSeries();
dc.Index index = new dc.Index() { Val = new UInt32Value© };
dc.Order order = new dc.Order() { Val = new UInt32Value© };
c++;
dc.SeriesText seriesText = new dc.SeriesText();
dc.StringReference stringReference = new dc.StringReference();
dc.StringCache stringCache = new dc.StringCache();
dc.PointCount pointCount = new dc.PointCount() { Val = (UInt32Value)1U };
dc.StringPoint stringPoint = new dc.StringPoint() { Index = (UInt32Value)0U };
dc.NumericValue numericValue = new dc.NumericValue();
// numericValue.Text = "Services";
numericValue.Text = csa.SeriesTitle;
stringPoint.Append(numericValue);
stringCache.Append(pointCount);
stringCache.Append(stringPoint);
stringReference.Append(stringCache);
seriesText.Append(stringReference);
dc.ChartShapeProperties chartShapeProperties = new dc.ChartShapeProperties();
d.SolidFill solidFill = new d.SolidFill();
d.Outline outline = new d.Outline() { Width = 25400 };
d.NoFill noFill = new d.NoFill();
outline.Append(noFill);
chartShapeProperties.Append(solidFill);
chartShapeProperties.Append(outline);
dc.InvertIfNegative invertIfNegative = new dc.InvertIfNegative() { Val = false };
dc.DataLabels dataLabels = new dc.DataLabels();
dc.ShowLegendKey showLegendKey = new dc.ShowLegendKey() { Val = false };
dc.ShowValue showValue = new dc.ShowValue() { Val = false };
if (IsLabel)
{
showValue.Val = true;
}
dc.ShowCategoryName showCategoryName = new dc.ShowCategoryName() { Val = false };
dc.ShowSeriesName showSeriesName = new dc.ShowSeriesName() { Val = false };
dc.ShowPercent showPercent = new dc.ShowPercent() { Val = false };
dc.ShowBubbleSize showBubbleSize = new dc.ShowBubbleSize() { Val = false };
dc.ShowLeaderLines showLeaderLines = new dc.ShowLeaderLines() { Val = false };
dataLabels.Append(showLegendKey);
dataLabels.Append(showValue);
dataLabels.Append(showCategoryName);
dataLabels.Append(showSeriesName);
dataLabels.Append(showPercent);
dataLabels.Append(showBubbleSize);
dataLabels.Append(showLeaderLines);
dc.CategoryAxisData categoryAxisData = new dc.CategoryAxisData();
dc.StringReference strReference = new dc.StringReference();
dc.StringCache stringCacheXaxis = new dc.StringCache();
dc.PointCount pointCountXaxis = new dc.PointCount() { Val = (UInt32Value)xAxisLabelCount };
uint i = 0;
// Fill data for chart
foreach (var item in chartList)
{
dc.StringPoint numericPointXaxis = new dc.StringPoint() { Index = new UInt32Value(i) };
dc.NumericValue numericValueXaxis = new dc.NumericValue();
numericValueXaxis.Text = item.XaxisLabel;
numericPointXaxis.Append(numericValueXaxis);
stringCacheXaxis.Append(numericPointXaxis);
i++;
}
stringCacheXaxis.Append(pointCountXaxis);
strReference.Append(stringCacheXaxis);
categoryAxisData.Append(strReference);
dc.Values values = new dc.Values();
dc.NumberReference numberReference = new dc.NumberReference();
dc.NumberingCache numberingCacheServices = new dc.NumberingCache();
i = 0;
// Fill data for chart
foreach (var item in chartList)
{
dc.NumericPoint numericPointYaxis = new dc.NumericPoint() { Index = new UInt32Value(i) };
dc.NumericValue numericValueYaxis = new dc.NumericValue();
barChartFormatCode = item.DataType;
if (barChartFormatCode.Contains("percent"))
{
float yAxisValue = Convert.ToSingle(item.YaxisValue);
numericValueYaxis.Text = (yAxisValue / 100).ToString();
}
else
{
numericValueYaxis.Text = item.YaxisValue;
}
numericPointYaxis.Append(numericValueYaxis);
numberingCacheServices.Append(numericPointYaxis);
i++;
}
dc.FormatCode formatCode = new dc.FormatCode();
if (barChartFormatCode.Contains("number"))
{
// formatCode.Text = "#,#0.0"; //"#,##0.0";
formatCode.Text = "#,#0";
}
else if (barChartFormatCode.Contains("percent"))
{
formatCode.Text = "0.00%";
}
dc.PointCount pointCountYaxis = new dc.PointCount() { Val = (UInt32Value)xAxisLabelCount };
numberingCacheServices.Append(formatCode);
numberingCacheServices.Append(pointCountYaxis);
numberReference.Append(numberingCacheServices);
values.Append(numberReference);
barChartSeries.Append(index);
barChartSeries.Append(order);
barChartSeries.Append(seriesText);
barChartSeries.Append(chartShapeProperties);
barChartSeries.Append(invertIfNegative);
barChartSeries.Append(dataLabels);
barChartSeries.Append(categoryAxisData);
barChartSeries.Append(values);
barChart1.Append(barChartSeries);
#endregion
}
#region shapeProperties1
dc.ShapeProperties shapeProperties1 = new dc.ShapeProperties();
d.NoFill noFill5 = new d.NoFill();
d.Outline outline9 = new d.Outline() { Width = 26400 };
d.NoFill noFill6 = new d.NoFill();
outline9.Append(noFill6);
shapeProperties1.Append(noFill5);
shapeProperties1.Append(outline9);
#endregion
plotArea1.Append(layout1);
plotArea1.Append(shapeProperties1);
#region Legend
dc.Legend legend1 = new dc.Legend();
dc.LegendPosition legendPosition1 = new dc.LegendPosition();
if (objChartArea.ChartType.ToLower().Contains("bar"))
{
legendPosition1.Val = dc.LegendPositionValues.Bottom;
}
else
{
legendPosition1.Val = dc.LegendPositionValues.Bottom;
}
dc.Overlay overlay3 = new dc.Overlay() { Val = true };
dc.ChartShapeProperties chartShapeProperties9 = new dc.ChartShapeProperties();
d.NoFill noFill7 = new d.NoFill();
d.Outline outline10 = new d.Outline() { Width = 26400 };
d.NoFill noFill8 = new d.NoFill();
outline10.Append(noFill8);
chartShapeProperties9.Append(noFill7);
chartShapeProperties9.Append(outline10);
dc.TextProperties textProperties3 = new dc.TextProperties();
d.BodyProperties bodyProperties5 = new d.BodyProperties();
d.ListStyle listStyle5 = new d.ListStyle();
d.Paragraph paragraph5 = new d.Paragraph();
d.ParagraphProperties paragraphProperties5 = new d.ParagraphProperties();
d.DefaultRunProperties defaultRunProperties5 = new d.DefaultRunProperties() { FontSize = 900, Bold = false, Italic = false, Underline = d.TextUnderlineValues.None, Strike = d.TextStrikeValues.NoStrike, Baseline = 0 };
d.SolidFill solidFill12 = new d.SolidFill();
d.RgbColorModelHex rgbColorModelHex7 = new d.RgbColorModelHex() { Val = "000000" };
solidFill12.Append(rgbColorModelHex7);
d.LatinFont latinFont9 = new d.LatinFont() { Typeface = "Arial", Panose = "020B0604020202020204", PitchFamily = 34, CharacterSet = 0 };
d.EastAsianFont eastAsianFont3 = new d.EastAsianFont() { Typeface = "Verdana" };
d.ComplexScriptFont complexScriptFont9 = new d.ComplexScriptFont() { Typeface = "Arial", Panose = "020B0604020202020204", PitchFamily = 34, CharacterSet = 0 };
defaultRunProperties5.Append(solidFill12);
defaultRunProperties5.Append(latinFont9);
defaultRunProperties5.Append(eastAsianFont3);
defaultRunProperties5.Append(complexScriptFont9);
paragraphProperties5.Append(defaultRunProperties5);
d.EndParagraphRunProperties endParagraphRunProperties4 = new d.EndParagraphRunProperties() { Language = "en-US" };
paragraph5.Append(paragraphProperties5);
paragraph5.Append(endParagraphRunProperties4);
textProperties3.Append(bodyProperties5);
textProperties3.Append(listStyle5);
textProperties3.Append(paragraph5);
if (!IsLabel)
{
legend1.Append(legendPosition1);//Necessary
}
else
legend1.Append(legendPosition1);
legend1.Append(overlay3);//Necessary
legend1.Append(chartShapeProperties9);
legend1.Append(textProperties3);
#endregion
dc.PlotVisibleOnly plotVisibleOnly1 = new dc.PlotVisibleOnly() { Val = true };
dc.DisplayBlanksAs displayBlanksAs1 = new dc.DisplayBlanksAs() { Val = dc.DisplayBlanksAsValues.Gap };
dc.ShowDataLabelsOverMaximum showDataLabelsOverMaximum1 = new dc.ShowDataLabelsOverMaximum() { Val = false };
chart1.Append(autoTitleDeleted1);
chart1.Append(plotArea1);
chart1.Append(plotVisibleOnly1);
chart1.Append(displayBlanksAs1);
chart1.Append(showDataLabelsOverMaximum1);
if (!IsLabel)
{
chart1.Append(legend1);
}
else
{
chart1.Append(legend1);
}
dc.ChartShapeProperties chartShapeProperties10 = new dc.ChartShapeProperties();
d.NoFill noFill9 = new d.NoFill();
d.Outline outline11 = new d.Outline() { Width = 6350 };
d.NoFill noFill10 = new d.NoFill();
outline11.Append(noFill10);
chartShapeProperties10.Append(noFill9);
chartShapeProperties10.Append(outline11);
dc.TextProperties textProperties4 = new dc.TextProperties();
d.BodyProperties bodyProperties6 = new d.BodyProperties();
d.ListStyle listStyle6 = new d.ListStyle();
d.Paragraph paragraph6 = new d.Paragraph();
d.ParagraphProperties paragraphProperties6 = new d.ParagraphProperties();
d.DefaultRunProperties defaultRunProperties6 = new d.DefaultRunProperties() { FontSize = 925, Bold = false, Italic = false, Underline = d.TextUnderlineValues.None, Strike = d.TextStrikeValues.NoStrike, Baseline = 0 };
d.SolidFill solidFill13 = new d.SolidFill();
d.RgbColorModelHex rgbColorModelHex8 = new d.RgbColorModelHex() { Val = "000000" };
solidFill13.Append(rgbColorModelHex8);
d.LatinFont latinFont10 = new d.LatinFont() { Typeface = "Verdana" };
d.EastAsianFont eastAsianFont4 = new d.EastAsianFont() { Typeface = "Verdana" };
d.ComplexScriptFont complexScriptFont10 = new d.ComplexScriptFont() { Typeface = "Verdana" };
defaultRunProperties6.Append(solidFill13);
defaultRunProperties6.Append(latinFont10);
defaultRunProperties6.Append(eastAsianFont4);
defaultRunProperties6.Append(complexScriptFont10);
paragraphProperties6.Append(defaultRunProperties6);
d.EndParagraphRunProperties endParagraphRunProperties5 = new d.EndParagraphRunProperties() { Language = "en-US" };
paragraph6.Append(paragraphProperties6);
paragraph6.Append(endParagraphRunProperties5);
textProperties4.Append(bodyProperties6);
textProperties4.Append(listStyle6);
textProperties4.Append(paragraph6);
chartSpace1.Append(date19041);
chartSpace1.Append(editingLanguage1);
chartSpace1.Append(roundedCorners1);
chartSpace1.Append(alternateContent1);
chartSpace1.Append(chart1);
chartSpace1.Append(chartShapeProperties10);
chartSpace1.Append(textProperties4);
chartPart.ChartSpace = chartSpace1;
CreateExcelFile(chartPart, lstChartSubArea, excelSourceId, filepath);
//Generate content of the MainDocumentPart
GeneratePartContent(mainPart, chartPartRId, IsLabel);
//save this part
objWordDocx.MainDocumentPart.Document.Save();
//save and close the document
objWordDocx.Close();
Hi Arijit,
Thanks for sharing the code. I am afraid your query is not related to Aspose.Words. If you want to create charts using Aspose.Words, please refer to the following article. Hope this helps you.
Dear Tahir,
Actually I cannot change the existing code as it is being used and I have to add the title using this code. Please see if you have any solution for this.