ECharts and golang practical guide: making various types of statistical charts

WBOY
Release: 2023-12-17 22:30:19
Original
975 people have browsed it

ECharts和golang实践指南: 制作各种类型的统计图表

ECharts and golang practice guide: Making various types of statistical charts

With the advent of the digital age, the value of data has received more and more attention. However, the value of data only exists in its analysis and utilization. To better analyze data, charts are one of the essential tools. In this article, we will introduce how to use ECharts and golang, two famous tools, to create various types of statistical charts.

Preparation

First, we need to prepare the ECharts and golang environments.

ECharts is an open source data visualization library based on JavaScript, with good interactivity and dynamic effects, and supports multiple chart types. We can install ECharts in the following two ways:

  1. Download the echarts.js file directly and introduce it in HTML
  2. Use npm to install

Here we choose the first method, which can be introduced in the HTML file after downloading:

<script src="/path/echarts.min.js"></script>
Copy after login

golang is a static type, compiled language, which emphasizes concurrency and efficiency, and is obtained in back-end development. Wide range of applications. We need to install the golang environment, which can be installed by downloading the corresponding installation package from the official website.

Making a Histogram

The histogram is a common chart type that shows differences in data. The following describes how to use ECharts and golang to implement histograms.

First, we need to prepare the required data. For example, in golang:

data := []int{120, 200, 150, 80, 70, 110, 130}
Copy after login

Then, we need to define an instance of ECharts and set the basic properties of the chart. For example:

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

var option = {
  title: {
    text: '柱状图'
  },
  tooltip: {},
  xAxis: {
    data: ['A', 'B', 'C', 'D', 'E', 'F', 'G']
  },
  yAxis: {},
  series: [{
    name: '数据',
    type: 'bar',
    data: data
  }]
};

chart.setOption(option);
Copy after login

In the code, we define an ECharts instance and specify the id of the chart, and then define the basic properties of the chart, including title, X/Y axis and data series. Finally, we set these properties to the chart via the setOption method.

Making a Line Chart

A line chart is a type of chart that shows data trends. The following describes how to use ECharts and golang to implement a line chart.

First, we need to prepare the required data. For example, in golang:

xData := []string{"2010", "2011", "2012", "2013", "2014", "2015", "2016"}
yData := []int{120, 200, 150, 80, 70, 110, 130}
Copy after login

Then, we need to define an instance of ECharts and set the basic properties of the chart. For example:

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

var option = {
  title: {
    text: '折线图'
  },
  tooltip: {},
  xAxis: {
    data: xData
  },
  yAxis: {},
  series: [{
    name: '数据',
    type: 'line',
    data: yData
  }]
};

chart.setOption(option);
Copy after login

In the code, we define an ECharts instance and specify the id of the chart, and then define the basic properties of the chart, including title, X/Y axis and data series. The difference is that here we set the type of the data series to line, which is the polyline type.

Making a Pie Chart

A pie chart is a type of chart that displays the proportion of data. The following describes how to use ECharts and golang to implement a pie chart.

First, we need to prepare the required data. For example, in golang:

data := []struct {
  Value float64 `json:"value"`
  Name  string  `json:"name"`
}{
  {Value: 335, Name: "A"},
  {Value: 310, Name: "B"},
  {Value: 234, Name: "C"},
  {Value: 135, Name: "D"},
  {Value: 1548, Name: "E"}
}
Copy after login

Then, we need to define an instance of ECharts and set the basic properties of the chart. For example:

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

var option = {
  title: {
    text: '饼图'
  },
  tooltip: {},
  series: [{
    name: '数据',
    type: 'pie',
    radius: '50%',
    data: data,
    roseType: 'angle'
  }]
};

chart.setOption(option);
Copy after login

In the code, we define an ECharts instance and specify the id of the chart, and then define the basic properties of the chart, including the title and data series. The difference is that here we set the type of the data series to pie, which is the pie chart type, and set other properties of the pie chart, such as radius and roseType.

To sum up, we introduced how to use ECharts and golang to create bar charts, line charts and pie charts, and provided specific code examples. Of course, this is just the tip of the iceberg of ECharts and golang. Both tools have very rich usage scenarios and functions. Readers can continue to learn and explore in depth.

The above is the detailed content of ECharts and golang practical guide: making various types of statistical charts. For more information, please follow other related articles on the PHP Chinese website!

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