'Adding a second line to a graph in excel via c#
I want to create a simple line on a graph in excel that I have generated with code. I add the data to an excel sheet and can generate a single graph of that data. my code to generate the graph is
int cRowCount = worksheet.UsedRange.Rows.Count;
Range employeeNames = worksheet.Range["A2:A" + cRowCount];
Range kpiValues = worksheet.Range["E2:E" + cRowCount];
Chart kpiChart = (Chart)workbook.Charts.Add(misValue, misValue, 1, misValue);
kpiChart.SetSourceData(employeeNames, misValue);
kpiChart.ChartType = XlChartType.xlLineMarkers;
var seriesX = (Series)kpiChart.SeriesCollection(1);
seriesX.XValues = employeeNames;
seriesX.Values = kpiValues;
kpiChart.Legend.Clear();
workbook.Sheets["KPI Export"].Move(workbook.Sheets[1]);
kpiChart.HasTitle = false;
workbook.Sheets[2].name = "KPI chart";
// save the application
workbook.SaveAs(tbxExcelSave.Text + "\\" + tbxName.Text, XlFileFormat.xlOpenXMLWorkbook,
misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
// Exit from the application
writeToApp.Quit();
GC.Collect();
The graph looks as follows.
Graph of all values I now want to use the final data point to create a horizontal line in the same graph as the final data point is the average of all the data points. With this I will be able to see what is above or below the "average" of the data.
Really all I need to do is add a simple horizontal line to the graph at the Y coordinate of the final value. Any assistance would be greatly appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|