


ECharts and Java interface: How to quickly implement statistical charts such as line charts, bar charts, pie charts, etc.
Dec 17, 2023 pm 10:37 PMECharts and Java interface: How to quickly implement statistical charts such as line charts, bar charts, pie charts, etc., need specific code examples
With the advent of the Internet era, data Analytics are becoming increasingly important. Statistical charts are a very intuitive and powerful display method. Charts can display data more clearly, allowing people to better understand the connotation and patterns of the data. In Java development, we can use ECharts and Java interfaces to quickly display various statistical charts.
ECharts is a data visualization chart library based on HTML5 Canvas developed by Baidu. It can easily draw a variety of charts, including line charts, bar charts, pie charts, etc. The Java interface is a technology that enables back-end data to interact with front-end display, and can pass data to the front-end through Java code. Next, we will introduce how to use ECharts and Java interfaces to quickly display various statistical charts.
- Implementing a line chart
A line chart is a chart that represents data trends by connecting data points. In ECharts, we can implement a line chart through the following code:
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例 var myChart = echarts.init(document.getElementById('main')); // 指定圖表的配置項(xiàng)和數(shù)據(jù) var option = { title: { text: '折線圖' }, tooltip: {}, legend: { data:['銷(xiāo)量'] }, xAxis: { data: ["周一","周二","周三","周四","周五","周六","周日"] }, yAxis: {}, series: [{ name: '銷(xiāo)量', type: 'line', data: [5, 20, 36, 10, 20, 30, 40] }] }; // 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。 myChart.setOption(option);
The above code shows how to use ECharts to implement a line chart. We can set various parameters of the chart in the option object, such as the title of the chart, the labels of the x-axis and y-axis, etc. By passing data to the data attribute in the series, the corresponding data can be displayed in the chart. Finally use the setOption method to render the chart.
How to implement a line chart in Java? We can pass data to the front end through the following Java code:
@RequestMapping("/echarts") @ResponseBody public Map<String,Object> echarts(){ List<String> categories = new ArrayList<String>(); categories.add("周一"); categories.add("周二"); categories.add("周三"); categories.add("周四"); categories.add("周五"); categories.add("周六"); categories.add("周日"); List<Integer> data = new ArrayList<Integer>(); data.add(5); data.add(20); data.add(36); data.add(10); data.add(20); data.add(30); data.add(40); Map<String,Object> result = new HashMap<String,Object>(); result.put("categories",categories); result.put("data",data); return result; }
In the above code, we create a Map object and put the x-axis and y-axis data into categories and data respectively. Then return the Map object to the front end.
Finally, add the following JS code to the front-end code to complete the back-end data rendering of the line chart:
$.ajax({ url: '/echarts', type: 'get', success: function(data){ var categories = data.categories; var data = data.data; var option = { title: { text: '折線圖' }, tooltip: {}, legend: { data:['銷(xiāo)量'] }, xAxis: { data: categories }, yAxis: {}, series: [{ name: '銷(xiāo)量', type: 'line', data: data }] }; myChart.setOption(option); }, error: function(){ alert('error'); } });
- Implementing the bar chart
A bar chart is a chart used to compare the size of different categories of data. In ECharts, we can implement histograms through the following code:
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例 var myChart = echarts.init(document.getElementById('main')); // 指定圖表的配置項(xiàng)和數(shù)據(jù) var option = { title: { text: '柱狀圖' }, tooltip: {}, legend: { data:['銷(xiāo)量'] }, xAxis: { data: ["周一","周二","周三","周四","周五","周六","周日"] }, yAxis: {}, series: [{ name: '銷(xiāo)量', type: 'bar', data: [5, 20, 36, 10, 20, 30, 40] }] }; // 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。 myChart.setOption(option);
The above code shows how to use ECharts to implement histograms. Similar to the line chart, we can set various parameters of the chart in the option object, such as the title of the chart, the labels of the x-axis and y-axis, etc. At the same time, we only need to set the type attribute to 'bar' to convert the line chart into a bar chart.
How to implement a histogram in Java? We can pass data to the front end through the following Java code:
@RequestMapping("/echarts") @ResponseBody public Map<String,Object> echarts(){ List<String> categories = new ArrayList<String>(); categories.add("周一"); categories.add("周二"); categories.add("周三"); categories.add("周四"); categories.add("周五"); categories.add("周六"); categories.add("周日"); List<Integer> data = new ArrayList<Integer>(); data.add(5); data.add(20); data.add(36); data.add(10); data.add(20); data.add(30); data.add(40); Map<String,Object> result = new HashMap<String,Object>(); result.put("categories",categories); result.put("data",data); return result; }
In the above code, we create a Map object and put the x-axis and y-axis data into categories and data respectively. Then return the Map object to the front end. Add the following JS code to the front-end code to complete the back-end data rendering of the histogram:
$.ajax({ url: '/echarts', type: 'get', success: function(data){ var categories = data.categories; var data = data.data; var option = { title: { text: '柱狀圖' }, tooltip: {}, legend: { data:['銷(xiāo)量'] }, xAxis: { data: categories }, yAxis: {}, series: [{ name: '銷(xiāo)量', type: 'bar', data: data }] }; myChart.setOption(option); }, error: function(){ alert('error'); } });
- Implementing the pie chart
The pie chart is a way to represent A chart of data proportions. In ECharts, we can implement pie charts through the following code:
// 基于準(zhǔn)備好的dom,初始化echarts實(shí)例 var myChart = echarts.init(document.getElementById('main')); // 指定圖表的配置項(xiàng)和數(shù)據(jù) var option = { title: { text: '餅圖', subtext: '數(shù)據(jù)來(lái)自網(wǎng)絡(luò)' }, tooltip: { trigger: 'item', formatter: '{a} <br/>: {c} (377j5v51b%)' }, legend: { orient: 'vertical', left: 10, data: ['直接訪問(wèn)','郵件營(yíng)銷(xiāo)','聯(lián)盟廣告','視頻廣告','搜索引擎'] }, series: [ { name: '訪問(wèn)來(lái)源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold' } }, labelLine: { show: false }, data: [ {value: 335, name: '直接訪問(wèn)'}, {value: 310, name: '郵件營(yíng)銷(xiāo)'}, {value: 234, name: '聯(lián)盟廣告'}, {value: 135, name: '視頻廣告'}, {value: 1548, name: '搜索引擎'} ] } ] }; // 使用剛指定的配置項(xiàng)和數(shù)據(jù)顯示圖表。 myChart.setOption(option);
The above code shows how to use ECharts to implement pie charts. Similar to line charts and bar charts, we can set various parameters of the chart in the option object, such as the title of the chart, the labels of the x-axis and y-axis, etc. By passing data to the data attribute in the series, the corresponding data can be displayed in the chart. Note that the pie chart data here needs to meet the format corresponding to the value and name, such as {value:335, name:'direct access'}.
How to implement a pie chart in Java? We can pass data to the front end through the following Java code:
@RequestMapping("/echarts") @ResponseBody public List<Map<String,Object>> echarts(){ List<Map<String,Object>> result = new ArrayList<Map<String,Object>>(); Map<String,Object> data1 = new HashMap<String,Object>(); data1.put("value",335); data1.put("name","直接訪問(wèn)"); result.add(data1); Map<String,Object> data2 = new HashMap<String,Object>(); data2.put("value",310); data2.put("name","郵件營(yíng)銷(xiāo)"); result.add(data2); Map<String,Object> data3 = new HashMap<String,Object>(); data3.put("value",234); data3.put("name","聯(lián)盟廣告"); result.add(data3); Map<String,Object> data4 = new HashMap<String,Object>(); data4.put("value",135); data4.put("name","視頻廣告"); result.add(data4); Map<String,Object> data5 = new HashMap<String,Object>(); data5.put("value",1548); data5.put("name","搜索引擎"); result.add(data5); return result; }
In the above code, we create a Map array with 5 elements and store the values ??and names in the Map. Then return the Map array to the front end. Add the following JS code to the front-end code to complete the back-end data rendering of the pie chart:
$.ajax({ url: '/echarts', type: 'get', success: function(data){ var legendData = []; var seriesData = []; $.each(data,function(index,item){ legendData.push(item.name); seriesData.push(item); }); var option = { title: { text: '餅圖', subtext: '數(shù)據(jù)來(lái)自網(wǎng)絡(luò)' }, tooltip: { trigger: 'item', formatter: '{a} <br/>: {c} (377j5v51b%)' }, legend: { orient: 'vertical', left: 10, data: legendData }, series: [ { name: '訪問(wèn)來(lái)源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold' } }, labelLine: { show: false }, data: seriesData } ] }; myChart.setOption(option); }, error: function(){ alert('error'); } });
Summary:
Through the above examples, we can find that ECharts and Java interfaces are used to display statistics The chart is very simple. You only need to call the Java interface in the front-end code to obtain the data, and then pass it to the option object of ECharts. In the Java interface, the data needs to be encapsulated into a collection object such as Map or List first, and then returned to the front end. Naturally, it is more convenient to use Spring Boot. The combination of ECharts and Java interface can adapt to various development models with front-end and back-end separation.
The above is the detailed content of ECharts and Java interface: How to quickly implement statistical charts such as line charts, bar charts, pie charts, etc.. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In today's context where data visualization is becoming more and more important, many developers hope to use various tools to quickly generate various charts and reports so that they can better display data and help decision-makers make quick judgments. In this context, using the Php interface and ECharts library can help many developers quickly generate visual statistical charts. This article will introduce in detail how to use the Php interface and ECharts library to generate visual statistical charts. In the specific implementation, we will use MySQL

