We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Mail Merge - update data in chart

Hi there,

I'm trying to create a dynamic chart using mail merge.
Is it possible to use mail merge fields in the chart data source?
Please take a look at the attached screenshot.
I'd like to update four values for Q1,Q2,Q3 and Q4 with values from my data source.
The idea is to generate the same chart but with different values (always same number of rows, but different value for row).

Best regards,
Tomasz

Attachment: word_charts_85f0374.zip

12 Replies

PR Poorani Rajendran Syncfusion Team September 10, 2019 02:18 PM UTC

Hi Tomasz,

To meet your requirement, we suggest you insert values for the multiple charts directly from the data source in the Word document. We have prepared the sample application for the same and it can be downloaded from the below link:
https://www.syncfusion.com/downloads/support/forum/147394/ze/GenerateWord78826911

In this sample, we have done the following things:
1. Creates Word document
2. Append the chart using AppendChart API based on the values from the data source.
3. Saves and close the Word document.

If we misunderstood any of your requirement, kindly provide us your complete end requirement, thereby we will analyze the feasibility further on the mentioned requirement and update you with the appropriate solution at the earliest.

Please let us know if you have any other questions.

Regards,
Poorani Rajendran



TJ Tomasz Jagusz September 13, 2019 07:31 AM UTC

Hi Poorani,

thank You so much for the provided sample.
This is almost exactly what I need.

I have two additional questions:
1. Now You search WChart using "WChart chart = paragraph.ChildEntities[1] as WChart;". I want to have multiple charts in my template. Can I search for chart by it's ChartTitle (in template You provided it is titled "Sales").
So I'd like to have such function: WChart chart = document.SearchChartByTitle("Sales");
This is just a pseudo code, but I hope You will understand what I want to get.
2. How can I update chart series names that show below chart (1st Qrt, 2nd Qrt etc). chart.Categories property is read-only.


TJ Tomasz Jagusz September 13, 2019 07:46 AM UTC

Hi again,

I've addressed first one using below code:

    public static class WordDocumentExtensions
    {
        public static T SingleOrDefault<T>(this WordDocument wordDocument, Func<T, bool> func)
        {
            foreach (WSection section in wordDocument.Sections)
            {
                foreach (WParagraph paragraph in section.Paragraphs)
                {
                    var item = paragraph.ChildEntities.OfType<T>().SingleOrDefault(func);
                    if (item != null)
                    {
                        return item;
                    }
                }
            }

            return default(T);
        }

        public static WChart FindChartByTitle(this WordDocument wordDocument, string chartTitle)
        {
            if (wordDocument == null) throw new NullReferenceException($"{nameof(wordDocument)} can't be null");
            var chart = wordDocument.SingleOrDefault<WChart>(c => c.ChartTitle == chartTitle);
            return chart;
        }
    }

Now You can search chart using:

WChart chart = document.FindChartByTitle("Sales");

Is there a better/faster way of searching for element in document?



PR Poorani Rajendran Syncfusion Team September 14, 2019 11:55 AM UTC

Hi Tomasz,

Thank you for your update,

Regarding I'd like to have such function: WChart chart = document.SearchChartByTitle("Sales");
Currently DocIO does not provide support for finding the chart using its property(Title) in the Word document. We already logged a requirement to find an item in Word document using its property. We will also consider your suggestions to find chart in this requirement. We have logged this requirement as a feature request in our database. We don’t have any immediate plans to implement this feature. At the planning stage for every release cycle, we review all open features. We will let you know when this feature is implemented, and it can be tracked through our feedback portal:
https://www.syncfusion.com/feedback/3141/support-for-finding-an-item-in-word-document-using-its-property


As a workaround, we suggest you iterate through the document elements and finds the chart by its title. We have prepared a sample application for the same.


Regarding
update chart series names:
To update the chart series names, we suggest you use CategoryLabels API. We have prepared a sample application for the same.

Please find the sample link for both requirements and it can be downloaded from the below link:
https://www.syncfusion.com/downloads/support/forum/147394/ze/GenerateWord592661794

Please let us know if you have any other questions.

Regards,
Poorani Rajendran



TJ Tomasz Jagusz September 16, 2019 08:27 AM UTC

Hi Poorani,

