7.7.0 • Published 2 years ago

r-chart v7.7.0

Weekly downloads
247
License
ISC
Repository
github
Last release
2 years ago

r-chart (react editable line chart and bar chart)

alt text alt text

description

  • developed by reactjs.
  • create line chart , bar chart and both in one container.
  • editable points By drag or popup.
  • add point on chart abilty.
  • remove point on chart ability.
  • high performance.
  • mobile support(support touch events).
  • responsive.
  • customizable style.
  • zoomable.
  • up to 500000 point support.
  • multi select points

Instalation

npm i r-chart

Usage

import react from "react";
import Chart from "r-chart";
<Chart />

end of preview

Code:
import React,{Component,Fragment} from "react";
import Chart from './r-chart'
import './style.css';
export default class App extends Component{
    state = {
        targets:[
            {date:'January',amount:10,size:8},
            {date:'February',amount:30},
            {date:'March',amount:20},
            {date:'May',amount:15},
            {date:'July',amount:50},
            {date:'August',amount:55},
            {date:'September',amount:40},
            {date:'October',amount:20},
            {date:'November',amount:40},
            {date:'December',amount:50},
        ],
        sales:[
            {date:'January',amount:0},
            {date:'February',amount:20},
            {date:'March',amount:40},
            {date:'April',amount:40},
            {date:'May',amount:45},
            {date:'June',amount:30},
            {date:'July',amount:10}
        ],
        logs:[]
    }
    change({point,key,value,dataIndex,pointIndex,drag}){
        var {targets,sales,logs} = this.state;
        if(dataIndex === 0){
            targets[pointIndex].amount = value;
            if(!drag){//drag end
                logs.push(`You changed this.state.targets[${pointIndex}].amount to ${value}`);
            }
            this.setState({targets,logs})    
        }
        else if(dataIndex === 1){
            sales[pointIndex].amount = value;
            if(!drag){//drag end
                logs.push(`You changed this.state.saled[${pointIndex}].amount to ${value}`);
            }
            this.setState({sales,logs})   
        }
    }
    add({key,value,dataIndex,pointIndex}){
        var {logs} = this.state;
        var newPoint = {date:key,amount:value};
        if(dataIndex === 0){
            let {targets} = this.state;
            targets.splice(pointIndex,0,newPoint); 
            logs.push(`You added ${JSON.stringify(newPoint)} to this.state.targets`)
            this.setState({targets,logs})
        }
        else if(dataIndex === 1){
            let {sales} = this.state;
            sales.splice(pointIndex,0,{date:key,amount:value}); 
            logs.push(`You added ${JSON.stringify(newPoint)} to this.state.sales`)
            this.setState({sales,logs})
        }
    }
    remove({point,key,value,dataIndex,pointIndex}){
        var {logs} = this.state;
        if(dataIndex === 0){
            let {targets} = this.state;
            targets.splice(pointIndex,1);
            logs.push(`You removed this.state.targets[${pointIndex}]`);
            this.setState({targets,logs});
        }
        else if(dataIndex === 1){
            let {sales} = this.state;
            sales.splice(pointIndex,1);
            logs.push(`You removed this.state.sales[${pointIndex}]`)
            this.setState({sales,logs});
        }
    }
    render(){
        var {targets,sales,logs} = this.state;
        return (
            <Fragment>
                <h5>Try to drag points or click on points to open popup and edit clicked point (remove or edit)</h5>
                <h5>Try to add point where plus is apear near mouse cursor (you can add point in empty places)</h5>
                <Chart
                    data={[ 
                        {
                            type:'line',
                            color:'#0688f3',
                            getPointStyle:(point)=>{return {radius:5,fill:'blue'}},
                            getPointText:(point)=>{return {value:point.date,y:-20}},
                            title:'Data1',areaOpacity:.1,
                            points:targets
                            getKey={({point})=>point.date}
                            getValue={({point})=>point.amount}
                        },
                        {
                            type:'bar',
                            color:'#03ebcc',
                            title:'Data2',
                            points:sales,
                            getKey={({point})=>point.date}
                            getValue={({point})=>point.amount}
                        },
                    ]} 
                    keyAxis={{
                        edit:(text)=>text.slice(0,3),
                        zoom:true,
                        title:'Date'
                    }}
                    valueAxis={{
                        edit:(value)=>value + '%',
                        gridColor:'#ddd',
                        zoom:true,
                        title:'Amount'
                    }}
                    keys={['January','February','March','April','May','June','July','August','September','October','November','December']}
                    onChange={this.change.bind(this)}
                    onAdd={this.add.bind(this)}
                    onRemove={this.remove.bind(this)}
                />
                <h3>Logs</h3>
                <div className='logs'>
                    <ul>
                        {logs.map((log,i)=><li key={i}>{log}</li>)}
                    </ul>
                </div>
            </Fragment> 
        )
    }
}
Preview(Click image and open demo on stackblitz):

