ECharts pie chart: how to display data proportions
With the widespread application of data visualization, ECharts, an excellent visualization library, has also received more and more attention. Among them, pie charts are widely used to display the proportion of data. This article will introduce how to use ECharts pie charts to display data proportions and provide specific code examples.
1. Basic concepts of ECharts pie charts
First of all, we need to understand the basic concepts of pie charts. Pie charts are often used to represent the proportion of data. Specific values are converted into angle sizes, and then represented by the size of the sector area. The size of each sector is proportional to their numerical value.
2. How to implement ECharts pie chart
To use ECharts to draw a pie chart, you need to first introduce the ECharts library and create a div tag with a specified size to display the pie chart. The specific code is as follows:
<style type="text/css"> #myChart { width: 400px; height: 400px; } </style> <div id="myChart"></div> <script src="echarts.min.js"></script>
Among them, the style tag is used to specify the size of the div tag where the pie chart is located. The script tag introduces the min version of the ECharts library.
Then, we need to implement the pie chart through JavaScript code. The specific code is as follows:
var myChart = echarts.init(document.getElementById('myChart')); var option = { title: { text: '饼图示例', left: 'center' }, tooltip: {}, legend: { data:['数据1', '数据2', '数据3'] }, series: [ { name:'数据占比', type:'pie', radius: '55%', center: ['50%', '60%'], data:[ {value:335, name:'数据1'}, {value:310, name:'数据2'}, {value:234, name:'数据3'} ], itemStyle: { emphasis: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } } ] }; myChart.setOption(option);
In the above code, we first use the echarts.init() method to initialize the ECharts instance. Then, we define a JavaScript object option, which defines various properties and data needed in the pie chart. Finally, we use the setOption() method to apply this option object to the ECharts instance to generate a pie chart.
Specifically, the option object contains the following attributes:
- title: used to set the title of the pie chart;
- tooltip: used to set the mouse Prompt information when floating;
- legend: used to set the label of each sector area in the pie chart;
- series: used to set the specific data series in the pie chart, including radius, data , style, etc.
3. Style setting of ECharts pie chart
In addition to basic data display, ECharts pie chart also provides a variety of style setting options, which can be achieved by modifying the corresponding properties. Pie charts in different styles.
- Inner and outer radius
Adjust the inner and outer radius of the pie chart by setting the radius attribute to control the size of the sector area. The following code:
series: [ { type: 'pie', radius: ['50%', '70%'], data: [ {value: 335, name: '数据1'}, {value: 310, name: '数据2'}, {value: 234, name: '数据3'}, {value: 135, name: '数据4'}, {value: 1548, name: '数据5'} ] } ]
In the above code, the radius attribute contains an array, and the two values in the array represent the percentage of the inner and outer radius respectively. In this example, the inner radius is 50% and the outer radius is 70%.
- Legend position
Adjust the position and direction of the legend by setting the x, y, and orient attributes in the legend attribute. The following code:
legend: { x: 'left', y: 'center', orient: 'vertical', data: ['数据1', '数据2', '数据3', '数据4', '数据5'] }
In the above code, the x attribute sets the horizontal position of the legend to the left, the y attribute sets the vertical position of the legend to the center, and the orient attribute sets the direction of the legend to the vertical direction.
- Shadow effect
By setting the emphasis attribute in the itemStyle attribute, you can add shadow and other effects to the fan-shaped area to enhance the visual effect of the pie chart. The following code:
itemStyle: { emphasis: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } }
In the above code, shadowBlur represents the blur degree of the shadow, shadowOffsetX and shadowOffsetY represent the horizontal and vertical offset of the shadow, and shadowColor represents the color of the shadow.
4. ECharts Pie Chart Example
Below, we give a specific ECharts pie chart example, which contains the basic data and style settings mentioned above. The code is as follows:
<style type="text/css"> #myChart { width: 400px; height: 400px; } </style> <div id="myChart"></div> <script src="echarts.min.js"></script> <script> var myChart = echarts.init(document.getElementById('myChart')); var option = { title: { text: '饼图示例', left: 'center' }, tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)' }, legend: { orient: 'vertical', left: 10, top: 20, data:['数据1', '数据2', '数据3', '数据4', '数据5'] }, series: [ { name: '访问来源', type: 'pie', radius: ['40%', '60%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold' } }, labelLine: { show: false }, data:[ {value:335, name:'数据1'}, {value:310, name:'数据2'}, {value:234, name:'数据3'}, {value:135, name:'数据4'}, {value:1548, name:'数据5'} ] } ] }; myChart.setOption(option); </script>
This pie chart includes the following features:
- Added a legend and adjusted the legend position to the upper left corner;
- Added mouse hover The prompt pop-up window shows the percentage;
- adds a shadow effect and sets a highlight effect when the mouse is hovering.
The above is an introduction to the basic implementation of ECharts pie charts and some style setting examples. I hope readers can have a certain understanding of ECharts pie charts through this article and be able to apply them correctly in actual development.
The above is the detailed content of ECharts pie chart: how to display data proportions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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

ECharts and Java interface: How to quickly implement statistical charts such as line charts, bar charts, and pie charts. Specific code examples are required. With the advent of the Internet era, data analysis has become more and more 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 software developed by Baidu

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

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

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 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

ECharts and golang technical guide: Practical tips for creating various statistical charts, specific code examples are required. Introduction: In the field of modern data visualization, statistical charts are an important tool for data analysis and visualization. ECharts is a powerful data visualization library, while golang is a fast, reliable and efficient programming language. This article will introduce you to how to use ECharts and golang to create various types of statistical charts, and provide code examples to help you master this skill. Preparation

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

The steps to create a funnel chart using ECharts and Python interfaces require specific code examples. A funnel chart is a visual chart commonly used to display data processes and conversion rates. In data analysis and business decision-making, funnel charts can clearly display conversion rates at different stages, helping analysts better understand data processes and optimize business processes. This article will introduce how to create a funnel chart using ECharts and Python interfaces, and provide specific code examples. First, we need to install the necessary Python libraries and ECha
