File tree Expand file tree Collapse file tree 3 files changed +142
-0
lines changed Expand file tree Collapse file tree 3 files changed +142
-0
lines changed Original file line number Diff line number Diff line change 1+ import React from 'react' ;
2+ import { Pie } from 'react-chartjs-2' ;
3+
4+ const data = {
5+ labels : [
6+ 'Red' ,
7+ 'Green' ,
8+ 'Yellow'
9+ ] ,
10+ datasets : [ {
11+ data : [ 300 , 50 , 100 ] ,
12+ backgroundColor : [
13+ '#FF6384' ,
14+ '#36A2EB' ,
15+ '#FFCE56'
16+ ] ,
17+ hoverBackgroundColor : [
18+ '#FF6384' ,
19+ '#36A2EB' ,
20+ '#FFCE56'
21+ ]
22+ } ]
23+ } ;
24+
25+ const legendOpts = {
26+ onClick : ( e , item ) => alert ( `Item with text ${ item . text } and index ${ item . index } clicked` ) ,
27+ onHover : ( e , item ) => alert ( `Item with text ${ item . text } and index ${ item . index } hovered` ) ,
28+ } ;
29+
30+ export default React . createClass ( {
31+ displayName : 'LegendExample' ,
32+
33+ getInitialState ( ) {
34+ return {
35+ legend : legendOpts
36+ }
37+ } ,
38+
39+ applyLegendSettings ( ) {
40+ const { value } = this . legendOptsInput ;
41+
42+ try {
43+ const opts = JSON . parse ( value ) ;
44+ this . setState ( {
45+ legend : opts
46+ } ) ;
47+ } catch ( e ) {
48+ alert ( e . message ) ;
49+ throw Error ( e ) ;
50+ }
51+ } ,
52+
53+ render ( ) {
54+ return (
55+ < div >
56+ < h2 > Legend Handlers Example</ h2 >
57+ < p > Hover over label and click</ p >
58+ < Pie data = { data } legend = { this . state . legend } />
59+ </ div >
60+ ) ;
61+ }
62+ } )
Original file line number Diff line number Diff line change 1+ import React from 'react' ;
2+ import { Pie } from 'react-chartjs-2' ;
3+
4+ const data = {
5+ labels : [
6+ 'Red' ,
7+ 'Green' ,
8+ 'Yellow'
9+ ] ,
10+ datasets : [ {
11+ data : [ 300 , 50 , 100 ] ,
12+ backgroundColor : [
13+ '#FF6384' ,
14+ '#36A2EB' ,
15+ '#FFCE56'
16+ ] ,
17+ hoverBackgroundColor : [
18+ '#FF6384' ,
19+ '#36A2EB' ,
20+ '#FFCE56'
21+ ]
22+ } ]
23+ } ;
24+
25+ const legendOpts = {
26+ display : true ,
27+ position : 'top' ,
28+ fullWidth : true ,
29+ reverse : false ,
30+ labels : {
31+ fontColor : 'rgb(255, 99, 132)'
32+ }
33+ } ;
34+
35+ export default React . createClass ( {
36+ displayName : 'LegendExample' ,
37+
38+ getInitialState ( ) {
39+ return {
40+ legend : legendOpts
41+ }
42+ } ,
43+
44+ applyLegendSettings ( ) {
45+ const { value } = this . legendOptsInput ;
46+
47+ try {
48+ const opts = JSON . parse ( value ) ;
49+ this . setState ( {
50+ legend : opts
51+ } ) ;
52+ } catch ( e ) {
53+ alert ( e . message ) ;
54+ throw Error ( e ) ;
55+ }
56+ } ,
57+
58+ render ( ) {
59+ return (
60+ < div >
61+ < h2 > Legend Options Example</ h2 >
62+ < textarea
63+ cols = "40"
64+ rows = "15"
65+ ref = { input => { this . legendOptsInput = input ; } }
66+ defaultValue = { JSON . stringify ( this . state . legend , null , 2 ) } > </ textarea >
67+ < div >
68+ < button onClick = { this . applyLegendSettings } > Apply legend settings</ button >
69+ </ div >
70+ < Pie data = { data } legend = { this . state . legend } redraw />
71+ </ div >
72+ ) ;
73+ }
74+ } )
Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ import BubbleExample from './components/bubble';
1313import MixedDataExample from './components/mix' ;
1414import RandomizedDataLineExample from './components/randomizedLine' ;
1515import CrazyDataLineExample from './components/crazyLine' ;
16+ import LegendOptionsExample from './components/legend-options'
17+ import LegendHandlersExample from './components/legend-handlers'
1618
1719class App extends React . Component {
1820 render ( ) {
@@ -42,6 +44,10 @@ class App extends React.Component {
4244 < RandomizedDataLineExample />
4345 < hr />
4446 < CrazyDataLineExample />
47+ < hr />
48+ < LegendOptionsExample />
49+ < hr />
50+ < LegendHandlersExample />
4551 </ div >
4652 ) ;
4753 }
You can’t perform that action at this time.
0 commit comments