alt text

r-chart consists of a set of data, and each data consists of a set of points.
r-chart have 2 axis (key axis and value axis)
key axis is based on chart keys array
value axis is based on points value
root props
ProptypeDefaultDescription
dataArray of objectsRequiredlist of chart data
keysArray of strings or numbersRequiredlist of chart keys
keyAxisobjectRequiredkey axis properties
valueAxisobjectRequiredvalue axis properties
labelSizenumber40set size of horizontal labels
labelRotatenumber0angle of labels on horizontal axis
onChangefunctionOptionalchange points value in chart popup or dragging points.
onAddfunctionOptionalget point details for add
onRemovefunctionOptionalget point details for remove
htmlfunctionOptionaladd custom html on chart(example: add a button on chart)
each data object properties
PropertyTypeDefaultDescription
typestring('line' or 'bar')'line'type of chart data
titlestring'untitle'title of chart data
getKeyfunctionRequiredget key from point object
getValuefunctionRequiredget value from point object
pointsarray of objectsrequiredpoints of chart data
colorstring(color)'#000'color of chart data
dasharray of 2 number(int)Optionaldash style of line of data
lineWidthnumber2line width of line of data
areabooleanfalseshow line chart area
getPointStylefunctionOptionalget point object and returns style of line chart point
getPointTextfunctionOptionalget point object and returns text for render on chart point
editablebooleantrueSpecifies whether chart points can be edited or not
draggablebooleanfalseSpecifies whether chart points can be edited by drag or not
keyAxis properties
ProptypeDefaultDescription
gridColorstring(color)Optionalset grid lines on key axis
titlestringRequired for show in chart popuptitle of key axis
linesArray of objectsOptionalset lines by custom style on key axis
editfunctionOptionalget each key label and return edited it
zoombooleanfalseset key axis zoomable
sizenumber50set thickness of key axis
valueAxis properties
ProptypeDefaultDescription
gridColorstring(color)Optionalset grid lines on value axis
titlestringRequired for show in chart popuptitle of value axis
linesArray of objectsOptionalset lines by custom style on value axis
editfunctionOptionalget each value label and return edited it
zoombooleanfalseset value axis zoomable
sizenumber50set thickness of key value
onChange props

is a function that get changed point details as a parameter. this parameter type is object and has this properties:

  • dataIndex(index of data of changed point)(number)
  • pointIndex(index of changed point)(number)
  • value(value of point)(number)
  • key(key of point)(string or number)
  • point(object of point)(object)
  • drag(if is true mean this point is changed by drag)(boolean)

Line Chart

Code:
<Chart
    data={[
      {
        type:'line',
        title:'data1',
        color:'blue',
        getKey:(point)=>point.date,
        getValue:(point)=>point.value,
        points:[
          {date:'January',value:10},
          {date:'February',value:15},
          {date:'March',value:25},
          {date:'April',value:30},
          {date:'May',value:40},
          {date:'June',value:35},
          {date:'July',value:40},
          {date:'August',value:60},
          {date:'September',value:60},
          {date:'October',value:75},
          {date:'November',value:80},
          {date:'December',value:100}
        ],

      },
      {
        type:'line',
        title:'data2',
        getKey:(point)=>point.date,
        getValue:(point)=>point.value,
        color:'crimson',
        points:[
          {date:'January',value:20},
          {date:'February',value:35},
          {date:'March',value:15},
          {date:'April',value:40},
          {date:'May',value:60},
          {date:'June',value:55},
          {date:'July',value:50},
          {date:'August',value:70},
          {date:'September',value:65},
          {date:'October',value:85},
          {date:'November',value:90},
          {date:'December',value:100}
        ],
      }
    ]}
    keys={[
      'January','February','March','April','May','June','July','August','September','October','November','December'
    ]}
  />
Preview(Click image and open demo on stackblitz):

alt text

Bar Chart

