我的解决方案是根据散点图中的最小和最大 x 值在每条趋势线的开头和结尾添加一个额外的数据点。
这可以通过
- 计算趋势线的斜率
- 计算趋势线的截距
- 获取散点图中的最小 x 值
- 获取散点图中的最大 x 值
- 使用斜率、截距和最小 x 值计算最小 y 值
- 使用斜率、截距和最大 x 值计算最大 y 值
- 将具有 x 和 y 值的两个点添加到趋势线
以下代码片段将起点和终点添加到两条趋势线,然后是计算斜率和截距的简单函数:
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);
}