案例一:中国省市区级地图展示

ECharts的地图图表功能非常强大,可以轻松实现中国省市区级地图的展示。以下是一个简单的示例代码:

// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入中国地图数据
require('echarts/map/js/china');

// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
var option = {
    title: {
        text: '中国省市区级地图'
    },
    tooltip: {
        trigger: 'item'
    },
    series: [{
        name: '销量',
        type: 'map',
        mapType: 'china',
        label: {
            show: true
        },
        data: [{
            name: '北京',
            value: Math.round(Math.random() * 1000)
        }, {
            name: '上海',
            value: Math.round(Math.random() * 1000)
        }]
    }]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

案例二:世界地图展示

ECharts也支持世界地图的展示,以下是一个示例代码:

// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入世界地图数据
require('echarts/map/js/world');

// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
var option = {
    title: {
        text: '世界地图'
    },
    tooltip: {
        trigger: 'item',
        formatter: function (params) {
            return params.name + ' : ' + params.value;
        }
    },
    series: [{
        name: '访问来源',
        type: 'map',
        mapType: 'world',
        roam: true,
        label: {
            show: true
        },
        data: [{
            name: '巴西',
            value: 86
        }, {
            name: '印度',
            value: 28
        }, {
            name: '美国',
            value: 70
        }, {
            name: '俄罗斯',
            value: 49
        }]
    }]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

案例三:散点图地图

散点图地图可以直观地展示地理位置与数据的关系,以下是一个示例代码:

// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入世界地图数据
require('echarts/map/js/world');

// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
var option = {
    title: {
        text: '散点图地图'
    },
    tooltip: {
        trigger: 'item'
    },
    xAxis: {
        type: 'value'
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        name: '数据',
        type: 'scatter',
        coordinateSystem: 'cartesian2d',
        data: [{
            value: [104.974, 30.527],
            itemStyle: {
                color: 'red'
            }
        }, {
            value: [78.834, 39.915],
            itemStyle: {
                color: 'blue'
            }
        }, {
            value: [121.29, 31.230],
            itemStyle: {
                color: 'green'
            }
        }]
    }]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

案例四:柱状图地图

柱状图地图可以直观地展示地理位置与柱状数据的关系,以下是一个示例代码:

// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入世界地图数据
require('echarts/map/js/world');

// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
var option = {
    title: {
        text: '柱状图地图'
    },
    tooltip: {
        trigger: 'item'
    },
    xAxis: {
        type: 'value'
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        name: '数据',
        type: 'bar',
        coordinateSystem: 'cartesian2d',
        data: [{
            value: [104.974, 30.527],
            itemStyle: {
                color: 'red'
            }
        }, {
            value: [78.834, 39.915],
            itemStyle: {
                color: 'blue'
            }
        }, {
            value: [121.29, 31.230],
            itemStyle: {
                color: 'green'
            }
        }]
    }]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

案例五:折线图地图

折线图地图可以展示地理位置与折线数据的关系,以下是一个示例代码:

// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入世界地图数据
require('echarts/map/js/world');

// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
var option = {
    title: {
        text: '折线图地图'
    },
    tooltip: {
        trigger: 'item'
    },
    xAxis: {
        type: 'value'
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        name: '数据',
        type: 'line',
        coordinateSystem: 'cartesian2d',
        data: [{
            value: [104.974, 30.527],
            itemStyle: {
                color: 'red'
            }
        }, {
            value: [78.834, 39.915],
            itemStyle: {
                color: 'blue'
            }
        }, {
            value: [121.29, 31.230],
            itemStyle: {
                color: 'green'
            }
        }]
    }]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

案例六:散点图组合柱状图地图

散点图组合柱状图地图可以同时展示地理位置、散点数据和柱状数据,以下是一个示例代码:

// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入世界地图数据
require('echarts/map/js/world');

// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
var option = {
    title: {
        text: '散点图组合柱状图地图'
    },
    tooltip: {
        trigger: 'item'
    },
    xAxis: {
        type: 'value'
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        name: '数据',
        type: 'scatter',
        coordinateSystem: 'cartesian2d',
        data: [{
            value: [104.974, 30.527],
            itemStyle: {
                color: 'red'
            }
        }, {
            value: [78.834, 39.915],
            itemStyle: {
                color: 'blue'
            }
        }, {
            value: [121.29, 31.230],
            itemStyle: {
                color: 'green'
            }
        }]
    }, {
        name: '柱状数据',
        type: 'bar',
        coordinateSystem: 'cartesian2d',
        data: [{
            value: [104.974, 30.527],
            itemStyle: {
                color: 'red'
            }
        }, {
            value: [78.834, 39.915],
            itemStyle: {
                color: 'blue'
            }
        }, {
            value: [121.29, 31.230],
            itemStyle: {
                color: 'green'
            }
        }]
    }]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

案例七:散点图组合折线图地图

散点图组合折线图地图可以同时展示地理位置、散点数据和折线数据,以下是一个示例代码:

// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入世界地图数据
require('echarts/map/js/world');

// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
var option = {
    title: {
        text: '散点图组合折线图地图'
    },
    tooltip: {
        trigger: 'item'
    },
    xAxis: {
        type: 'value'
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        name: '数据',
        type: 'scatter',
        coordinateSystem: 'cartesian2d',
        data: [{
            value: [104.974, 30.527],
            itemStyle: {
                color: 'red'
            }
        }, {
            value: [78.834, 39.915],
            itemStyle: {
                color: 'blue'
            }
        }, {
            value: [121.29, 31.230],
            itemStyle: {
                color: 'green'
            }
        }]
    }, {
        name: '折线数据',
        type: 'line',
        coordinateSystem: 'cartesian2d',
        data: [{
            value: [104.974, 30.527],
            itemStyle: {
                color: 'red'
            }
        }, {
            value: [78.834, 39.915],
            itemStyle: {
                color: 'blue'
            }
        }, {
            value: [121.29, 31.230],
            itemStyle: {
                color: 'green'
            }
        }]
    }]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

案例八:散点图组合柱状图折线图地图

散点图组合柱状图折线图地图可以同时展示地理位置、散点数据、柱状数据和折线数据,以下是一个示例代码:

// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入世界地图数据
require('echarts/map/js/world');

// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
var option = {
    title: {
        text: '散点图组合柱状图折线图地图'
    },
    tooltip: {
        trigger: 'item'
    },
    xAxis: {
        type: 'value'
    },
    yAxis: {
        type: 'value'
    },
    series: [{
        name: '数据',
        type: 'scatter',
        coordinateSystem: 'cartesian2d',
        data: [{
            value: [104.974, 30.527],
            itemStyle: {
                color: 'red'
            }
        }, {
            value: [78.834, 39.915],
            itemStyle: {
                color: 'blue'
            }
        }, {
            value: [121.29, 31.230],
            itemStyle: {
                color: 'green'
            }
        }]
    }, {
        name: '柱状数据',
        type: 'bar',
        coordinateSystem: 'cartesian2d',
        data: [{
            value: [104.974, 30.527],
            itemStyle: {
                color: 'red'
            }
        }, {
            value: [78.834, 39.915],
            itemStyle: {
                color: 'blue'
            }
        }, {
            value: [121.29, 31.230],
            itemStyle: {
                color: 'green'
            }
        }]
    }, {
        name: '折线数据',
        type: 'line',
        coordinateSystem: 'cartesian2d',
        data: [{
            value: [104.974, 30.527],
            itemStyle: {
                color: 'red'
            }
        }, {
            value: [78.834, 39.915],
            itemStyle: {
                color: 'blue'
            }
        }, {
            value: [121.29, 31.230],
            itemStyle: {
                color: 'green'
            }
        }]
    }]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

案例九:自定义地图

ECharts允许自定义地图,你可以使用任何你想要的地图数据,以下是一个示例代码:

// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入自定义地图数据
require('echarts/map/js/customMap');

// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
var option = {
    title: {
        text: '自定义地图'
    },
    tooltip: {
        trigger: 'item'
    },
    series: [{
        name: '销量',
        type: 'map',
        mapType: 'customMap',
        label: {
            show: true
        },
        data: [{
            name: '北京',
            value: Math.round(Math.random() * 1000)
        }, {
            name: '上海',
            value: Math.round(Math.random() * 1000)
        }]
    }]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

案例十:ECharts地图交互

ECharts地图图表支持多种交互效果,如点击事件、鼠标悬停事件等,以下是一个示例代码:

// 引入 ECharts 主模块
var echarts = require('echarts/lib/echarts');
// 引入世界地图数据
require('echarts/map/js/world');

// 基于准备好的dom,初始化echarts实例
var myChart = echarts.init(document.getElementById('main'));

// 指定图表的配置项和数据
var option = {
    title: {
        text: 'ECharts地图交互'
    },
    tooltip: {
        trigger: 'item',
        formatter: function (params) {
            return params.name + ' : ' + params.value;
        }
    },
    series: [{
        name: '访问来源',
        type: 'map',
        mapType: 'world',
        roam: true,
        label: {
            show: true
        },
        data: [{
            name: '巴西',
            value: 86
        }, {
            name: '印度',
            value: 28
        }, {
            name: '美国',
            value: 70
        }, {
            name: '俄罗斯',
            value: 49
        }]
    }]
};

// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);

// 监听点击事件
myChart.on('click', function (params) {
    if (params.componentType === 'series') {
        alert('点击了 ' + params.name);
    }
});

// 监听鼠标悬停事件
myChart.on('mouseover', function (params) {
    if (params.componentType === 'series') {
        alert('鼠标悬停在 ' + params.name);
    }
});

通过以上10个案例,相信你已经掌握了ECharts地图图表的基本使用方法。在实际应用中,你可以根据自己的需求调整图表样式、数据、交互效果等,使你的地图图表更加美观、实用。