Elaine Byene
Elaine Byene

Reputation: 4142

Convert numbers to degrees in Angular 8

I'm getting 25 from the API which must be converted to -45 and the end result must be:

<div style="transform: rotate(-45deg);"> 

My current code looks like this:

<div style="transform: rotate({{data.angle}}deg);"> 

Basically, I need to convert

0   to -90
25  to -45
50  to   0
75  to  45
100 is  90

FYI, I'm creating a speedometer. You may see the HTML demo: https://jsfiddle.net/bcgzrdfL/

Upvotes: 0

Views: 384

Answers (1)

Yurii
Yurii

Reputation: 4911

This is more of a mathematics problem than programming one. Linear equation y = a*x + b satisfies your transformation conditions. After solving it, given your inputs you'll get the following function:

toDegrees(input: number): number {
  return 1.8 * input - 90;
}

And the usage in the template:

<div style="transform: rotate({{toDegrees(data.angle)}}deg);"> 

Upvotes: 4

Related Questions