When I have got four categories but I give only data for three of them. The output file has data in the fourth category that was not provided in the program.
Case 1:
Template Presentation File:
Source Code:
- Code: Select all
using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Presentation.Drawing;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PptxTester
{
class Program
{
static void Main(string[] args)
{
//Load template presentation
Presentation templatePresentation = new Presentation();
templatePresentation.LoadFromFile("slides_as_template.pptx");
//Create New Presentation
Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation();
presentation.Slides.RemoveAt(0);
//Create Temporary Blank Presentation
var blankPresentation = new Spire.Presentation.Presentation();
//Get slide from template presentation. index:1
ISlide cloneSlide = templatePresentation.Slides.ToArray().ElementAtOrDefault(1);
//---
//Get Chart
IChart chart = cloneSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Chart 14") as IChart;
int seriesCount = 4;
int categoryCount = 4;
chart.ChartData[1, 0].Value = "January";
chart.ChartData[2, 0].Value = "February";
chart.ChartData[3, 0].Value = "March";
chart.ChartData[4, 0].Value = "April";
chart.ChartData[0, 1].Value = "Charts";
chart.ChartData[0, 2].Value = "Tables";
chart.ChartData[0, 3].Value = "Pictures";
chart.ChartData[0, 4].Value = "Textboxes";
chart.ChartData[1, 1].Value = 11;
chart.ChartData[1, 2].Value = 15;
chart.ChartData[1, 3].Value = 4;
chart.ChartData[1, 4].Value = 7;
chart.ChartData[2, 1].Value = 25;
chart.ChartData[2, 2].Value = 60;
chart.ChartData[2, 3].Value = 77;
chart.ChartData[2, 4].Value = 30;
chart.ChartData[3, 1].Value = 40;
chart.ChartData[3, 2].Value = 50;
chart.ChartData[3, 3].Value = 65;
chart.ChartData[3, 4].Value = 44;
//***
var startIndexAddSeries = chart.Series.Count + 1;
for (int i = startIndexAddSeries; i <= 4; i++)
{
chart.Series.Append(chart.ChartData[0, i]);
}
if (chart.Series.Count > seriesCount)
{
for (int i = chart.Series.Count - 1; i >= seriesCount; i--)
{
chart.Series.RemoveAt(i);
}
}
chart.Categories.CategoryLabels = chart.ChartData["A2", "A" + (categoryCount + 1)];
for (int i = 0; i < seriesCount; i++)
{
string letter = GetColumnName(i + 1);
chart.Series[i].Values = chart.ChartData[letter + "2", letter + (categoryCount + 1)];
}
//Insert slide to the blank presentation
blankPresentation.Slides.Append(cloneSlide);
//Save and launch to view the PPTX document.
blankPresentation.SaveToFile("clear-data-08.pptx", Spire.Presentation.FileFormat.Pptx2010);
}
public static string GetColumnName(int index)
{
const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var value = "";
if (index >= letters.Length)
value += letters[index / letters.Length - 1];
value += letters[index % letters.Length];
return value;
}
}
}
Case 2:
To fix the above problem which is described in case 1 I added this line of code to clear chart data. It works.
- Code: Select all
chart.ChartData.Clear(0, 0, categoryCount + 1, seriesCount + 1);
But this line brokes the chart edit data functionality. We are starting to get a "The linked file isn't available" error from the PowerPoint application when we click on the edit data.
Source Code:
- Code: Select all
using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Presentation.Drawing;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PptxTester
{
class Program
{
static void Main(string[] args)
{
//Load template presentation
Presentation templatePresentation = new Presentation();
templatePresentation.LoadFromFile("slides_as_template.pptx");
//Create New Presentation
Spire.Presentation.Presentation presentation = new Spire.Presentation.Presentation();
presentation.Slides.RemoveAt(0);
//Create Temporary Blank Presentation
var blankPresentation = new Spire.Presentation.Presentation();
//Get slide from template presentation. index:1
ISlide cloneSlide = templatePresentation.Slides.ToArray().ElementAtOrDefault(1);
//---
//Get Chart
IChart chart = cloneSlide.Shapes.ToArray().FirstOrDefault(x => x.Name == "Chart 14") as IChart;
int seriesCount = 4;
int categoryCount = 4;
//This line brokes the chart edit data functionality. We get a "The linked file isn't available" error from the PowerPoint application when we click on the edit data.
chart.ChartData.Clear(0, 0, categoryCount + 1, seriesCount + 1);
chart.ChartData[1, 0].Value = "January";
chart.ChartData[2, 0].Value = "February";
chart.ChartData[3, 0].Value = "March";
chart.ChartData[4, 0].Value = "April";
chart.ChartData[0, 1].Value = "Charts";
chart.ChartData[0, 2].Value = "Tables";
chart.ChartData[0, 3].Value = "Pictures";
chart.ChartData[0, 4].Value = "Textboxes";
chart.ChartData[1, 1].Value = 11;
chart.ChartData[1, 2].Value = 15;
chart.ChartData[1, 3].Value = 4;
chart.ChartData[1, 4].Value = 7;
chart.ChartData[2, 1].Value = 25;
chart.ChartData[2, 2].Value = 60;
chart.ChartData[2, 3].Value = 77;
chart.ChartData[2, 4].Value = 30;
chart.ChartData[3, 1].Value = 40;
chart.ChartData[3, 2].Value = 50;
chart.ChartData[3, 3].Value = 65;
chart.ChartData[3, 4].Value = 44;
//***
var startIndexAddSeries = chart.Series.Count + 1;
for (int i = startIndexAddSeries; i <= 4; i++)
{
chart.Series.Append(chart.ChartData[0, i]);
}
if (chart.Series.Count > seriesCount)
{
for (int i = chart.Series.Count - 1; i >= seriesCount; i--)
{
chart.Series.RemoveAt(i);
}
}
chart.Categories.CategoryLabels = chart.ChartData["A2", "A" + (categoryCount + 1)];
for (int i = 0; i < seriesCount; i++)
{
string letter = GetColumnName(i + 1);
chart.Series[i].Values = chart.ChartData[letter + "2", letter + (categoryCount + 1)];
}
//Insert slide to the blank presentation
blankPresentation.Slides.Append(cloneSlide);
//Save and launch to view the PPTX document.
blankPresentation.SaveToFile("clear-data-08.pptx", Spire.Presentation.FileFormat.Pptx2010);
}
public static string GetColumnName(int index)
{
const string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var value = "";
if (index >= letters.Length)
value += letters[index / letters.Length - 1];
value += letters[index % letters.Length];
return value;
}
}
}
Also if I use these code to clear chart data I am still getting a "The linked file isn't available" error from the PowerPoint application when we click on the edit data.
- Code: Select all
for (int i = 0; i <= seriesCount; i++)
{
for (int j = 0; j <= categoryCount; j++)
{
chart.ChartData[i, j].Value = string.Empty;
}
}
How can I get the chart correctly without losing edit data functionality on the PowerPoint?
Thanks