Using LINQ create chart in word

Hello,
I am try to using LINQ engine to update the pre-created word file with chart. The chart is simply contain data pair like <Timestamp, value>. the Timestamp will be x Axis and value will be y. My question is in the sample, I saw example like:<<foreach [in contracts.GroupBy(c => c.Date.Month)]>>, however, I don’t want my data grouped together, I removed the groupBy then got error. How can I make it work with out group by?

My second question is is it possible to put two series in one chart? If so how should I write the expression (using LINQ engine method) ?

Third question is is it possible to add an secondary y Axis (using LINQ engine method) ?

Thank you!

Hi Gracie,

Thanks for your inquiry.

miamia516:
I don’t want my data grouped together, I removed the groupBy then got error. How can I make it work with out group by?

Please refer to the following articles:
Pie Chart Template
Using Charts to Represent Sequential Data

If you still face problem, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

miamia516:
My second question is is it possible to put two series in one chart? If so how should I write the expression (using LINQ engine method) ?

Please use ReportingEngine.BuildReport method (Document, Object[], String[]) to populate the specified template document with data from the specified sources making it a ready report. The second parameter of this method is an array of data source objects and the third parameter is an array of names to reference the data source objects within the template.

miamia516:
Third question is is it possible to add an secondary y Axis (using LINQ engine method) ?

Yes, you can add secondary Y-Axis in chart template and populate it using LINQ Reporting Engine.

Thank you for the reply. I’m glad to hear all the feture are supported by Aspose.word. For the second and third question, multiple series, and secondary y Axis, will you send me some sample code or related documentations?

Gracie

Hi Gracie,

Thanks for your inquiry. We are creating the code example for this scenario and will get back to you soon.

Hi Gracie,

Thanks for your patience. Please check the attached application to work with multiple chart series and secondary Y-Axis. We have also attached the input and output documents with this post. Hope this helps you.

Hi tahir,

Thank you so much for the code sample, I’m glad how easy it can do the multiple series and secondary Y-Axis. Our company is seriously considering to use Aspose for our reports. They want me to use Aspose trial to create one of our report for evaluating.

When I look into the sample data your provide in the code, it was first grouped by categories, then series data, like
category1 ->( Series1 data 1, Series 2 data1)
category2 ->(Series 1 data 2, Series 2 data2)
but our data is sequence data and not grouped by category1. The structure looks like this:
Series1(timestamp1, value1), (timestamp2, value2),(timestamp3, value3)…
Series2(timestamp4, value4), (timestamp5, value5),(timestamp6, value6)…

Each series may not have exactly same data points, and their timestamps are also random depends on when is the data come in.
For example:
Series1: ('2017-1-1 03:00:00 ', ‘5’), ('2017-1-1 05:00:00 ', ‘8’),('2017-1-1 09:00:00 ', ‘2’)…
Series2: ('2017-1-1 02:00:00 ', ‘1’), ('2017-1-1 06:00:00 ', ‘4’),('2017-1-1 10:00:00 ', ‘9’)…

Will you kindly provide an code example on how to work with this kid data structure to create an multi Y-Axis Chart?

Thank you!

Gracie

Hi Gracie,

Thanks for your inquiry. Please manually create your expected output Word document using Microsoft Word and attach it here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

Hello Tahir,
I attached one of the chart we want to created in png format, will it work for you?

Hi Gracie,

Thanks for sharing the image. It would be great if you please share expected output Word document here for our reference. We will then provide you more information on this along with code.

Hi Tahir,

Attached is the chart I created in word file. Let me know if that is what you are looking for.
Thank you!

Gracie

Hi Gracie,

Thanks for sharing the detail. Please use the same approach shared in my above post to get the desired output. You need to modify your template document and code according to your requirement.

Please check the following modified code example. We have attached the modified template with this post for your kind reference.

We suggest you please read following article:
Using Charts to Represent Sequential Data

