前言
样式
直接样式
这些样式一般都可以设置颜色或者背景或者字体等样式, 他们会覆盖主题中的样式 itemStyle textStyle lineStyle areaStyle label
<! DOCTYPE html >
< html lang = " en" > < head> < meta charset = " UTF-8" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < meta http-equiv = " X-UA-Compatible" content = " ie=edge" > < title> 样式</ title> < script src = " https://cdn.jsdelivr.net/npm/echarts@5.4.1/dist/echarts.min.js" > </ script>
</ head> < body>
< div id = ' app' style = " width : 600px; height : 400px" > </ div>
< script> var mCharts = echarts. init ( document. getElementById ( "app" ) ) var pieData = [ { name : 'pass' , value : 80 , itemStyle : { color : '#00FF00' } } , { name : 'fail' , value : 10 } , { name : 'skip' , value : 5 , itemStyle : { color : '#00FFFF' } } , { name : 'error' , value : 5 , label : { color : '#FF0000' } , itemStyle : { color : '#FF0000' } } ] var option = { title : { text : '测试结果' , textStyle : { color : 'blue' } } , series : [ { type : 'pie' , data : pieData, label : { show : true , formatter : function ( arg ) { console. log ( arg) return arg. data. name + '\n' + arg. percent + '%' } } , selectedMode : 'multiple' , selectedOffset : 30 , radius : [ '50%' , '80%' ] } ] } mCharts. setOption ( option)
</ script>
</ body> </ html>
高亮样式
鼠标滑过或者点击形成的高亮状态. 而高亮样式是针对于元素的高亮状态设定的样式
<! DOCTYPE html >
< html lang = " en" > < head> < meta charset = " UTF-8" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < meta http-equiv = " X-UA-Compatible" content = " ie=edge" > < title> 样式</ title> < script src = " https://cdn.jsdelivr.net/npm/echarts@5.4.1/dist/echarts.min.js" > </ script>
</ head> < body>
< div id = ' app' style = " width : 600px; height : 400px" > </ div>
< script> var mCharts = echarts. init ( document. getElementById ( "app" ) ) var pieData = [ { name : 'pass' , value : 80 , itemStyle : { color : '#00FF00' } } , { name : 'fail' , value : 10 } , { name : 'skip' , value : 5 , itemStyle : { color : '#00FFFF' } , emphasis : { itemStyle : { color : 'pink' } } } , { name : 'error' , value : 5 , label : { color : '#FF0000' } , itemStyle : { color : '#FF0000' } } ] var option = { title : { text : '测试结果' , textStyle : { color : 'blue' } } , series : [ { type : 'pie' , data : pieData, label : { show : true , formatter : function ( arg ) { console. log ( arg) return arg. data. name + '\n' + arg. percent + '%' } } , selectedMode : 'multiple' , selectedOffset : 30 , radius : [ '50%' , '80%' ] } ] } mCharts. setOption ( option)
</ script>
</ body> </ html>
自适应
监听窗口大小变化事件 在事件处理函数中调用 ECharts 实例对象的 resize 即可
<! DOCTYPE html >
< html lang = " en" > < head> < meta charset = " UTF-8" > < meta name = " viewport" content = " width=device-width, initial-scale=1.0" > < meta http-equiv = " X-UA-Compatible" content = " ie=edge" > < title> 自适应</ title> < script src = " https://cdn.jsdelivr.net/npm/echarts@5.4.1/dist/echarts.min.js" > </ script>
</ head> < body>
< div id = ' app' style = " height : 600px" > </ div>
< script> var myCharts = echarts. init ( document. getElementById ( 'app' ) ) var xDataArr = [ '张三' , '李四' , '王五' , '大明白' , '小糊涂' ] var yDataArr = [ 88 , 92 , 63 , 77 , 94 ] var option = { xAxis : { type : 'category' , data : xDataArr} , yAxis : { type : 'value' } , series : [ { name : '分数' , type : 'bar' , data : yDataArr, markPoint : { data : [ { type : 'max' , name : '最大值' } , { type : 'min' , name : '最小值' } ] } , markLine : { data : [ { type : 'average' , name : '平均值' } ] } , label : { show : true , rotate : 30 , } , barWidth : '30%' } ] } myCharts. setOption ( option) window. onresize = function ( ) { myCharts. resize ( ) }
</ script>
</ body> </ html>