I created a chart in Spire.XLS and saved it as an image.
The resulting image is different from the one shown in the Excel file: the legend has inverted the order, and now the second series is listed first:
I did a few tests, and found out that the problem only occurs when the second series does not use the primary axis.
Here is the code:
- Code: Select all
int count = 10;
Workbook workbook = new Workbook();
workbook.CreateEmptySheets(1);
Worksheet sheet = workbook.Worksheets[0];
Chart chart = sheet.Charts.Add(ExcelChartType.ScatterLine);
chart.DataRange = sheet.Range[2, 1, count + 2, 1];
chart.SeriesDataFromRange = false;
chart.Legend.Position = LegendPositionType.Top;
chart.LeftColumn = 5;
chart.TopRow = 2;
chart.RightColumn = 13;
chart.BottomRow = 22;
sheet.Range[1, 1].Value = "X";
sheet.Range[1, 2].Value = "Y1";
sheet.Range[1, 3].Value = "Y2";
for (int i = 0; i < count; i++)
{
sheet.Range[(i + 2), 1].NumberValue = i;
sheet.Range[(i + 2), 2].NumberValue = i * i;
sheet.Range[(i + 2), 3].NumberValue = i + 3;
}
chart.PrimaryCategoryAxis.Title = "X";
chart.Series[0].Name = "Y1";
chart.Series[0].CategoryLabels = sheet.Range[2, 1, count + 1, 1];
chart.Series[0].Values = sheet.Range[2, 2, count + 1, 2];
chart.Series[0].UsePrimaryAxis = true;
chart.Series.Add();
chart.Series[1].Name = "Y2";
chart.Series[1].CategoryLabels = sheet.Range[2, 1, count + 1, 1];
chart.Series[1].Values = sheet.Range[2, 3, count + 1, 3];
chart.Series[1].UsePrimaryAxis = false; //if this line is commented, the problem goes away
//Save and Launch Excel
workbook.SaveToFile("chart.xlsx");
Image image = workbook.SaveChartAsImage(sheet, 0);
image.Save("chart.png", ImageFormat.Png);
Thanks for the support,
Francesco