以下代码利用go的统计图表生成库,通过调用163的沪深历史数据,生成一个统计图表。没有太多科技。
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
charts "github.com/vicanso/go-charts"
)
// 获取历史数据
func WebSearch(url string) (ret []string) {
var lineData string
res, err := http.Get(url)
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
fmt.Printf("status code error: %d %s", res.StatusCode, res.Status)
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
fmt.Println(err)
}
doc.Find(".table_bg001 tbody tr").Each(func(i int, s *goquery.Selection) {
lineData = ""
s.Find("td").Each(func(n int, m *goquery.Selection) {
lineData = fmt.Sprintf("%s %s", lineData, m.Text())
})
ret = append(ret, lineData)
})
return
}
// 保存文件
func writeFile(buf []byte, fileName string) error {
tmpPath := "./"
err := os.MkdirAll(tmpPath, 0700)
if err != nil {
return err
}
file := filepath.Join(tmpPath, fileName)
err = ioutil.WriteFile(file, buf, 0600)
if err != nil {
return err
}
return nil
}
// 错误处理
func OnErr(err error) {
if err != nil {
panic(err)
}
}
// 数组倒序函数
func Reverse(arr *[]string) {
var temp string
length := len(*arr)
for i := 0; i < length/2; i++ {
temp = (*arr)[i]
(*arr)[i] = (*arr)[length-1-i]
(*arr)[length-1-i] = temp
}
}
func MakeChartPic(values [][]float64, x []string, legend []string, title, file string) {
buf, err := ioutil.ReadFile("./msyh.ttf")
OnErr(err)
err = charts.InstallFont("noto", buf)
OnErr(err)
font, _ := charts.GetFont("noto")
charts.SetDefaultFont(font)
p, err := charts.LineRender(
values,
charts.TitleTextOptionFunc(title),
charts.HeightOptionFunc(900),
charts.WidthOptionFunc(1440),
charts.XAxisDataOptionFunc(x),
charts.TitleOptionFunc(charts.TitleOption{Left: "20%"}),
charts.LegendLabelsOptionFunc(legend, charts.PositionCenter),
)
OnErr(err)
buf, err = p.Bytes()
OnErr(err)
writeFile(buf, file)
}
func main() {
fmt.Println("获取数据中...")
var Array_SJ []string
var values1, values2 [][]float64
var SZ, SE []float64 // 上指 上证成交额
var HZ, HE []float64 // 沪指 沪证成交额
ShangZhen := WebSearch("http://quotes.money.163.com/trade/lsjysj_zhishu_000001.html")
HuZhen := WebSearch("http://quotes.money.163.com/trade/lsjysj_zhishu_399001.html")
Reverse(&ShangZhen)
Reverse(&HuZhen)
fmt.Println("生成图形中...")
for _, n := range ShangZhen {
data := strings.Split(strings.ReplaceAll(n, ",", ""), " ")
Array_SJ = append(Array_SJ, strings.ReplaceAll(data[1], "2022", ""))
d, _ := strconv.ParseFloat(data[5], 64)
SZ = append(SZ, d)
d, _ = strconv.ParseFloat(data[9], 64)
SE = append(SE, d/10000/10000)
}
for _, n := range HuZhen {
data := strings.Split(strings.ReplaceAll(n, ",", ""), " ")
//Array_SJ = append(Array_SJ, strings.ReplaceAll(data[1], "2022", ""))
d, _ := strconv.ParseFloat(data[5], 64)
HZ = append(HZ, d)
d, _ = strconv.ParseFloat(data[9], 64)
HE = append(HE, d/10000/10000)
}
fmt.Println("生成成交统计图") // -----------------------------------------------------------------------
values1 = append(values1, SE, HE)
MakeChartPic(values1, Array_SJ, []string{"深成交", "沪成交"}, "成交额", "chart1.png")
fmt.Println("生成指数统计图") // -------------------------------------------------------
values2 = append(values2, SZ, HZ)
MakeChartPic(values2, Array_SJ, []string{"深指", "沪指"}, "指数", "chart2.png")
}