• 在wcharczuk/go-chart图表上打印文字


    先看效果:

    NewImage

    源码

    package main

    import (
        "bytes"
        "fmt"
        "io/ioutil"
        "log"
        "os"

        "github.com/wcharczuk/go-chart/drawing"

        "github.com/golang/freetype/truetype"
        "github.com/wcharczuk/go-chart"
    )

    // getZWFont 加载字体
    func getZWFont() *truetype.Font {

        fontFile := "/Library/Fonts/Microsoft-YaHei.ttf"
        //fontFile := "/Library/Fonts/AppleMyungjo.ttf"

        // 读字体数据
        fontBytes, err := ioutil.ReadFile(fontFile)
        if err != nil {
            log.Println(err)
            return nil
        }
        font, err := truetype.Parse(fontBytes)
        if err != nil {
            log.Println(err)
            return nil
        }
        return font
    }

    func drawChart() {

        buffer := bytes.NewBuffer([]byte{})

        graph := chart.Chart{
            Series: []chart.Series{
                chart.ContinuousSeries{
                    XValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
                    YValues: []float64{1.0, 2.0, 3.0, 4.0, 5.0},
                },
                chart.AnnotationSeries{
                    Annotations: []chart.Value2{
                        {XValue: 1.0, YValue: 1.0, Label: "One11"},
                        {XValue: 2.0, YValue: 2.0, Label: "Two"},
                        {XValue: 3.0, YValue: 3.0, Label: "Three"},
                        {XValue: 4.0, YValue: 4.0, Label: "Four"},
                        {XValue: 5.0, YValue: 5.0, Label: "Five"},
                    },
                },
            },
            Font: getZWFont(),
            Elements: []chart.Renderable{
                TextInfo([]string{"test郭红俊yyy", "dddwf分发给ff"}),
            },
        }
        err := graph.Render(chart.PNG, buffer)
        if err != nil {
            log.Fatal(err)
        }

        fo, err := os.Create("o1.png")
        if err != nil {
            panic(err)
        }

        if _, err := fo.Write(buffer.Bytes()); err != nil {
            panic(err)
        }
    }

    func main() {
        drawChart()
    }

    // TextInfo 显示文字
    func TextInfo(txtArr []string) chart.Renderable {
        return func(r chart.Renderer, cb chart.Box, chartDefaults chart.Style) {
            log.Println(fmt.Sprintf("h:%d; w:%d", cb.Height(), cb.Width()))
            log.Println(chartDefaults)
            log.Println(cb)

            r.SetFont(getZWFont())
            r.SetFontColor(drawing.ColorBlue)
            r.SetFontSize(14)
            i := 0
            for _, txt := range txtArr {
                r.Text(txt, 60+i*10, 40+i*30)
                i++
            }
            log.Println("12444")
        }
    }

  • 相关阅读:
    SR-IOV(Single Root I/O Virtualization)
    DHCP&DHCPv6
    Linux 上的基础网络设备详解
    当Linux用尽内存-oom
    真爱了--网络工程师技能图谱
    程序员必备技能:如何画好架构图?
    Linux内存使用情况以及内存泄露情况
    Neutron 消息回调系统
    linux bridge
    OpenStack-Neutron-code
  • 原文地址:https://www.cnblogs.com/ghj1976/p/11363715.html
Copyright © 2020-2023  润新知