@@ -11,13 +11,21 @@ components.push({
1111} )
1212
1313export default class AppleStockChart extends React . Component {
14+ constructor ( props ) {
15+ super ( props )
16+
17+ this . state = {
18+ editMode : true ,
19+ overridePosition : { }
20+ }
21+ }
1422 render ( ) {
1523 const buttons = [ ]
1624
1725 const examples = [ ]
1826 examples . push ( {
1927 name : "Basic" ,
20- demo : AppleStockChartRaw ,
28+ demo : AppleStockChartRaw ( this . state . editMode ) ,
2129 source : `
2230import { XYFrame, DividedLine } from "../../components"
2331import { data } from '../sampledata/apple_stock'
@@ -91,6 +99,92 @@ const customTooltip = d => <div className="tooltip-content">
9199 `
92100 } )
93101
102+ examples . push ( {
103+ name : "Editable Annotations" ,
104+ demo : (
105+ < div >
106+ < p >
107+ react-annotation already has built in functionality for adjusting
108+ the annotations it creates. You can activate the note editing
109+ control points by setting `editMode: true` on any of your
110+ annotations. With that in place, you can also set the drag,
111+ dragStart or dragEnd properties of the annotation to pass the new
112+ annotation position data to whatever you're using to manage state.
113+ In this simple example, I just override the dx/dy based on the new
114+ values but you could pass this back to a central annotation store or
115+ other method of saving changes.
116+ </ p >
117+ < button
118+ style = { { color : "black" } }
119+ onClick = { ( ) => this . setState ( { editMode : ! this . state . editMode } ) }
120+ >
121+ { this . state . editMode ? "Turn off editMode" : "Turn on editMode" }
122+ </ button >
123+ { AppleStockChartRaw (
124+ this . state . editMode ,
125+ this . state . overridePosition ,
126+ d => {
127+ this . setState ( {
128+ overridePosition : {
129+ ...this . state . overridePosition ,
130+ [ d . noteIndex ] : {
131+ dx : d . updatedSettings . dx ,
132+ dy : d . updatedSettings . dy
133+ }
134+ }
135+ } )
136+ }
137+ ) }
138+ </ div >
139+ ) ,
140+ source : `
141+ constructor(props) {
142+ super(props)
143+
144+ this.state = {
145+ editMode: true,
146+ overridePosition: {}
147+ }
148+ }
149+
150+ render() {
151+ const annotations = [{
152+ type: "x",
153+ date: "7/9/1997",
154+ note: { label: "Steve Jobs Returns", align: "middle" },
155+ color: "rgb(0, 162, 206)",
156+ dy: -10,
157+ dx: 0,
158+ connector: { end: "none" },
159+ editMode,
160+ onDragEnd: annotationInfo => {
161+ annotationInfo => {
162+ this.setState({
163+ overridePosition: {
164+ ...this.state.overridePosition,
165+ [annotationInfo.noteIndex]: {
166+ dx: annotationInfo.updatedSettings.dx,
167+ dy: annotationInfo.updatedSettings.dy
168+ }
169+ }
170+ })
171+ }
172+
173+ }
174+ }]
175+
176+ annotations.forEach((d, i) => {
177+ if (this.state.overridePosition[i]) {
178+ d.dx = overridePosition[i].dx
179+ d.dy = overridePosition[i].dy
180+ }
181+ })
182+
183+ return <XYFrame
184+ {...as above example}
185+ />
186+ }`
187+ } )
94188 return (
95189 < DocumentComponent
96190 name = "Stock Chart with Annotations and Divided Line"
@@ -108,13 +202,18 @@ const customTooltip = d => <div className="tooltip-content">
108202 rel = "noopener noreferrer"
109203 >
110204 Susie Lu's Apple stock chart
111- </ a > .
205+ </ a >
206+ .
112207 </ p >
113208 < p >
114209 It also uses a custom x scale using xScaleType to pass a scale built
115210 with D3's scaleTime, as well as tooltip processing rules using
116211 tooltipContent.
117212 </ p >
213+ < p >
214+ (If you want to see how to allow your users to edit annotations, check
215+ out the second example below)
216+ </ p >
118217 </ DocumentComponent >
119218 )
120219 }
0 commit comments