Home  >  Article  >  Backend Development  >  Teach you how to easily draw various statistical charts using ECharts and golang

Teach you how to easily draw various statistical charts using ECharts and golang

WBOY
WBOYOriginal
2023-12-17 23:10:16915browse

Teach you how to easily draw various statistical charts using ECharts and golang

Teach you to use ECharts and golang to easily draw various statistical charts

Introduction:
In the field of data visualization, ECharts is a very popular JavaScript chart library , which provides a rich and powerful API capable of drawing various types of statistical charts. As an efficient and concise programming language, golang can also be used to generate data and be combined with ECharts to draw charts. This article will be based on examples and teach you how to use ECharts and golang to easily draw various statistical charts.

Prerequisite preparation:
Before you start, you need to install golang and ECharts. For specific installation steps, please refer to the official documentation. It is assumed that you have completed the installation.

1. Draw a histogram

First, we will use golang to generate a set of random data and use ECharts to draw a histogram.
Code example:

package main

import (
    "fmt"
    "math/rand"
    "time"

    "github.com/go-echarts/go-echarts/charts"
)

func randomData() []struct {
    Name  string
    Value int
} {
    data := []struct {
        Name  string
        Value int
    }{}
    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    for i := 0; i < 10; i++ {
        data = append(data, struct {
            Name  string
            Value int
        }{Name: fmt.Sprintf("数据 %d", i), Value: r.Intn(100)})
    }
    return data
}

func main() {
    // 生成随机数据
    data := randomData()

    // 创建柱状图对象
    bar := charts.NewBar()

    // 设置X轴数据
    xData := []string{}
    for _, d := range data {
        xData = append(xData, d.Name)
    }
    bar.SetXAxis(xData)

    // 设置Y轴数据
    yData := []int{}
    for _, d := range data {
        yData = append(yData, d.Value)
    }
    bar.AddYAxis("数值", yData)

    // 设置图表标题
    bar.SetGlobalOptions(charts.WithTitleOpts(opts.Title{
        Title:    "柱状图示例",
        Subtitle: "数据来源:随机数",
    }))

    // 保存图表到文件
    bar.Render("bar.html")
}

2. Draw a line chart

Next, we will use golang to generate a set of time series data and use ECharts to draw a line chart.
Code example:

package main

import (
    "time"

    "github.com/go-echarts/go-echarts/charts"
    "github.com/go-echarts/go-echarts/opts"
)

func main() {
    // 创建折线图对象
    line := charts.NewLine()

    // 设置X轴数据
    xData := []string{}
    start := time.Date(2021, time.January, 1, 0, 0, 0, 0, time.UTC)
    for i := 0; i < 365; i++ {
        xData = append(xData, start.AddDate(0, 0, i).Format("2006-01-02"))
    }
    line.SetXAxis(xData)

    // 设置Y轴数据
    yData := []opts.LineData{}
    for i := 0; i < len(xData); i++ {
        yData = append(yData, opts.LineData{Value: i})
    }
    line.AddYAxis("数值", yData)

    // 设置图表标题
    line.SetGlobalOptions(charts.WithTitleOpts(opts.Title{
        Title:    "折线图示例",
        Subtitle: "数据来源:时间序列",
    }))

    // 保存图表到文件
    line.Render("line.html")
}

In the above example, we used golang's random number and time series generation functions to generate histogram and line chart data respectively. Then, use the ECharts API to set the X-axis and Y-axis data and add it to the chart object. Finally, set the chart's title and save the chart to an HTML file.

Conclusion:
Through the above examples, we can see that it is very simple to use ECharts and golang to draw various statistical charts. You can generate different types of data according to your own needs and use ECharts' API for customized settings. I hope this article can help you quickly get started with data visualization development and deepen your understanding of ECharts and golang.

The above is the detailed content of Teach you how to easily draw various statistical charts using ECharts and golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn