Repository navigation
Expand file tree
/
Copy pathchart.go
More file actions
55 lines (51 loc) · 1.57 KB
/
Copy pathchart.go
File metadata and controls
55 lines (51 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Chart Render API – Go example
// Usage: CHARTQUERY_API_KEY=your-key go run chart.go
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"
)
var client = &http.Client{Timeout: 120 * time.Second}
func call(method, target string, body []byte) []byte {
var reader io.Reader
if body != nil {
reader = bytes.NewReader(body)
}
req, _ := http.NewRequest(method, target, reader)
req.Header.Set("x-api-key", os.Getenv("CHARTQUERY_API_KEY"))
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
if resp.StatusCode >= 400 {
fmt.Fprintf(os.Stderr, "HTTP %d: %s\n", resp.StatusCode, data)
os.Exit(1)
}
return data
}
func main() {
_ = url.Values{}
_ = json.Valid
params := url.Values{}
params.Set("query", "{\"type\":\"bar\",\"title\":\"Monthly Sales\",\"legend\":\"bottom\",\"width\":1200,\"height\":600,\"device_pixel_ratio\":1,\"data\":{\"labels\":[\"Jan\",\"Feb\",\"Mar\",\"Apr\",\"May\",\"Jun\"],\"datasets\":[{\"label\":\"2025\",\"data\":[42,58,37,65,49,72],\"backgroundColor\":\"rgba(99,102,241,0.85)\"},{\"label\":\"2026\",\"data\":[51,67,44,71,58,85],\"backgroundColor\":\"rgba(249,115,22,0.85)\"}]},\"scales\":{\"y\":{\"beginAtZero\":true}}}")
target := "https://api.chartquery.com/v1/chart"
if len(params) > 0 {
target += "?" + params.Encode()
}
data := call("GET", target, nil)
if err := os.WriteFile("chart.png", data, 0o644); err != nil {
panic(err)
}
fmt.Println("Saved chart.png")
}