How to remove secondary axis and update legends of chart by LINQ Reporting using .NET

Hi,

I’m using Aspose Words for .Net V20.5.0 and also chart templates as explained in here:
https://docs.aspose.com/display/wordsnet/Working+with+Charts

In my case I have a chart with a secondary Y axis. Is there a way to access the legends of that secondary axis? I would like to update the legend text and hide other ones.
I have been able to access only the primary Y axis legends with this code:

Chart chart = shape.Chart;
ChartSeriesCollection seriesColl = chart.Series; // Items in seriesColl will be primary axis only

Regards

@CarlosB

You can get the chart legend using Chart.Legend property. Could you please ZIP and attach your input Word document and expected output? We will then provide you more information about your query.

Hi,

Actually the Chart.Legend property doesn’t work in this case since it just allows to set the Overlay and Legends box position:

https://reference.aspose.com/words/net/aspose.words.drawing.charts/chart/properties/legend

I need to edit the text of the elements inside the legends box and remove Secondary Chart series as well.
I have attached a zip with the files. There are 3 files:

  • The template for the chart.
  • The current output.
  • The expected output.

You will notice that in the template there are 10 chart series: 5 primary (1 to 5) and 5 secondary (6 to 10).

It is expected that data will be related between the 2 axis. So, in the example of the expected output you will find data for 3 people, where:
Person 1 => Serie1 and Serie6
Person 2 => Serie2 and Serie7
Person 3 => Serie3 and Serie8

It is expected that series 4, 5, 9 and 10 should be deleted since were not used. Also it is expected that in the legend box each person appear only 1 time. Since I’m updating the text for the legends corresponding to the Primary Series, the legend corresponding to the secondary series should be hidden.

So as I mentioned in my first post, I don’t have issues implementing this behavior for the primary series legends, however I don’t have a clue of how to do it with the secondary series legends.

Thanks for your help.

FileSamples.zip (59.4 KB)

@CarlosB

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

We apologize for your inconvenience.

@CarlosB

You can achieve your requirements using the approach shared below. We will close WORDSNET-20643 after your confirmation.

Please note that legend entries contain series names. You can edit a legend entry dynamically. A corresponding series name should be set dynamically. Please read the following article.
Setting Chart Title, Series Names, and Axis Titles Dynamically

Please do not use static series name in the template document. You can use an expression that will be evaluated dynamically instead of a static series name.

For series to be removed from the chart based upon conditions dynamically, define the conditions in names of these series using removeif tags having the following syntax.

<<removeif [conditional_expression]>>

Please read the following article for more detail.
Including Chart Series Dynamically

If this does not work for you, 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 along with your data source object. We will investigate the issue and implement your requirement accordingly.

Hi @tahir.manzoor,

The information supplied was very helpful. Some stuff such as axis titles, series names and other things can be done using the LINQ queries. However it didn’t work for all cases. I have updated the template using LINQ queries and also I have attached a working sample project.

I have a couple of questions here:

  1. How can I manage correctly the indexes in the template in order to avoid the “index out of range” exception? For example in the sample project it is passing the serie names in a list of strings named serieNames and in the template it is accessing them by index. However template is expecting 5 serie names tops but in code it only sets 3 series. So serieNames[3] and serieNames[4] will throw index out of range exception.
  2. Also from the sample project, the class named WordChartDataSource has a property named SerieName. Code is passing a list of this elements to the template. Could you please supply an example of how can I set correctly the serie names using that property?
  3. Please run the supplied code and generate the sample report. I was able to set up the removeif statement to remove unused series from the chart including the ones for the secondary axis. However I still need to hide from the legend box the legends corresponding to the secondary axis but keeping the data in the chart. As you will see in the sample report, legends in the legends box appear twice and they should appear only once. Could you help with this too?

Thanks for your help.

SampleProject.zip (219.4 KB)

@CarlosB

We have tested the scenario and have not faced the shared exception.

We are working over these queries and will get back to you soon.

We have logged this issue as WORDSNET-20689 in our issue tracking system. You will be notified via this forum thread once this issue is resolved.

We have not found this issue while using the latest version of Aspose.Words 20.6. Could you please share some more detail about this issue?

Hi @tahir.manzoor,
I have attached again the sample project, this time if you run it you will get the index out of range exception. The source code is exactly the same as the previous time. The change is in the template, I just added serieName[3] and serieName[4] to the series 7, 8, 9 and 10. Code is just setting values for serieName[0], serieName[1] and serieName[2].

Thanks

SampleProject.zip (219.3 KB)

@CarlosB

We have logged this issue as WORDSNET-20692 in our issue tracking system. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@CarlosB

Regarding WORDSNET-20643, we have closed this issue as ‘Not a bug’.

Starting from Aspose.Words 20.7, you can remove chart legend entries corresponding to duplicates from a template. Then the duplicates do not appear in a result document. To remove a chart legend entry in a template using MS Word, it is required to select the entry and press Delete.

We have closed the issue WORDSNET-20689 as ‘Not a bug’.

This can be done using the same syntax as would be used in C#. That is, expression serieNames[0] can be replaced with expression dataSource[0].SerieName. Please check the attached template document. WORDSNET-20689.zip (22.5 KB)

Hi @tahir.manzoor,

I will test it in Aspose Words 20.7 when it become available and let you know.

Thanks

@CarlosB

It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue WORDSNET-20692 as ‘Not a Bug’.

In order to avoid the exception, you need to use the same approach as would be used in C#. For example, expression <<[serieNames[3]]>> can be replaced with expression <<[3 < serieNames.Count ? serieNames[3] : ""]>>. Please check the attached modified template document. WORDSNET-20692.zip (22.4 KB)

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