Changing the Color of the Specified Category Name in the Chart by JS

  • Last update:June 19, 2025
  • Overview

    Problem

    Sometimes, during chart display, you need to highlight the impacts of certain categories on values by displaying the category names in a special way.

    For example, when displaying the monthly sales data, you can highlight the category names for weekends in red. The following figure shows the effect.

    Implementation Method

    You can customize the format of category axis labels by JS.

    Example

    Report Design

    1. Create a general report and create a built-in dataset named Embedded1, as shown in the following figure.

    2. Insert a line chart into a cell and bind the chart data, as shown in the following figure.

    3. Select the line chart, customize the format of axis labels under Style > Axis > X Axis, and select Use HTML to Parse Text Content, as shown in the following figure.

    The JS code is as follows:

    function(){
        var date = new Date(this);
        if(date.getDay()==0||date.getDay()==6){
            return "<font color='red'>"+date.getDate()+"</font>"
        }else{
            return date.getDate();
        }
    }
    iconNote:
    If NaN is displayed when you preview the report on an iOS mobile device, the problem may be caused by the format of the time. You can replace var date = new Date(this); in the code with var date= new Date(Date.parse(this.replace(/-/g,  "/")));.  

    Effect Display

    PC

    Save the report and click Pagination Preview. The following figure shows the preview effect.

    Mobile Terminal

    The report can be previewed on both the DataAnalyst app and the HTML5 terminal. The following figure shows the effect.

    Template Download

    Attachment List


    Theme: Chart
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy