Loading...
A contour line chart displays closed curves connecting points of equal value on a map or data visualization. The most common example is topographic maps where contour lines connect points of equal elevation, with numbers marked on the lines indicating the elevation height. Contour lines can be classified into four types based on their function: index contours, intermediate contours, supplementary contours, and auxiliary contours.
Beyond topographic maps, contour lines are widely used in data visualization to display three-dimensional data distribution on a two-dimensional plane, such as temperature distribution, pressure distribution, probability density distribution, etc. Contour lines generally do not intersect, but sometimes they may coincide. All points on the same contour line have the same value. In areas where contour lines are sparse, the value changes gradually; where contour lines are dense, the value changes steeply.
Other Names: Contour Plot, Isoline Chart, Level Curve Chart
Chart Type | Contour Line Chart |
---|---|
Suitable Data | Three-dimensional data: two continuous variables (X, Y coordinates) and one numerical variable (Z value) |
Function | Display distribution patterns and value changes of continuous data on a two-dimensional plane |
Data-to-Visual Mapping | X coordinate maps to horizontal axis position Y coordinate maps to vertical axis position Z value shown through contour line levels and labels Optionally use color to represent value magnitude |
Suitable Data Volume | Continuous two-dimensional grid data; more data points result in smoother contour lines |
Here is a typical application scenario for contour line charts:
Terrain Elevation Distribution Map. The chart below shows the elevation distribution of a mountainous area. Through contour lines, you can intuitively see the distribution of peaks, valleys, and slopes. Dense contour lines indicate steep slopes, while sparse contour lines indicate gentle terrain.
import { Chart } from '@antv/g2';// Simulate terrain elevation dataconst terrainData = [];for (let x = 0; x <= 50; x += 2) {for (let y = 0; y <= 50; y += 2) {// Simulate mountain terrain: elevation distribution of two peaksconst elevation1 = 100 * Math.exp(-((x - 15) ** 2 + (y - 15) ** 2) / 200);const elevation2 = 80 * Math.exp(-((x - 35) ** 2 + (y - 35) ** 2) / 150);const elevation = elevation1 + elevation2 + 10; // Base elevationterrainData.push({ x, y, elevation });}}const chart = new Chart({container: 'container',autoFit: true,});chart.options({type: 'cell',data: terrainData,encode: {x: 'x',y: 'y',color: 'elevation',},style: {stroke: '#333',strokeWidth: 0.5,inset: 0.5,},scale: {color: {palette: 'viridis',type: 'sequential',},},legend: {color: {length: 300,layout: { justifyContent: 'center' },labelFormatter: (value) => `${Math.round(value)}m`,},},tooltip: {title: 'Elevation Information',items: [{ field: 'x', name: 'Longitude' },{ field: 'y', name: 'Latitude' },{ field: 'elevation', name: 'Elevation', valueFormatter: (value) => `${Math.round(value)}m` },],},});chart.render();
This example demonstrates:
Using lines to draw contour outlines, closer to traditional topographic map representation.
import { Chart } from '@antv/g2';// Generate contour line dataconst generateContourLines = () => {const lines = [];const levels = [20, 40, 60, 80, 100]; // Contour line levelslevels.forEach((level, index) => {// Generate circular lines for each contour levelconst points = [];const centerX = 25;const centerY = 25;const baseRadius = 5 + index * 4;for (let angle = 0; angle <= 360; angle += 5) {const radian = (angle * Math.PI) / 180;const radius = baseRadius + Math.sin(angle * Math.PI / 45) * 2; // Add some variationconst x = centerX + radius * Math.cos(radian);const y = centerY + radius * Math.sin(radian);points.push({ x, y, level, lineId: `line_${level}` });}lines.push(...points);});return lines;};const contourLines = generateContourLines();const chart = new Chart({container: 'container',autoFit: true,height: 400,});chart.options({type: 'line',data: contourLines,encode: {x: 'x',y: 'y',color: 'level',series: 'lineId',},style: {strokeWidth: 2,strokeOpacity: 0.8,},scale: {color: {type: 'sequential',palette: 'oranges',},x: { nice: true },y: { nice: true },},axis: {x: { title: 'Distance (km)' },y: { title: 'Distance (km)' },},legend: {color: {title: 'Elevation (m)',layout: { justifyContent: 'center' },},},tooltip: {title: 'Contour Information',items: [{ field: 'level', name: 'Elevation', valueFormatter: (value) => `${value}m` },],},});chart.render();