PoshCode Logo PowerShell Code Repository

ConvertTo-GoogleChartNum (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/686"></script>download | new post

A rounding encoder for Google Charts data: converts numbers to their relative values within Google’s 0..4095 range, and encodes them in their slightly strange 2-digit base 64 encoding.

  1. ## Google Chart API extended value encoding function
  2. #########################################################################
  3. #function ConvertTo-GoogleChartNum{
  4. BEGIN {
  5.    ## Google's odydecody is a 64 character array
  6.    $ody = "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9","-","."
  7.  
  8.    ## The actual filter function
  9.    filter encode {
  10.       # we have a hard-coded "overflow" value
  11.       if($_ -ge ($ody.Count * $ody.Count) ) { return "__" }
  12.       $y = -1  # $y is a ref variable, so it has to be defined
  13.       $x = [Math]::DivRem( $_, $ody.Count, [ref]$y )
  14.       return "$($ody[$x])$($ody[$y])"
  15.    }
  16.    ## Handle numbers as parameters
  17.    [int[]]$nums = $args | % { [int]$_ }
  18. }
  19. ## Or handle numbers from the pipeline. We don't care :-)
  20. PROCESS {
  21.    if($_ -ne $null) { $nums += $_ }
  22. }
  23. #}
  24.  
  25. END {
  26.    $diff($nums | sort  | select -last 1) / ($ody.Count * $ody.Count -1)
  27.    $nums | %{$_/$diff} | encode
  28. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me