$(function(){
var name = new Array();
var value=new Array();
var count = new Array();
$.post(
"BaseAction/SheetAction/ContributeSheetPie",
function(data)
{
name=data.name;
value=data.value;
pie(value,name);
},
"json"
);
$('#show').click(function(){
var i=1;
var pie=document.getElementById('pie');
var bar=document.getElementById('bar');
if(i==1)
{
var count = new Array();
pie.style.display="none";
bar.style.display="block";
$.post(
"BaseAction/SheetAction/ContributeSheet",
function(data)
{
name=data.name;
count=data.count;
bar(name,count);//Uncaught TypeError: bar is not a function
},
"json"
);
i=0;
}
if(i==0)
{
pie.style.display="block";
bar.style.display="none";
$.post(
"BaseAction/SheetAction/ContributeSheetPie",
function(data)
{
name=data.name;
value=data.value;
pie(value,name);//Uncaught TypeError: pie is not a function
},
"json"
);
}
});
});
//---------------------------pie餅形分析圖--------------------------------------------------------------------------------
function pie(value,name)
{
// 路徑配置
require.config({
paths: {
echarts: '<%=path%>/js/echarts/build/dist'
}
});
require(
[
'echarts',
'echarts/chart/pie',
'echarts/chart/funnel'
],
function (ec)
{
// 基於准備好的dom,初始化echarts圖表
var myChart = ec.init(document.getElementById('pie'));
var option = {
title :
{
text: '顧客貢獻',
x:'center'
},
tooltip :
{
trigger: 'item',
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
legend:
{
orient : 'vertical',
x : 'left',
data:name
},
toolbox:
{
show : true,
feature :
{
mark : {show: true},
dataView : {show: true, readOnly: false},
magicType :
{
show: true,
type: ['pie', 'funnel'],
option:
{
funnel:
{
x: '25%',
width: '50%',
funnelAlign: 'left',
max: 1548
}
}
},
restore : {show: true},
saveAsImage : {show: true},
}
},
calculable : true,
series :
[
{
name:"訂單數",
type:"pie",
radius : '55%',
center: ['50%', '60%'],
data :value
}
]
};
// 為echarts對象加載數據
myChart.setOption(option);
}
);
};
//---------------------------pie餅形分析圖--------------------------------------------------------------------------------
function bar(name,count)
{
// 路徑配置
require.config({
paths: {
echarts: '<%=path%>/js/echarts/build/dist'
}
});
// 使用
require(
[
'echarts',
// 使用柱狀圖就加載bar模塊,按需加載
'echarts/chart/line',
'echarts/chart/bar'
],
function (ec) {
// 基於准備好的dom,初始化echarts圖表
var myChart = ec.init(document.getElementById('bar'));
var option = {
tooltip: {
trigger:'axis',
},
legend: {
data:['貢獻度']
},
toolbox: {
show : true,
feature : {
mark : {show: true},
dataView : {show: true, readOnly: false},
magicType : {show: true, type: ['line', 'bar']},
restore : {show: true},
saveAsImage : {show: true}
}
},
calculable : true,
xAxis : [
{
type : 'category',
data : name
}
],
yAxis : [
{
type : 'value'
}
],
series : [
{
"name":"訂單數",
"type":"bar",
"data":count,
markPoint : {
data : [
{type : 'max', name: '最大值'},
{type : 'min', name: '最小值'}
]
},
markLine : {
data : [
{type : 'average', name: '平均值'}
]
}
}
]
};
// 為echarts對象加載數據
myChart.setOption(option);
}
);
};
</script>
你看下上文就知道了,你的$('#show').click(function(){點擊事件裡面定義了一個pie變量,為dom對象,dom對象又不失方法,當然會報錯了
pie(value,name);//Uncaught TypeError: pie is not a function
改為
window.pie(value,name);
使用window作用域下的pie函數,而不是click中的pie變量