示例:实现拖拽
这是一个小例子,介绍如何在 Apache EChartsTM 中实现图形元素的拖拽。从这个例子中,你将看到,如何基于 ECharts API 做出具有丰富交互的图表。
这个例子主要实现了,拖拽修改曲线上的点,从而改变曲线的形状。虽然例子简单,但是基于此可以做出更复杂的应用,例如可视化地编辑图表。那么,就让我们从这个简单的例子开始吧。
实现基本拖拽
首先,我们创建一个基本的折线图。
var symbolSize = 20;
var data = [
[15, 0],
[-50, 10],
[-56.5, 20],
[-46.5, 30],
[-22.1, 40]
];
myChart.setOption({
xAxis: {
min: -100,
max: 80,
type: 'value',
axisLine: { onZero: false }
},
yAxis: {
min: -30,
max: 60,
type: 'value',
axisLine: { onZero: false }
},
series: [
{
id: 'a',
type: 'line',
smooth: true,
// Set a big symbolSize for dragging convenience.
symbolSize: symbolSize,
data: data
}
]
});
因为折线图的拐点本身不支持拖拽,所以我们使用 ECharts 的图形组件(graphic component),在每个拐点上,覆盖一个可拖拽的圆点。
myChart.setOption({
// Declare a graphic component, which contains some graphic elements
// with the type of 'circle'.
// Here we have used the method `echarts.util.map`, which has the same
// behavior as Array.prototype.map, and is compatible with ES5-.
graphic: echarts.util.map(data, function(dataItem, dataIndex) {
return {
// 'circle' means this graphic element is a shape of circle.
type: 'circle',
shape: {
// The radius of the circle.
r: symbolSize / 2
},
// Transform is used to located the circle. position:
// [x, y] means translate the circle to the position [x, y].
// The API `convertToPixel` is used to get the position of
// the circle, which will introduced later.
position: myChart.convertToPixel('grid', dataItem),
// Make the circle invisible (but mouse event works as normal).
invisible: true,
// Make the circle draggable.
draggable: true,
// Give a big z value, which makes the circle cover the symbol
// in line series.
z: 100,
// This is the event handler of dragging, which will be triggered
// repeatly while dragging. See more details below.
// A util method `echarts.util.curry` is used here to generate a
// new function the same as `onPointDragging`, except that the
// first parameter is fixed to be the `dataIndex` here.
ondrag: echarts.util.curry(onPointDragging, dataIndex)
};
})
});
在上面的代码中,使用了 API convertToPixel,这个 API 可以把数据转换成“像素坐标”。基于“像素坐标”,我们可以把图形元素绘制在画布上。“像素坐标”是指,坐标系的原点在画布的左上角。在语句 myChart.convertToPixel('grid', dataItem)
中,第一个参数 'grid'
表示 dataItem
是在第一个直角坐标系(grid)中定义的。
注意: convertToPixel
不可以在第一次 setOption
前调用。也就是说,它只能在坐标系(grid/polar/...)初始化后才能使用。
现在拐点们已经变得可以拖拽了。接着,我们在这些拐点上绑定拖拽事件的监听函数。
// This function will be called repeatly while dragging.
// The mission of this function is to update `series.data` based on
// the new points updated by dragging, and to re-render the line
// series based on the new data, by which the graphic elements of the
// line series can be synchronized with dragging.
function onPointDragging(dataIndex) {
// Here the `data` is declared in the code block in the beginning
// of this article. The `this` refers to the dragged circle.
// `this.position` is the current position of the circle.
data[dataIndex] = myChart.convertFromPixel('grid', this.position);
// Re-render the chart based on the updated `data`.
myChart.setOption({
series: [
{
id: 'a',
data: data
}
]
});
}
在上面的代码中,使用了 API convertFromPixel,它是 convertToPixel 的逆向过程。myChart.convertFromPixel('grid', this.position)
把像素坐标转换成直角坐标系(grid)中的数据值。
最后,加上这些代码,使得这些图形元素能够响应画布尺寸的变化。
window.addEventListener('resize', function() {
// Re-calculate the position of each circle and update chart using `setOption`.
myChart.setOption({
graphic: echarts.util.map(data, function(item, dataIndex) {
return {
position: myChart.convertToPixel('grid', item)
};
})
});
});
添加 tooltip 组件
现在,基本功能已经在第一部分实现了。如果我们希望在拖拽的时候,数据能够实时显示,我们可以使用 tooltip 组件。但是,tooltip 组件有其默认的“显示/隐藏”规则,这在当前场景不适用。所以我们需要为当前场景定制“显示/隐藏”规则。
把这些代码片段加到上面的代码块中。
myChart.setOption({
// ...,
tooltip: {
// Means disable default "show/hide rule".
triggerOn: 'none',
formatter: function(params) {
return (
'X: ' +
params.data[0].toFixed(2) +
'<br>Y: ' +
params.data[1].toFixed(2)
);
}
}
});
myChart.setOption({
graphic: data.map(function(item, dataIndex) {
return {
type: 'circle',
// ...,
// Customize "show/hide rule", show when mouse over, hide when mouse out.
onmousemove: echarts.util.curry(showTooltip, dataIndex),
onmouseout: echarts.util.curry(hideTooltip, dataIndex)
};
})
});
function showTooltip(dataIndex) {
myChart.dispatchAction({
type: 'showTip',
seriesIndex: 0,
dataIndex: dataIndex
});
}
function hideTooltip(dataIndex) {
myChart.dispatchAction({
type: 'hideTip'
});
}
API dispatchAction 被用来显示和隐藏 tooltip,其中 showTip 和 hideTip action 被派发。
完整代码
完整代码如下所示。
import echarts from 'echarts';
var symbolSize = 20;
var data = [
[15, 0],
[-50, 10],
[-56.5, 20],
[-46.5, 30],
[-22.1, 40]
];
var myChart = echarts.init(document.getElementById('main'));
myChart.setOption({
tooltip: {
triggerOn: 'none',
formatter: function(params) {
return (
'X: ' +
params.data[0].toFixed(2) +
'<br />Y: ' +
params.data[1].toFixed(2)
);
}
},
xAxis: { min: -100, max: 80, type: 'value', axisLine: { onZero: false } },
yAxis: { min: -30, max: 60, type: 'value', axisLine: { onZero: false } },
series: [
{ id: 'a', type: 'line', smooth: true, symbolSize: symbolSize, data: data }
]
});
myChart.setOption({
graphic: echarts.util.map(data, function(item, dataIndex) {
return {
type: 'circle',
position: myChart.convertToPixel('grid', item),
shape: { r: symbolSize / 2 },
invisible: true,
draggable: true,
ondrag: echarts.util.curry(onPointDragging, dataIndex),
onmousemove: echarts.util.curry(showTooltip, dataIndex),
onmouseout: echarts.util.curry(hideTooltip, dataIndex),
z: 100
};
})
});
window.addEventListener('resize', function() {
myChart.setOption({
graphic: echarts.util.map(data, function(item, dataIndex) {
return { position: myChart.convertToPixel('grid', item) };
})
});
});
function showTooltip(dataIndex) {
myChart.dispatchAction({
type: 'showTip',
seriesIndex: 0,
dataIndex: dataIndex
});
}
function hideTooltip(dataIndex) {
myChart.dispatchAction({ type: 'hideTip' });
}
function onPointDragging(dataIndex, dx, dy) {
data[dataIndex] = myChart.convertFromPixel('grid', this.position);
myChart.setOption({
series: [
{
id: 'a',
data: data
}
]
});
}
利用上面介绍的知识,可以实现更多的功能。例如,可以添加 dataZoom 组件 与图表联动,或者,我们可以在坐标系上做一个画图板。发挥你的想象力吧~