public Dictionary <int, double []> GetResult(int id) { List <int> years = _context.AccountValue.Select(e => e.Year).Distinct().Cast <int>().ToList(); Kpi _kpi = _context.Kpi.Where(e => e.Kpiid == id).FirstOrDefault(); ChartData chartData = new ChartData(); if (_kpi != null) { string formula = _kpi.Formula; string[] accounts = formula.Split(new char[] { '/', '*', '+', '-' }); for (int i = 0; i < years.Count; i++) { evaluator.ClearVariables(); foreach (string account in accounts) { var isNumeric = double.TryParse(account, out double n); if (!isNumeric) { Account ac = _context.Account.Where(acc => acc.Name == account).FirstOrDefault(); AccountValue value = _context.AccountValue.Where(val => val.AccountId == ac.AccountId && val.Year == years[i]).FirstOrDefault(); evaluator.AddVariable(ac.Name, value.Amount.ToString()); } } double[] dataArray = new double[1]; dataArray[0] = evaluator.EvaluateExpression(_kpi.Formula); chartData.AddValue(years[i], dataArray); } } return(chartData.GetData()); }
public override void Write(IReportWriter writer, object datasource) { expressionEvaluator.AddVariable("Context", writer.Context); expressionEvaluator.Compile(datasource.GetType()); var value = expressionEvaluator.EvaluateExpression(datasource); var valueString = value == null ? string.Empty : value.ToString(); writer.WriteTextElement(X, Y, valueString); }