The steps to draw a dashboard using ECharts and Python interface require specific code examples. Summary: ECharts is an excellent data visualization tool that can easily perform data processing and graphics drawing through the Python interface. This article will introduce the specific steps to draw a dashboard using ECharts and Python interface, and provide sample code. Keywords: ECharts, Python interface, dashboard, data visualization Introduction Dashboard is a commonly used form of data visualization, which uses

How to use histograms to display data in ECharts ECharts is a JavaScript-based data visualization library that is very popular and widely used in the field of data visualization. Among them, the histogram is the most common and commonly used chart type, which can be used to display the size, comparison and trend analysis of various numerical data. This article will introduce how to use ECharts to draw histograms and provide code examples. First, we need to introduce the ECharts library into the HTML file, which can be introduced in the following way

Writing method: 1. Define an interface named MyInterface; 2. Define a method named myMethod() in the MyInterface interface; 3. Create a class named MyClass and implement the MyInterface interface; 4. Create a MyClass class Object and assign its reference to a variable of type MyInterface.

How to use calendar charts to display time data in ECharts ECharts (Baidu’s open source JavaScript chart library) is a powerful and easy-to-use data visualization tool. It offers a variety of chart types, including line charts, bar charts, pie charts, and more. The calendar chart is a very distinctive and practical chart type in ECharts, which can be used to display time-related data. This article will introduce how to use calendar charts in ECharts and provide specific code examples. First, you need to use

How to use ECharts and PHP interfaces to generate statistical charts Introduction: In modern web application development, data visualization is a very important link, which can help us display and analyze data intuitively. ECharts is a powerful open source JavaScript chart library. It provides a variety of chart types and rich interactive functions, and can easily generate various statistical charts. This article will introduce how to use ECharts and PHP interfaces to generate statistical charts, and give specific code examples. 1. Overview of ECha

Rethink the way MyBatis is written MyBatis is a very popular Java persistence framework that can help us simplify the writing process of database operations. However, in daily use, we often encounter some confusions and bottlenecks in writing methods. This article will rethink the way MyBatis is written and provide some specific code examples to help readers better understand and apply MyBatis. Use the Mapper interface to replace SQL statements in the traditional MyBatis writing method.

How to use a map heat map to display city heat in ECharts ECharts is a powerful visual chart library that provides various chart types for developers to use, including map heat maps. Map heat maps can be used to show the popularity of cities or regions, helping us quickly understand the popularity or density of different places. This article will introduce how to use the map heat map in ECharts to display city heat, and provide code examples for reference. First, we need a map file containing geographic information, EC
