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:
Here we choose the first method, which can be introduced in the HTML file after downloading:
<script src="/path/echarts.min.js"></script>
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}
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);
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}
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);
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"} }
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);
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!