表格和echart二級聯動,並通過點擊echart高亮表徵圖儲存格,echart高亮

來源:互聯網
上載者:User

表格和echart二級聯動,並通過點擊echart高亮表徵圖儲存格,echart高亮

html 部分

 

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.bgc {
background: #08a9f2;
}
tr,td {
cursor: default;
}
.lh{
background: red;
color:#fff;
}
</style>
</head>
<body>


<table width=100% border="1px" cellspacing="0px">
<thead>
<th>名稱</th>
<th chartType=bar>溫度(bar)</th>
<th chartType=bar>高度(bar)</th>
<th chartType=line>銷量(line)</th>
<th chartType=bar>進口(bar)</th>
<th chartType=line>收入(line)</th>
</thead>
<tbody>
<tr>
<td>一月</td>
<td>234</td>
<td>345</td>
<td>123</td>
<td>234</td>
<td>444</td>
</tr>
<tr>
<td>二月</td>
<td>123</td>
<td>234</td>
<td>345</td>
<td>347</td>
<td>285</td>
</tr>
<tr>
<td>三月</td>
<td>345</td>
<td>654</td>
<td>234</td>
<td>654</td>
<td>523</td>
</tr>
<tr>
<td>四月</td>
<td>864</td>
<td>234</td>
<td>647</td>
<td>347</td>
<td>237</td>
</tr>
<tr>
<td>五月</td>
<td>435</td>
<td>737</td>
<td>348</td>
<td>964</td>
<td>285</td>
</tr>
</tbody>
</table>
<!-- 為 ECharts 準備一個具備大小(寬高)的 DOM -->
<div id="main1"><!-- 引入Jq -->
<script src="lib/jQuery/jquery-3.1.1.min.js"></script>
<!-- 引入 echarts -->
<script src="echarts.min.js"></script>
<script>
   // 找到tbody下的tr並遍曆
$('tbody tr').each(function () {
     // 點擊當前這個tr
$(this).click(function () {
       // 當前這行高亮,並去掉同級其他兄弟元素的高亮 
$(this).addClass('bgc').siblings().removeClass('bgc');
       // 當前這行的同級元素的子項目去除高亮(儲存格的) 
$(this).siblings().children().removeClass('lh');
       // 建立4個數組,動態添加到echart裡面的 
var data1 = [], title1 = [], chartType = [], namedata = [];
       // 獲得td 和 th
var td = $(this).find("td"), th = $('th');
       // 遍曆th
td.each(function (i) {
         // 第一個 0索引的不要 其他的添加上去 
if (i != 0)
data1.push($(this).html());
else
title1.push($(this).html());
})
       // 同上 
th.each(function (i) {
if (i != 0)
namedata.push($(this).html());
chartType.push($(this).attr("chartType"));
})
       // 根據上面的參數動態擷取圖 
var myChart = echarts.init(document.getElementById('main1'));
var option = {
xAxis: {
data: namedata,
// axisTick:false // X軸 向下的線
},
yAxis: {
name: '最高時期',
},
series: [
{
name: title1 + '柱狀圖',
type: 'bar',
barWidth: '30%', // 柱子寬度
itemStyle: {
normal: {color: '#08a9f2',} // 柱子顏色
},
data: data1,
markPoint : {
clickable:true,
data : [
{type : 'max', name: '最大值'},
{type : 'min', name: '最小值'}
]
}
},
{
name: title1 + '折線圖',
type: 'line',
itemStyle: {
normal: {color: '#72b201'} // 折線顏色
},
data: data1
}
]
}
myChart.setOption(option);
myChart.on('click', function (params) {
// 點擊到了 markPoint 上
if (params.componentType === 'markPoint')
return false;
// 通過索引高亮當前點擊
$(td).eq(namedata.indexOf(params.name) + 1 ).addClass('lh').siblings().removeClass('lh');
// 通過查詢高亮當前點擊
/*
*
*
*
$(td).each(function(i){
if(params.data == $(this).text())
$(this).addClass('lh')
else
$(this).removeClass('lh')
})
*
*
*
*/
});
})
})
</script>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.