Home  >  Article  >  Backend Development  >  Learn to use ECharts and golang to create stunning statistical charts

Learn to use ECharts and golang to create stunning statistical charts

WBOY
WBOYOriginal
2023-12-17 10:26:34620browse

Learn to use ECharts and golang to create stunning statistical charts

Learn to use ECharts and Golang to create stunning statistical charts

With the continuous growth of data and the development of technology, data visualization has become an important part of conveying information and displaying results. important way. The design and presentation of data statistical charts not only require beautiful and intuitive display effects, but also require a high degree of interactivity and flexibility. This article will introduce how to use ECharts and Golang, two powerful tools, to achieve stunning statistical charts.

ECharts is a JavaScript-based data visualization library open sourced by Baidu. It has rich data visualization types and supports multiple platforms and browsers. Through ECharts, we can easily draw various forms of charts, such as line charts, bar charts, pie charts, etc. At the same time, ECharts also has powerful interactive capabilities, which can realize interaction between users and charts through mouse interaction, chart component linkage, etc.

Golang is a programming language developed by Google, which is efficient, simple and safe. Through Golang, we can quickly build high-performance web applications and combine them with ECharts to achieve a complete process of data acquisition, processing and display.

Next, we will use a practical case to demonstrate how to use ECharts and Golang to build a stunning statistical chart.

First, let us consider a requirement: Suppose we want to count the monthly sales of a certain region and display it in the form of a line chart. We can obtain monthly sales data through Golang and draw the corresponding line chart through ECharts.

First of all, in Golang, we need to use an HTTP request to obtain sales data. You can use the famous HTTP request library "gorilla/mux".

package main

import (
    "encoding/json"
    "log"
    "net/http"

    "github.com/gorilla/mux"
)

type Sale struct {
    Month  string  `json:"month"`
    Amount float64 `json:"amount"`
}

func main() {
    r := mux.NewRouter()
    r.HandleFunc("/sales", getSales).Methods("GET")
    log.Fatal(http.ListenAndServe(":8000", r))
}

func getSales(w http.ResponseWriter, r *http.Request) {
    sales := []Sale{
        {Month: "Jan", Amount: 1000.00},
        {Month: "Feb", Amount: 1500.00},
        {Month: "Mar", Amount: 2000.00},
        // 其他月份的销售额数据
    }

    json.NewEncoder(w).Encode(sales)
}

The above code uses the gorilla/mux library to create a route and defines a GET request processing function getSales, which returns a JSON array containing sales data. In the getSales function, we define a fixed example to simulate sales data, which you can modify according to the actual situation.

Next, we use ECharts to draw the line chart, which can be achieved by introducing the ECharts library file in the HTML file and calling the corresponding API in the JavaScript code.

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>销售统计</title>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.1.2/dist/echarts.min.js"></script>
</head>

<body>
    <div id="chart" style="width: 600px;height:400px;"></div>

    <script>
        var chart = echarts.init(document.getElementById('chart'));

        // 发起HTTP请求获取销售额数据
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "http://localhost:8000/sales", true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var sales = JSON.parse(xhr.responseText);
                var months = [];
                var amounts = [];
                for (var i = 0; i < sales.length; i++) {
                    months.push(sales[i].month);
                    amounts.push(sales[i].amount);
                }

                // 绘制折线图
                chart.setOption({
                    xAxis: {
                        data: months
                    },
                    yAxis: {},
                    series: [{
                        name: '销售额',
                        type: 'line',
                        data: amounts
                    }]
                });
            }
        }
        xhr.send();
    </script>
</body>

</html>

In the above code, we initiate an HTTP request through XMLHttpRequest to obtain sales data, and then parse and use the ECharts API to draw a line chart. When drawing a line chart, we use month as the x-axis and sales as the y-axis. We configure the style and data of the chart by setting parameters such as xAxis, yAxis, and series.

Through the above code examples, we can see that stunning statistical charts can be easily achieved using ECharts and Golang. You only need to obtain the data through HTTP requests, and then use ECharts to draw charts on the front end, which can be easily displayed. At the same time, the powerful interactive capabilities of ECharts can further enhance the visualization effect of charts, allowing users to have more in-depth interactions with charts.

Of course, the above example is just a simple demonstration. In practice, you need to adjust and expand it according to specific needs and data structures. I hope this article helps you understand how to use ECharts and Golang to create stunning statistical charts!

The above is the detailed content of Learn to use ECharts and golang to create stunning statistical charts. 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