新聞中心
本章是專門為您講解 ECharts 圖表的提示框組件 tooltip 的一些基本的屬性設置,一下是有關于提示框組件的通用介紹:

創(chuàng)新互聯(lián)網(wǎng)絡公司擁有十年的成都網(wǎng)站開發(fā)建設經(jīng)驗,數(shù)千家客戶的共同信賴。提供網(wǎng)站建設、成都網(wǎng)站建設、網(wǎng)站開發(fā)、網(wǎng)站定制、外鏈、建網(wǎng)站、網(wǎng)站搭建、成都響應式網(wǎng)站建設公司、網(wǎng)頁設計師打造企業(yè)風格,提供周到的售前咨詢和貼心的售后服務
提示框組件可以設置在多種地方:
- 可以設置在全局,即 tooltip
- 可以設置在坐標系中,即 grid.tooltip、polar.tooltip、single.tooltip
- 可以設置在系列中,即 series.tooltip
- 可以設置在系列的每個數(shù)據(jù)項中,即 series.data.tooltip
tooltip.show | boolean
[ default: true ]
是否顯示提示框組件,包括提示框浮層和 axisPointer。
tooltip.trigger | string
[ default: 'item' ]
提示框組件的觸發(fā)類型。
可選:
- 'item':數(shù)據(jù)項圖形觸發(fā),主要在散點圖,餅圖等無類目軸的圖表中使用。
- 'axis':坐標軸觸發(fā),主要在柱狀圖,折線圖等會使用類目軸的圖表中使用。在 ECharts 2.x 中只支持類目軸上使用 axis trigger,在 ECharts 3 中支持在直角坐標系和極坐標系上的所有類型的軸。并且可以通過 axisPointer.axis 指定坐標軸。
- 'none':什么都不觸發(fā)。
tooltip.showContent | boolean
[ default: true ]
是否顯示提示框浮層,默認顯示。只需 tooltip 觸發(fā)事件或顯示 axisPointer 而不需要顯示內(nèi)容時可配置該項為 false。
tooltip.alwaysShowContent | boolean
[ default: false ]
是否永遠顯示提示框內(nèi)容,默認情況下在移出可觸發(fā)提示框區(qū)域后 一定時間 后隱藏,設置為 true 可以保證一直顯示提示框內(nèi)容。
該屬性為 ECharts 3.0 中新加。
tooltip.triggerOn | string
[ default: 'mousemove|click' ]
提示框觸發(fā)的條件,可選:
- 'mousemove':鼠標移動時觸發(fā)。
- 'click':鼠標點擊時觸發(fā)。
- 'mousemove|click':同時鼠標移動和點擊時觸發(fā)。
- 'none':不在 'mousemove' 或 'click' 時觸發(fā),用戶可以通過 action.tooltip.showTip 和 action.tooltip.hideTip 來手動觸發(fā)和隱藏。也可以通過 axisPointer.handle 來觸發(fā)或隱藏。
該屬性為 ECharts 3.0 中新加。
tooltip.showDelay | number
[ default: 0 ]
浮層顯示的延遲,單位為 ms,默認沒有延遲,也不建議設置。在 triggerOn 為 'mousemove' 時有效。
tooltip.hideDelay | number
[ default: 100 ]
浮層隱藏的延遲,單位為 ms,在 alwaysShowContent 為 true 的時候無效。
tooltip.enterable | boolean
[ default: false ]
鼠標是否可進入提示框浮層中,默認為false,如需詳情內(nèi)交互,如添加鏈接,按鈕,可設置為 true。
tooltip.confine | boolean
[ default: false ]
是否將 tooltip 框限制在圖表的區(qū)域內(nèi)。
當圖表外層的 dom 被設置為 'overflow: hidden',或者移動端窄屏,導致 tooltip 超出外界被截斷時,此配置比較有用。
tooltip.transitionDuration | number
[ default: 0.4 ]
提示框浮層的移動動畫過渡時間,單位是 s,設置為 0 的時候會緊跟著鼠標移動。
tooltip.position | string, Array, Function
提示框浮層的位置,默認不設置時位置會跟隨鼠標的位置。
可選:
- Array:
通過數(shù)組表示提示框浮層的位置,支持數(shù)字設置絕對位置,百分比設置相對位置。
示例:// 絕對位置,相對于容器左側 10px, 上側 10 px
position: [10, 10]
// 相對位置,放置在容器正中間 position: ['50%', '50%'] - Function:
回調(diào)函數(shù),格式如下
(point: Array, params: Object|Array.參數(shù):
point: 鼠標位置,如 [20, 40]。
params: 同 formatter 的參數(shù)相同。
dom: tooltip 的 dom 對象。
rect: 只有鼠標在圖形上時有效,是一個用x, y, width, height四個屬性表達的圖形包圍盒。
size: 包括 dom 的尺寸和 echarts 容器的當前尺寸,例如:{contentSize: [width, height], viewSize: [width, height]}。
返回值:
可以是一個表示 tooltip 位置的數(shù)組,數(shù)組值可以是絕對的像素值,也可以是相 百分比。
也可以是一個對象,如:{left: 10, top: 30},或者 {right: '20%', bottom: 40}。
如下示例:position: function (point, params, dom, rect, size) {
// 固定在頂部
return [point[0], '10%'];
}或者:
position: function (pos, params, dom, rect, size) {
// 鼠標在左側時 tooltip 顯示到右側,鼠標在右側時 tooltip 顯示到左側。
var obj = {top: 60};
obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 5;
return obj;
} - 'inside':
鼠標所在圖形的內(nèi)部中心位置,只在 trigger 為'item'的時候有效。 - 'top':
鼠標所在圖形上側,只在 trigger 為'item'的時候有效。 - 'left':
鼠標所在圖形左側,只在 trigger 為'item'的時候有效。 - 'right':
鼠標所在圖形右側,只在 trigger 為'item'的時候有效。 - 'bottom':
鼠標所在圖形底側,只在 trigger 為'item'的時候有效。
tooltip.formatter | string, Function
提示框浮層內(nèi)容格式器,支持字符串模板和回調(diào)函數(shù)兩種形式。
1、字符串模板
模板變量有 {a}, ,{c},gdo6jnc,{e},分別表示系列名,數(shù)據(jù)名,數(shù)據(jù)值等。 在 trigger 為 'axis' 的時候,會有多個系列的數(shù)據(jù),此時可以通過 {a0}, {a1}, {a2} 這種后面加索引的方式表示系列的索引。 不同圖表類型下的 {a},,{c},tbxubp7 含義不一樣。 其中變量{a}, , {c}, qpcifdj在不同圖表類型下代表數(shù)據(jù)含義為:
- 折線(區(qū)域)圖、柱狀(條形)圖、K線圖 : {a}(系列名稱),(類目值),{c}(數(shù)值), p2vz9g1(無)
- 散點圖(氣泡)圖 : {a}(系列名稱),(數(shù)據(jù)名稱),{c}(數(shù)值數(shù)組), aqvkpd7(無)
- 地圖 : {a}(系列名稱),(區(qū)域名稱),{c}(合并數(shù)值), gpvjfms(無)
- 餅圖、儀表盤、漏斗圖: {a}(系列名稱),(數(shù)據(jù)項名稱),{c}(數(shù)值), s2j8gds(百分比)
更多其它圖表模板變量的含義可以見相應的圖表的 label.normal.formatter 配置項。
示例:
formatter: '{b0}: {c0}
{b1}: {c1}'2、回調(diào)函數(shù)
回調(diào)函數(shù)格式:
(params: Object|Array, ticket: string, callback: (ticket: string, html: string)) => string第一個參數(shù) params 是 formatter 需要的數(shù)據(jù)集。格式如下:
{
componentType: 'series',
// 系列類型
seriesType: string,
// 系列在傳入的 option.series 中的 index
seriesIndex: number,
// 系列名稱
seriesName: string,
// 數(shù)據(jù)名,類目名
name: string,
// 數(shù)據(jù)在傳入的 data 數(shù)組中的 index
dataIndex: number,
// 傳入的原始數(shù)據(jù)項
data: Object,
// 傳入的數(shù)據(jù)值
value: number|Array,
// 數(shù)據(jù)圖形的顏色
color: string,
// 餅圖的百分比
percent: number,
//
galleryViewPath: ,
//
galleryEditorPath: ,
//
imagePath: ,
//
gl: ,
}在 trigger 為 'axis' 的時候,或者 tooltip 被 axisPointer 觸發(fā)的時候,params 是多個系列的數(shù)據(jù)數(shù)組。其中每項內(nèi)容格式同上,并且,
{
componentType: 'series',
// 系列類型
seriesType: string,
// 系列在傳入的 option.series 中的 index
seriesIndex: number,
// 系列名稱
seriesName: string,
// 數(shù)據(jù)名,類目名
name: string,
// 數(shù)據(jù)在傳入的 data 數(shù)組中的 index
dataIndex: number,
// 傳入的原始數(shù)據(jù)項
data: Object,
// 傳入的數(shù)據(jù)值
value: number|Array,
// 數(shù)據(jù)圖形的顏色
color: string,
}注: ECharts 2.x 使用數(shù)組表示各參數(shù)的方式不再支持。
第二個參數(shù) ticket 是異步回調(diào)標識,配合第三個參數(shù) callback 使用。 第三個參數(shù) callback 是異步回調(diào),在提示框浮層內(nèi)容是異步獲取的時候,可以通過 callback 傳入上述的 ticket 和 html 更新提示框浮層內(nèi)容。
示例:
formatter: function (params, ticket, callback) {
$.get('detail?name=' + params.name, function (content) {
callback(ticket, toHTML(content));
});
return 'Loading';
}tooltip.backgroundColor | Color
[ default: 'rgba(50,50,50,0.7)' ]
設置提示框浮層的背景顏色。
tooltip.borderColor | Color
[ default: '#333' ]
設置提示框浮層的邊框顏色。
tooltip.borderWidth | number
[ default: 0 ]
設置提示框浮層的邊框?qū)挕?/p>
tooltip.padding | number
[ default: 5 ]
設置提示框浮層內(nèi)邊距,單位為 px,默認各方向內(nèi)邊距為 5px,接受數(shù)組分別設定上右下左邊距。
使用示例:
// 設置內(nèi)邊距為 5
padding: 5
// 設置上下的內(nèi)邊距為 5,左右的內(nèi)邊距為 10
padding: [5, 10]
// 分別設置四個方向的內(nèi)邊距
padding: [
5, // 上
10, // 右
5, // 下
10, // 左
]tooltip.textStyle | Object
設置提示框浮層的文本樣式。詳細的文字樣式設置見:提示框組件文字的樣式
tooltip.extraCssText | string
額外附加到浮層的 css 樣式。如下為浮層添加陰影的示例:
extraCssText:'box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);' 文章名稱:創(chuàng)新互聯(lián)ECharts教程:ECharts提示框組件屬性
網(wǎng)頁路徑:http://www.5511xx.com/article/djeisjp.html


咨詢
建站咨詢
