Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Data Not Displaying in Correct Order #2275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
moha-b opened this issue Feb 7, 2025 · 2 comments
Open

Data Not Displaying in Correct Order #2275

moha-b opened this issue Feb 7, 2025 · 2 comments
Labels
charts Charts component open Open

Comments

@moha-b
Copy link

moha-b commented Feb 7, 2025

Bug description

I'm using SfCartesianChart in my Flutter app to display monthly sales and cost data. The data is correctly sorted based on the month order before being passed to the chart, but the chart does not display the data correctly.

Even though debugging logs confirm that the data is sorted correctly before rendering, the chart does not reflect the correct order in the X-axis.

What I Have Tried:

✅ Sorting Data Properly:

  • I implemented a reusable sortByMonth function that sorts the months based on English order and then converts them back to Arabic months before displaying them.
  • Debugging prints confirm that the data is correctly sorted before rendering.
// English month order mapping
final Map<String, int> monthOrder = {
  'January': 1,
  'February': 2,
  'March': 3,
...
};

// Arabic month conversion mapping
final Map<int, String> arabicMonths = {
  1: 'يناير',
  2: 'فبراير',
  3: 'مارس',
...
};

List<DataModel> sortByMonth(List<DataModel> data) {
  // Sort data based on English month order
  data.sort(
      (a, b) => (monthOrder[a.month] ?? 0).compareTo(monthOrder[b.month] ?? 0));

  // Convert back to Arabic months
  return data.map((e) {
    return DataModel(
      month: arabicMonths[monthOrder[e.month] ?? 0] ?? e.month,
      value: e.value,
      label: e.label,
      year: e.year,
    );
  }).toList();
}

✅ Ensuring Sorted Data is Used:

  • I assigned the sorted data to the dataSource of SfCartesianChart.

✅ Disabling Internal Chart Sorting:

  • I explicitly set sortingOrder: SortingOrder.none.

✅ Checked State Updates:

  • The data is correctly set using setState() in a StatefulWidget.

✅ Debugging Output Confirms Correct Order:

print("Sorted Direct Costs Data: ${sortedDirectCostsData.map((e) => e.month).toList()}");
print("Sorted Sales Data: ${sortedSalesData.map((e) => e.month).toList()}");

Output confirms data is in the right order before being passed to the chart, yet the chart still displays incorrect order.

Expected Behavior:

The X-axis should display months in the correct order (Arabic months) after sorting.

Actual Behavior:

The X-axis displays months in random or incorrect order, despite sorting being correct before rendering.

Flutter & Syncfusion Versions:

Flutter: 3.24.5
syncfusion_flutter_charts: ^27.1.53
Dart: 3.5.4

Questions:

  • Why is the Syncfusion chart ignoring the sorted order of months?

  • Is there an issue with CategoryAxis forcing its order?

  • How can I ensure that Arabic month labels appear in the correct order?

Steps to reproduce

  1. make a dummy list of this object
class DataModel {
  final String label;
  final double value;
  final String month;
  final int year;

