We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Chart with SecondaryValueAxis

I'm having some problems trying to create a chart with two series on two Y axes. The first series should be a line series on the left axis and the second series should be a column series on the right axis.

I'm seeing two problems at this point:
1) As soon as I add the second (column) series to the chart, the first series turns into a column series too.
2) The SecondaryValueAxis is showing up on the left of the chart beside the PrimaryValueAxis. I would prefer it on the right side of the chart.

This is the code I'm using:
ExcelEngine excelEngine = new ExcelEngine();
IApplication application = excelEngine.Excel;
IWorkbook workbook = application.Workbooks.Create(5);
IWorksheet sheet = workbook.Worksheets[0];

// Inserting sample data for the chart.
sheet.Range["A1"].Text = "Date";
sheet.Range["B1"].Text = "LeftYAxis";
sheet.Range["C1"].Text = "RightYAxis";
// Months
sheet.Range["A2"].DateTime = new DateTime(2008, 12, 9);
sheet.Range["A3"].DateTime = new DateTime(2008, 12, 10);
sheet.Range["A4"].DateTime = new DateTime(2008, 12, 11);
sheet.Range["A5"].DateTime = new DateTime(2008, 12, 12);

sheet.Range["B2"].Number = 20.6;
sheet.Range["B3"].Number = 20.61;
sheet.Range["B4"].Number = 19.45;
sheet.Range["B5"].Number = 19.36;

sheet.Range["C2"].Number = 80.49959;
sheet.Range["C3"].Number = 61.5017;
sheet.Range["C4"].Number = 83.62421;
sheet.Range["C5"].Number = 78.1308;

// Embedded Chart
IChartShape chart = sheet.Charts.Add();
// Setting chart type.
chart.ChartType = ExcelChartType.Line;

//chart.PrimaryValueAxis.MajorGridLines.Border.LinePattern = ExcelChartLinePattern.Dot;
// LeftAxisSeries
IChartSerie leftAxisSeries = chart.Series.Add("LeftAxis", ExcelChartType.Line);
leftAxisSeries.Values = sheet.Range["B2:B5"];
// RightAxisSeries
IChartSerie rightAxisSeries = chart.Series.Add("RightAxis", ExcelChartType.Column_Clustered);
rightAxisSeries.Values = sheet.Range["C2:C5"];
rightAxisSeries.UsePrimaryAxis = false;

chart.PrimaryCategoryAxis.CategoryLabels = sheet.Range["A2:A5"];
chart.SecondaryCategoryAxis.CategoryLabels = sheet.Range["A2:A5"];
chart.SecondaryCategoryAxis.Visible = false;

//Export(workbook);

workbook.Close();

excelEngine.Dispose();

I'm attaching two example spreadsheets, Goal.xls shows about what I'm hoping to get, SF.xls shows what I'm getting.

Thanks,
Bill Massie





sf_7c70e207.zip

6 Replies

LR Lokesh R Syncfusion Team December 10, 2009 06:06 AM UTC

hi Bill Massie,

Thank you for your interest in Syncfusion products.

Here by, i have given the link for the application,with sample output.

http://help.syncfusion.com/support/XlsIO/chart%20with%20secondary%20value%20axis.zip

Please try this and let me know if you have any questions.

Regards,
R.lokesh


BM Bill Massie NO LONGER WITH COMPANY December 10, 2009 04:08 PM UTC

It's perfect! Thanks so much for your help.


BM Bill Massie NO LONGER WITH COMPANY December 10, 2009 05:45 PM UTC

Ok, I know it's lame to say "it's perfect" and then come back with an additional request.

Is there any way to get the bar series on the right axis? When I tried to play with the code, if I dealt with the line series first I got an error.

I basically just swapped the code for formatting serieOne and serieTwo.

serieOne.SerieType = ExcelChartType.Line_Markers;
serieOne.Name = sheet.Range["B1"].Text;
chart.PrimaryValueAxis.Title = sheet.Range["C1"].Text;
chart.PrimaryValueAxis.TitleArea.TextRotationAngle = 90;
//Format marker
serieOne.SerieFormat.MarkerStyle = ExcelChartMarkerType.Diamond;
serieOne.SerieFormat.MarkerSize = 8;


serieTwo.SerieFormat.Fill.FillType = ExcelFillType.Gradient;
serieTwo.SerieFormat.Fill.TwoColorGradient(ExcelGradientStyle.Vertical, ExcelGradientVariants.ShadingVariants_2);
serieTwo.SerieFormat.Fill.GradientColorType = ExcelGradientColor.TwoColor;
serieTwo.Name = sheet.Range["C1"].Text;
//Show value as data labels
serieTwo.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
serieTwo.DataPoints.DefaultDataPoint.DataLabels.Position = ExcelDataLabelPosition.Outside;
serieTwo.UsePrimaryAxis = false;

When I do that I get an Index out of range exception on this line:
chart.SecondaryCategoryAxis.IsMaxCross = true;
This is the stacktrace.

at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at Syncfusion.XlsIO.Implementation.Charts.ChartFormatCollection.get_Item(Int32 index)
at Syncfusion.XlsIO.Implementation.Charts.ChartSerieImpl.GetCommonSerieFormat()
at Syncfusion.XlsIO.Implementation.Charts.ChartSerieImpl.DetectSerieTypeStart()
at Syncfusion.XlsIO.Implementation.Charts.ChartSerieImpl.get_StartType()
at Syncfusion.XlsIO.Implementation.Charts.ChartCategoryAxisImpl.GetStartChartType()
at Syncfusion.XlsIO.Implementation.Charts.ChartCategoryAxisImpl.get_IsChartBubbleOrScatter()
at Syncfusion.XlsIO.Implementation.Charts.ChartCategoryAxisImpl.set_IsMaxCross(Boolean value)
at SyncFusionTest.MainPage.SFSupportTest(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)


This is not a dealbreaker. We can live with it like this, but it would be nice to get an understanding of what's going on.

Thanks,
Bill Massie


LR Lokesh R Syncfusion Team December 11, 2009 02:04 PM UTC

hi Bill Massie,

Could you please report this issue through Direct Trac Developer Support System
https://www.syncfusion.com/account/login?ReturnUrl=%2fsupport%2fdirecttrac%2fincidents because you can take the advantage of the expertise of a dedicated support engineer and a guaranteed response time and we hope you will take advantage of this system as well. Thank you for your participation in Syncfusion’s Community Forums”.

with regards,
R.Lokesh


BM Bill Massie NO LONGER WITH COMPANY December 11, 2009 06:34 PM UTC

Will do. Thanks for your help.


LR Lokesh R Syncfusion Team December 14, 2009 05:08 AM UTC

hi Bill Massie,

Thank you for your kind response.

with regards,
R.lokesh

Loader.
Live Chat Icon For mobile
Up arrow icon