If x_list = 1,2,3...18,19,20 it works, the equation of the trend line in Excel matches the equation given by chart.Series[0].TrendLines[0].Formula. However, using the values I include in this code snippet, the equations don't match.
- Code: Select all
//Create an Excel and add a sheet
Workbook workbook = new Workbook();
workbook.CreateEmptySheets(1);
Worksheet sheet = workbook.Worksheets[0];
List<double> x_list = new List<double>() {
1549.445, 1549.45, 1549.455, 1549.46, 1549.465,
1549.47, 1549.475, 1549.48, 1549.485, 1549.49,
1549.495, 1549.5, 1549.505, 1549.51, 1549.515,
1549.52, 1549.525, 1549.53, 1549.535, 1549.54
};
List<double> y_list = new List<double>() {
697.2427277, 697.1438931, 697.3850398, 698.8061502, 700.0726429,
700.7809151, 701.7333576, 702.2041319, 702.8688828, 703.0724705,
702.6475168, 702.3722817, 701.990371, 701.5504769, 700.5902129,
700.0273651, 699.3373101, 698.6473923, 697.9619781, 697.3975184
};
//ReadCSV(ref x_list, ref y_list);
//Fill Excel cells with sample data
sheet.Name = "Sheet1";
// x axis
for (int i = 0; i < x_list.Count; i++)
{
sheet.Range["A" + (i + 1)].Value = x_list[i].ToString();
}
// y axis
for (int i = 0; i < y_list.Count; i++)
{
sheet.Range["B" + (i + 1)].Value = y_list[i].ToString();
}
//Create a columnclustered chart
Chart chart = sheet.Charts.Add(ExcelChartType.ScatterMarkers);
chart.DataRange = sheet.Range["B1:B" + (y_list.Count)];
chart.SeriesDataFromRange = false;
//Add trendline and datatable to the chart
chart.Series[0].TrendLines.Add(TrendLineType.Polynomial);
chart.Series[0].TrendLines[0].Order = 6;
chart.Series[0].TrendLines[0].DisplayEquation = true;
chart.HasDataTable = true;
chart.Series[0].CategoryLabels = sheet.Range["A1:A" + (x_list.Count)];
chart.Series[0].Values = sheet.Range["B1:B" + (y_list.Count)];
sheet.Range["C1"].Value = chart.Series[0].TrendLines[0].Formula;
//Save the document as .xlsx file
workbook.SaveToFile("Sample1.xlsx", ExcelVersion.Version2013);
Can anyone tell my why these equations don't match and how I could ensure that they do match?