Home > Web Front-end > JS Tutorial > body text

How to use pictogram bar chart to display data in ECharts

WBOY
Release: 2023-12-17 10:29:03
Original
1235 people have browsed it

How to use pictogram bar chart to display data in ECharts

How to use pictograms to display data in ECharts requires specific code examples

In the field of data visualization, ECharts is a powerful and easy-to-use open source visualization Library. It provides a wealth of chart types that can help us display data in an intuitive way. Among them, the pictographic column chart is an intuitive and interesting chart type that can display data in vivid graphics. This article will introduce how to use pictograms to display data in ECharts and provide specific code examples.

First, we need to introduce the ECharts library file. It can be imported through online CDN, or the library file can be imported locally after downloading it. After introducing the library file, we can start using ECharts to create pictogram charts.

Next, we need to prepare the data to be displayed. Suppose we want to display a number of animals, including cats, dogs, birds, and fish. The data can be represented by an array of objects, each containing the name and number of animals. For example:

var data = [
  { name: '猫', value: 10 },
  { name: '狗', value: 15 },
  { name: '鸟', value: 8 },
  { name: '鱼', value: 20 }
];
Copy after login

Next, we need to create a chart instance and set up its basic configuration. For example, we can create a container element and use the echarts.init method provided by ECharts to initialize the chart.

var chart = echarts.init(document.getElementById('chart-container'));
Copy after login

Then, we can set the basic properties of the chart, such as title, legend, axis, etc. For example, we can set the title of the chart to "Animal Number Statistics", the axis names to "Animals" and "Number", and add a legend to the chart to display the name of each animal. The code example is as follows:

chart.setOption({
  title: {
    text: '动物数量统计'
  },
  legend: {
    data: ['猫', '狗', '鸟', '鱼']
  },
  xAxis: {
    name: '动物'
  },
  yAxis: {
    name: '数量'
  },
  series: []
});
Copy after login

Finally, we need to add the specific data of the pictogram column chart. In ECharts, we can use the series attribute to set the chart data. We need to use type: 'pictorialBar' to specify the chart type as a pictorial bar chart, and specify the symbol to use through the symbol attribute. Here we can use the default symbols provided by ECharts or use custom symbols. The code example is as follows:

chart.setOption({
  // 之前的配置...
  series: [{
    type: 'pictorialBar',
    symbol: 'circle',
    data: data.map(function(item) {
      return {
        value: item.value,
        symbolSize: [30, 30], // 符号的大小
        symbolOffset: [0, '-50%'], // 符号的偏移量
        name: item.name
      };
    })
  }]
});
Copy after login

After completing the above steps, we have successfully created an ECharts chart that uses a pictogram to display data.

To summarize, the steps for using pictograms to display data in ECharts are as follows:

  1. Introduce the library files of ECharts.
  2. Prepare the data to be displayed.
  3. Create a chart instance and set its basic configuration.
  4. Add specific data for the pictogram column chart.

I hope this article can help readers understand how to use pictograms to display data in ECharts, and understand it more intuitively through code examples. Using ECharts to create pictograms can make the data more interesting and visual, and improve the data display effect.

The above is the detailed content of How to use pictogram bar chart to display data in ECharts. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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