Small example: implementing calendar chart in ECharts

original
2017/04/19 20:41
Reading 2.1W

 calendar

Small example: implementing calendar chart in ECharts

stay ECharts Published In version 3.5 , a new calendar coordinate system is added to draw charts in the calendar. How to quickly write a calendar chart?

 calendar

The above effect can be achieved through the following three steps:

Step 1: Import the js file

Downloaded Latest full version ECharts.min.js is sufficient, and there is no need to import other files separately.

 <script src="echarts.min.js"></script> <script> // ... </script>

Step 2: Specify the DOM element as the chart container.

Like other charts in ECharts, create a DOM as a container for drawing charts:

 <div id="main" style="width=100%; height = 400px"></div>

Initialize with ECharts:

 var myChart = echarts.init(document.getElementById('main'));

Step 3: Configure parameters

Take a common calendar chart as an example: calendar coordinates+heatmap chart:

 var option = { visualMap: { show: false, min: 0, max: 1000 }, calendar: { range: '2017' }, series: { type: 'heatmap', coordinateSystem: 'calendar', data: [['2017-01-02', 900], ['2017-01-02', 877], ['2017-01-02', 699], ...] } } myChart.setOption(option);

On the basis of the heatmap diagram, add coordinateSystem: 'calendar' and calendar: { range: '2017' } , the heatmap map becomes a calendar map in seconds.

If the chart is not displayed correctly, you can check the following possibilities:

  • Whether the JS file is loaded correctly;
  • echarts Whether the variable exists;
  • Whether the console reports errors;
  • DOM element in echarts.init Whether there is height and width.
  • if it is type: heatmap , check whether the visualMap

Complete sample code attached

 <! DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ECharts</title> <script src="echarts.min.js"></script> </head> <body> <div id="main" style="width:100%;height:400px;"></div> <script type="text/javascript"> var myChart = echarts.init(document.getElementById('main')); //Analog data function getVirtulData(year) { year = year || '2017'; var date = +echarts.number.parseDate(year + '-01-01'); var end = +echarts.number.parseDate(year + '-12-31'); var dayTime = 3600 * 24 * 1000; var data = []; for (var time = date;  time <= end; time += dayTime) { data.push([ echarts.format.formatTime('yyyy-MM-dd', time), Math.floor(Math.random() * 10000) ]); } return data; } var option = { visualMap: { show: false, min: 0, max: 10000 }, calendar: { range: '2017' }, series: { type: 'heatmap', coordinateSystem: 'calendar', data: getVirtulData(2017) } }; myChart.setOption(option); </script> </body> </html>

These are the steps to draw the simplest calendar chart. If you want to further customize it, you can also customize the configuration parameters.

Custom configuration parameters

When using calendar coordinates to draw a calendar chart, various attributes can be customized:

  • range : set the time range. It can support a year, a month, a day, or cross years.

  • cellSize : Set the size of the calendar grid, which can support setting different heights and widths, and also support adaptive auto.

  • width height : You can also directly set the overall height and width of the calendar chart to make it adaptive based on the existing height and width.

  • orient : Sets the direction of the coordinates, either horizontally or vertically.

  • splitLine : Set the style of the separation line, or it can not be displayed directly.

  • itemStyle : Set the style of the calendar cell. You can customize the background color, box line color size type, transparency, and even add shadows.

  • dayLabel : Set the week style in the coordinates. You can set the day of the week from which it starts. You can quickly set the Chinese and English, or even customize the Chinese and English mix, or do not display locally. You can display it as you want through the formatter.

  • monthLabel : Set the style of the month in the coordinate. Like the week, you can quickly set the Chinese English and custom mix and match.

  • yearLabel : Set the coordinate style, which is displayed for one year by default. What you want to display through the formatter text can be customized through the string function, and you can choose from the top, bottom, left, right directions.

For complete configuration item parameters, see: calendar API

Other forms of calendar coordinate system

Calendar coordinate system is a new kind of ECharts Coordinate system, which provides the ability to draw charts on the calendar; Therefore, in addition to making common calendar charts, we can use calendar coordinate systems in thermodynamic charts, scatter charts, and relationship charts.

Use the thermal diagram in the calendar coordinate system: Code+effect address

 Thermal calendar

Use scatter chart in calendar coordinate system: Code+effect address

 Scatter Calendar

You can also mix different charts, for example, the following example, and place the thermodynamic chart and relationship chart at the same time: Code+effect address

 Mixed calendar

Other richer effects

Flexible utilization ECharts The combination of chart and coordinate system, as well as API, can also achieve more rich effects.

For example, making the lunar calendar: Code+effect address

 lunar calendar

For example, using chart.convertToPixel Interface to draw a pie chart in the calendar coordinate system: Code+effect address

 Pie Chart Calendar

Summary

Above, we briefly introduced some basic uses of ECharts calendar coordinate system, hoping to help you.

For more complete configuration items, please refer to ECharts official website Detailed documentation , or to ECharts Gallery View on Calendar coordinate system works

Also, there are many interesting charts that you can play together to unlock new play methods~

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
two Collection
one fabulous
 Back to top
Top