通过echarts绘制//数据 var dataAll=[ { "column1": "燃煤", "column2": "申能股份", "column3": "2023-01", "column4": 317750.73, "column5": 295578.54 }, { "column1": "燃煤", "column2": "申能股份", "column3": "2023-02", "column4": 396408.15, "column5": 376156.47 }, { "column1": "燃煤", "column2": "申能股份", "column3": "2023-04", "column4": 315044.31, "column5": "301267.0" }, { "column1": "燃气", "column2": "崇明", "column3": "2023-01", "column4": 521.88, "column5": 511.01 }, { "column1": "燃气", "column2": "崇明", "column3": "2023-02", "column4": 9657.24, "column5": 9482.77 }, { "column1": "燃气", "column2": "崇明", "column3": "2023-04", "column4": 5930.67, "column5": 5825.95 }, { "column1": "燃气", "column2": "淮北", "column3": "2023-01", "column4": 43226.7, "column5": 41509.4 }, { "column1": "燃气", "column2": "淮北", "column3": "2023-02", "column4": 50857.25, "column5": "49068.0" }, { "column1": "燃气", "column2": "淮北", "column3": "2023-04", "column4": "45654.0", "column5": "14564.0" }, { "column1": "燃气", "column2": "申能股份", "column3": "2023-04", "column4": 31044.31, "column5": "30267.0" }, { "column1": "燃气", "column2": "申皖", "column3": "2023-01", "column4": 31620.29, "column5": "30050.0" }, { "column1": "燃气", "column2": "申皖", "column3": "2023-02", "column4": 60660.21, "column5": "57776.0" }, ]来绘制多列柱状图,,column1作为第一次,cloumn2为第二层,clomn3为第三层,动态获取column1的值,作为图列
时间: 2023-07-12 11:56:10 浏览: 85
vue中使用echarts实现省市地图绘制,根据数据在地图上显示柱状图信息
好的,您可以参考以下代码实现您的需求:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>多列柱状图</title>
<!-- 引入 ECharts 文件 -->
<script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
</head>
<body>
<!-- 定义一个具有一定高度的 div 容器 -->
<div id="chart" style="height: 500px;"></div>
<script type="text/javascript">
// 定义数据
var dataAll=[
{
"column1": "燃煤",
"column2": "申能股份",
"column3": "2023-01",
"column4": 317750.73,
"column5": 295578.54
},
{
"column1": "燃煤",
"column2": "申能股份",
"column3": "2023-02",
"column4": 396408.15,
"column5": 376156.47
},
{
"column1": "燃煤",
"column2": "申能股份",
"column3": "2023-04",
"column4": 315044.31,
"column5": "301267.0"
},
{
"column1": "燃气",
"column2": "崇明",
"column3": "2023-01",
"column4": 521.88,
"column5": 511.01
},
{
"column1": "燃气",
"column2": "崇明",
"column3": "2023-02",
"column4": 9657.24,
"column5": 9482.77
},
{
"column1": "燃气",
"column2": "崇明",
"column3": "2023-04",
"column4": 5930.67,
"column5": 5825.95
},
{
"column1": "燃气",
"column2": "淮北",
"column3": "2023-01",
"column4": 43226.7,
"column5": 41509.4
},
{
"column1": "燃气",
"column2": "淮北",
"column3": "2023-02",
"column4": 50857.25,
"column5": "49068.0"
},
{
"column1": "燃气",
"column2": "淮北",
"column3": "2023-04",
"column4": "45654.0",
"column5": "14564.0"
},
{
"column1": "燃气",
"column2": "申能股份",
"column3": "2023-04",
"column4": 31044.31,
"column5": "30267.0"
},
{
"column1": "燃气",
"column2": "申皖",
"column3": "2023-01",
"column4": 31620.29,
"column5": "30050.0"
},
{
"column1": "燃气",
"column2": "申皖",
"column3": "2023-02",
"column4": 60660.21,
"column5": "57776.0"
},
];
// 获取所有的 column1 的值
var column1List = dataAll.map(function (item) {
return item.column1;
});
// 去重
var column1Set = new Set(column1List);
var column1UniqueList = Array.from(column1Set);
// 定义图例
var legendData = column1UniqueList;
// 定义 X 轴数据
var xAxisData = dataAll.map(function (item) {
return item.column3;
});
// 定义系列
var series = [];
// 处理每个 column1 的数据
column1UniqueList.forEach(function (column1) {
// 获取当前 column1 的所有数据
var data = dataAll.filter(function (item) {
return item.column1 === column1;
});
// 处理数据,将 column2 作为每个系列的名称,将 column4 作为每个系列的数据
var seriesData = data.map(function (item) {
return {
name: item.column2,
value: item.column4
}
});
// 将数据添加到系列中
series.push({
name: column1,
type: 'bar',
stack: '总量',
data: seriesData
});
});
// 使用 ECharts 初始化图表
var myChart = echarts.init(document.getElementById('chart'));
// 配置项
var option = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
}
},
legend: {
data: legendData
},
xAxis: {
type: 'category',
data: xAxisData
},
yAxis: {
type: 'value'
},
series: series
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
</script>
</body>
</html>
```
这段代码会根据您提供的数据绘制一个多列柱状图,并且根据 column1 的值动态生成图例。
阅读全文