Skip to content

Instantly share code, notes, and snippets.

@mravey
Created April 2, 2012 08:27
Show Gist options
  • Save mravey/2281690 to your computer and use it in GitHub Desktop.
Save mravey/2281690 to your computer and use it in GitHub Desktop.
Draw a line from one item point to another

Draw a line from one item point to another
To draw a line mousedown on an item and drag to another and mouseup to draw the line permanently.

Ext.setup({
onReady: function() {
var data = [];
for (var i = 0; i < 10; i++) {
data.push({
x: i,
y: parseInt(Math.random() * 100, 10)
});
}
var startPoint = null;
var draw = false;
var line = null;
var chart = new Ext.chart.Chart({
store: new Ext.data.JsonStore({
fields: ['x', 'y'],
data: data
}),
axes: [{
type: 'Numeric',
position: 'left',
fields: ['y']
}, {
type: 'Category',
position: 'bottom',
fields: ['x']
}],
series: [{
type: 'line',
axis: 'left',
xField: 'x',
yField: 'y',
showMarkers: false,
listeners: {
'itemmousedown': function(serie, item, event) {
line = new Ext.draw.Sprite({
type: 'path',
path: [
['M', event.pageX, event.browserEvent.offsetY]
],
stroke: 'red',
'stroke-width': 2
});
startPoint = item.point;
draw = true;
},
'itemmouseup': function(serie, item, event) {
if (line.attr.path.length > 1) {
serie.surface.add({
type: 'path',
path: [
['M', startPoint[0], startPoint[1]],
['L', item.point[0], item.point[1]]
],
stroke: 'red',
'stroke-width': 2
});
serie.surface.renderFrame();
chart.surfaces.events.remove(line);
chart.surfaces.events.renderFrame();
}
draw = false;
}
}
}]
});
chart.on('redraw', function() {
var surface = chart.surfaces.events;
surface.on('mousemove', function(event) {
if (draw == true) {
if (line.attr.path.length == 1) {
line.attr.path.push(['L', event.pageX, event.browserEvent.offsetY]);
}
else {
line.attr.path[1] = ['L', event.pageX, event.browserEvent.offsetY];
}
chart.surfaces.events.add(line);
chart.surfaces.events.renderFrame();
}
});
surface.on('mouseup', function(event) {
if (line.attr.path.length > 1) {
chart.surfaces.events.remove(line);
chart.surfaces.events.renderFrame();
draw = false;
}
});
});
var panel = new Ext.chart.Panel({
fullscreen: true,
chart: chart
});
}
});
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="http://testfactory.fr/chart/sencha-touch.css" media="all">
<link rel="stylesheet" type="text/css" href="http://testfactory.fr/chart/touch-charts-full.css" media="all">
<script type="text/javascript" src="http://testfactory.fr/chart/sencha-touch-debug.js"></script>
<script type="text/javascript" src="http://testfactory.fr/chart/touch-charts-debug.js"></script>
<script type="text/javascript" src="app.js"></script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment