Chart in Aspose Word Doc

We need to isert a chart into an Aspose Word generated document. What would be the best way to insert a chart using Aspose Word?

Hi Jim,

Thanks for your query. Unfortunately, The requested feature is not supported yet in Aspose.Words. We had already logged this feature request as WORDSNET-4617 in our issue tracking system. I have linked this forum thread to the same feature and you will be notified via this forum thread once this feature is available. We apologize for your inconvenience.

However, you can use Aspose.Cells to create charts and convert the charts to image. Once you have image of chart, you can insert that image to Word document by using Aspose.Words as shown in following code snippet.

//Instantiating a Workbook object
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
//Adding a new worksheet to the Excel object
int sheetIndex = workbook.Worksheets.Add();
//Obtaining the reference of the newly added worksheet by
//passing its sheet index
Aspose.Cells.Worksheet worksheet = workbook.Worksheets[sheetIndex];
//Adding a sample value to "A1" cell
worksheet.Cells["A1"].PutValue(50);
//Adding a sample value to "A2" cell
worksheet.Cells["A2"].PutValue(100);
//Adding a sample value to "A3" cell
worksheet.Cells["A3"].PutValue(150);
//Adding a sample value to "B1" cell
worksheet.Cells["B1"].PutValue(4);
//Adding a sample value to "B2" cell
worksheet.Cells["B2"].PutValue(20);
//Adding a sample value to "B3" cell
worksheet.Cells["B3"].PutValue(50);
//Adding a chart to the worksheet
int chartIndex = worksheet.Charts.Add(Aspose.Cells.Charts.ChartType.Pie3D, 5, 0, 15, 5);
//Accessing the instance of the newly added chart
Aspose.Cells.Charts.Chart chart = worksheet.Charts[chartIndex];
//Adding Series Collection (chart data source) to the chart ranging from "A1" cell to "B3"
chart.NSeries.Add("A1:B3", true);
//Converting chart to image.
chart.ToImage(MyDir + "Chart.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertImage(MyDir + "Chart.Jpeg");
doc.Save(MyDir + "AsposeOut.docx");

The issues you have found earlier (filed as WORDSNET-4617) have been fixed in this .NET update and this Java update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(15)

Is there an example of how to add a chart to a Word doc using the public chart API in WORDNET-4617?

Hi Jim,

Thanks
for your inquiry. Please check following code example for your kind reference. Hope this helps you.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Add chart with default data. You can specify different chart types and sizes.
Shape shape = builder.InsertChart(ChartType.Column, 432, 252);
// Chart property of Shape contains all chart related options.
Chart chart = shape.Chart;
// Get chart series collection.
ChartSeriesCollection seriesColl = chart.Series;
// Delete default generated series.
seriesColl.Clear();
// Create category names array, in this example we have two categories.
string[] categories = new string[] { "AW Category 1", "AW Category 2" };
// Adding new series. Please note, data arrays must not be empty and arrays must be the same size.
seriesColl.Add("AW Series 1", categories, new double[] { 1, 2 });
seriesColl.Add("AW Series 2", categories, new double[] { 3, 4 });
seriesColl.Add("AW Series 3", categories, new double[] { 5, 6 });
seriesColl.Add("AW Series 4", categories, new double[] { 7, 8 });
seriesColl.Add("AW Series 5", categories, new double[] { 9, 10 });
doc.Save(MyDir + "InsertChartColumn.docx");

Hi Jim,

Further to my last post, I suggest you please read about ‘working with charts’ from here:
https://docs.aspose.com/words/net/working-with-charts/

hi, Tahir

if i want to copy the source chart from excel to word, not just a image, how can i do it?

this chart can not even change title prop? can not change each ChartSeries’s color?

Hi Pyntia,
Thanks for your inquiry.
sendreams:

if i want to copy the source chart from excel to word, not just a image, how can i do it?

Please check my colleague’s reply from here:
https://forum.aspose.com/t/42708
sendreams:

this chart can not even change title prop?

Please use Chart.Title.Text to to get or set the text of the chart title. If null or empty value is specified, auto generated title will be shown. Please check following highlighted code snippet.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Add chart with default data. You can specify different chart types and sizes.
Shape shape = builder.InsertChart(ChartType.Column, 432, 252);
// Chart property of Shape contains all chart related options.
Chart chart = shape.Chart;
chart.Title.Text = "AW Chart Title";

sendreams:

can not change each ChartSeries’s color?

Unfortunately, Aspose.Words does not support the requested feature at the moment. However, I have logged this feature request as WORDSNET-12275 in our issue tracking system. You will be notified via this forum thread once this feature is available.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-12275) have been fixed in this Aspose.Words for .NET 21.6 update and this Aspose.Words for Java 21.6 update.