Code:
<Chart
    data={[
      {
        type:'bar',
        title:'data1',
        color:'blue',
        getKey:(point)=>point.date,
        getValue:(point)=>point.value,
        points:[
          {date:'January',value:10},
          {date:'February',value:15},
          {date:'March',value:25},
          {date:'April',value:30},
          {date:'May',value:40},
          {date:'June',value:35},
          {date:'July',value:40},
          {date:'August',value:60},
          {date:'September',value:60},
          {date:'October',value:75},
          {date:'November',value:80},
          {date:'December',value:100}
        ],

      },
      {
        type:'bar',
        title:'data2',
        getKey:(point)=>point.date,
        getValue:(point)=>point.value,
        color:'crimson',
        points:[
          {date:'January',value:20},
          {date:'February',value:35},
          {date:'March',value:15},
          {date:'April',value:40},
          {date:'May',value:60},
          {date:'June',value:55},
          {date:'July',value:50},
          {date:'August',value:70},
          {date:'September',value:65},
          {date:'October',value:85},
          {date:'November',value:90},
          {date:'December',value:100}
        ],
      }
    ]}
    keys={[
      'January','February','March','April','May','June','July','August','September','October','November','December'
    ]}
  />
Preview(Click image and open demo on stackblitz):

alt text

Line Chart And Bar Chart

Code:
<Chart
    data={[
      {
        type:'bar',
        title:'data1',
        color:'blue',
        getKey:(point)=>point.date,
        getValue:(point)=>point.value,
        points:[
          {date:'January',value:10},
          {date:'February',value:15},
          {date:'March',value:25},
          {date:'April',value:30},
          {date:'May',value:40},
          {date:'June',value:35},
          {date:'July',value:40},
          {date:'August',value:60},
          {date:'September',value:60},
          {date:'October',value:75},
          {date:'November',value:80},
          {date:'December',value:100}
        ],

      },
      {
        type:'line',
        title:'data2',
        getKey:(point)=>point.date,
        getValue:(point)=>point.value,
        color:'crimson',
        points:[
          {date:'January',value:20},
          {date:'February',value:35},
          {date:'March',value:15},
          {date:'April',value:40},
          {date:'May',value:60},
          {date:'June',value:55},
          {date:'July',value:50},
          {date:'August',value:70},
          {date:'September',value:65},
          {date:'October',value:85},
          {date:'November',value:90},
          {date:'December',value:100}
        ],
      }
    ]}
    keys={[
      'January','February','March','April','May','June','July','August','September','October','November','December'
    ]}
  />
Preview(Click image and open demo on stackblitz):

alt text

Label Size

Set width of horizontal axis labels by 'labelSize' prop to prevent those to interference .
Code:
<Chart
  ...
  labelSize={90}
  ...
/>
Preview(Click image and open demo on stackblitz):

alt text

Edit Labels

Set 'edit' function in ketAxis props to edit 'key axis' labels.
Set 'edit' function in valueAxis props to edit 'value axis' labels.
Code:
<Chart
  ...
  keyAxis={{
    ...
    edit:(key)=>key.slice(0,3)
    ...
  }}
  valueAxis={{
    ...
    edit:(value)=>value + '%'
    ...
  }}
  ...
/>`
Preview(Click image and open demo on stackblitz):

alt text

Grid Lines

Set 'key_gridColor' to show and set color of grid lines on key axis.
Set 'value_gridColor' to show and set color of grid lines on value axis.
Code:
<Chart
  ...
  keyAxis={{
    ...
    gridLines:'#ddd'
    ...
  }}
  valueAxis={{
    ...
    gridLines:'#ddd'
    ...
  }}
  ...
/>`
Preview(Click image and open demo on stackblitz):

alt text

Set Multi Data by diffrent styles

Set 3 data on 'data' prop by diffrent styles.
Controlling line chart style by 'dash' , 'lineWidth' , 'color' and 'areaOpacity' property on data
Code:
<Chart
  ...
  data={[
    {
      ...
      title:'data1',
      color:'blue',
      dash:[4,2],
      area:true
      ...
    },
    {
      ...
      title:'data2',
      color:'#03ebcc',
      lineWidth:4,
      ...
    },
    {
      ...
      title:'data3',
      color:'#e414c8',
      dash:[7,5],
    }
  ]}
  ...
/>
Preview(Click image and open demo on stackblitz):

alt text

