在 OpenHarmony(ArkUI) 中,图表样式的设置方式取决于你使用的是哪种图表方案。下面按常见情况给你说明。
Charts 组件(OpenHarmony 3.2+,如 LineChart / BarChart)OpenHarmony 提供了基础图表组件(部分版本在 @ohos/charts 或 sample 中)。
import { LineChart, LineChartModel } from '@ohos/charts'
@Entry
@Component
struct ChartExample {
build() {
Column() {
LineChart({
model: new LineChartModel()
.setData([10, 20, 15, 30, 25])
.setXAxisData(["Mon", "Tue", "Wed", "Thu", "Fri"])
})
}
}
}
LineChartModel()
.setLineColor(Color.Blue)
.setLineWidth(3)
.setPointColor(Color.Red)
.setPointRadius(5)
.setXAxisColor(Color.Gray)
.setYAxisColor(Color.Gray)
.setAxisWidth(1)
.setBackgroundColor('#F5F5F5')
Canvas 自定义图表(最灵活 ✅)OpenHarmony 推荐方式之一。
@Entry
@Component
struct CanvasChart {
private settings: RenderingContextSettings = new RenderingContextSettings(true)
private ctx: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
build() {
Canvas(this.ctx)
.width('100%')
.height(300)
.onReady(() => {
this.ctx.strokeStyle = '#007DFF' // 线条颜色
this.ctx.lineWidth = 3 // 线条宽度
this.ctx.beginPath()
this.ctx.moveTo(50, 250)
this.ctx.lineTo(150, 150)
this.ctx.lineTo(250, 200)
this.ctx.stroke()
})
}
}
✅ 可完全控制:
option = {
color: ['#007DFF'],
lineStyle: {
width: 2,
type: 'solid'
},
xAxis: {
axisLine: { lineStyle: { color: '#999' } }
}
}
| 需求 | 推荐方式 |
|---|---|
| 简单图表 | Charts 组件 |
| 高度自定义 | Canvas |
| 复杂交互 | ECharts / 三方库 |
如果你能告诉我:
我可以直接给你完整可运行示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。