go-echarts生成html


前言

输入数据生成坐标轴


一、代码

package main

import (
	"encoding/json"
	"fmt"
	"github.com/go-echarts/go-echarts/v2/charts"
	"github.com/go-echarts/go-echarts/v2/components"
	"github.com/go-echarts/go-echarts/v2/opts"
	"github.com/go-toast/toast"
	"io"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strconv"
	"time"
)

var folderPath string
var ecPath string

func genLine3dData(s []int) []opts.LineData {
	data := make([][2]int, 0)
	for index, value := range s {
		data = append(data, [2]int{index, value})
	}
	ret := make([]opts.LineData, 0, len(data))
	for _, d := range data {
		ret = append(ret, opts.LineData{Value: []interface{}{d[0], d[1]}})
	}
	return ret
}

func line3DBase(s Info) *charts.Line {
	var a opts.AxisPointer
	a.Type = "line"
	a.Snap = true
	line := charts.NewLine()
	line.SetGlobalOptions(
		charts.WithTitleOpts(opts.Title{Title: s.Title}),
		charts.WithTooltipOpts(opts.Tooltip{
			Show:        true,
			Trigger:     "axis",
			TriggerOn:   "",
			Formatter:   "",
			AxisPointer: &a,
		}),
	)
	line.AddSeries("", genLine3dData(s.Data))
	line.SetSeriesOptions(charts.WithLineChartOpts(opts.LineChart{
		Smooth: true,
	}))

	return line
}

func unixToStr(timeUnix int64, layout string) string {
	timeStr := time.Unix(timeUnix, 0).Format(layout)
	return timeStr
}
func main() {
	path, _ := os.Getwd()
	infos := getProperties(path)
	var count = 0
	for _, info := range infos {

		count++
		create(info, path)
	}
	//创建assets文件

	//copy一份js到
	ecPath = filepath.Join(filepath.Join(path, "assets"), "ec.jsecharts.min.js")
	join := filepath.Join(folderPath, "assets")
	os.Mkdir(join, 0777)
	// 再修改权限
	os.Chmod(join, 0777)
	//c path
	cpath := filepath.Join(join, "ec.jsecharts.min.js")
	os.Create(cpath)

	CopyFile(cpath, ecPath)

	str := "生成结束,共生成" + strconv.Itoa(count) + "张图像"
	notification := toast.Notification{
		AppID:   "Microsoft.Windows.Shell.RunDialog",
		Title:   "智能画图",
		Message: str,
	}
	err := notification.Push()
	if err != nil {
		log.Fatalln(err)
	}

}
func CopyFile(dst, src string) {
	data, err := ioutil.ReadFile(src)
	if err != nil {
		fmt.Println("err 有", err)
		return
	}
	err = ioutil.WriteFile(dst, data, 0666)
	if err != nil {
		fmt.Println("写文件出错了", err)
	}
}

func create(s Info, path string) {
	page := components.NewPage()
	page.AddCustomizedCSSAssets()
	page.AddCharts(
		line3DBase(s),
	)
	nowUnix := time.Now().Unix() //获取当前时间戳
	nowStr := unixToStr(nowUnix, "2006-01-02")

	folderPath = filepath.Join(path, nowStr+"曲线图")

	os.Mkdir(folderPath, 0777)
	// 再修改权限
	os.Chmod(folderPath, 0777)

	folderPath1 := filepath.Join(folderPath, s.Title+".html")

	f, err := os.Create(folderPath1)
	if err != nil {
		panic(err)
	}
	page.Render(io.MultiWriter(f))
}
func getProperties(path string) []Info {
	path = path + "/info.json"
	fileHanle, _ := os.OpenFile(path, os.O_RDONLY, 0666)
	defer fileHanle.Close()
	// 按行处理txt
	all, _ := ioutil.ReadAll(fileHanle)
	var infos = make([]Info, 1)
	json.Unmarshal(all, &infos)
	return infos
}

type Info struct {
	Title string `json:"title,omitempty"`
	Data  []int  `json:"data,omitempty"`
}

二、info.json

[
  {"title":"y=x", "data":[1,2,3,4,5,6,7,8,9,10]},
  {"title":"y=2x", "data":[2,4,6,8,10]},
  {"title":"y=x的平方", "data":[0,1,4,9,16]}
]

三、图片

在这里插入图片描述

四、改进

只有正坐标系是不符合宝老师的要求的,进行改进

package main

