ComponentDefinitions:
  Sparkline:
    DefinitionType: CanvasComponent
    Description: Line chart or sparkline drawn as SVG from a table with a Value column. No chart control, works with the Standard license.
    CustomProperties:
      Points:
        PropertyKind: Input
        DisplayName: Points
        DataType: Table
        RaiseOnReset: false
        Description: Table with one numeric column named Value, in chronological order. Use RenameColumns or ForAll to map your own column.
        Default: '=Table({Value: 12}, {Value: 18}, {Value: 14}, {Value: 22}, {Value: 19}, {Value: 27}, {Value: 25}, {Value: 31})'
      LineColor:
        PropertyKind: Input
        DisplayName: LineColor
        DataType: Text
        RaiseOnReset: false
        Description: Hex color of the line, for example #1F6FEB.
        Default: ="#1F6FEB"
      ShowArea:
        PropertyKind: Input
        DisplayName: ShowArea
        DataType: Boolean
        RaiseOnReset: false
        Description: Fills the area below the line with a light tint.
        Default: =true
      StrokeWidth:
        PropertyKind: Input
        DisplayName: StrokeWidth
        DataType: Number
        RaiseOnReset: false
        Description: Line width in pixels.
        Default: =2
      LastValue:
        PropertyKind: Output
        DisplayName: LastValue
        DataType: Number
        Description: The last value of the table.
    Properties:
      Fill: =RGBA(0, 0, 0, 0)
      Height: =60
      Width: =200
      LastValue: =Last(Sparkline.Points).Value
    Children:
      - imgSparkSvg:
          Control: Image
          Properties:
            TabIndex: =0
            AccessibleLabel: ="Trend line with " & CountRows(Sparkline.Points) & " points"
            Height: =Sparkline.Height
            Image: |-
              =With(
                  {
                      vals: Sparkline.Points,
                      w: 200,
                      h: 60,
                      pad: 4
                  },
                  With(
                      {
                          n: CountRows(vals),
                          lo: Min(vals, Value),
                          hi: Max(vals, Value)
                      },
                      If(
                          n < 2,
                          "",
                          With(
                              {
                                  pts: Concat(
                                      Sequence(n),
                                      Text((Value - 1) / (n - 1) * w, "0.0", "en-US") & "," & Text(h - pad - (Index(vals, Value).Value - lo) / Max(hi - lo, 0.000001) * (h - 2 * pad), "0.0", "en-US"),
                                      " "
                                  )
                              },
                              "data:image/svg+xml;utf8," & EncodeUrl(
                                  "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 200 60' preserveAspectRatio='none'>" &
                                  If(Sparkline.ShowArea, "<polygon points='0," & h & " " & pts & " " & w & "," & h & "' fill='" & Sparkline.LineColor & "' fill-opacity='0.12'/>", "") &
                                  "<polyline points='" & pts & "' fill='none' stroke='" & Sparkline.LineColor & "' stroke-width='" & Sparkline.StrokeWidth & "' stroke-linejoin='round' stroke-linecap='round' vector-effect='non-scaling-stroke'/></svg>"
                              )
                          )
                      )
                  )
              )
            ImagePosition: =ImagePosition.Stretch
            Width: =Sparkline.Width
            X: =0
            Y: =0
