2

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.

Dale K
  • 25,246
  • 15
  • 42
  • 71
Mateus Arruda
  • 305
  • 4
  • 14
  • 1
    Why not simply Clear the series.Points before adding a new set of points? Also: 'Not working' is a not a helpful problem description!! – TaW Dec 16 '18 at 21:55
  • At `chart1.Series.Clear();` I clear the points and at the end of the AddPlayerForm_Load method I add a new set of points. By 'not working' I mean that the chart doesn't update. I stays the same, but is not showing any errors. – Mateus Arruda Dec 16 '18 at 22:05
  • 1
    _chart1.Series.Clear();_ This actually doesn't just delete the points but deletes all series with all their setup. If no changes show most likely your data do not really change (enough). – TaW Dec 16 '18 at 23:01
  • It worked! @TaW if you want to make a answer I will mark it as best answer since it helped me to solve the issue. Thank you. – Mateus Arruda Dec 17 '18 at 15:42
  • I really can't because all I did was point you to a slightly inefficient method. The real error must have beed somewhere else, probably in not setting up the data correctly the 2nd+ time around. - As it stands I doubt that this post will be useful in the future. – TaW Dec 17 '18 at 15:46

2 Answers2

1

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.

Mateus Arruda
  • 305
  • 4
  • 14
0

I never worked with the Winforms-Charttool but have you already tried just to call refresh on the chart1-Object? https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datavisualization.charting.chart.refresh?view=netframework-4.7.2

  • You basically __never need to call refresh__ on chart in order to show changed values. – TaW Dec 17 '18 at 15:44
  • With this method the chart started to blink even after I increase the Time Tick interval. So I prefered use the Series.Points.Clear(). But it was also useful, thanks! – Mateus Arruda Dec 17 '18 at 15:44