-1

{{result.percentage}} gives me a number from 0 to 100.

If I get 25, I want it to convert to -45 degrees or if I get 0, I want it to convert to -90 degrees or if I get 75, then it gets converted to 45 degrees.

[ngStyle]="{'transform': 'rotate(' + result.percentage + 'deg)'}"

Current, this gives an output style="transform: rotate(25deg);" which I want to convert to style="transform: rotate(-45deg);"

How do I go about this?

FYI, I need to bind the data of this speedometer: https://jsfiddle.net/bcgzrdfL/

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Elaine Byene
  • 3,868
  • 12
  • 50
  • 96

3 Answers3

1

Looks like you need a math whiz, not a framework expert, however I'm going to take a shot at an answer:

[ngStyle]="{'transform': 'rotate(' + ((result.percentage * 1.8) - 90) + 'deg)'}"
Damian C
  • 2,111
  • 2
  • 15
  • 17
1

Something like this?

html

[ngStyle]="{'transform': 'rotate(' + convertedPercentage + 'deg)'"

component

get convertedPercentage() {
  return (this.result.percentage * 1.8) - 90;
}
C. Trenholm
  • 351
  • 1
  • 10
1

from a performance perspective, you're better off calculating the value only when it needs to be calculated, rather than doing the math in template.

styles = {}

setStyles(percentage) {
   this.styles = {
     'transform': 'rotate(' + ((percentage * 1.8) - 90) + 'deg)'
   }
}

call that when you get / assign your result or change your percentage, then use in template like:

[ngStyle]="styles"

the way you have it now, the degrees get calculated on every change detection cycle instead of only when they need to. This is a bad practice that will lead to poor app performance if you keep doing it.

bryan60
  • 28,215
  • 4
  • 48
  • 65