HarmonyOS Next 有遮盖的折线图

最近在学习鸿蒙App开发,刚好学到画折线图这部分,也有一些第三方库,功能确实很强大,基本都满足一般的需要。本着学习的态度,弄清楚原理,就手动做了demo,以备记录。好了,废话少说,直接上代码。

`@Entry @Component export struct TestChartPage { private settings: RenderingContextSettings = new RenderingContextSettings(true) private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) private readonly POINT_COLOR = '#ff0000' private readonly POINT_RADIUS = 4 @State points:number[][]= [] aboutToAppear(): void {

ini 复制代码
let randomArray:number[][]= []
const count = 7
const maxWidth = 350
const offset = maxWidth / (count - 1)

for (let i = 0;i < count;i++){
  let x = offset * i
  if (x == 0) {
    x = 10
  }
  let y = Math.floor(Math.random() * 300) + 1;
  randomArray.push([x,y])
}

this.points = randomArray

}

// 绘制数据点 点的位置不是很准确,有待优化。 private drawDataPoints() { this.points.forEach(point => { this.context.beginPath() this.context.arc(point[0], point[1], this.POINT_RADIUS, 0, Math.PI * 2) this.context.fillStyle = this.POINT_COLOR this.context.fill() }) }

build() { Column() { Canvas(this.context) .backgroundColor(Color.Orange) .width('100%') .height(400) .onReady(() => { this.context.strokeStyle = '#0078ff'; this.context.lineWidth = 2;

ini 复制代码
      this.context.beginPath();
      this.context.moveTo(this.points[0][0], this.points[0][1]);

      for (let i = 0; i < this.points.length - 1; i++) {
        const p0 = this.points[i];
        const p1 = this.points[i + 1];
        const t0 = this.points[i];
        const t1 = this.points[i + 1];
        const k = 1/3;

        const x1 = p0[0] + t0[0] * k;
        const y1 = p0[1] + t0[1] * k;

        const x2 = p1[0] - t1[0] * k;
        const y2 = p1[1] - t1[1] * k;

        this.context.bezierCurveTo(x1, y1,x2, y2, p1[0], p1[1]);
      }

      this.context.lineTo(this.points[this.points.length - 1][0],400)
      this.context.lineTo(this.points[0][0],400)
      this.context.lineTo(this.points[0][0],this.points[0][1])

      //  创建渐变填充
      const gradient = this.context.createLinearGradient(0, 0, 0, 400);
      gradient.addColorStop(0, 'rgba(135, 206, 235, 0.5)'); // 半透明蓝色
      gradient.addColorStop(1, 'rgba(135, 206, 235, 1)'); // 完全透明
      this.context.closePath();
      this.context.fillStyle = gradient;
      this.context.fill();
      
      this.context.beginPath();
      this.context.moveTo(this.points[0][0], this.points[0][1]);
      for (let i = 0; i < this.points.length - 1; i++) {
        const p0 = this.points[i];
        const p1 = this.points[i + 1];
        const t0 = this.points[i];
        const t1 = this.points[i + 1];
        const k = 1/3;

        const x1 = p0[0] + t0[0] * k;
        const y1 = p0[1] + t0[1] * k;

        const x2 = p1[0] - t1[0] * k;
        const y2 = p1[1] - t1[1] * k;

        this.context.bezierCurveTo(x1, y1,x2, y2, p1[0], p1[1]);
      }
      this.context.stroke();
      // 绘制数据点
      this.drawDataPoints()
    })
}
.height('100%')
.width('100%')

} } `

最终的效果图如下:

相关推荐
Zero1017134 分钟前
【React的useMemo钩子详解】
前端·react.js·前端框架
养军博客6 分钟前
spring boot3.0自定义校验注解:文章状态校验示例
java·前端·spring boot
uperficialyu17 分钟前
2025年01月10日浙江鑫越系统科技前端面试
前端·科技·面试
付朝鲜41 分钟前
用自写的jQuery库+Ajax实现了省市联动
java·前端·javascript·ajax·jquery
coderYYY1 小时前
多个el-form-item两列布局排齐且el-select/el-input组件宽度撑满
前端·javascript·vue.js·elementui·前端框架
荔枝吖1 小时前
项目中会出现的css样式
前端·css·html
Dontla1 小时前
何时需要import css文件?怎么知道需要导入哪些css文件?为什么webpack不提示CSS导入?(导入css导入规则、css导入规范)
前端·css·webpack
小堃学编程2 小时前
前端学习(2)—— CSS详解与使用
前端·css·学习
蓝婷儿2 小时前
第一章:HTML基石·现实的骨架
前端·html
Watermelo6172 小时前
前端如何应对精确数字运算?用BigNumber.js解决JavaScript原生Number类型在处理大数或高精度计算时的局限性
开发语言·前端·javascript·vue.js·前端框架·vue·es6