如何使用 jquery 函数将系列数据传递给 echart 选项
How to pass series data to echart option using jquery function
我想要一个使用 eCharts 的动态条形图。我正在尝试使用 jquery 函数传递系列值,但它不是 accepting.Hre 是我的代码 -
var data="[{name:'Complete Project', type:'bar', data:[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]},{name:'New Project', type:'bar', data:[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]}]";
getBarGraph(data);
echart初始化代码为-
function getBarGraph(data){
var dom = document.getElementById("echart-bar");
var myChart = echarts.init(dom);
option = null;
option = {
tooltip : {
trigger: 'axis'
},
xAxis : [
{
type : 'category',
data : ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
}
],
yAxis : [{ type : 'value' }],
series : data
};
if (option && typeof option === "object") {
myChart.setOption(option, true);
}
}
但在浏览器中没有结果。那么我如何传递系列值?
提前致谢
尝试下面的代码它会起作用。我在评论中添加了详细信息。
输出已附加。
https://www.screencast.com/t/3yLCOmRVa
<!doctype html>
<html>
<head>
<title>ECharts Sample</title>
<script src="http://echarts.baidu.com/dist/echarts.min.js"></script>
</head>
<body>
<div id="echart-bar" style="width: 500px; height: 350px;"></div>
<script>
//You need to quote your labels, otherwise will get an unexpected token in JSON
var series='[{"name":"Complete Project", "type":"bar", "data":[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]},{"name":"New Project", "type":"bar", "data":[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]}]';
//Call the function
getBarGraph(series);
function getBarGraph(data){
var dom = document.getElementById('echart-bar');
var myChart = echarts.init(dom);
var option = {
title: { text: 'ECharts Sample' },
tooltip : {
trigger: 'axis'
},
xAxis : [
{
type : 'category',
data : ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
}
],
yAxis : [{ type : 'value' }],
//Use the JavaScript function JSON.parse() to convert text into a JavaScript object:
series: JSON.parse(data),
};
myChart.setOption(option);
}
</script>
</body>
</html>
我想要一个使用 eCharts 的动态条形图。我正在尝试使用 jquery 函数传递系列值,但它不是 accepting.Hre 是我的代码 -
var data="[{name:'Complete Project', type:'bar', data:[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]},{name:'New Project', type:'bar', data:[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]}]";
getBarGraph(data);
echart初始化代码为-
function getBarGraph(data){
var dom = document.getElementById("echart-bar");
var myChart = echarts.init(dom);
option = null;
option = {
tooltip : {
trigger: 'axis'
},
xAxis : [
{
type : 'category',
data : ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
}
],
yAxis : [{ type : 'value' }],
series : data
};
if (option && typeof option === "object") {
myChart.setOption(option, true);
}
}
但在浏览器中没有结果。那么我如何传递系列值?
提前致谢
尝试下面的代码它会起作用。我在评论中添加了详细信息。
输出已附加。 https://www.screencast.com/t/3yLCOmRVa
<!doctype html>
<html>
<head>
<title>ECharts Sample</title>
<script src="http://echarts.baidu.com/dist/echarts.min.js"></script>
</head>
<body>
<div id="echart-bar" style="width: 500px; height: 350px;"></div>
<script>
//You need to quote your labels, otherwise will get an unexpected token in JSON
var series='[{"name":"Complete Project", "type":"bar", "data":[2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]},{"name":"New Project", "type":"bar", "data":[2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]}]';
//Call the function
getBarGraph(series);
function getBarGraph(data){
var dom = document.getElementById('echart-bar');
var myChart = echarts.init(dom);
var option = {
title: { text: 'ECharts Sample' },
tooltip : {
trigger: 'axis'
},
xAxis : [
{
type : 'category',
data : ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
}
],
yAxis : [{ type : 'value' }],
//Use the JavaScript function JSON.parse() to convert text into a JavaScript object:
series: JSON.parse(data),
};
myChart.setOption(option);
}
</script>
</body>
</html>