Displaying Persian/Jalali dates in react apexchart

1.1k Views Asked by At

Could you guide me on how to make a "Datetime X-Axis" chart with react Apexchart.js when my dates are in Persian? at last, I should make a "brushChart" with Datetime X-Axis, if you have a similar project that can help me please send link of them

I had these codes below and I tried "defaultLocale" and "name" with both "Per" and "Fa" but none of them worked and getting a error and the app crashed.

chart: {
  id: 'reactchart-example',
  defaultLocale: 'Fa',
  locales: [
    {
      name: 'per',
      options: {
        months: [
          'دی',
          'بهمن',
          'اسفند',
          'فروردین',
          'اردیبهشت',
          'خرداد',
          'تیر',
          'مرداد',
          'شهریور',
          'مهر',
          'آبان',
          'آذر',
        ],
        shortMonths: ['دی', 'بهم', 'اسف', 'فرو', 'ارد', 'خرد', 'تیر', 'مرد', 'شهر', 'مهر', 'آبا', 'آذر'],
        days: ['یکشنبه', 'دوشنبه', 'سه شنبه', 'چهارشنبه', 'پنج شنبه', 'جمعه', 'شنبه'],
        shortDays: ['یک', 'دو', 'سه', 'چهار', 'پنج', 'جمع', 'شنب'],
        toolbar: {
          download: 'دانلود SVG',
          selection: 'انتخاب',
          selectionZoom: 'انتخاب بزرگنمایی',
          zoomIn: 'بزرگنمایی',
          zoomOut: 'کوچک نمایی',
          pan: 'جابجایی',
          reset: 'بازگرداندن',
        },
      },
    },
  ],
},

thanks for your help

2

There are 2 best solutions below

6
sima ghoreyshi On BEST ANSWER

you don't need to add the whole config in your chart object, there is a json file for every locale in this address in your node_modules folder: "apexcharts/dist/locales/"

all you need is to import your desired locale json file in your code like below

import fa from "apexcharts/dist/locales/fa.json"

and then simply set "locales" array and "defaultLocale" in the object "chart" like below:

chart: {
  //The following two lines are the key answer
  locales: [fa], //or multi language like [fa, en]
  defaultLocale: 'fa',
  
  //your other configs ...
},
0
amir jj On

it's possible to use fa.json content in the <script></script> tag simply also, if don't use any frontend frameworks also like:

<script>
var farsi_local = {
          "name": "fa",
          "options": {
            "months": [
              "فروردین",
              "اردیبهشت",
              "خرداد",
              "تیر",
              "مرداد",
              "شهریور",
              "مهر",
              "آبان",
              "آذر",
              "دی",
              "بهمن",
              "اسفند"
            ],
            "shortMonths": [
              "فرو",
              "ارد",
              "خرد",
              "تیر",
              "مرد",
              "شهر",
              "مهر",
              "آبا",
              "آذر",
              "دی",
              "بهمـ",
              "اسفـ"
            ],
            "days": [
              "یکشنبه",
              "دوشنبه",
              "سه شنبه",
              "چهارشنبه",
              "پنجشنبه",
              "جمعه",
              "شنبه"
            ],
            "shortDays": ["ی", "د", "س", "چ", "پ", "ج", "ش"],
            "toolbar": {
              "exportToSVG": "دانلود SVG",
              "exportToPNG": "دانلود PNG",
              "exportToCSV": "دانلود CSV",
              "menu": "منو",
              "selection": "انتخاب",
              "selectionZoom": "بزرگنمایی انتخابی",
              "zoomIn": "بزرگنمایی",
              "zoomOut": "کوچکنمایی",
              "pan": "پیمایش",
              "reset": "بازنشانی بزرگنمایی"
            }
          }
        };
</script>

then use it in the chart itself:

chart: {
      locales: [farsi_local],
      defaultLocale: 'fa',
.
.
.