Extract raw data from chart

I use the code code with VSTO to populate a data[,] with the raw values from charts.


What’s the recommended way to do the equivalent with your API? I see a few ways that might possibly work but feel like I’d be setting up to fail certain edge cases. I also see a public ChartDataWorkbook.GetCellCollection method but it is undocumented and doesn’t like any of the ways I’ve tried calling it.

I’m on an evaluation license now and this is the only issue remaining to sort out before I can make the purchase.

Thanks!

foreach (Powerpoint.Shape shape in slide.Shapes)
{
if (shape.HasChart != 0)
{
var chart = shape.Chart;
var workbook = (Excel.Workbook)chart.ChartData.Workbook;
var range = (Excel.Range)workbook.ActiveSheet.UsedRange;
var data = new object[range.Rows.Count, range.Columns.Count];
for (var row = 1; row <= range.Rows.Count; row++)
{
for (var col = 1; col <= range.Columns.Count; col++)
{
data[row - 1, col - 1] = range.Cells.Item[row, col].Value2;
}
}

Hi Robert,

Thanks for sharing the interest in Aspose prodcuts.


I have observed the requirements shared by you and like to share that Aspose.Slides and Aspose.Cells in together serve the purpose for you. Also in your code, you have used both VSTO component for PowerPoint and Excel. Please use the attached sample code for your kind reference in this regard. I have used Aspose.Slides for .NET 8.4.0 and Aspose.Cells for .NET on my end.

Please share, if I may help you further in this regard.

Many Thanks,

That code gives me all the tables in the spreadsheet and only one of those will correspond to the chart. How can I extract just the data used in a given chart?

Hi,

I have observed your further requirements. Please use the following sample code to serve the purpose.

public static void testChart2()
{
String path=@“D:\Aspose Data”;

//Load presentation
PresentationEx pres = new PresentationEx(path + “TestChart.pptx”);

//Access the desired chart shape
ChartEx chart = (ChartEx) pres.Slides[0].Shapes[0];

//Save chart data to workbook stream
Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(chart.ChartData.ReadWorkbookStream());


var ranges = book.Worksheets.GetNamedRangesAndTables();
foreach (Aspose.Cells.Range range in ranges)
{
Object[,] data = new Object[range.RowCount, range.ColumnCount];
//data = (Object[,])range.Value;


//var data = new object[range.Rows.Count, range.Columns.Count];
for (var row = 0; row < range.RowCount; row++)
{
for (var col = 0; col < range.ColumnCount; col++)
{
data[row , col ] = range[row, col].Value;
}
}


}


}

Please share if I may help you further in this regard.

Many Thanks,

This is getting all tables



(var ranges = book.Worksheets.GetNamedRangesAndTables():wink:



How do I get only the table that the chart corresponds to?

Hi Robert,

I like to share that chart data has only one worksheet in it. I have modified the sample code for your kind reference. Please try using the attached sample code on your end to serve the purpose.

public static void testChart2()
{
String path=@“D:\Aspose Data”;

//Load presentation
PresentationEx pres = new PresentationEx(path + “TestChart.pptx”);

//Access the desired chart shape
ChartEx chart = (ChartEx) pres.Slides[0].Shapes[0];

//Save chart data to workbook stream
Aspose.Cells.Workbook book = new Aspose.Cells.Workbook(chart.ChartData.ReadWorkbookStream());


var ranges = book.Worksheets.GetNamedRangesAndTables();

Aspose.Cells.Range range2 = book.Worksheets[book.Worksheets.ActiveSheetIndex];

foreach (Aspose.Cells.Range range in ranges)
{
Object[,] data = new Object[range.RowCount, range.ColumnCount];
//data = (Object[,])range.Value;

//Selecting the active worksheet index for range
if (range.Worksheet.Index == book.Worksheets.ActiveSheetIndex)
{

//var data = new object[range.Rows.Count, range.Columns.Count];
for (var row = 0; row < range.RowCount; row++)
{
for (var col = 0; col < range.ColumnCount; col++)
{
data[row, col] = range[row, col].Value;
}
}
}

}

}


Please share, if I may help you further in this regard.

Many Thanks,