这段代码已经使用TypeScript进行了改写。请注意,在使用之前确保你的项目环境中有支持TypeScript的构建工具和相关依赖。如果你需要将这段代码用于前端框架(如React、Vue等),还需要与相关框架进行集成,以正确渲染和显示仪表盘。
let colors: string[] = ['#1ac47c', '#ffb661', '#ff625e'];
let option: Option = {
title: {
text: 'CPU使用率',
top: '4%',
x: 'center',
textStyle: { color: '#ffffff', fontSize: 24, fontWeight: 'normal' }
},
series: [
{
type: 'gauge',
min: 0,
max: 100,
axisLine: {
lineStyle: {
width: 36,
color: [
[0.7, colors[0]],
[0.9, colors[1]],
[1, colors[2]]
]
}
},
splitLine:{
length:'12%',
},
axisTick:{ show:false },
axisLabel:{ fontSize :16 ,fontWeight :'bold',distance :10},
pointer:{ width :12 ,length :'60%'},
markPoint:{
animation:false,
silent:true,
data:[
{x:'50%',y:'50%',symbol:'circle',symbolSize:50},
{x:'50%',y:'50%',symbol:'circle',symbolSize:30,itemStyle:{color:'#fff'}}
],
},
detail :{
formatter :'{value}%',
fontSize : 50 ,
fontWeight :'bold'
},
data:[
{
value:95.66,
}
]
}
]
};
interface Option {
title: {
text: string;
top: string;
x: string;
textStyle: { color: string; fontSize: number; fontWeight: string };
};
series: GaugeSeries[];
}
interface GaugeSeries {
type: 'gauge';
min?: number;
max?: number;
axisLine?: AxisLine;
splitLine?: SplitLine;
axisTick?: { show:boolean };
axisLabel?: { fontSize:number ;fontWeight:string ;distance:number};
pointer?: { width :number ,length :string};
markPoint ?: MarkPoint
detail ?:Detail
data ?: DataItem[]
}
interface AxisLine{
lineStyle ?:{
width:number,
color:[number,string] | [[number,string],[number,string]] | [[number,string],[number,string],[number,string]]
}
}
interface SplitLine{
length? :string
}
interface MarkPoint{
animation ?:boolean ,
silent? :boolean,
data ?:MarkPointData[]
}
interface MarkPointData{
x:string,
y:string,
symbol ?:'circle',
symbolSize ?:number,
itemStyle ?:{color:string}
}
interface Detail{
formatter? :string,
fontSize:number,
fontWeight:string
}
interface DataItem{
value:number
}
请注意,这只是代码的类型声明和适配,实际在浏览器中使用时仍需加载ECharts库并进行相应的渲染配置。
内容由零声教学AI助手提供,问题来源于学员提问




