Sorting problem with RadCartesianChart with live updates

1 Answer 54 Views
Chart
Giuliano
Top achievements
Rank 1
Iron
Giuliano asked on 05 Sep 2024, 05:09 PM | edited on 05 Sep 2024, 06:49 PM

I'm working with the RadCartesianChart and using a BarSeries. In the series I have values that change, items that ger removed, and items that get added. I'd like for them to be sorted. I think I am implementing it correctly but could be mistaken. When the chart updates it displays values that are not sorted

 

I have attached a demo project to this post. I am using version 2024.2.514. Please let me know if there is anything I need to change or if I'm doing something incorrect.

 

Thank You,

Giuliano

1 Answer, 1 is accepted

Sort by
1
Accepted
Martin Ivanov
Telerik team
answered on 09 Sep 2024, 12:04 PM

Hello Giuliano,

This doesn't work as expected, because the categories in the chart's CategoricalAxis won't be automatically re-ordered on collection changed based on the sort description. The categories are ordered by their order of entering the plot area of the chart.

To achieve your requirement, you can clear the _datapoints collection and then re-populate it again with the updated data. This way the categories will be recreated and the sorting will be the expected one. Here is one way to do this in your UpdateValues() method:

  private void UpdateValues(object source, ElapsedEventArgs e)
  {
      var data = new List<ChartModel>();

      for (int i = 0; i < 50; i++)
      {
          data.Add(new ChartModel()
          {
              Value       = _random.Next(0, 100),
              Category    = _random.Next(0, 100),
          });
      }

      Application.Current.Dispatcher.Invoke(() =>
      {
          var datapointsCopy = _datapoints.ToList();
          datapointsCopy.RemoveAll(item => !data.Any(datapoint => datapoint.Category == item.Category));
          

          foreach (var datapoint in data)
          {
              var update = datapointsCopy.FirstOrDefault(x => x.Category == datapoint.Category);

              if (update is null)
              {
                  datapointsCopy.Add(new ChartModel()
                  {
                      Value       = datapoint.Value,
                      Category    = datapoint.Category
                  });
              }
              else
              {
                  update.Value    = datapoint.Value;
                  update.Category = datapoint.Category;
              }
          }

          _datapoints.Clear();
          foreach (var datapoint in datapointsCopy)
          {
              _datapoints.Add(datapoint);
          }
      });
  }

Can you try this and let me know if it helps?

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Chart
Asked by
Giuliano
Top achievements
Rank 1
Iron
Answers by
Martin Ivanov
Telerik team
Share this question
or