Search code examples
c#winformsvisual-studio-2010chartsasp.net-charts

Chart control: Design messed Up after clearing and re-adding Y-Values


Been struggling with this through out the day. I have three series on a chart that look like this. NOTE: I am using the winforms control.

The values are being added based on calculations from input. I am using this code to add the values to each series.

        this.chart1.Series["green"].Points.AddY(greenvalue);
        this.chart1.Series["totalsaving"].Points.AddY(totalsavingvalue);
        this.chart1.Series["blue"].Points.AddY(bluevalue);

The series properties I have set like this. Green and totalsaving are both set to StackedColumn, blue is set to Column for chart type.

enter image description here

I then have a button to start over which brings the user back to the input area and I am then using this code to clear the series values on the start over click.

        chart1.Series["totalsaving"].Points.Clear();
        chart1.Series["green"].Points.Clear();
        chart1.Series["blue"].Points.Clear();

The same calculation click is being used as above to calculate and populate the series data. The problem is when I click the calculate button to calculate the values after I have cleared them, the total savings, and the green are missing. Just the blue is shown.

Am I doing something wrong with the way I am trying to clear the values so I can re calculate?


Solution

  • OK, from the edits, comments, our chat and the joim.me session enough data has accumulated to answer the question.

    • You have twisted the display by adding an extra data point to the blue series in the designer.
    • This point occupies slot 1 but remains invisible as its value = 0.
    • This pushes the next point in the series to slot 2
    • After clearing the points it is gone and the display doesn't work anymore.
    • The disappearing of the two columns probably was caused by hard coded widths.

    You have several paths you can follow:

    • recreating the extra point with value = 0 before adding the real data (not recommended)

    • not adding the extra point in the first place but forcing each point into its slot by using Points.AddXY instead of Points.AddY with X being the slot.

    • not clearing the points but updating their values by using the SetValueY method. After all three data points have beend assigned their new values you need to call chart1.Invalidate() to make it show.

    Fore easiest styling of all those properties, some of which are deeply burried inside of property strings(!), you may even decide to add and style&polish all three points in the designer and only update their y-values like this:

       chart1.Series["green"].SetValueY(greenvalue);
       chart1.Series["totalsaving"].SetValueY(totalsavingvalue);
       chart1.Series["blue"].SetValueY(bluevalue);
       chart1.Invalidate();
    

    The choice is yours, but in any case I recommend setting the proper X values, be it in code or in the desginer..