Chart.ToImage - the XAxis labels shown in Legend

Hello,
We are trying to save chart to image, but the legend isn’t shown as expected.

You can see the chart legend is “Test” in my uploaded excel file. I write the code to save it to image:
using Aspose.Cells;

var currentDirectory = Environment.CurrentDirectory;
var excel = new Workbook(currentDirectory + “\ChartStackedBar.xlsx”);
if (excel.Worksheets.Count > 0 && excel.Worksheets[0].Charts.Count > 0)
{
var chart = excel.Worksheets[0].Charts[0];
chart.ToImage(currentDirectory + “\chart.png”);
}

Unfortunately, the XAxis labels shown in legend in chart.png.
It can be reproduced when run my uploaded code.

Do you have any idea how to save image correctly?

ChartStackedBarToImage.zip (20.1 KB)

@amegroup

Cause

The issue you’re experiencing with the chart legend showing the X-axis labels instead of the expected legend title (“Test”) is likely due to the way the chart is being rendered when converting it to an image. This can occur if the chart’s series or legend settings are not properly configured.

Solution

To resolve this, you can use the ImageOrPrintOptions class to specify additional rendering options when converting the chart to an image. This allows you to control various aspects of the rendering process, including the legend display.

Code

Here’s an updated version of your code that includes the use of ImageOrPrintOptions:

using Aspose.Cells;
using Aspose.Cells.Rendering;

var currentDirectory = Environment.CurrentDirectory;
var excel = new Workbook(currentDirectory + "\\ChartStackedBar.xlsx");
if (excel.Worksheets.Count > 0 && excel.Worksheets[0].Charts.Count > 0)
{
    var chart = excel.Worksheets[0].Charts[0];

    // Create an instance of ImageOrPrintOptions
    ImageOrPrintOptions options = new ImageOrPrintOptions
    {
        ImageFormat = System.Drawing.Imaging.ImageFormat.Png,
        VerticalResolution = 300,
        HorizontalResolution = 300
    };

    // Convert chart to image with specified options
    chart.ToImage(currentDirectory + "\\chart.png", options);
}

This code sets the image format to PNG and specifies the resolution, which will help ensure that the chart is rendered correctly with the appropriate legend.

Next Steps

  1. Run the updated code to see if the legend displays correctly.
  2. If the issue persists, check the chart settings in your Excel file to ensure that the legend is properly configured.
  3. Explore the Aspose.Cells documentation for more details on chart rendering options.

Please let me know if you need further assistance or if this resolves your issue!

Hello,
Your suggestion still not works.
You can run my uploaded to try it.

@amegroup
By testing with sample file and code on the latest version v25.6, we can reproduce the issue. The XAxis labels shown in legend when converting chart to image.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CELLSNET-58693

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Hi @amegroup
For this issue, the result of our fix is as follows, it will be effective in 25.7 version.
250707.png (6.2 KB)

The issues you have found earlier (filed as CELLSNET-58693) have been fixed in this update. This message was posted using Bugs notification tool by leoluo