views:

528

answers:

3

I discovered Microsoft's .Net charting controls from another post here, and so far I love them. For anyone needing it, here is the link: http://code.msdn.microsoft.com/mschart

I do everything during runtime, such as creating series and slapping them on a chart area. I can successfully create a Candlestick chart, by supplying it with an X value and 4 Y values, and when I add it to the chart like so:

// "Price" is the .Name property of this series
chart1.Series.Add(priceseries);

It works great. Next, I pored through the samples that came with this lovely control for the financial formulas. The sample says to use the moving average formula (just using this as an example, I can't get any of them to work), the code is this:

chart1.DataManipulator.FinancialFormula(FinancialFormula.MovingAverage,"5","Input","Simple");

Where "5" is the period to use, so that can be whatever I want. "Input" is the data source series it seems, and "Simple" is the output series. To spin this into a way that works with my code, I created a button and did this:

    private void button1_Click(object sender, EventArgs e)
    {
        chart1.DataManipulator.FinancialFormula(FinancialFormula.MovingAverage, "5", "Price", "SMA");     
    }

Nice and simple, ya? Well, as soon as I click that button, my chart control displays a big, red, unhappy X and NOT the moving average series (which I named "SMA") on the chart in any way, shape, or form.

Adding to the above code, I have tried pre-creating the "SMA" series, I have tried adding the "SMA" series to the chart after doing the financialformula call, and both end up with the big mean red X. Going through the sample code again, it appears that the single line of code is all that is needed to generate the extra series of data, but I'm stuck! It doesn't help that the red X has no debugging info whatsoever :(

Any ideas on how to get rid of the big X, and have it display the new series of data instead?

Update: Just as a test, I removed the bit about adding the series to Chart1, then I added the chart1.DataManipulator bit right after it. As expected, the initial series that has all my initial data doesn't appear (because I removed the part adding it to the chart), but when the next line of code does the formula application - no big red X. There is no data shown, but no error code either - so I guess that is an improvement? This is leading me to believe that there is some sort of problem with either the initial dataset I'm applying the formula to, or something having to do with the view/boundaries of the chart control itself. If I find anything more on that track, I will post it as a second update.

A: 

The big red X is the control that encountered a problem in running/interpreting the formula and gave up the ghost. Perhaps the formula was too complex and wieldy for the Chart control to handle it...Maybe, there's a more up-to-date version of the code that has lifted the restriction. There's bound to be something on codeplex.com for this.

This can happen equally at design time also, when designing a form, and a control is dragged on to it, a few properties set here and there and to get a big red X, this was a common occurrence when a control misbehaves at design time also.

Hope this helps, Best regards, Tom.

tommieb75
+1  A: 

I discovered the source of my problem:

priceseries.IsXValueIndexed = true;

I did this initially to omit any blank data points (which would be the weekends in a stock price scenario). When I changed the value to false, all of a sudden the application of the formula worked fine. The downside is that I have blank spots for weekend dates where there is no data! But that is another issue I can work through on my own.

For anyone else having this issue, see this reference article for additional information: http://msdn.microsoft.com/en-us/library/dd456699(VS.100).aspx

The note about grouping your data first is no joke. Do not omit any data points!

Bill Sambrone
A: 

Happened to me too.

While using FinancialFormulas (FinancialFormula.ExponentialMovingAverage in particular) series ChartType should be set to SeriesChartType.Line.

In my code it was set to SeriesChartType.FastLine and this was throwing OverflowException from System.Drawing.dll

Infinum