export const drawLine = ( ctx , startX , startY , endX , endY , color ? ) => { if ( color ) { ctx . save ( ) ; ctx . strokeStyle = color ; } ctx . beginPath ( ) ; ctx . moveTo ( startX + 0.5 , startY + 0.5 ) ; ctx . lineTo ( endX + 0.5 , endY + 0.5 ) ; ctx . closePath ( ) ; ctx . stroke ( ) ; if ( color ) { ctx . restore ( ) ; } } ; export const drawRect = ( ctx , startX , startY , width , height , color ? ) => { if ( color ) { ctx . save ( ) ; ctx . fillStyle = color ; } ctx . fillRect ( startX , startY , width , height ) ; if ( color ) { ctx . restore ( ) ; } } ; export const text2Ellipsis = ( ctx , str , maxWidth , ellipsis = '…' ) => { if ( typeof str !== 'string' ) { return str ; } const len = str . length ; if ( len === 0 ) { return str ; } let ellipsisWidth = ctx . measureText ( ellipsis ) . width ; let maxStr = '' ; let maxStrWidth = 0 ; let i = 0 ; do { i ++ ; maxStr = str . substring ( 0 , i ) ; maxStrWidth = ctx . measureText ( maxStr ) . width ; } while ( maxStrWidth < maxWidth && i < len ) ; if ( i === len && ( maxStrWidth <= maxWidth || maxStrWidth <= ellipsisWidth ) ) { return maxStr ; } else { while ( maxStrWidth + ellipsisWidth >= maxWidth && i > 0 ) { maxStr = maxStr . substring ( 0 , i ) ; maxStrWidth = ctx . measureText ( maxStr ) . width ; i -- ; } return maxStr . substring ( 0 , i ) + ellipsis ; } } ; export const noData = ( ctx , x , y , w , h , originW = 140 ) => { let originH = originW + originW * 0.4 ; let centerX = x + w / 2 ; let centerY = y + h / 2 - ( originH - originW ) ; ctx . save ( ) ; ctx . fillStyle = '#ebebeb' ; ctx . beginPath ( ) ; ctx . arc ( centerX , centerY , originW / 2 , 0 , Math . PI * 2 , false ) ; ctx . fill ( ) ; ctx . strokeStyle = '#ffffff' ; ctx . lineWidth = originW / 20 ; ctx . beginPath ( ) ; let rectWidth = originW / 3 ; let degree = 30 ; let radians = Math . PI / ( 180 / degree ) ; let cosWidth = Math . cos ( radians ) * rectWidth ; let sinWidth = Math . sin ( radians ) * rectWidth ; ctx . save ( ) ; ctx . beginPath ( ) ; ctx . lineTo ( centerX , centerY - sinWidth * 2 ) ; ctx . lineTo ( centerX + cosWidth , centerY - sinWidth ) ; ctx . lineTo ( centerX + cosWidth , centerY - sinWidth + rectWidth ) ; ctx . lineTo ( centerX , centerY + rectWidth ) ; ctx . lineTo ( centerX - cosWidth , centerY - sinWidth + rectWidth ) ; ctx . lineTo ( centerX - cosWidth , centerY - sinWidth ) ; ctx . closePath ( ) ; ctx . stroke ( ) ; ctx . beginPath ( ) ; ctx . lineTo ( centerX , centerY ) ; ctx . lineTo ( centerX - cosWidth , centerY - sinWidth ) ; ctx . stroke ( ) ; ctx . beginPath ( ) ; ctx . lineTo ( centerX , centerY ) ; ctx . lineTo ( centerX + cosWidth , centerY - sinWidth ) ; ctx . stroke ( ) ; ctx . beginPath ( ) ; ctx . lineTo ( centerX , centerY ) ; ctx . lineTo ( centerX , centerY + rectWidth ) ; ctx . stroke ( ) ; ctx . beginPath ( ) ; ctx . lineTo ( centerX + cosWidth / 2 , centerY - sinWidth / 2 ) ; ctx . lineTo ( centerX - cosWidth / 2 , centerY - 3 * sinWidth / 2 ) ; ctx . stroke ( ) ; ctx . restore ( ) ; let fontSize = originW * 0.2 ; ctx . fillStyle = '#cecece' ; ctx . textAlign = 'center' ; ctx . textBaseline = 'middle' ; ctx . font = fontSize + 'px -apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Hiragino Sans GB","Microsoft YaHei","Helvetica Neue",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol"' ; ctx . fillText ( '暂无数据' , centerX , centerY + originW / 2 + ( originH - originW ) / 2 ) ; ctx . restore ( ) ; } ;