  DataModel({
    required this.label,
    required this.value,
    required this.month,
    required this.year,
  });
  1. fill it with random data
  2. sort it based on months u can use the function in the description sortByMonth function
  3. pass the data to the charts in the Code sample section

Code sample

Chart 1
SfCartesianChart(
  primaryXAxis: CategoryAxis(),
  series: <CartesianSeries>[
    ColumnSeries<DataModel, String>(
      sortingOrder: SortingOrder.none, // Disabled internal sorting
      dataSource: sortedDirectCostsData, // Already sorted before passing
      xValueMapper: (data, _) => data.month, // Arabic month
      yValueMapper: (data, _) => data.value,
      name: 'التكاليف المباشرة',
    ),
    ColumnSeries<DataModel, String>(
      sortingOrder: SortingOrder.none,
      dataSource: sortedSalesData,
      xValueMapper: (data, _) => data.month,
      yValueMapper: (data, _) => data.value,
      name: 'المبيعات',
    ),
  ],
);
Chart 2
SfCartesianChart(
                  primaryXAxis: CategoryAxis(
                    majorGridLines: MajorGridLines(width: 0),
                    labelStyle: TextStyle(color: Colors.grey, fontSize: 12),
                  ),
                  primaryYAxis: NumericAxis(
                    minimum: 0,
                    maximum: 18000,
                    interval: 3000,
                    labelFormat: '{value}',
                    axisLine: AxisLine(width: 0),
                    majorTickLines: MajorTickLines(size: 0),
                  ),
                  tooltipBehavior: TooltipBehavior(enable: true),
                  series: <LineSeries<DataModel, String>>[
                    LineSeries<DataModel, String>(
                      dataSource: sortedDailyExpenses,
                      xValueMapper: (DataModel expense, _) => expense.month,
                      yValueMapper: (DataModel expense, _) => expense.value,
                      enableTooltip: true,
                      selectionBehavior: SelectionBehavior(enable: true),
                      markerSettings: MarkerSettings(
                        isVisible: true,
                        shape: DataMarkerType.circle,
                        borderColor: Colors.blue,
                        borderWidth: 2,
                      ),
                      color: Colors.blue,
                      dataLabelSettings: DataLabelSettings(
                        isVisible: true,
                        textStyle: TextStyle(
                          color: Colors.blue,
                          fontSize: 12,
                        ),
                      ),
                    ),
                  ],
                )

Screenshots or Video

Chart 1

Image

Chart 2

Image

Stack Traces

Stack Traces

i will write the month number for more declaration

I/flutter ( 7403): After Sorting Daily Expenses: [يناير, أبريل, نوفمبر] => [11,4,1] 
I/flutter ( 7403): Sorted Direct Costs Data: [يناير, نوفمبر] => [4,1]
I/flutter ( 7403): Sorted Sales Data: [يناير, أبريل, نوفمبر] => [11,4,1]

month sorting is correct and the data assigned correctly

On which target platforms have you observed this bug?

Android

Flutter Doctor output

Doctor output
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.24.5, on Microsoft Windows [Version 10.0.26100.2894], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 35.0.0-rc3)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.12.4)
[√] Android Studio (version 2024.2)
[√] Android Studio (version 2023.3)
[√] IntelliJ IDEA Ultimate Edition (version 2024.2)
[√] Connected device (4 available)
[√] Network resources
@VijayakumarMariappan VijayakumarMariappan added charts Charts component open Open labels Feb 11, 2025
@PreethikaSelvam
Copy link
Contributor

Hi @moha-b,

We have analyzed your query and found that the sorting works correctly when using English month names. However, after replacing them with Arabic month strings, the chart no longer maintains the intended chronological order. To resolve this, we implemented a sorting mechanism by alphabetically sorting the keys of rawSeries1 and rawSeries2.

This ensures that the data is displayed in the correct order for chart visualization. We then mapped the sorted keys to the respective data points. For the Arabic version, we translated the English month names using the isArabic flag. This approach ensures that the months are displayed chronologically from January to December, regardless of the language setting. We have shared a code snippet and a sample for your reference.

Code Snippet

final bool isArabic;

  @override
  Widget build(BuildContext context) {
    const arabicMonths = {
      'January': 'يناير',
      'February': 'فبراير',
      'March': 'مارس',
      'April': 'أبريل',
      'May': 'مايو',
      'June': 'يونيو',
      'July': 'يوليو',
      'August': 'أغسطس',
      'September': 'سبتمبر',
      'October': 'أكتوبر',
      'November': 'نوفمبر',
      'December': 'ديسمبر',
    };

    final Map<String, double> rawSeries1 = {
      'January': 50,
      'February': 80,
      'March': 60,
      'April': 90,
      'May': 85,
      'June': 70,
      'July': 75,
      'August': 65,
      'September': 55,
      'October': 45,
      'November': 78,
      'December': 95,
    };

    final Map<String, double> rawSeries2 = {
      'January': 70,
      'February': 65,
      'March': 75,
      'April': 55,
      'May': 60,
      'June': 55,
      'July': 68,
      'August': 72,
      'September': 64,
      'October': 58,
      'November': 81,
      'December': 88,
    };

    // Alphabetical sorting
    final sortedKeys = rawSeries1.keys.toList()..sort();

    final List<ChartData> series1Data = sortedKeys
        .map((month) => ChartData(
              isArabic ? arabicMonths[month]! : month,
              rawSeries1[month]!,
            ))
        .toList();

    final List<ChartData> series2Data = sortedKeys
        .map((month) => ChartData(
              isArabic ? arabicMonths[month]! : month,
              rawSeries2[month]!,
            ))
        .toList();

    return SfCartesianChart(
      tooltipBehavior: TooltipBehavior(enable: true),
      title: ChartTitle(
          text: isArabic
              ? 'المبيعات الشهرية (مرتب أبجدياً)'
              : 'Monthly Sales (Alphabetical)'),
      legend: const Legend(isVisible: true),
      primaryXAxis: CategoryAxis(
        title: AxisTitle(text: isArabic ? 'الشهر' : 'Month'),
      ),
      series: <CartesianSeries>[
        ColumnSeries<ChartData, String>(
          sortingOrder: SortingOrder.none,
          name: isArabic ? 'المنتج أ' : 'Product A',
          dataSource: series1Data,
          xValueMapper: (data, _) => data.x,
          yValueMapper: (data, _) => data.y,
        ),
        ColumnSeries<ChartData, String>(
          sortingOrder: SortingOrder.none,
          name: isArabic ? 'المنتج ب' : 'Product B',
          dataSource: series2Data,
          xValueMapper: (data, _) => data.x,
          yValueMapper: (data, _) => data.y,
        ),
      ],
    );
  }
}

