Hi,
Is it possible to create a Clone of ChartObject Or NSeries Collection object?
I am trying but it is not showing me the methods,
Do you have anything for this?
Thanks in Advance.
Thanks & Regards,
Sanjeev Gharmode
Hi Sanjeev,
We are not 100% sure about your actual requirement. Could you elaborate and explain it more why do you need to copy a chart object or NSeries collection? I think you may re-write your own code to create similar chart using Aspose.APIs.
Sample code:
Cells C = workbook.Worksheets[0].Cells;
C.SetColumnWidth(0, 10);
C.SetColumnWidth(1, 10);
C[0, 0].PutValue("Age");
C[0, 1].PutValue("Income");
double Income = 50000.00;
for (int i = 1; i < 52; i++)
{
C[i, 0].PutValue(i - 1);
C[i, 1].PutValue(Income);
C[i, 1].Style.Number = 42;
Income = Income + (Income * 0.05);
}
int ChartIndex = workbook.Worksheets[0].Charts.Add(ChartType.Bar3DClustered, 0, 4, 50, 20);
Chart Ch = workbook.Worksheets[0].Charts[0];
Ch.NSeries.Add("B2:B52", true);
Ch.NSeries.CategoryData = "A2:A52";
ASeries S;
S = Ch.NSeries[0];
S.Name = "=B1";
Ch.CategoryAxis.TickLabelSpacing = 1;
Ch.CategoryAxis.TickMarkSpacing = 1;
Ch.IsLegendShown = true;
Ch.Title.Text = "Age vs. Income";
Worksheet sheet2 = workbook.Worksheets[workbook.Worksheets.Add()];
int chindex = sheet2.Charts.Add(Ch.Type, Ch.ChartObject.UpperLeftRow, Ch.ChartObject.UpperLeftColumn, Ch.ChartObject.LowerRightRow, Ch.ChartObject.LowerRightColumn);
Chart Ch1 = sheet2.Charts[0];
Ch1.NSeries.Add("Sheet1!B2:B52", true);
Ch1.NSeries.CategoryData = Ch.NSeries.CategoryData;
ASeries S1;
S1 = Ch1.NSeries[0];
S1.Name = Ch1.NSeries[0].Name;
Ch1.CategoryAxis.TickLabelSpacing = Ch.CategoryAxis.TickLabelSpacing;
Ch1.CategoryAxis.TickMarkSpacing = Ch.CategoryAxis.TickMarkSpacing;
Ch1.IsLegendShown = Ch.IsLegendShown;
Ch1.Title.Text = Ch.Title.Text;
workbook.Save(@"f:\test\outduplicate.xls", FileFormatType.Excel2000);
Hi Amjad,
Thank you for your response.
My Requirement is as below,
I have one chart object already defined and set with all the properties requried for the chart in one Excel File.
Properties like NSeries, Title, AxisValues, Datalabels and etc.
I am fetching data from the Database and refrshing data of the chart series of the existing file opening using Aspose library. After refreshing data i am clearing existing series of the chart and applying new series. While clearing NSeries, Aspose clears all the property values related to NSeries.
Now, i want to create a copy of the NSeries or Chartobject before clearing NSeries of chart object. So that i can apply the existing properties to the new NSeries collection.
I assume this will give you clear idea about my requirement… Awaiting your quick response… it would be helpfull, if you provide Method like MemberwiseClone().
Thanks & Regards,
Sanjeev Gharmode
Hi Sanjeev,
I think you may try to add the data series as follows:
e.g..,
for (int i = 0; i < SourceChart.NSeries.Count; i++)
{
DestinationChart.NSeries.Add(SourceChart.NSeries[i].Values, true);
}
Thank you.
Please find my code here… Actually i want to change NSeries of existing chart item… where as i dont have any NSeries created through code.
Please advice,
foreach (Aspose.Cells.Chart chartObect in rangesToCharts[rangeDefn[0]])
{
string Cell1 = ExcelUtilAspose.GetAlphCell(rngName.FirstRow +2, rngName.FirstColumn+1);
string Cell2 = ExcelUtilAspose.GetAlphCell(rngName.RowCount+1, rngName.ColumnCount);
string sRangeName = “’” + worksheet.Name + “’!” + Cell1+":"+Cell2;
chartObect.NSeries.Clear();// Here i want some code which will keep my chartObject unchangable after clear of next statement
chartObect.NSeries.Add(sRangeName, true);
for (int i = 0; i < (rngName.ColumnCount-rngName.FirstColumn); i++)
{
string SeriesName = “=’” + worksheet.Name + “’!” + ExcelUtilAspose.GetAlphCell(rngName.FirstRow+1, rngName.FirstColumn + i+1);
chartObect.NSeries[i].Name = SeriesName;
}
Cell1 = ExcelUtilAspose.GetAlphCell(rngName.FirstRow + 2, rngName.FirstColumn);
Cell2 = ExcelUtilAspose.GetAlphCell(rngName.RowCount + 1, rngName.FirstColumn);
sRangeName = “’” + worksheet.Name + “’!” + Cell1 + “:” + Cell2;
chartObect.NSeries.CategoryData = sRangeName;
}
Hope this will now clear my requirement
Thanks & Regards,
Sanjeev Gharmode
Hi,
Well, when you use chartobject.NSeries.Clear() method, it will clear all the data series, its relevant objects and attributes.
Anyways, we will look into your scenario further and get back to you soon.
Thank you.
Hi Amjad,
Any updates on my request, Actually i was trying this using reflection to create the same object or deep copy of NSeries or chart object. But if you can help have Clone or such types of method then it will be helpfull.
Thanks in advcance....
Sanjeev Gharmode
Please try to consult the following code with the attached template file for your requirement.
Sample code:
Workbook workbook = new Workbook();
workbook.Open(@"F:\test\Files\Book1.xls");
Worksheet worksheet = workbook.Worksheets[0];
Range rngName = worksheet.Cells.CreateRange("A1", "C5");
Chart chart = worksheet.Charts[0];
int seriesCount = chart.NSeries.Count;
for (int i = 0; i < rngName.ColumnCount; i++)
{
string SeriesName = "=" + worksheet.Name + "!" + CellsHelper.CellIndexToName(rngName.FirstRow, rngName.FirstColumn + i);
string SeriesValue = "=" + worksheet.Name + "!" +
CellsHelper.CellIndexToName(rngName.FirstRow + 2, rngName.FirstColumn + i)
+ ":" + CellsHelper.CellIndexToName(rngName.FirstRow + rngName.RowCount - 1, rngName.FirstColumn + i);
if (i < seriesCount)
{
chart.NSeries[i].Values = SeriesValue;
chart.NSeries[i].Name = SeriesName;
}
else
{
chart.NSeries.Add(SeriesValue, true);
chart.NSeries[i].Name = SeriesName;
}
}
string categoryName = "=" + worksheet.Name + "!" +
CellsHelper.CellIndexToName(rngName.FirstRow + 1, rngName.FirstColumn)
+ ":" + CellsHelper.CellIndexToName(rngName.FirstRow + 1, rngName.FirstColumn + rngName.ColumnCount - 1);
chart.NSeries.CategoryData = categoryName;
workbook.Save(@"F:\test\Files\dest.xls");
Thank you.