Skip to content

Commit 9b9c826

Browse files
committed
add line chart dot and gesture handler.
1 parent 450bfb6 commit 9b9c826

File tree

10 files changed

+183
-104
lines changed

10 files changed

+183
-104
lines changed

.dockerignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
6+
# Runtime data
7+
pids
8+
*.pid
9+
*.seed
10+
11+
# Directory for instrumented libs generated by jscoverage/JSCover
12+
lib-cov
13+
14+
# Coverage directory used by tools like istanbul
15+
coverage
16+
17+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
18+
.grunt
19+
20+
# node-waf configuration
21+
.lock-wscript
22+
23+
# Compiled binary addons (http://nodejs.org/api/addons.html)
24+
build/Release
25+
26+
# Dependency directory
27+
node_modules
28+
29+
# Optional npm cache directory
30+
.npm
31+
32+
# Optional REPL history
33+
.node_repl_history
34+
.DS_Store

charts/Bar.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default class Bar extends Component{
4545
getTooltipBac.apply(this);
4646

4747
this.state = {
48-
highlight:-1,
48+
active:-1,
4949
crossHair:{}
5050
};
5151
}
@@ -119,7 +119,6 @@ export default class Bar extends Component{
119119
{ this.getBars() }
120120
{ this.getCoords() }
121121
{ this.getTitle() }
122-
{ this.getSubtitle() }
123122
{ this.getCrossHair() }
124123
{ this.getTooltips() }
125124
</Surface>
@@ -140,17 +139,17 @@ export default class Bar extends Component{
140139
key = { index }
141140
d={ this.getBarD(data,index) }
142141
fill={
143-
this.state.highlight == index ?
142+
this.state.active == index ?
144143
data.activeFill :
145144
data.normalFill
146145
}
147146
stroke={
148-
this.state.highlight == index ?
147+
this.state.active == index ?
149148
data.activeStroke :
150149
data.normalStroke
151150
}
152151

153-
strokeWidth = {2}
152+
strokeWidth = {1}
154153

155154
strokeCap="butt"
156155
strokeJoin="miter"
@@ -215,28 +214,28 @@ export default class Bar extends Component{
215214
const index = parseInt(relativeX/itemWidth);
216215

217216
this.setState({
218-
highlight:index
217+
active:index
219218
})
220219
}
221220
}
222221

223222
getTooltips(){
224223
const {
225-
highlight
224+
active
226225
} = this.state;
227226

228-
if( highlight < 0 || highlight > this.props.series.length-1){
227+
if( active < 0 || active > this.props.series.length-1){
229228
return <Shape/>
230229
}
231230

232231
const {
233232
xAxis,yAxis
234-
} = this.barPos[highlight]
233+
} = this.barPos[active]
235234

236-
const tooltipText = this.props.tooltip.text(highlight,this.props.series[highlight].data);
235+
const tooltipText = this.props.tooltip.text(active,this.props.series[active].data);
237236
const approximateTextLength = 8 * tooltipText.length;
238237

239-
const width = approximateTextLength + 10;
238+
const width = approximateTextLength;
240239
const height = 8 + 10;
241240

242241
let xMiddle = xAxis+(this.itemWidth*0.3);
@@ -256,7 +255,7 @@ export default class Bar extends Component{
256255
y:yMiddle - height/2,
257256
width,
258257
height,
259-
r:12
258+
r:5
260259
})
261260
}
262261

@@ -275,7 +274,7 @@ export default class Bar extends Component{
275274
x:-100,
276275
y:-100,
277276
},
278-
highlight:-1
277+
active:-1
279278
})
280279
}
281280
}

