echarts多图标切换
(1)首先界面界面如图所示:此时默认的信息为总体车间的数据 数据中的信息 分别为麻阳 凤凰 吉首 怀化数据信息
(2)点击信号机 会出现与之对应的数据 其余类似
代码展示如下:
js代码
重点:1. 代码中总体车间为用户第一个所看到的
2.点击信号机,轨道,道岔 要进行数据的准确的传递 以数组的形式的将各个车站数据分开
var myChart = echarts.init(document.getElementById('chart_2'));
//第一个数据代表总车间质量对比
echart_2([
[10,56,56,67],
[55,77,88,99],
[66,77,88,99],
[55,64,78,89],
]);
var totalworkshop_data={"mayang":[{"count":10},{"count":56},{"count":56},{"count":67}],
"fenghuang":[{"count":55},{"count":77},{"count":88},{"count":99}],
"jishou":[{"count":66},{"count":77},{"count":88},{"count":90}],
"huaihua":[{"count":55},{"count":64},{"count":78},{"count":89}]
};
var XHJ_data={"mayang":[{"count":12},{"count":80},{"count":30},{"count":44}],
"fenghuang":[{"count":20},{"count":30},{"count":50},{"count":48}],
"jishou":[{"count":70},{"count":18},{"count":50},{"count":60}],
"huaihua":[{"count":80},{"count":90},{"count":50},{"count":60}]
};
var GD_data={"mayang":[{"count":10},{"count":56},{"count":56},{"count":67}],
"fenghuang":[{"count":55},{"count":77},{"count":88},{"count":99}],
"jishou":[{"count":66},{"count":77},{"count":88},{"count":90}],
"huaihua":[{"count":55},{"count":64},{"count":78},{"count":89}]
};
var DC_data={"mayang":[{"count":78},{"count":96},{"count":86},{"count":67}],
"fenghuang":[{"count":95},{"count":77},{"count":68},{"count":77}],
"jishou":[{"count":86},{"count":97},{"count":78},{"count":100}],
"huaihua":[{"count":89},{"count":44},{"count":68},{"count":89}]
};//代表对应的总体车间 信号机 轨道 道岔
echart_2(totalworkshop_data);
function echart_2(seriesDataArr) {//原始为seriesDataArr
// 基于准备好的dom,初始化echarts实例
var myangData=[];
var fenghuangData=[];
var jishouData=[];
var huaihuaData=[];
//默认加载第一组数据即total车间质量
if(seriesDataArr.mayang!=null){
for(var i=0;i<seriesDataArr.mayang.length;i++){
myangData.push(seriesDataArr.mayang[i].count);
}
}
if(seriesDataArr.fenghuang!=null){
for(var i=0;i<seriesDataArr.fenghuang.length;i++){
fenghuangData.push(seriesDataArr.fenghuang[i].count);
}
}
if(seriesDataArr.jishou!=null){
for(var i=0;i<seriesDataArr.jishou.length;i++){
jishouData.push(seriesDataArr.jishou[i].count);
}
}
if(seriesDataArr.huaihua!=null){
for(var i=0;i<seriesDataArr.huaihua.length;i++){
huaihuaData.push(seriesDataArr.huaihua[i].count);
}
}
//m默认加载第一组数据
let option={
tooltip:{
trigger:'axis',
// backgroundColor:'rgb()',
},
grid: {
top:'10%',
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true,
show:true,
borderColor:'#012f4a',
broderWidth:0,
},
xAxis:{
type:'category',
splitLine:{
show:false,
},
axisLabel:{
show:true,
textStyle:{
fontFamily:'微软雅黑',
color:'rgb(4,30,49)'
},
scale:true,
},
data:['1','2','3','4'],
axisLine:{
symbol: ['none', 'arrow'], // 是否显示轴线箭头
symbolSize: [8, 10], // 箭头大小
symbolOffset: [0, 10],
lineStyle: {
color: 'rgb(94,75,205)',
width: 2,
type: 'solid'
}
},
axisTick:{
show:false
}
},
yAxis:{
type:'value',
boundaryGap:false,
textStyle:{
color:'rgb(255,255,255)'
},
axisLine:{
symbol: ['none', 'arrow'], // 是否显示轴线箭头
symbolSize: [8, 10], // 箭头大小
symbolOffset: [0, 10],
lineStyle: {
color: 'rgb(94,75,205)',
width: 2,
type: 'solid'
}
},
axisLabel:{
show:true,
textStyle:{
fontFamily:'微软雅黑',
color:'#ffffff' ,
},
},
splitLine:{
show:true,
lineStyle:{
color:'rgb(0,43,69)'
}
},
axisTick:{
show:false,
}
},
legend:{
show:true,
origin:'horizontal',
bottom:10,
textStyle:{
color:"rgba(167,92,92,1)"
},
data:[{
name:'麻阳西站',
icon:'path://M78.625,93.094L66.494,72.081l12.132-21.013h24.264l12.132,21.013l-12.132,21.013H78.625z M95.547,80.377l4.79-8.296 l-4.79-8.296h-9.579l-4.79,8.296l4.79,8.296H95.547z',
textStyle:{
fontSize:12,
fontWeiht:'bolder',
color:'#FFFDFE',
fontFamily:'微软雅黑',
},
},
{
name:'凤凰西站',
icon:'path://M78.625,93.094L66.494,72.081l12.132-21.013h24.264l12.132,21.013l-12.132,21.013H78.625z M95.547,80.377l4.79-8.296 l-4.79-8.296h-9.579l-4.79,8.296l4.79,8.296H95.547z',
textStyle:{
fontSize:12,
fontWeiht:'bolder',
color:'#FFFDFE',
fontFamily:'微软雅黑',
},
},
{
name:'吉首东站',
icon:'path://M78.625,93.094L66.494,72.081l12.132-21.013h24.264l12.132,21.013l-12.132,21.013H78.625z M95.547,80.377l4.79-8.296 l-4.79-8.296h-9.579l-4.79,8.296l4.79,8.296H95.547z',
textStyle:{
fontSize:12,
fontWeiht:'bolder',
color:'#FFFDFE',
fontFamily:'微软雅黑',
},
},
{
name:'怀化南站',
icon:'path://M78.625,93.094L66.494,72.081l12.132-21.013h24.264l12.132,21.013l-12.132,21.013H78.625z M95.547,80.377l4.79-8.296 l-4.79-8.296h-9.579l-4.79,8.296l4.79,8.296H95.547z',
textStyle:{
fontSize:12,
fontWeiht:'bolder',
color:'#FFFDFE',
fontFamily:'微软雅黑',
},
}
],
padding:[25,8,-10,0],
},
series:[{
name:'麻阳西站',
type:'bar',
data:myangData, //seriesDataArr[0]
barGap:0,
barWidth : 15,
itemStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb(250,70,188)'
}, {
offset: 1,
color: 'red'
}]),
barBorderRadius: [5,5,0,0],
},
},
},
{
name:'凤凰西站',
type:'bar',
data: fenghuangData,//seriesDataArr[1],
barGap:0,
barWidth : 15,
itemStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb(142,70,250)'
}, {
offset: 1,
color: 'rgb(237,70,250)'
}]),
barBorderRadius: [5,5,0,0],
},
},
},
{
name:'吉首东站',
type:'bar',
data:jishouData, //seriesDataArr[2],
barGap:0,
barWidth : 15,
itemStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb( 70,250,196)'
}, {
offset: 1,
color: 'rgb( 67,187,251)'
}]),
barBorderRadius: [5,5,0,0],
},
},
},
{
name:'怀化南站',
type:'bar',
data: huaihuaData,//seriesDataArr[3],
barGap:0,
barWidth : 15,
itemStyle: {
normal: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
offset: 0,
color: 'rgb( 0,191,159)'
}, {
offset: 1,
color: 'rgb( 250,175,70)'
}]),
barBorderRadius: [5,5,0,0],
},
},
},
]
} ; // 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
window.addEventListener("resize",function(){
myChart.resize();
});
}
$(".btnclass").click(function(){
let goodsName=$(this).data("value");
$( this).attr("stycle","background-color:red");
$( this).siblings().attr("style", "");
switch (goodsName) {
case "信号机":
echart_2(XHJ_data);
break;
case "轨道":
echart_2(GD_data);
break;
case "道岔":
echart_2(DC_data);
break;
case"总体车间":
echart_2(totalworkshop_data);
default:
console.log("点错啦");
break;
}
});
HTML代码:
需要引入对应的插件 否则会报错
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script src="js/echarts.min.js"></script>
<script src="js/echart.js"></script>
<div class="left_bottom public">
<div class="min-title">
<span >车间质量对比</span>
</div>
<div class="buttonText">
<ul >
<li class="btnclass colorbtn" data-value="总体车间" ">总体车间</li>
<li class="btnclass" data-value="信号机">信号机</li>
<li class="btnclass" data-value="轨道">轨道</li>
<li class="btnclass" data-value="道岔">道岔</li>
</ul>
</div>
<div id="chart_2" style="width:100%;height:80%; float: left;"></div>
<div class="boxfoot"></div>
</div>
css代码块:
.min-title{
width: 200px;
height: 40px;
background-color: rgb(4,30,49);
/*margin: 0 auto;*/
top: -20px;
position: absolute;
left: 50%;
border-radius: 20px;
text-align: center;
margin-left: -90px;
}
.min-title span{
display: inline-block;
line-height: 40px;
color: white;
background-color: rgb(4,30,49);
}
.buttonText{
width: 200px;
height: 30px;
right:-250px;
text-align: left;
max-width: inherit;
position: relative;
margin-top: 3%;
}
/*添加 */
li{
list-style: none;
}
ul{
list-style: none;
margin-left: -80px;
}
.btnclass{
padding: 0 12px;
margin: 0;
float: left;
color: rgb(93,204,240);
}
.btnclass:hover{
cursor: pointer;
color: cornflowerblue;
}
如有错误 请及时留言