Get Point Style

Set circle on each points by set 'getPointStyle' function prop on data.
Code:
<Chart
  ...
  data={[
    {
      ...
      pointStyle:{
        radius:5,fill:'blue',stroke:'rgba(0,0,255,.1)',lineWidth:6
      }
      ...
    }
  ]}
  ...
/>
Preview(Click image and open demo on stackblitz):

alt text

Code:
<Chart
  ...
  data={[
    {
      ...
      getPointStyle:(point)=>{
        if(point.date === 'January'){
          return {radius:5,fill:'blue'}
        }
        if(point.date === 'August'){
          return {radius:8,stroke:'red',lineWidth:2,dash:[4,3]}
        }
        else{
          return {radius:5}
        }
      }
      ...
    }
  ]}
  ...
/>
Preview(Click image and open demo on stackblitz):

alt text

Set Lines

Set lines by 'lines' prop width custom style on 'keyAxis' or 'valueAxis' props.
Code:
<Chart
  ...
  keyAxis={
    ...
    lines:[
        {key:'June',dash:[2,2],color:'red',lineWidth:1}
    ]
    ...
  }
  valueAxis={
    ...
    lines:[
        {value:50,dash:[8,5],color:'blue',lineWidth:2}
    ]
    ...
  }
  ...
/>
Preview(Click image and open demo on stackblitz):

alt text

Set Text On Points

Set text on each points by set 'getPointText' function props on data that read text value from point object and can get custom style.
Code:
<Chart
  ...
  data={[
    {
      ...
      points:[
        {date:'January',percent:10,size:'low'},
          {date:'February',percent:15,size:'low'},
          {date:'March',percent:25,size:'low'},
          {date:'April',percent:30,size:'low'},
          {date:'May',percent:40,size:'medium'},
          {date:'June',percent:35,size:'medium'},
          {date:'July',percent:40,size:'medium'},
          {date:'August',percent:60,size:'high'},
          {date:'September',percent:60,size:'high'},
          {date:'October',percent:75,size:'high'},
          {date:'November',percent:80,size:'high'},
          {date:'December',percent:100,size:'high'}
      ],
      getPointText:(point)=>{
        return {
          value:point.size,
          top:20,
          fontSize:12,
          rotate:point.date === 'December'?90:0,
          align:[0,0]
        }
      }
      ...
    }
  ]}
  ...
/>
Preview(Click image and open demo on stackblitz):

alt text

Rotate Horizontal Labels

rotate horizontal labels by 'labelRotate' props.
Code:
<Chart
  ...
  labelRotate={45}
  ...
/>
Preview(Click image and open demo on stackblitz):

alt text

Set Axis Size

Set axis thickness by 'size' property in keyAxis or valueAxis.
Code:
<Chart
  ...
  keyAxis={{
    size:90  
  }}
  valueAxis={{
    edit:(value)=>value * 1000 + '$',
    size:70  
  }}
  labelRotate={90}
  labelSize={40}
  ...
/>
Preview(Click image and open demo on stackblitz):

alt text

7.7.0

2 years ago

7.6.0

3 years ago

7.5.3

3 years ago

7.5.2

3 years ago

7.5.1

3 years ago

7.5.0

3 years ago

7.4.4

3 years ago

7.4.3

3 years ago

7.4.2

3 years ago

7.4.5

3 years ago

7.4.1

3 years ago

7.4.0

3 years ago

7.3.4

3 years ago

7.3.3

3 years ago

7.3.1

3 years ago

7.3.0

3 years ago

7.3.2

3 years ago

7.2.1

3 years ago

7.1.3

3 years ago

7.1.2

3 years ago

7.1.1

3 years ago

7.1.0

3 years ago

7.0.0

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

5.0.0

4 years ago

4.1.3

4 years ago

4.2.0

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.11

4 years ago

4.0.10

4 years ago

4.0.9

4 years ago

4.0.8

4 years ago

4.0.5

4 years ago

4.0.4

4 years ago

4.0.7

4 years ago

4.0.6

4 years ago

4.0.3

4 years ago

4.0.1

4 years ago

4.0.2

4 years ago

3.0.4

4 years ago

4.0.0

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.15

4 years ago

2.0.16

4 years ago

2.0.14

4 years ago

2.0.12

4 years ago

2.0.11

4 years ago

2.0.9

4 years ago

2.0.10

4 years ago

2.0.8

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago