Search code examples
c#chartsdatasetmschart

Creating a Series based on the difference of 2 other Series in MS Charts


2 series are created in my chart. I would like to create another series (H) whose values are the values of the 1st series (v1) minus the values of the 2nd series (S). As you can see S is derived using a financial formula. Does anyone know how I can create series H?

chartIndicators.DataSource = data;
chartIndicators.Series["v1"].XValueMember = "x";
chartIndicators.Series["v1"].YValueMembers = "y1";
chartIndicators.DataBind();


    // transform to macd  
chartIndicators.DataManipulator.FinancialFormula(FinancialFormula.MovingAverageConvergenceDivergence, "v1");

// signal
chartIndicators.DataManipulator.FinancialFormula(FinancialFormula.ExponentialMovingAverage, "9", "v1", "S");

Can I generate series H from the following datasets - if so, how can I do that? or is there a better way to do this?

DataSet dsV1 = chartIndicators.DataManipulator.ExportSeriesValues("v1");
DataSet dsS = chartIndicators.DataManipulator.ExportSeriesValues("S");

Solution

  • I found the solution for this myself:

        for (int i = 0; i < dsS.Tables[0].DefaultView.Count;i++) {
            double y = Convert.ToDouble(dsV1.Tables[0].DefaultView[i][1]) - Convert.ToDouble(dsS.Tables[0].DefaultView[i][1]);
            double x = Convert.ToDateTime(dsV1.Tables[0].DefaultView[i][0]).ToOADate();
            chartIndicators.Series["H"].Points.AddXY(x, y);                
        }