Spire.Presentation is a professional PowerPoint® compatible library that enables developers to create, read, write, modify, convert and Print PowerPoint documents. Get free and professional technical support for Spire.Presentation for .NET, Java, Android, C++, Python.

Mon Oct 28, 2024 12:10 pm

I am trying to populate an existing chart with my data below. However this chart's datarange is missing the category axis, so it fails. I need to initialise it, how do I do it?

My data:
"" | positive | negative
Woolworths | 53.42% | 56.80%
Coles | 25.88% | 25.38%
No outlet in particular | 9.80% | 3.49%
IGA | 3.30% | 5.08%
Farmer Jacks | 3.22% | 2.45%


Screenshot 2024-10-28 120614.png

boosted_d16
 
Posts: 34
Joined: Wed Apr 19, 2023 11:41 am

Tue Oct 29, 2024 10:08 am

Hello,

Thanks for your inquiry. You can refer to the following code to set the area of the category axis. If you have any further questions, please provide us with your input document for a detailed investigation. You can upload here or send it to us via email( Amin.gan@e-iceblue.com ). Thank you in advance.
Code: Select all
       Presentation presentation = new Presentation();
            presentation.LoadFromFile(@"test.pptx");
            IChart chart = presentation.Slides[0].Shapes[0] as IChart;
            string[,] data = new string[,]
{
  {"","positive","negative" },
  {"Woolworths","53.42%","56.80%"},
  {"Coles","25.88%","25.38%" },
  {"No outlet in particular","9.80%","3.49%" },
  {"IGA","3.30%","5.08%" },
  {"Farmer Jacks","3.22%","2.45%" }
};

            for (int i = 0; i < data.GetLength(0); i++)
            {
                for (int j = 0; j < data.GetLength(1); j++)
                {
            // Convert to numbers
                    if (data[i, j].EndsWith("%"))
                    {
                        data[i, j] = data[i, j].TrimEnd('%');
                    }

                    double percentageValue;
                    if (double.TryParse(data[i, j], NumberStyles.Any, CultureInfo.InvariantCulture, out percentageValue))
                    {
                        double numericValue = percentageValue ;
                        chart.ChartData[i, j].Value = Math.Round(numericValue, 4) ;
       
                    }       
                    else
                    {
                        chart.ChartData[i, j].Value = data[i, j];
                    }
                }
            }
            // Set series labels
            chart.Series.SeriesLabel = chart.ChartData["B1", "C1"];

            // Set category labels
            chart.Categories.CategoryLabels = chart.ChartData["A2", "A6"];

            // Assign values to each series
            chart.Series[0].Values = chart.ChartData["B2", "B6"];
            chart.Series[1].Values = chart.ChartData["C2", "C6"];
            chart.PrimaryValueAxis.DisplayUnit = ChartDisplayUnitType.Hundreds;
            presentation.SaveToFile(@"test_out.pptx", FileFormat.Pptx2010);

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 282
Joined: Mon Jul 15, 2024 5:40 am

Tue Oct 29, 2024 3:50 pm

I think you have misunderstood.

I want to access this chart's category labels but it does not exist. Therefore, you cannot access the chart.ChartData property.

This one is effectively null and trying to access it throws an error.

Unfortunately in order to manipulate chart data and provide a valid transformation such property needs to be at least initialized for us to be able to add values to it.

It's null and read only instead (so we can't even initialize on our own...) - is there a way to have it initialized even if the chart is not valid


sgp-98-corrupt-spire.zip

boosted_d16
 
Posts: 34
Joined: Wed Apr 19, 2023 11:41 am

Wed Oct 30, 2024 9:58 am

Hello,

Thanks for your feedback. At present, there is indeed an issue with obtaining the data source. I have recorded this problem in our bug tracking system with the number SPIREPPT-2640. Our dev personnel will further assist in troubleshooting, and I will notify you immediately once there is progress.

Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 282
Joined: Mon Jul 15, 2024 5:40 am

Return to Spire.Presentation