Draggable Select Range Plugin | Chart.jsDraggable Select Range Plugin | Chart.js
Get Started
Introduction
Get Started
Introduction
  • Introduction

    • Installation
    • Usage
    • Configuration
    • Apis
    • Event
    • Example & Demo

Introduction

This is a ChartJS selection range plugin for line charts. Selects a range of the chart from the X-axis. You can visually select the desired range on the chart using the mouse and the draggable feature.

It is also possible to display it on the chart by selecting the desired interval with numerical data and using the method available in the plugin. This is a ChartJS selection range plugin for line charts. Selects a range of the chart from the X-axis. You can visually select the desired range on the chart using the mouse and the draggable feature.

Installation

Run the following command in the path of the project folder

npm install chartjs-plugin-draggable-selectrange

OR Use cdn:

https://cdn.jsdelivr.net/gh/mohamadhpp/chartjs-plugin-draggable-select-range@master/src/index.min.js

Usage

Import the plugin on the desired page using the line below.

import draggableSelectRangePlugin from 'chartjs-plugin-draggable-selectrange';

Then you can register the plugin in Chart.js using the following code.

Chart.register(draggableSelectRangePlugin);

Then we use it in the codes as follows.

plugins:
{
    draggableSelectRange:
    {
        enable: true,
            
        text:
        {
            enable: true,
            
            offset: -15,
            padding: 1
        },

        onSelectComplete: (event) =>
        {
            // Print start & end data range
            console.log(event.range[0])
            console.log(event.range[1])
        }
    }
}

Configuration

The properties of the plugin are as follows:

optiontypedefaultdescription
enableboolfalseSpecifies the current state of the plugin
unselectColorstringrgba(255,255,255,0.65)Using this you can choose a specific color for the parts that are not selected so that only the selected part is visible
borderColorstring#2388FFChoose a color for the line separating the selected and unselected part
borderWidthnumber2For the dividing line between selected and unselected parts, we select Width based on pixels
startDataIndexobjectundefinedThis is an object that represents the starting index values ​​of the data in the selected range. It also includes other values ​​related to the index
endDataIndexobjectundefinedThis is an object that shows the end index values ​​of the data in the selected range. It also includes other values ​​related to the index
textobject---Contains data related to the text of the selected interval on the separator line
text.enablebooltrueIf this valu e is true, the text will be displayed
text.colorstring#000Specifies the color of the text
text.offsetnumber0The distance of the text is from right and left
text.paddingnumber0The padding of the text is from the right and left
text.fontobject---You can change the font and size of the text
text.font.familystringArialIts value includes the font family of the text
text.font.sizenumber13The value includes the text size

Apis

The plugin methods are as follows:

methodparamdescription
clearDrawchartUsing this method, you can clear the selected range on the chart. The input value of this method is chart, which is the same as the chart element.
setRangechart
options
range
With this method, the desired range can be selected on the chart. The chart value of the created chart element, the options of the plugin configuration and the range value of a list containing 2 elements, the first element of the starting point data and the second element of the end point data.

Event

In this plugin, there is only one event, and it is for the time of choosing the desired interval.

optionparamdescription
onSelecteventThis event is for choosing the interval on the chart. The event parameter has 2 values ​​boundingBox and range. The boundingBox value contains the information of the coordinates of the selected range and the range value contains the selected data range whose length is 2. The value of range[0] is equal to the data of the start range and the value of range[1] is equal to the data of the end range.

Example & Demo

Demo

In the example folder, there is a example to explain the plugin. The demo version is also available online in the link below.

Show online demo: Online Demo