vue+element 柱状图
时间: 2023-07-08 09:48:29 浏览: 134
好的,下面是基于 Vue.js 和 Element UI 组件库实现的柱状图示例:
1. 安装 Element UI 组件库并按需引入相应组件:
```
npm i element-ui -S
```
```js
// 在 main.js 中按需引入组件
import Vue from 'vue';
import {
Button,
Select,
Option,
Row,
Col,
Input,
Form,
FormItem,
Loading,
Message,
MessageBox,
Notification,
Container,
Header,
Aside,
Main,
Menu,
Submenu,
MenuItem,
MenuItemGroup,
Breadcrumb,
BreadcrumbItem,
Card,
Table,
TableColumn,
Pagination,
Dialog,
Dropdown,
DropdownMenu,
DropdownItem,
Tabs,
TabPane,
Checkbox,
CheckboxGroup,
Radio,
RadioGroup,
Switch,
DatePicker,
TimePicker,
Upload,
Steps,
Step,
Carousel,
CarouselItem,
Tooltip,
Popover,
Alert,
Tag,
Badge,
Progress,
Tree,
Cascader,
Avatar,
Divider,
Image,
Popconfirm,
Slider,
Transfer,
ColorPicker,
Scrollbar,
Collapse,
CollapseItem
} from 'element-ui';
Vue.use(Button);
Vue.use(Select);
Vue.use(Option);
Vue.use(Row);
Vue.use(Col);
Vue.use(Input);
Vue.use(Form);
Vue.use(FormItem);
Vue.use(Loading.directive);
Vue.prototype.$loading = Loading.service;
Vue.prototype.$message = Message;
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$notify = Notification;
Vue.use(Container);
Vue.use(Header);
Vue.use(Aside);
Vue.use(Main);
Vue.use(Menu);
Vue.use(Submenu);
Vue.use(MenuItem);
Vue.use(MenuItemGroup);
Vue.use(Breadcrumb);
Vue.use(BreadcrumbItem);
Vue.use(Card);
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(Pagination);
Vue.use(Dialog);
Vue.use(Dropdown);
Vue.use(DropdownMenu);
Vue.use(DropdownItem);
Vue.use(Tabs);
Vue.use(TabPane);
Vue.use(Checkbox);
Vue.use(CheckboxGroup);
Vue.use(Radio);
Vue.use(RadioGroup);
Vue.use(Switch);
Vue.use(DatePicker);
Vue.use(TimePicker);
Vue.use(Upload);
Vue.use(Steps);
Vue.use(Step);
Vue.use(Carousel);
Vue.use(CarouselItem);
Vue.use(Tooltip);
Vue.use(Popover);
Vue.use(Alert);
Vue.use(Tag);
Vue.use(Badge);
Vue.use(Progress);
Vue.use(Tree);
Vue.use(Cascader);
Vue.use(Avatar);
Vue.use(Divider);
Vue.use(Image);
Vue.use(Popconfirm);
Vue.use(Slider);
Vue.use(Transfer);
Vue.use(ColorPicker);
Vue.use(Scrollbar);
Vue.use(Collapse);
Vue.use(CollapseItem);
```
2. 在组件中使用 echarts 绘制柱状图并绑定数据:
```html
<template>
<div class="chart-container">
<div ref="chart" class="chart"></div>
</div>
</template>
<script>
import echarts from 'echarts';
export default {
name: 'BarChart',
data() {
return {
chartData: [
{ month: '一月', sales: 1000 },
{ month: '二月', sales: 1200 },
{ month: '三月', sales: 800 },
{ month: '四月', sales: 1500 },
{ month: '五月', sales: 1300 },
{ month: '六月', sales: 1700 },
{ month: '七月', sales: 900 }
]
}
},
mounted() {
// 基于准备好的dom,初始化echarts实例
let myChart = echarts.init(this.$refs.chart);
// 指定图表的配置项和数据
let option = {
title: {
text: '月度销售额',
subtext: '单位:元'
},
tooltip: {},
xAxis: {
data: this.chartData.map(item => item.month),
axisLabel: {
interval: 0, // 强制显示所有刻度标签
rotate: 45, // 旋转角度
margin: 10 // 刻度标签与轴线之间的距离
}
},
yAxis: {},
series: [{
name: '销售额',
type: 'bar',
data: this.chartData.map(item => item.sales),
itemStyle: {
// 柱形图圆角
barBorderRadius: 5
}
}]
};
// 使用刚指定的配置项和数据显示图表。
myChart.setOption(option);
}
}
</script>
<style scoped>
.chart-container {
width: 100%;
height: 400px;
display: flex;
justify-content: center;
align-items: center;
}
.chart {
width: 80%;
height: 80%;
}
</style>
```
以上就是基于 Vue.js 和 Element UI 组件库实现的柱状图示例。其中,echarts 是一个非常强大的数据可视化库,可以通过配置项和数据来绘制各种类型的图表。
阅读全文