0.2.0 • Published 3 years ago

chartjs-plugin-dragsegment v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

chartjs-plugin-dragsegment

A plugin for Chart.js

Add ability to drag segments (line chartsjs).

Drag segments plugin animation

Demo

Installation

Use npm to install plugin:

npm install chartjs-plugin-dragsegment

Register plugin in Chartjs:

import Chart from 'chart.js';
import ChartJSdragSegment from 'chartjs-plugin-dragsegment';
// ...
Chart.pluginService.register(ChartJSdragSegment);
// ...

Or include it in your html (Chart should be available on window):

<script src="node_modules/chartjs-plugin-dragsegment/dist/chartjs-plugin-dragsegment.min.js"></script>

Usage

Important options in config when you create new chart:

const ctx = getChartContext();
const chartConfig = {
  // ...
  type: 'line',
  // ...
  options: {
    // ...
    dragSegment: true,
    // ...
  }
};

new Chart(ctx, chartConfig);

If dragSegment is Object it contain plugin options.

new Chart(ctx, {
  type: 'line',
  options: {
    // ...
    dragSegment: {
      // allow to drag segments verticaly (default: true)
      vertical: true,

      // allow to drag segments horizontaly (default: false)
      horizontal: false,

      // onDrag will be executed before coordinates updating
      // @chart - ChartJS instance
      // @points - Object , of points {x, y} for each dataset, witch will update their coordinates
      //   points = {
      //     datasetIndex: {
      //       elementIndex: {
      //         x // optional, not present if not modified
      //         y // optional, not present if not modified
      //       }
      //     }
      //   }
      //   You can set new values (add, remove, ...) for points
      onDrag(chart, points) {
        if (Math.random() < 0.5) {
          return false;
        }
        return true;
      }
    }
    // ...
  }
);
0.2.0

3 years ago

0.1.0

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago