HarmonyOS Canvas绘制曲线相关

目前Canvas是否支持绘制如图所示的曲线图以及绘制曲线图的下方阴影,如果有得话能否给出一个Demo参考。

HarmonyOS
2024-12-26 15:49:06
1655浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

参考示例:

@Entry
@Component
struct IndexView {
  highTemperature: number[] = [21, 16, 17, 21, 21, 22, 24, 22, 25, 23, 24, 26, 25, 24, 24];
  lowTemperature: number[] = [14, 13, 12, 13, 15, 13, 17, 15, 16, 18, 18, 20, 22, 20, 20];
  settings: RenderingContextSettings = new RenderingContextSettings(true)
  context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
  canvasHeight: number = 120;
  itemWidth: number = 50;
  build() {
    Row() {
      Column({ space: 15 }) {
        Text('温度曲线图')
          .fontSize(18)
          .fontWeight(FontWeight.Bold)
        Scroll() {
          Canvas(this.context)
            .width(this.highTemperature.length * this.itemWidth + this.itemWidth)
            .height(this.canvasHeight)
            .backgroundColor('#f1f3f5')
            .onReady(() => {
              // 找出高温曲线的最大值,和低温曲线的最小值,用于后续确定曲线竖直方向的比例
              const maxTemperature: number = Math.max(...this.highTemperature);
              const minTemperature: number = Math.min(...this.lowTemperature);
              // 此处要求绘制的点的上下限之间的差值完全等于画布的高度的70%,也就是max ~ min之间的温度要均匀分布在canvasHeight * 70% = 84之间。
              // step 表示每改变 1℃,纵坐标改变的高度
              const step: number = this.canvasHeight * 0.7 / (maxTemperature - minTemperature);
              // curveChartMargin 表示曲线图大小极值与画布上下边的距离
              const curveChartMargin: number = this.canvasHeight * 0.15;
              // 设置曲线样式
              this.context.lineWidth = 1;
              this.context.font = 'normal bold 35px';
              this.context.fillStyle = '#008000';
              this.context.strokeStyle = '#008000';
              this.context.globalAlpha = 1;
              // 存放坐标的数组
              let xPos: number[] = [];
              let highYPos: number[] = [];
              let lowYPos: number[] = [];
              for (let i: number = 0; i < this.highTemperature.length; i++) {
                // 确定每个点的坐标,包括高温和低温,其中,高温和低温坐标的横坐标都是一致的
                let x: number = (i + 1) * this.itemWidth;
                let yHeight: number = this.canvasHeight - (curveChartMargin + (this.highTemperature[i] - minTemperature) * step);
                let yLow: number = this.canvasHeight - (curveChartMargin + (this.lowTemperature[i] - minTemperature) * step);
                // 存放数据
                xPos.push(x);
                highYPos.push(yHeight);
                lowYPos.push(yLow);
                // 给每个点画出一个圆并填充颜色,这里设置圆的半径为2
                let region: Path2D = new Path2D();
                region.ellipse(x, yHeight, 2, 2, 0, 0, Math.PI * 2);
                region.ellipse(x, yLow, 2, 2, 0, 0, Math.PI * 2);
                this.context.fill(region);
                // 准备气温文字
                let highTemperatureStr: string = `${this.highTemperature[i]}℃`;
                let lowTemperatureStr: string = `${this.lowTemperature[i]}℃`;
                // 绘制高温点文字
                this.context.fillText(highTemperatureStr, x - this.context.measureText(highTemperatureStr)
                  .width / 2, yHeight - 6);
                // 绘制低温点文字
                this.context.fillText(lowTemperatureStr, x - this.context.measureText(lowTemperatureStr)
                  .width / 2, yLow + 15);
              }
              // 绘制高温曲线
              // 首先设置初始点
              this.context.beginPath();
              this.context.moveTo(xPos[0], highYPos[0]);
              for (let i: number = 1; i < xPos.length; i++) {
                let x0: number = i * this.itemWidth;
                let y0: number = this.canvasHeight - (curveChartMargin + (this.highTemperature[i - 1] - minTemperature) * step);
                // 使用三次贝塞尔曲线绘制
                this.context.bezierCurveTo(x0 + (xPos[i] - x0) * 0.3, y0, xPos[i] - (xPos[i] - x0) * 0.3, highYPos[i], xPos[i], highYPos[i]);
              }
              this.context.stroke();
              // 绘制低温曲线
              // 记得首先要设置初始点
              this.context.beginPath();
              this.context.moveTo(xPos[0], lowYPos[0]);
              for (let i: number = 1; i < xPos.length; i++) {
                let x0: number = i * 50;
                let y0: number = this.canvasHeight - (curveChartMargin + (this.lowTemperature[i - 1] - minTemperature) * step);
                // 使用三次贝塞尔曲线
                this.context.bezierCurveTo(x0 + (xPos[i] - x0) * 0.3, y0, xPos[i] - (xPos[i] - x0) * 0.3, lowYPos[i], xPos[i], lowYPos[i]);
              }
              this.context.stroke();
              // 制作闭合图像
              this.context.lineTo(this.highTemperature.length * this.itemWidth + this.itemWidth, this.canvasHeight)
              this.context.lineTo(0, this.canvasHeight + 0)
              // 为了确保线不会显示在页面上,我们将线的宽度设为0,颜色设为完全透明
              this.context.lineWidth = 0;
              this.context.strokeStyle = '#00000000';
              this.context.stroke();
              this.context.fill()
            })
        }
        .scrollable(ScrollDirection.Horizontal)
        .scrollBar(BarState.Off)
      }
      .width('100%')
    }
    .height('100%')
    .border({ width: { bottom: 20 }, color: Color.Black })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
分享
微博
QQ
微信
回复
2024-12-26 17:36:42


相关问题
HarmonyOS Canvas绘制image的API相关问题
799浏览 • 1回复 待解决
HarmonyOS 绘制圆滑折线或绘制曲线
838浏览 • 1回复 待解决
HarmonyOS 地图绘制曲线的点击
609浏览 • 1回复 待解决
HarmonyOS 贝塞尔曲线绘制
989浏览 • 1回复 待解决
HarmonyOS Canvas绘制圆角
952浏览 • 1回复 待解决
HarmonyOS Canvas绘制圆角矩形
1056浏览 • 1回复 待解决
如何操作canvas重新绘制
1938浏览 • 1回复 待解决
HarmonyOS 如何使用canvas绘制虚线
624浏览 • 1回复 待解决
HarmonyOS Canvas绘制内容如何更新
648浏览 • 1回复 待解决
Canvas绘制内容如何动态更新
2828浏览 • 1回复 待解决
HarmonyOS canvas如何绘制成图片导出
738浏览 • 1回复 待解决
如何使用canvas绘制圆角矩形
1266浏览 • 1回复 待解决
arkUI怎么绘制三段贝塞尔曲线
349浏览 • 1回复 待解决
HarmonyOS 日历相关绘制
676浏览 • 1回复 待解决
HarmonyOS 如何在Canvas中直接绘制SVG?
798浏览 • 1回复 待解决
Canvas如何触发刷新重复绘制
1657浏览 • 1回复 待解决
HarmonyOS 如何清空canvas绘制的内容
859浏览 • 1回复 待解决
HarmonyOS Canvas中关于绘制图片问题
1068浏览 • 1回复 待解决