使用echarts在网页中具备下拉菜单,通过下拉菜单切换显示不同的图表; 2、图表1:采用柱状图分别显示男性、女性的两组数据,每组数据显示生还和去世人数; 3、图表2:采用饼图分别显示男性、女性的两组数据,每组数据显示生还和去世的比例; 4、图表3:采用线状图分别显示1,2,3等舱的生还人员数量; 5、图表4:采用散点图显示1,2,3等舱与生还人员年龄的对应关系。
时间: 2023-05-26 07:07:02 浏览: 103
暂时不支持做图表的操作,我只能提供相关代码供您参考:
HTML部分:
```
<body>
<div class="container">
<div class="form-group">
<label for="chart-type-select">选择图表类型:</label>
<select class="form-control" id="chart-type-select">
<option value="chart1">男女生还情况柱状图</option>
<option value="chart2">男女生还情况饼图</option>
<option value="chart3">1,2,3等舱生还情况线状图</option>
<option value="chart4">1,2,3等舱与生还人员年龄散点图</option>
</select>
</div>
<div id="chart-container" style="height: 400px;"></div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.1.3/js/bootstrap.min.js"></script>
<script src="./chart.js"></script>
</body>
```
JavaScript部分:
```
var myChart;
var chart1 = {
legend: {
data: ['男性生还', '男性去世','女性生还','女性去世'],
bottom: 'bottom'
},
tooltip: {},
xAxis: {
data: ["男性", "女性"]
},
yAxis: {},
series: [{
name: "男性生还",
type: "bar",
data: [80, 60]
},
{
name: "男性去世",
type: "bar",
data: [130, 50]
},
{
name: "女性生还",
type: "bar",
data: [100, 80]
},
{
name: "女性去世",
type: "bar",
data: [40, 20]
}
]
};
var chart2 = {
tooltip: {
trigger: 'item',
formatter: "{a} <br/>{b} : {c} ({d}%)"
},
legend: {
orient: 'vertical',
left: 'left',
data: ['男性生还','男性去世','女性生还','女性去世']
},
series: [
{
name: '生还情况',
type: 'pie',
radius: '55%',
center: ['50%', '60%'],
data: [
{value: 80, name: '男性生还'},
{value: 130, name: '男性去世'},
{value: 100, name: '女性生还'},
{value: 40, name: '女性去世'}
],
label: {
show: true,
formatter: '{b} : {c} ({d}%)'
},
emphasis: {
label: {
show: true,
fontSize: '20',
fontWeight: 'bold'
}
}
}
]
};
var chart3 = {
xAxis: {
type: 'category',
data: ['1等舱', '2等舱', '3等舱']
},
yAxis: {
type: 'value'
},
series: [{
data: [91, 86, 119],
type: 'line'
}]
};
var chart4 = {
xAxis: {
type: 'category',
name: '舱位',
data: ['1等舱', '2等舱', '3等舱']
},
yAxis: {
type: 'value',
name: '年龄'
},
series: [{
data: [
[1, 38],
[2, 27],
[3, 22]
],
type: 'scatter',
name: '生还',
symbolSize: function (data) {
return data[1]/2;
},
label: {
show: true,
formatter: function (param) {
return param.data[1] + '岁';
},
position: 'top'
}
}]
};
function renderChart(chartType) {
if (myChart != null && myChart != "" && myChart != undefined) {
myChart.dispose();
}
myChart = echarts.init(document.getElementById('chart-container'));
switch (chartType) {
case "chart1":
myChart.setOption(chart1);
break;
case "chart2":
myChart.setOption(chart2);
break;
case "chart3":
myChart.setOption(chart3);
break;
case "chart4":
myChart.setOption(chart4);
break;
default:
myChart.setOption(chart1);
}
}
$(document).ready(function () {
renderChart("chart1");
$('#chart-type-select').on('change', function() {
var chartType = this.value;
renderChart(chartType);
});
});
```
阅读全文