thank You for providing the new sample :)
The only thing missing was updating the actual labels in data source:

            string[] dataValues = new string[] { "10", "20", "40", "80" };
            //set labels
            chart.ChartData.SetValue(2, 1, "A");
            chart.ChartData.SetValue(3, 1, "B");
            chart.ChartData.SetValue(4, 1, "C");
            chart.ChartData.SetValue(5, 1, "D");
            //Sets the values of the charts from data source
            chart.ChartData.SetValue(2, 2, dataValues[0]);
            chart.ChartData.SetValue(3, 2, dataValues[1]);
            chart.ChartData.SetValue(4, 2, dataValues[2]);
            chart.ChartData.SetValue(5, 2, dataValues[3]);

            chart.ChartTitle = "Works!";

everything works :) 


PR Poorani Rajendran Syncfusion Team September 16, 2019 09:31 AM UTC

Hi Tomasz,

Thank you for your update.

To set the data labels in the chart, we suggest you to enable the DataLabels.IsValue API. We have prepared a sample application for the same and it can be downloaded from the below link:
https://www.syncfusion.com/downloads/support/forum/147394/ze/GenerateWord-66960086

Please refer the below UG documentation link to know more about working with Charts  in Word document using DocIO:
https://help.syncfusion.com/file-formats/docio/working-with-charts

Please let us know if you have any other questions.

Regards,
Poorani Rajendran



TJ Tomasz Jagusz September 19, 2019 07:57 AM UTC

Hi again,

I think I found a bug.
When I open a word document, manipulate chart data and want to save it as PDF I get empty chart.
When I comment out data manipulation part the chart is correctly exported.
I've installed the necessary packages, below is the code I've added to Your sample:

wordDocument.ChartToImageConverter = new ChartToImageConverter();
DocToPDFConverter converter = new DocToPDFConverter();
PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument);
pdfDocument.Save("Sample1.pdf");
wordDocument.Close();

could You please check this on Your site?
This is very important to me.

Best regards,
Tomasz


VA Vijayasurya Anandhan Syncfusion Team September 19, 2019 10:20 AM UTC

Hi Tomasz,

Thank you for your update.

We have tried to reproduce the issue in our end, but it is working fine in our end.
Please find the sample used in our end to reproduce the issue from the below link: 
https://www.syncfusion.com/downloads/support/forum/147394/ze/Chart_Sample1515190792.zip

Action done in sample:

1. Opened the Word document with chart.

2. Performed Chart manipulations.

3. Converted the Word document to PDF.

We suspect that the problem might be due to the code snippet used at your end. So, could you please provide us the below details to reproduce the problem in our end.

1. Input Word document.
2. Modified sample with complete code snippet.
3. Generated PDF document in your end.

Thereby we will analyze further and provide you the appropriate solution at the earliest.

Note: If you have any confidential data in your Word document, please replace with some dummy data and provide us the same. We just need your document to recreate the problem you face.

Please let us know if you have any other questions.

Regards,
Vijayasurya A

 



TJ Tomasz Jagusz September 19, 2019 11:55 AM UTC

Hi Vijayasurya,

please take look at the attached sample.
There are two files: Sample1.docx, Sample1.pdf that are generated from template places in Data folder.
If You comment chart manipulation part everything works fine.

If You have any questions please let me know.

Bets regards,
Tomasz

Attachment: GenerateWordCharts_ToPdf_7432b862.zip


VA Vijayasurya Anandhan Syncfusion Team September 20, 2019 09:14 AM UTC

Hi Tomasz,

On further analyzing the provided sample, we can see that the numeric values used for chart manipulations are in "string" datatype and so the values are not updated properly, which leads to chart missing problem in the generated PDF document.

To overcome this problem, we suggest to use numeric datatype or object for numeric values update, which will perform the required modifications as per your requirement.

As per above suggestion, we have modified the provided sample to meet your requirement, which can be downloaded from the below link.
https://www.syncfusion.com/downloads/support/forum/147394/ze/Modified_sample-1532735998.zip

Please let us know if you have any questions.

Regards,
Vijayasurya A   



TJ Tomasz Jagusz September 20, 2019 11:07 AM UTC

Hi again,

now everything works perfectly.
Thank You :)


VA Vijayasurya Anandhan Syncfusion Team September 20, 2019 11:29 AM UTC

Hi Tomasz,

Thank you for your update.

We are glad to know that the reported problem is fixed.

Regards,
Vijayasurya A
 


Loader.
Live Chat Icon For mobile
Up arrow icon