List<TableObjectCollection> values = new List<TableObjectCollection>();
TableObject currentGroup = null;
TableObjectCollection currentCollection = null;
values.Add(new TableObjectCollection());
currentCollection = values[values.Count - 1];
currentGroup = new TableObject("12:00:00 AM"); 
currentCollection.Add(currentGroup);
currentGroup.Add("Flow", 10);
currentGroup.Add("Rainfall", 2);
currentGroup = new TableObject("01:00:00 AM");
currentCollection.Add(currentGroup);
currentGroup.Add("Flow", 15);
currentGroup.Add("Rainfall", 4);
currentGroup = new TableObject("02:00:00 AM");
currentCollection.Add(currentGroup);
currentGroup.Add("Flow", 23);
currentGroup.Add("Rainfall", 7);
ReportingEngine re = new ReportingEngine();
Document doc = new Document(MyDir + "ChartTemplate.docx");
re.BuildReport(doc, values.ToArray(), new String[] { "myData" }); // build the report
doc.Save(MyDir + "Out.docx");

Hi Tahir,
The chart works for me! thank you so much! However, I tried to setup the chart template myself in world, I am not be able to make the template work with the code. For example, I saw there is a pre-created series call #REF, I don’t know how to add it and without it, the code will throw an error. Will you be able to show me step by step how did you config the chart template in the word file?

Another problem, I need the chart y axis title read from the code, I am not be able to make it work. I had attached the sample project

Thank you!

Gracie

Hi Gracie,

Thanks for your inquiry. The #REF chart series is incorrect. We have removed it and attached the modified template document with this post. You need to create the chart template as shown in attached document. Please check the chart templates from here:
Typical Templates

miamia516:
Another problem, I need the chart y axis title read from the code, I am not be able to make it work. I had attached the sample project

Unfortunately, Aspose.Words does not support the requested feature at the moment. Your request has been linked to the appropriate issue (WORDSNET-13933) and you will be notified as soon as it is available. Sorry for the inconvenience.

It seems that you want to update the chart’s vertical axis title using LINQ Reporting. Please confirm. We will then log this feature according to your requirement.

*tahir.manzoor:

It seems that you want to update the chart’s vertical axis title using LINQ Reporting. Please confirm. We will then log this feature according to your requirement.*

Yes, so currently there is no way to update chart’s vertical axis title? That will be a problem for me to convince my team. Some of our chart’s titile, vertical and horizontal axis title will all be dynamically read from database. What if I just add an textbox on the position of the title? will that work?

Thank you!

Gracie

I had tried the textbox but when the textbox positioned on top of a chart, it will not replace the expression with the real value.
Will your team help on this?

Thank you!

Hi Gracie,

Thanks for sharing the detail. We have logged this feature request as WORDSNET-15164 in our issue tracking system. You will be notified via this forum thread as soon as it is available. Sorry for the inconvenience.

miamia516:
I had tried the textbox but when the textbox positioned on top of a chart, it will not replace the expression with the real value.

Could you please share your template document and code example here for testing? We will investigate the issue and provide you more information on this.

Hi Gracie,

Further to my last post, you may use chart legend instead of axis title in your template document. You can update chart’s legend using LINQ Reporting. Please check the attached template and following code example. Hope this helps you.

List data = new List()
{
new PointData { Time = "12:00:00 AM", Flow = 10, Rainfall = 2 },
new PointData { Time = "01:00:00 AM", Flow = 15, Rainfall = 4 },
new PointData { Time = "02:00:00 AM", Flow = 23, Rainfall = 7 }
};
List<string> seriesNames = new List<string>
{
    "Flow",
    "Rainfall"
};
Document doc = new Document(MyDir + "ChartTemplate.docx");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, new object[] { data, seriesNames }, new string[] { "data", "seriesNames" });
doc.Save(MyDir + "Out.docx");
public class PointData
{
    public string Time { get; set; }
    public int Flow { get; set; }
    public int Rainfall { get; set; }
}

Thank you! I’ll see if I can convince my team to use legend to show the value. Thanks again!

@Mission,
The issues you have found earlier (filed as WORDSNET-15164) have been fixed in this Aspose.Words for .NET 17.12 update and this Aspose.Words for Java 17.12 update.
Please also check the following articles:

The issues you have found earlier (filed as WORDSNET-13933) have been fixed in this Aspose.Words for .NET 23.9 update also available on NuGet.