class ChartData {
  ChartData(this.x, this.y);
  final String x;
  final double y;
}

Output:

Image

If you still face the issue, we kindly request you to try to replicate the reported issue in the below attached test sample, so that we can better assist you.

Note:
If you set the SortingOrder to ascending or descending, the chart will again sort based on the string values of the X-axis. This causes the labels to be sorted alphabetically in both English and Arabic, which disrupts the intended chronological order.
For example, with the X-values January, February and March:

In English, the months are sorted alphabetically by their first letter:
• January (J) → 10th in the alphabet
• February (F) → 6th in the alphabet
• March (M) → 13th in the alphabet

This leads to the following order:
• Ascending: February → January → March
• Descending: March → January → February

In Arabic, the months are sorted based on their first letter too. Here's the breakdown:
• يناير (January) starts with ي (Ya) → 28th letter in Arabic alphabet.
• فبراير (February) starts with ف (Fa) → 20th letter in Arabic alphabet.
• مارس (March) starts with م (Meem) → 24th letter in Arabic alphabet.

This leads to the following order:
• Ascending: فبراير (Feb) → مارس (Mar) → يناير (Jan)
• Descending: يناير (Jan) → مارس (Mar) → فبراير (Feb)

Regarding multiple series:
If the first series contains only January and February, and the second series contain January, February, and March, then the sorting is based only on the first series. The first series decides the axis rendering. This leads to the following order:
• Ascending: February → January → March
• Descending: January → February → March

<style> </style>
Month (EN - AR) English alphabets order number Arabic Alphabet Order Multiple Series (X-values) Single Series (X-values)
January - يناير February - فبراير March - مارس J - 10 F - 6 M - 13 ي - 28 ف - 20 م - 24 Series 1: January, February Series 2: January, February, March January, February, March
Sorting Order: None January February March January February March January → February → March Image January → February → March Image
Ascending February January March February March January February → January → March Image February → March → January Image
Descending March January February January March February March → January → February Image January → March → February Image

 

If this is not your case, we kindly request you to share with us more information on what you’re proposing as your requirement and usage along with the screenshot and complete sample.

Please check and get back to us if you require further assistance.

Regards,
Preethika Selvam.

gh2275 1.zip

@Saravanan-Madhesh Saravanan-Madhesh added waiting for customer response Cannot make further progress until the customer responds. and removed open Open labels Apr 28, 2025
@moha-b
Copy link
Author

moha-b commented Apr 29, 2025

Could you please add the Arabic or English months for sorting?


In your code sample, u showed the English months sorted alphabetically, then translated to Arabic. Be aware that the Arabic alphabet is different from the English one, so the Arabic alphabetical sorting is wrong.

But this is not our concern, I don't look for alphabetical sorting, I pass to the chart a sorted list, why is it sorting it based on the String?

so if I passed to it a list of integars does it sort it from 1 to 100 by default ?

@Saravanan-Madhesh Saravanan-Madhesh added open Open and removed waiting for customer response Cannot make further progress until the customer responds. labels May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
charts Charts component open Open
Projects
None yet
Development

No branches or pull requests

4 participants