87,903
社区成员
发帖
与我相关
我的任务
分享
<template>
<div class="echart">
<div id="chart" style="width: 600px;height: 400px"></div>
<div v-for="(item,i) in data" :key="i">
<input type="number" v-model.number="item[1]" :step="step"></input>
<input type="range" v-model.number="item[1]" :step="step" :min="0" :max="25"></input>
</div>
</div>
</template>
<script>
let echarts = require('echarts');
export default {
name: "echart",
data() {
return {
myEchart: null,
xLabel : ['250Hz', '1.25kHz', '6.3kHz'],
data:[['250Hz', 0], ['1.25kHz', 15], ['6.3kHz', 0]],
height: 0,
symbolSize: 20,
step: 5,//步长
stepLength: 0,
}
},
mounted() {
this.myEchart = echarts.init(document.getElementById('chart'));
let option = {
xAxis: {
type: 'category',
data: this.xLabel
},
yAxis: {
type: 'value',
interval:5,
min:0,
max:25
},
series: [{
data: this.data,
type: 'line',
smooth: true,
symbolSize: this.symbolSize,
}]
};
this.myEchart.setOption(option);
let that = this;
this.myEchart.setOption({
graphic: echarts.util.map(this.data, (item, dataIndex)=>{
return {
type: 'circle',
position: this.myEchart.convertToPixel('grid', item),
shape: {
r: this.symbolSize / 2
},
invisible: true,
draggable: true,
ondrag: echarts.util.curry(function (dataIndex) {
that.data[dataIndex] = [that.xLabel[dataIndex],that.myEchart.convertFromPixel('grid', this.position)[1]];
that.myEchart.setOption({
series: [{
data: that.data
}]
});
}, dataIndex),
onmousedown: echarts.util.curry(this.onmousedown, dataIndex),
onmouseup: echarts.util.curry(this.onmouseup, dataIndex),
z: 100
};
})
});
window.addEventListener('resize', ()=>{
this.myEchart.setOption({
graphic: echarts.util.map(this.data, (item, dataIndex)=>{
return {
position: this.myEchart.convertToPixel('grid', item)
};
})
});
});
},
methods:{
onmousedown(dataIndex){
this.height = this.data[dataIndex][1];
},
onmouseup(dataIndex){
this.stepLength = (this.data[dataIndex][1] - this.height)/this.step;
if (this.stepLength>0){
this.stepLength = Math.ceil(this.stepLength)
} else {
this.stepLength = Math.floor(this.stepLength)
}
this.$set(this.data,dataIndex,[this.xLabel[dataIndex],this.height + this.step*this.stepLength]);
}
},
watch:{
data:{
deep:true,
handler(val){
console.log(val);
this.myEchart.setOption({
series: [{
data: val
}]
});
this.myEchart.setOption({
graphic: echarts.util.map(val, (item)=>{
return {
position: this.myEchart.convertToPixel('grid', item)
};
})
});
}
}
},
}
</script>
这是vue的例子= =
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="utf-8">
<title>Demo2</title>
</head>
<body>
<div id="main" style="width: 600px;height:400px;"></div>
</body>
<script src="echarts.js"></script>
<script type="text/javascript">
let symbolSize = 20;
let step = 5;//步长
let stepLength = 0;
let height = 0;
let data = [['250Hz', 0], ['1.25kHz', 10], ['6.3kHz', 20]];
let xLabel = ['250Hz', '1.25kHz', '6.3kHz'];
let myChart = echarts.init(document.getElementById('main'));
myChart.setOption({
tooltip: {
triggerOn: 'none',
formatter: function (params) {
return 'Y: ' + params.data[1].toFixed(0);
}
},
xAxis: {
type: 'category',
data: ['250Hz', '1.25kHz', '6.3kHz']
},
yAxis: {
min: -0,
max: 25,
type: 'value',
axisLine: {onZero: false}
},
series: [
{
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(ondrag, dataIndex),
onmousedown: echarts.util.curry(onmousedown, dataIndex),
onmouseup: echarts.util.curry(onmouseup, dataIndex),
z: 100
};
})
});
window.addEventListener('resize', function () {
myChart.setOption({
graphic: echarts.util.map(data, function (item, dataIndex) {
return {
position: myChart.convertToPixel('grid', item)
};
})
});
});
function onmousedown(dataIndex) {
//onmousedown
height = data[dataIndex][1];
console.log(height);
}
function ondrag(dataIndex) {
data[dataIndex] = [xLabel[dataIndex],myChart.convertFromPixel('grid', this.position)[1]];
console.log(data,dataIndex);
myChart.setOption({
series: [{
data: data
}]
});
}
function onmouseup(dataIndex) {
//onmouseup
stepLength = (data[dataIndex][1] - height)/step;
if (stepLength>0){
stepLength = Math.ceil(stepLength)
} else {
stepLength = Math.floor(stepLength)
}
data[dataIndex] = [xLabel[dataIndex],height + step*stepLength];
console.log('up',data);
myChart.setOption({
series: [{
data: data
}]
});
myChart.setOption({
graphic: echarts.util.map(data, function (item, dataIndex) {
return {
position: myChart.convertToPixel('grid', item)
};
})
});
}
</script>
</html>
这个是拖拽折线图拖拽效果,两个例子你合并下应该就能完成你的效果了,下班了=。=不摸鱼了,的确最开始你这个效果用vue会方便的多
<!DOCTYPE html>
<html lang="ch">
<head>
<meta charset="UTF-8">
<title>Demo1</title>
</head>
<body>
</body>
<script src="echarts.js"></script>
<script>
let myChart = document.createElement('div');
myChart.style = "width: 600px;height:400px";
document.body.appendChild(myChart);
let chart = echarts.init(myChart);
let data = {
"0":5,
"1":15,
"2":0
};
let number = [];
let range = [];
let option = {
xAxis: {
type: 'category',
data: ['250Hz', '1.25kHz', '6.3kHz']
},
yAxis: {
type: 'value',
interval:5,
min:0,
max:25
},
series: [{
data: [data["0"],data["1"],data["2"]],
type: 'line',
smooth: true
}]
};
chart.setOption(option);
for (let i = 0; i < 3; i++) {
number[i] = document.createElement('input');
number[i].step = "5";
number[i].type = "number";
number[i].value = data[i+""];
number[i].onchange = function(){
data[i+""] = number[i].value;
};
range[i] = document.createElement('input');
range[i].step = "5";
range[i].min = "0";
range[i].max = "25";
range[i].type = "range";
range[i].value = data[i+""];
range[i].onchange = function(){
data[i+""] = range[i].value;
};
document.body.appendChild(number[i]);
document.body.appendChild(range[i]);
}
Object.keys(data).forEach(key => {
definedAttribute(data, key, data[key])
});
function definedAttribute(obj, key, val){
Object.defineProperty(obj, key, {
configurable: false,
get(){
return val
},
set(newVal){
if(val !== newVal){
val = newVal;
let option = {
series: [{
data: [data["0"],data["1"],data["2"]],
}]
};
chart.setOption(option);
for (let i = 0; i <3; i++) {
number[i].value = data[i+""];
range[i].value = data[i+""];
}
}
}
})
}
</script>
</html>