Search code examples
c#visual-studiowinformschartsradar-chart

Update Radar Chart in WinForms


I have the following UI where the values in the Radar Chart is calculated by the arithmetic mean of some of the TrackBar values. A 'Player' object takes those values, calculates the mean and returns it back to the Radar Chart data series.

What I want to do is when I change the value in the TrackBar the Chart change its values in real time. When I change the values, it recalculates the mean and then change the Charts shape.

enter image description here

Here is my code:

public AddPlayerForm()
    {
        InitializeComponent();
        Load += this.AddPlayerForm_Load;//reloads the chart component every continuously
        LoadComboBoxes();
        MetroFramework.Controls.MetroTrackBar[] trackBars = new MetroFramework.Controls.MetroTrackBar[20];
    }

    //loads the Chart Content
    private void AddPlayerForm_Load(object sender, EventArgs e)
    {
        /*I tried to clear the current chart to recalculate and redraw the chart in every cycle
        but its not working this way*/
        chart1.Series.Clear();//clear the current chart
        new_player.SetPlayerStats(  //get values from TrackBars
         mtTrackBarAttack.Value,
         mtTrackBarBallControl.Value,
         .
         .//all trackBar.Values
         .
         mtInjuryResistenceTB.Value,
         1,
         "Messi");

        //recalculate the arithimetic mean and returns as a tag to the chart
        //Ex: {"Agility",60.4}
        Dictionary<string, float> tags = new_player.MeanStats();//Player method that calculates the mean and returns a dictionary <String, float>
        //creates a new series of data
        chart1.Series.Add("s1");
        chart1.Series["s1"].ChartType = SeriesChartType.Radar;


        foreach (string tagname in tags.Keys)
        {
            //for each set of data, plots the name and its value in the Chart
            chart1.Series["s1"].Points.AddXY(tagname, tags[tagname]);

        }


    }

I'm trying to clear the old chart and redraw a new chart but is not working this way.


Solution

  • I managed to update the chart using two methods. First thanks to @TaW I used the chart.Series.Points.Clear() to redraw the data. Then, to update it automatically I had to use a Time tick counter to update the chart every 100 miliseconds thanks to @Tim in Add timer to a Windows Forms application.

    After the chart successfully updating, I tried to use the chart.Refresh() function mentioned by @ChrizzleWhizzle but the chart started to blink sometimes, so I prefered to use the first solution, but it was also helpful.

    Thank you for everyone's answer. If I get to know a better method I will post it.