Back to column formats
Column formattingSharePoint Online

Number Battery

This sample changes the appearance of the values in number columns (percent) to look like a battery.

Number Battery formatting preview
Sample preview · View original image

Copy. Apply. Make it yours.

Get this format

3 definitions

Review the requirements below, then paste this JSON into the matching formatting panel. Show me where.

Use your list's internal column names and replace any tenant-specific URLs or flow IDs. Formatting is presentation, not a way to secure data.

Column formatting
Download JSON
{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "style": {
    "display": "flex",
    "flex-direction": "row",
    "margin": "5px",
    "align-items": "center"
  },
  "children": [
    {
      "elmType": "div",
      "style": {
        "position": "relative",
        "display": "flex",
        "align-items": "center",
        "width": "100px",
        "height": "30px",
        "border": "2px solid",
        "border-radius": "5px",
        "padding": "2px"
      },
      "attributes": {
        "class": "ms-fontColor-neutralPrimary"
      },
      "children": [
        {
          "elmType": "div",
          "style": {
            "width": "@currentField.displayValue",
            "height": "100%"
          },
          "attributes": {
            "class": "=if(@currentField > 0.5 , 'ms-bgColor-tealLight' , if(@currentField > 0.2 , 'ms-bgColor-yellow' , 'ms-bgColor-red') )"
          }
        },
        {
          "elmType": "div",
          "txtContent": "@currentField.displayValue",
          "style": {
            "position": "absolute",
            "width": "100%",
            "text-align": "center",
            "font-weight": "bold",
            "font-size": "16px"
          },
          "attributes": {
            "class": "ms-fontColor-neutralPrimary"
          }
        }
      ]
    },
    {
      "elmType": "div",
      "style": {
        "width": "5px",
        "height": "7px",
        "border-radius": "0 2px 2px 0"
      },
      "attributes": {
        "class": "ms-bgColor-neutralPrimary"
      }
    }
  ]
}

From the contributor

How to use this sample

Summary

This sample changes the appearance of the values in number columns (percent) to look like a battery.

  • number-battery.json displays a battery of colors for the site's theme color. The background color will not change depending on the value.
  • number-battery-normal-color.json and number-battery-unicorn-color.json are independent of the site theme color. Also, the background color will change depending on the value: greater than 50%, greater than 20%, and greater than 0%.

screenshot of the sample

View requirements

This format can be applied to a Number column. It is expected that the values will be from 0 to 1 (percent).

Sample

Solution Author(s)
number-battery.json Tetsuya Kawahara
number-battery-normal-color.json Tetsuya Kawahara
number-battery-unicorn-color.json Tetsuya Kawahara

Version history

Version Date Comments
1.0 May 2, 2021 Initial release
1.1 June 23, 2021 Added 2 samples

Disclaimer

THIS CODE IS PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.