charts/Doughnut.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ export default class Doughnut extends Component{
111111
<Surface width={ width } height={ height } visible={true}>
112112
{ this.getDoughnut() }
113113
{ this.getTitle() }
114-
{ this.getSubtitle() }
115114
{ this.getTooltips() }
116115
</Surface>
117116
</GestureAware>

charts/Line.js

Lines changed: 89 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,25 @@ const {
2525
import {
2626
attachTitleHandlers,
2727
enableCrosshair,
28-
enableCoords
28+
enableCoords,
29+
getTooltipBac
2930
} from './subparts'
3031

31-
console.log( {
32-
attachTitleHandlers,
33-
enableCrosshair,
34-
enableCoords
35-
} )
36-
3732
export default class Line extends Component{
3833
constructor(props) {
3934
super(props);
4035

4136
attachTitleHandlers.apply(this);
4237
enableCrosshair.apply(this);
38+
getTooltipBac.apply(this);
4339
enableCoords('cartesian').apply(this);
4440

4541
this.state = {
46-
highlight:-1,
47-
crossHair:{}
42+
active:-1
4843
};
44+
45+
this.points = [];
46+
this.size = 4;
4947
}
5048

5149
static propTypes = {
@@ -97,9 +95,10 @@ export default class Line extends Component{
9795
>
9896
<Surface width={this.props.width} height={this.props.height} visible={true}>
9997
{ this.getLines() }
98+
{ this.getDots() }
10099
{ this.getCoords() }
101100
{ this.getTitle() }
102-
{ this.getSubtitle() }
101+
{ this.getTooltips() }
103102
{ this.getCrossHair() }
104103
</Surface>
105104
</GestureAware>
@@ -118,11 +117,11 @@ export default class Line extends Component{
118117
return (
119118
<Group>
120119
<Shape d={this.getLinesD(true)}
121-
stroke="#4d4d4d"
120+
stroke={ this.props.series[0].normalStroke }
122121
></Shape>
123122
<Shape d={this.getLinesD()}
124123
fill={new LinearGradient({
125-
'.1': 'rgb(12,20,12)',
124+
'.1': "#4990E2",
126125
'0.5': 'rgba(255,255,255,0)'
127126
},
128127
"0","0","0",(height-bottom)*2
@@ -150,14 +149,15 @@ export default class Line extends Component{
150149
const containerWidth = width-left-right;
151150
const containerHeight = height-top-bottom;
152151

153-
// const progress = this.state[`progress${index}`] || 0;
154-
// const xAxis = left + index*containerWidth/series.length
155-
// const yAxis = containerHeight - (data/yRange)*containerHeight*progress + top
156-
152+
const progress = this.state[`progress${index}`] || 0;
153+
const xAxis = left + index*containerWidth/series.length
154+
const yAxis = containerHeight - (data/yRange)*containerHeight*progress + top
157155

156+
/*
158157
const progress = this.state[`progress${series.length - index}`] || 0;
159158
const xAxis = left + (index*containerWidth/series.length)*progress
160159
const yAxis = containerHeight - (data/yRange)*containerHeight + top
160+
*/
161161

162162
if( index == 0 ){
163163
path.moveTo(xAxis,yAxis);
@@ -166,6 +166,11 @@ export default class Line extends Component{
166166

167167
path.lineTo(xAxis,yAxis);
168168

169+
this.points[index] = [
170+
xAxis,
171+
yAxis
172+
]
173+
169174
if( !stroke && index == series.length - 1 ){
170175
path.lineTo( xAxis, height - bottom)
171176
.lineTo( left, height - bottom)
@@ -176,6 +181,44 @@ export default class Line extends Component{
176181
return path;
177182
}
178183

184+
getDots = () => {
185+
return (
186+
this.points.map(([x,y],index) => {
187+
const active = this.state.active == index;
188+
const { size } = this;
189+
190+
const {
191+
normalFill,
192+
normalStroke,
193+
activeFill,
194+
activeStroke
195+
} = this.props.series[0];
196+
197+
// x -= size/2;
198+
// y -= size/2;
199+
200+
return (
201+
<Shape d={
202+
// new Path()
203+
// .moveTo(x,y)
204+
// .lineTo(x+size,y)
205+
// .lineTo(x+size,y+size)
206+
// .lineTo(x,y+size)
207+
// .close()
208+
209+
new Path()
210+
.moveTo(x-size/2,y)
211+
.arcTo(x+size/2,y,size/2,size/2)
212+
.arcTo(x-size/2,y,size/2,size/2)
213+
.close()
214+
}
215+
fill={ active ? activeFill : normalFill }
216+
key={`dot${index}`}/>
217+
)
218+
})
219+
)
220+
}
221+
179222
onStart = (ev) => {
180223
if( !this.props.disableCorssHair ) this.setCrossHair(ev);
181224
}
@@ -193,24 +236,39 @@ export default class Line extends Component{
193236
}
194237
})
195238

196-
const {
197-
series,width,height
198-
} = this.props
239+
const { size } = this;
199240

200-
const {
201-
left,top,right,bottom
202-
} = this.padding
241+
this.points.forEach(([x,y],index) => {
242+
if( ev.moveX > x - size/2 && ev.moveX < x + size/2){
243+
this.setState({
244+
active:index
245+
})
246+
}
247+
})
248+
}
203249

204-
const itemWidth = (width-left-right)/series.length;
205-
const relativeX = ev.moveX - left;
250+
getTooltips = () => {
251+
const { active } = this.state;
206252

207-
if( relativeX%itemWidth > itemWidth*0.2 && relativeX%itemWidth < itemWidth*0.8 ){
208-
const index = parseInt(relativeX/itemWidth);
253+
if( active < 0 ) return <Shape/>;
209254

210-
this.setState({
211-
highlight:index
212-
})
213-
}
255+
const activeData = this.props.series[active].data;
256+
257+
const tooltipText = this.props.tooltip.text(activeData,active);
258+
259+
const approximateTextLength = 10 * tooltipText.length;
260+
261+
const width = approximateTextLength + 2;
262+
const height = 10 + 2;
263+
264+
return this.getTooltipBac({
265+
tooltipText,
266+
x:this.points[active][0] - width/2,
267+
y:this.points[active][1] - 10 - height/2 ,
268+
width,
269+
height,
270+
r:4
271+
})
214272
}
215273

216274
onEnd = () => {
@@ -221,7 +279,7 @@ export default class Line extends Component{
221279
x:-100,
222280
y:-100,
223281
},
224-
highlight:-1
282+
active:-1
225283
})
226284
}
227285
}

charts/Pie.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ export default class Pie extends Component{
100100
<Surface width={this.props.width} height={this.props.height} visible={true}>
101101
{ this.getPies() }
102102
{ this.getTitle() }
103-
{ this.getSubtitle() }
104103
{ this.getTooltips() }
105104
</Surface>
106105
</GestureAware>

charts/subparts/coords/cartesian.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default function enableCoords(){
4646
(this.props.title ? 50 : 0) +
4747
( this.props.subtitle ? 50 : 0) + 20,
4848
right:20,
49-
bottom:50,
49+
bottom:20,
5050
left:20
5151
}
5252
}

0 commit comments

Comments
 (0)