Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Convert the string values in float64 for asciigraph
Browse files Browse the repository at this point in the history
The graph requires the input data type to float64, which requires me to
first convert and then round to two decimal places.
  • Loading branch information
jasonBirchall committed Jan 14, 2021
1 parent 0c52477 commit c280ca6
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions cmd/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ package cmd
import (
"encoding/json"
"fmt"
"math"
"strconv"

"github.com/guptarohit/asciigraph"
api "github.com/jasonbirchall/crypto-tracker/internal/api"
"github.com/spf13/cobra"
)
Expand All @@ -41,9 +44,9 @@ var graphCmd = &cobra.Command{
return err
}

// graph := asciigraph.Plot(data, asciigraph.Height(height))
graph := asciigraph.Plot(data, asciigraph.Height(height))

fmt.Println(data)
fmt.Println(graph)

return nil
},
Expand All @@ -53,9 +56,9 @@ var graphCmd = &cobra.Command{
// graph command. It queries the API package and collates a collection of
// float64 data type. This collection is then returned back to the command
// function.
func getData(coin string) ([]string, error) {
func getData(coin string) ([]float64, error) {
var p Price
var arr []string
var arr []float64

data, err := api.Query(coin)

Expand All @@ -64,8 +67,17 @@ func getData(coin string) ([]string, error) {
return arr, err
}

// Iterate over all strings in struct and convert to float64, which
// is requried by the asciigraph package.
for _, v := range p.DataPoints {
fmt.Println(v)
s, err := strconv.ParseFloat(v, 64)
if err != nil {
return arr, err
}

r := math.Round(s*100) / 100

arr = append(arr, r)
}

return arr, nil
Expand Down

0 comments on commit c280ca6

Please sign in to comment.