处理数据通过ecarts绘制一个图表拥有两个坐标系的多列柱状图var options={ data:[ { "column1": "男", "column2": 165, "column3": "2022-10" }, { "column1": "男", "column2": 178, "column3": "2022-11" }, { "column1": "男", "column2": 197, "column3": "2022-12" }, { "column1": "男", "column2": 120, "column3": "2023-01" }, { "column1": "男", "column2": 130, "column3": "2023-02" }, { "column1": "男", "column2": 137, "column3": "2023-03" }, { "column1": "女", "column2": 150, "column3": "2022-10" }, { "column1": "女", "column2": 201, "column3": "2022-11" }, { "column1": "女", "column2": 225, "column3": "2022-12" }, { "column1": "女", "column2": 104, "column3": "2023-01" }, { "column1": "女", "column2": 188, "column3": "2023-02" }, { "column1": "女", "column2": 148, "column3": "2023-03" } ] }
时间: 2023-06-28 10:07:58 浏览: 71
可以按照以下步骤来实现:
1. 导入 echarts 库并创建一个容器,用于显示图表:
```html
<div id="chart-container" style="width: 600px; height: 400px;"></div>
<script src="https://cdn.jsdelivr.net/npm/echarts@5.1.2/dist/echarts.min.js"></script>
```
2. 解析数据并将其转化为 echarts 柱状图所需的格式:
```javascript
var maleData = [];
var femaleData = [];
for (var i = 0; i < options.data.length; i++) {
var data = options.data[i];
if (data.column1 === "男") {
maleData.push({
value: data.column2,
xAxis: data.column3
});
} else {
femaleData.push({
value: data.column2,
xAxis: data.column3
});
}
}
var chartData = [
{
name: "男",
type: "bar",
data: maleData
},
{
name: "女",
type: "bar",
data: femaleData
}
];
```
3. 创建 echarts 实例并配置图表:
```javascript
var chart = echarts.init(document.getElementById("chart-container"));
var option = {
legend: {
data: ["男", "女"]
},
xAxis: [
{
type: "category",
axisTick: { show: false },
data: maleData.map(function(item) {
return item.xAxis;
})
},
{
type: "category",
axisTick: { show: false },
data: femaleData.map(function(item) {
return item.xAxis;
})
}
],
yAxis: {},
series: chartData,
tooltip: {
trigger: "axis"
}
};
chart.setOption(option);
```
这样就可以在页面中显示一个拥有两个坐标系的多列柱状图了。
阅读全文