import (
	"encoding/json"
	"fmt"
	"github.com/go-echarts/go-echarts/v2/charts"
	"github.com/go-echarts/go-echarts/v2/components"
	"github.com/go-echarts/go-echarts/v2/opts"
	"github.com/go-toast/toast"
	"io"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strconv"
	"time"
)

var folderPath string
var ecPath string

func genLine3dData(s []int, m bool) []opts.LineData {
	data := make([][2]int, 0)

	for i := len(s) - 1; i >= 0; i-- {
		var ss = s[i]
		if !m {
			ss = -s[i]
		}
		data = append(data, [2]int{-i, ss})
	}

	for index, value := range s {
		data = append(data, [2]int{index, value})
	}

	ret := make([]opts.LineData, 0, len(data))
	for _, d := range data {
		ret = append(ret, opts.LineData{Value: []interface{}{d[0], d[1]}})
	}
	return ret
}

func line3DBase(s Info) *charts.Line {
	var a opts.AxisPointer
	a.Type = "line"
	a.Snap = true
	line := charts.NewLine()
	line.SetGlobalOptions(
		charts.WithTitleOpts(opts.Title{Title: s.Title}),
		charts.WithTooltipOpts(opts.Tooltip{
			Show:        true,
			Trigger:     "axis",
			TriggerOn:   "",
			Formatter:   "",
			AxisPointer: &a,
		}),
	)
	line.AddSeries("", genLine3dData(s.Data, s.Mark))
	line.SetSeriesOptions(charts.WithLineChartOpts(opts.LineChart{
		Smooth: true,
	}))

	return line
}

func unixToStr(timeUnix int64, layout string) string {
	timeStr := time.Unix(timeUnix, 0).Format(layout)
	return timeStr
}
func main() {
	path, _ := os.Getwd()
	infos := getProperties(path)
	var count = 0
	for _, info := range infos {

		count++
		create(info, path)
	}
	//创建assets文件

	//copy一份js到
	ecPath = filepath.Join(filepath.Join(path, "assets"), "ec.jsecharts.min.js")
	join := filepath.Join(folderPath, "assets")
	os.Mkdir(join, 0777)
	// 再修改权限
	os.Chmod(join, 0777)
	//c path
	cpath := filepath.Join(join, "ec.jsecharts.min.js")
	os.Create(cpath)

	CopyFile(cpath, ecPath)

	str := "生成结束,共生成" + strconv.Itoa(count) + "张图像"
	notification := toast.Notification{
		AppID:   "Microsoft.Windows.Shell.RunDialog",
		Title:   "智能画图",
		Message: str,
	}
	err := notification.Push()
	if err != nil {
		log.Fatalln(err)
	}

}
func CopyFile(dst, src string) {
	data, err := ioutil.ReadFile(src)
	if err != nil {
		fmt.Println("err 有", err)
		return
	}
	err = ioutil.WriteFile(dst, data, 0666)
	if err != nil {
		fmt.Println("写文件出错了", err)
	}
}

func create(s Info, path string) {
	page := components.NewPage()
	page.AddCustomizedCSSAssets()
	page.AddCharts(
		line3DBase(s),
	)
	nowUnix := time.Now().Unix() //获取当前时间戳
	nowStr := unixToStr(nowUnix, "2006-01-02")

	folderPath = filepath.Join(path, nowStr+"曲线图")

	os.Mkdir(folderPath, 0777)
	// 再修改权限
	os.Chmod(folderPath, 0777)

	folderPath1 := filepath.Join(folderPath, s.Title+".html")

	f, err := os.Create(folderPath1)
	if err != nil {
		panic(err)
	}
	page.Render(io.MultiWriter(f))
}
func getProperties(path string) []Info {
	path = path + "/info.json"
	fileHanle, _ := os.OpenFile(path, os.O_RDONLY, 0666)
	defer fileHanle.Close()
	// 按行处理txt
	all, _ := ioutil.ReadAll(fileHanle)
	var infos = make([]Info, 1)
	json.Unmarshal(all, &infos)
	return infos
}

type Info struct {
	Title string `json:"title"`
	Data  []int  `json:"data"`
	Mark  bool   `json:"mark"`
}

五、info.json

[
  {"title":"y=x", "data":[0,1,2,3,4,5,6,7,8,9,10], "mark":false},
  {"title":"y=2x", "data":[0,2,4,6,8,10], "mark":false},
  {"title":"y=x的平方", "data":[0,1,4,9,16], "mark":true},
  {"title":"y=-2x^2", "data":[0,-2,-8,-18], "mark":true}
]

六、图片

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值