我的数据在x和y上都是数字,并使用mschart 4.0
绘制了数据我需要在我拥有的一堆点上添加趋势线/线性回归。关于x和y的数据都是数字(没有任何日期),例如(33.4,45.1)将是一个点。
在我从第一个链接下载的样本中,我在代码文件forecasting.aspx(.cs)中找到了一个线性回归样本,我发现了这个ms documentation
我在图表中添加了一个线性回归线,其中包含以下行(一旦设置了所有其他数据等)
Chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, parameters, "Input:Y", "Forecasting:Y,Range:Y,Range:Y2");
这两个问题在于他们假设使用日期。无论如何我继续执行它,但是我的回归线从x值20左右开始,并且如果我给参数Period a value,它将转到其中一个点(大约70)的max x值的x值但由于它不是从x值0开始,我不相信它是正确的。
任何人都知道如何实现这个目标?
答案 0 :(得分:2)
我的解决方案是在每个趋势线的开头和结尾添加一个额外的数据点,基于散点图中的最小和最大x值。
这可以通过
完成以下代码片段将起点和终点添加到两个趋势线,然后是简单函数来计算斜率和截距:
if (Chart1.Series["Budget Year"].Points.Count > 2 && Chart1.Series["Actual Last Year"].Points.Count > 2)
{
Chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, "Linear,0,false,false", Chart1.Series["Budget Year"], Chart1.Series["Trendline (Budget Year)"]);
Chart1.DataManipulator.FinancialFormula(FinancialFormula.Forecasting, "Linear,0,false,false", Chart1.Series["Actual Last Year"], Chart1.Series["Trendline (Actual Last Year)"]);
double budgetSlope = Utility.GetSlope(Chart1.Series["Trendline (Budget Year)"].Points[0], Chart1.Series["Trendline (Budget Year)"].Points[1]);
double lastYearSlope = Utility.GetSlope(Chart1.Series["Trendline (Actual Last Year)"].Points[0], Chart1.Series["Trendline (Actual Last Year)"].Points[1]);
double budgetIntercept = Utility.GetIntercept(Chart1.Series["Trendline (Budget Year)"].Points[0], Chart1.Series["Trendline (Budget Year)"].Points[1]);
double lastYearIntercept = Utility.GetIntercept(Chart1.Series["Trendline (Actual Last Year)"].Points[0], Chart1.Series["Trendline (Actual Last Year)"].Points[1]);
double minBudgetRevenue = Convert.ToDouble(dt.Select("RevCurrBudget = MIN(RevCurrBudget)")[0]["RevCurrBudget"]);
double maxBudgetRevenue = Convert.ToDouble(dt.Select("RevCurrBudget = MAX(RevCurrBudget)")[0]["RevCurrBudget"]);
double minLastYearRevenue = Convert.ToDouble(dt.Select("RevPrevActual = MIN(RevPrevActual)")[0]["RevPrevActual"]);
double maxLastYearRevenue = Convert.ToDouble(dt.Select("RevPrevActual = MAX(RevPrevActual)")[0]["RevPrevActual"]);
double minBudgetEBIT = (budgetSlope * minBudgetRevenue) + budgetIntercept;
double maxBudgetEBIT = (budgetSlope * maxBudgetRevenue) + budgetIntercept;
double minLastYearEBIT = (lastYearSlope * minLastYearRevenue) + lastYearIntercept;
double maxLastYearEBIT = (lastYearSlope * maxLastYearRevenue) + lastYearIntercept;
Chart1.Series["Trendline (Budget Year)"].Points.InsertXY(0, minBudgetRevenue, minBudgetEBIT);
Chart1.Series["Trendline (Budget Year)"].Points.AddXY(maxBudgetRevenue, maxBudgetEBIT);
Chart1.Series["Trendline (Actual Last Year)"].Points.InsertXY(0, minLastYearRevenue, minLastYearEBIT);
Chart1.Series["Trendline (Actual Last Year)"].Points.AddXY(maxLastYearRevenue, maxLastYearEBIT);
}
public static double GetSlope(DataPoint pt1, DataPoint pt2)
{
return GetSlope(pt1.XValue, pt1.YValues[0], pt2.XValue, pt2.YValues[0]);
}
public static double GetSlope(double x1, double y1, double x2, double y2)
{
return (y2 - y1) / (x2 - x1);
}
public static double GetIntercept(DataPoint pt1, DataPoint pt2)
{
double slope = GetSlope(pt1, pt2);
double y = pt1.YValues[0];
double x = pt1.XValue;
return y - (slope * x);
}