Home > Backend Development > Golang > ECharts and golang tutorial: Practical tips for making multi-category statistical charts

ECharts and golang tutorial: Practical tips for making multi-category statistical charts

WBOY
Release: 2023-12-17 20:06:42
Original
729 people have browsed it

ECharts和golang教程: 制作多类统计图表的实用技巧

ECharts and golang tutorial: Practical tips for making multi-category statistical charts, specific code examples are required

In terms of data visualization, charts are very practical. ECharts is a powerful open source visualization chart library, and golang is an efficient programming language that can be used for data analysis and chart production. This article will introduce how to use ECharts and golang to create various types of charts, including line charts, bar charts, scatter charts, pie charts, etc.

  1. Line Chart

A line chart is a classic statistical chart used to show data trends over time (or other metrics). Here is an example of a line chart made using ECharts and golang:

package main

import (
    "net/http"

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

func main() {
    // 创建一个gin实例
    r := gin.Default()

    // 创建一个折线图表
    line := charts.NewLine()

    // 添加X轴和Y轴数据
    line.AddXAxis([]string{"1月", "2月", "3月", "4月", "5月", "6月"}).
        AddYAxis("销量", []int{20, 50, 80, 120, 180, 200})

    // 将图表添加到gin实例中
    r.GET("/line", func(c *gin.Context) {
        c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
        line.Render(c.Writer)
    })

    // 启动服务器
    http.ListenAndServe(":8080", r)
}
Copy after login
  1. Bar chart

Bar charts are often used to compare differences between different data sets, especially Suitable for representing discrete, non-continuous data. The following is an example of a histogram made using ECharts and golang:

package main

import (
    "net/http"

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

func main() {
    // 创建一个gin实例
    r := gin.Default()

    // 创建一个柱状图表
    bar := charts.NewBar()

    // 添加X轴和Y轴数据
    bar.AddXAxis([]string{"苹果", "香蕉", "橘子", "葡萄", "梨", "西瓜"}).
        AddYAxis("销量", []int{20, 50, 80, 120, 180, 200})

    // 将图表添加到gin实例中
    r.GET("/bar", func(c *gin.Context) {
        c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
        bar.Render(c.Writer)
    })

    // 启动服务器
    http.ListenAndServe(":8080", r)
}
Copy after login
  1. Scatter plot

Scatter plots are used to show the relationship between two variables. Here is an example of a scatter chart made using ECharts and golang:

package main

import (
    "net/http"

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

func main() {
    // 创建一个gin实例
    r := gin.Default()

    // 创建一个散点图表
    scatter := charts.NewScatter()

    // 添加散点数据
    scatter.AddXAxis([]int{10, 20, 30, 40, 50, 60}).
        AddYAxis("A", []int{200, 300, 400, 400, 500, 600}).
        AddYAxis("B", []int{100, 200, 300, 400, 450, 600})

    // 将图表添加到gin实例中
    r.GET("/scatter", func(c *gin.Context) {
        c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
        scatter.Render(c.Writer)
    })

    // 启动服务器
    http.ListenAndServe(":8080", r)
}
Copy after login
  1. pie chart

A pie chart is a type of circular statistical chart used to display different data the ratio between. The following is an example of a pie chart made using ECharts and golang:

package main

import (
    "net/http"

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

func main() {
    // 创建一个gin实例
    r := gin.Default()

    // 创建一个饼图表
    pie := charts.NewPie()

    // 添加饼图数据
    pie.Add("苹果", 30).
        Add("香蕉", 20).
        Add("橘子", 10).
        Add("葡萄", 40)

    // 将图表添加到gin实例中
    r.GET("/pie", func(c *gin.Context) {
        c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8")
        pie.Render(c.Writer)
    })

    // 启动服务器
    http.ListenAndServe(":8080", r)
}
Copy after login

Summary

This article introduces how to use ECharts and golang to make various types of charts, including line charts, column charts, scatter charts, etc. Dot plots and pie charts and more. The above is a code example, you can try these techniques in your own projects to make more useful diagrams.

The above is the detailed content of ECharts and golang tutorial: Practical tips for making multi-category statistical charts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template