//-------------------------------------------------------------
//
// Copyright © Microsoft Corporation. All Rights Reserved.
//
//-------------------------------------------------------------
// @owner=alexgor, deliant
//=================================================================
// File: GeneralFormulas.cs
//
// Namespace: System.Web.UI.WebControls[Windows.Forms].Charting.Formulas
//
// Classes: GeneralFormulas
//
// Purpose: This class calculates Running total and average.
// Could be used for Pareto chart.
//
// Reviewed: GS - August 6, 2002
// AG - August 7, 2002
//
//===================================================================
using System;
#if Microsoft_CONTROL
namespace System.Windows.Forms.DataVisualization.Charting.Formulas
#else
namespace System.Web.UI.DataVisualization.Charting.Formulas
#endif
{
///
/// This class calculates Running total and average.
/// Could be used for Pareto chart
///
internal class GeneralFormulas : PriceIndicators
{
#region Properties
///
/// Formula Module name
///
override public string Name { get { return SR.FormulaNameGeneralFormulas; } }
#endregion
#region Formulas
///
/// Formula which calculates cumulative total.
/// ---------------------------------------------------------
/// Input:
/// - Y values.
/// Output:
/// - Running Total.
///
/// Arrays of doubles: 1. row - X values, 2. row - Y values
/// Arrays of doubles: 1. row - X values, 2. row - Moving average
private void RuningTotal(double [][] inputValues, out double [][] outputValues)
{
// There is not enough series
if( inputValues.Length != 2 )
{
throw new ArgumentException(SR.ExceptionPriceIndicatorsFormulaRequiresOneArray);
}
// Different number of x and y values
CheckNumOfValues( inputValues, 1 );
outputValues = new double [2][];
outputValues[0] = new double [inputValues[0].Length];
outputValues[1] = new double [inputValues[1].Length];
// Cumulative total
for( int index = 0; index < inputValues[0].Length; index++ )
{
outputValues[0][index] = inputValues[0][index];
if( index > 0 )
{
outputValues[1][index] = inputValues[1][index] + outputValues[1][index-1];
}
else
{
outputValues[1][index] = inputValues[1][index];
}
}
}
///
/// Running Average Formula
/// ---------------------------------------------------------
/// Input:
/// - Y values.
/// Output:
/// - Running Average.
///
/// Arrays of doubles: 1. row - X values, 2. row - Y values
/// Arrays of doubles: 1. row - X values, 2. row - Moving average
private void RunningAverage(double [][] inputValues, out double [][] outputValues)
{
// There is no enough series
if( inputValues.Length != 2 )
throw new ArgumentException(SR.ExceptionPriceIndicatorsFormulaRequiresOneArray);
// Different number of x and y values
CheckNumOfValues( inputValues, 1 );
outputValues = new double [2][];
outputValues[0] = new double [inputValues[0].Length];
outputValues[1] = new double [inputValues[1].Length];
// Total
double total = 0;
for( int index = 0; index < inputValues[0].Length; index++ )
{
total += inputValues[1][index];
}
// Runing Average
for( int index = 0; index < inputValues[0].Length; index++ )
{
outputValues[0][index] = inputValues[0][index];
if( index > 0 )
outputValues[1][index] = inputValues[1][index] / total * 100 + outputValues[1][index-1];
else
outputValues[1][index] = inputValues[1][index] / total * 100;
}
}
#endregion
#region Methods
///
/// Default constructor.
///
public GeneralFormulas()
{
}
///
/// The first method in the module, which converts a formula
/// name to the corresponding private method.
///
/// String which represent a formula name.
/// Arrays of doubles - Input values.
/// Arrays of doubles - Output values.
/// Array of strings - Formula parameters.
/// Array of strings - Extra Formula parameters from DataManipulator object.
/// Array of strings - Used for Labels. Description for output results.
override public void Formula( string formulaName, double [][] inputValues, out double [][] outputValues, string [] parameterList, string [] extraParameterList, out string [][] outLabels )
{
string name;
outputValues = null;
// Not used for these formulas.
outLabels = null;
name = formulaName.ToUpper(System.Globalization.CultureInfo.InvariantCulture);
try
{
switch( name )
{
case "RUNINGTOTAL":
RuningTotal( inputValues, out outputValues );
break;
case "RUNINGAVERAGE":
RunningAverage( inputValues, out outputValues );
break;
default:
outputValues = null;
break;
}
}
catch( IndexOutOfRangeException )
{
throw new InvalidOperationException( SR.ExceptionFormulaInvalidPeriod(name) );
}
catch( OverflowException )
{
throw new InvalidOperationException( SR.ExceptionFormulaNotEnoughDataPoints(name) );
}
}
#endregion
}
}