<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > Today mainly write about how to use Visual Studio in Excel to do chart (chart), many people on the Internet have discussed this problem, but I think here is still writing about C # for financial in the city, how to write, Because I think it's better to do it. </span>
First look at the structure of the chart in Excel:
The main use of the drawing here is ChartWizard, but I just started to make out of the image of the huge ugly ... And it's not right, don't know why, the type has been set to XY scatter chart, but the picture is still this urine sex.
Later, after the hint in the book, I realized this function, so duang, made a very beautiful chart!!!
Excel graphing "Update" 2015/3/10 public void Createchart<t> (vector<t> x, vector<t> y, string title, str ing horizontal, String vertical, string legend) {try {//ADD sheet. Excel.Workbook Pworkbook; Excel.Worksheet Psheet; if (Pexcel.activeworkbook = = null) {Pworkbook = (Excel.Workbook) invokemethodinternation Al (Pexcel.workbooks, "Add", Excel.XlWBATemplate.xlWBATWorksheet); Psheet = (excel.worksheet) pworkbook.activesheet; } else {pworkbook = Pexcel.activeworkbook; Psheet = (excel.worksheet) invokemethodinternational (pworkbook.worksheets, "Add", Type.Missing, Type.Missing, 1, Type.Missing); }//Add row labels + values. int sheetcolumn = 1; int sheetrow = 1; Print x Column Tosheetvertical (Psheet, Sheetrow, Sheetcolumn, horizontal, x); Print y-column tosheetvertical (Psheet, Sheetrow, Sheetcolumn + 1, vertical, y); Add charts to the workbook chart (XY point chart) Excel.Chart chart = PWorkbook.Charts.Add (Type.Missing, Type.Missing, Type.Missing, Type.Missing) as Excel.Chart; Excel.Range Range = (excel.range) psheet.get_range ("A2", "B" + (x.length+1). ToString ()); Chart. ChartWizard (range, Excel.XlChartType.xlXYScatterLinesNoMarkers, 1, Excel.XlRowCol.xlColumns, 1, 0, true, title, Horizontal, vertical, type.missing); Chart. SeriesCollection (1). Name = legend; } catch (Exception e) {Console.WriteLine ("Exception:" + e); } }
Class Test {static void Main (string[] args) {vector<double> t = new Vector<dou Ble> (new double[] {0.1, 1, 4, 9, 20, 30}, 0); vector<double> r = new vector<double> (new double[] {0.081, 0.07, 0.044, 0.07, 0.04, 0.03}, 0); Linearinterpolator Myinterpolatorh = new Linearinterpolator (t, R); Create the Abscissa values F (hard-coded for the moment) int M = 299; vector<double> term = new vector<double> (M, 1); Term[term. Minindex] = 0.1; Double step = 0.1; for (int j = term. Minindex + 1; J <= term. Maxindex; J + +) {Term[j] = term[j-1] + step; } vector<double> interpolatedlinear = myinterpolatorh.curve (term); Excelmechanisms ExL = new Excelmechanisms (); exl.printoneexcel<double> (term, interpolatedlinear, "Linear interpolated Curve", "Time", "Interest rate "," interpolated Interest rate "); Console.read (); }
Using system;using system.collections.generic;using system.linq;using system.text;namespace UserDefinedDataEXP{//Point column public class Pair <T> {public T first; Public T second; Public Pair (t first, T Second) {this.first = first; This.second = second; }}//double-line interpolation public class Bilinearinterpolator {private vector<double> X1arr; Private vector<double> X2arr; Private numericmatrix<double> matvals; private int N1; private int N2; Find the index of less than or equal to X in X1arr//Find the Index public pair<int> Findabscissa (double x,double y) that is less than or equal to Y in X2arr {int firstindex = 0; int secondindex = 0; for (int i = 0; I <= n1-1; i++) {if (x1arr[i]<=x && x<=x1arr[i+1]) {for (int j = 0; J <= n2-1; j + +) { if (X2arr[j] <= y && y <= x2arr[j + 1]) {return new Pai R<int> (FirstIndex, Secondindex); } secondindex++; }} firstindex++; } return null; }//Constructor public Bilinearinterpolator (vector<double> X1arr, vector<double> X2arr, numericmatrix< ;d ouble> gridvalues) {This.x1arr = X1arr; This.x2arr = X2arr; N1 = X1arr. Length-1; N2 = X2arr. Length-1; Matvals = gridvalues; }//returns the value on (x, y)! Public double Solve (double x, double y) {pair<int> p = Findabscissa (x, y); int i = P.first; int j = P.second; 4 box points, create variables for readability (see Wiki) Double Q11 = Matvals[i, j]; Double Q22 = matvals[i + 1, j + 1]; Double Q12 = matvals[i, j + 1]; Double Q21 = matvals[i + 1, j]; Double x1 = x1arr[i]; Double x2 = x2arr[i + 1]; Double y1 = x2arr[j]; Double y2 = x2arr[j + 1]; Double factor = 1.0/((x2-x1) * (y2-y1)); Return (Q11 * (x2-x) * (y2-y) + Q21 * (x-x1) * (y2-y) + Q12 * (x2-x) * (y-y1) + Q22 * (x-x1) * (Y-Y1)) * F Actor }//Based on the input (x, y) point column set, give the interpolated surface public numericmatrix<double> surface (vector<double> X1arr, Vecto R<double> X2arr) {numericmatrix<double> result=new numericmatrix<double> (X1arr. Length, X1arr. Length); for (int i = X1arr. Minindex; I <= X1arr. Maxindex; i++) {for (int j = X2arr. Minindex; J <= X2arr. Maxindex; J + +) {result[i, j] = Solve (X1arr[i], x2arr[j]); }} return result; } public numericmatrix<double> SurfacE () {//Create the interpolated surface, MEMBER DATA as Abscissae return surface (X1arr, X2arr); }}//Linearinterpolator "Update" 2015/3/10 public class Linearinterpolator {public vector<double> t ; Public vector<double> observedrate; Public Linearinterpolator (Vector<double> T, vector<double> observedrate) {this.t = t; This.observedrate = observedrate; Vector<double> Curve (vector<double> term) {int size = term. Length; vector<double> result = new vector<double> (term. Length,term. Minindex); for (int i =term. Minindex; I <=term. Maxindex; i++) {if (Term[i]<=t[t.minindex]) {Result[i] = Observedra Te[observedrate.minindex]; } else if (Term[i]>=t[t.maxindex]) {result[i] = Observedrate[observedrate.maxindex]; } else {for (int j = T.minindex; J <=t.MaxIndex-1; J + +) {if (Term[i] > T[j] && term[i] < T[j + 1]) {Result[i] = ((Term[i] -t[j + 1]) * Observedrate[j] + (T[j]-term[i]) * observedrate[j + 1])/(T[j]-t[j + 1]); }}}} return result; } }}
"C #" 14. Printoneexcel plotting in Excel & interest rate interpolation (linear)