| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- <template>
- <div class="market-container">
- <div class="market-title">
- <img src="@/assets/images/shb.png" class="title-icon" />
- 市场拓展
- </div>
- <div class="legend">
- <div class="legend-item">上月</div>
- <div class="legend-item">本月</div>
- </div>
- <div class="chart-wrap">
- <div id="marketChart" class="chart"></div>
- <div id="marketChart2" class="chart2"></div>
- </div>
- </div>
- </template>
- <script>
- import { mapState } from "vuex"
- import { api } from "@/api"
- import * as echarts from "echarts"
- export default {
- name: "MarketExpansion",
- props: {
- data: {
- type: Object,
- default: () => ({}),
- },
- },
- data() {
- return {
- chartInstanceA: null,
- chartInstanceB: null,
- chartData: {
- categories: [
- "新增户数",
- "新增项目",
- "新增合同",
- "新增联系人",
- "跟进企业",
- "新增企业",
- ],
- },
- }
- },
- computed: {
- ...mapState(["entCode", "communityId"]),
- },
- watch: {
- data: {
- handler(newVal, oldVal) {
- if (newVal) {
- this.initChart()
- }
- },
- deep: true,
- },
- },
- mounted() {
- this.init()
- window.addEventListener("resize", this.handleResize)
- },
- beforeDestroy() {
- if (this.chartInstanceA) {
- this.chartInstanceA.dispose()
- }
- window.removeEventListener("resize", this.handleResize)
- },
- methods: {
- // 初始化组件
- init() {
- this.initChart()
- },
- // 初始化图表
- initChart() {
- this.chartInstanceA = echarts.init(
- document.getElementById("marketChart")
- )
- this.chartInstanceB = echarts.init(
- document.getElementById("marketChart2")
- )
- const lastMonthData = Object.keys(this.data)
- .map((key, index) => {
- return -this.data[key].last_month
- })
- .reverse()
- const thisMonthData = Object.keys(this.data)
- .map((key, index) => {
- return this.data[key].this_month
- })
- .reverse()
- const optionA = {
- tooltip: {
- trigger: "axis",
- axisPointer: {
- type: "shadow",
- },
- backgroundColor: "rgba(17, 28, 49, 0.9)",
- borderColor: "rgba(64, 158, 255, 0.2)",
- textStyle: {
- color: "#ffffff",
- },
- formatter: (params) => {
- // 自定义tooltip格式
- let result = params[0].name + "<br/>"
- params.forEach((item) => {
- result +=
- item.marker +
- item.seriesName +
- ": " +
- Math.abs(item.value) +
- "<br/>"
- })
- return result
- },
- },
- grid: {
- left: "10%",
- right: "1%",
- bottom: "5%",
- top: "5%",
- containLabel: true,
- },
- xAxis: {
- type: "value",
- axisLabel: {
- formatter: (value) => {
- if (value < 0) return -value //这里是针对取负值
- else return value
- },
- },
- show: false, // 隐藏x轴
- },
- yAxis: [
- {
- type: "category",
- data: this.chartData.categories,
- axisLine: {
- show: false,
- },
- axisTick: {
- show: false,
- },
- axisLabel: {
- show: false,
- },
- },
- ],
- series: [
- {
- name: "上月",
- type: "bar",
- // barMinHeight: 20,
- data: lastMonthData,
- itemStyle: {
- // 左侧渐变色:从#F66757到#FCE2B4
- color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [
- { offset: 0, color: "#FCE2B4" },
- { offset: 1, color: "#F66757" },
- ]),
- borderRadius: [20, 0, 0, 20],
- },
- label: {
- show: true,
- position: "left",
- formatter: "{c}",
- color: "#ffffff",
- fontSize: 12,
- formatter: (value) => {
- return -value.data
- },
- },
- },
- ],
- }
- const optionB = {
- tooltip: {
- trigger: "axis",
- axisPointer: {
- type: "shadow",
- },
- backgroundColor: "rgba(17, 28, 49, 0.9)",
- borderColor: "rgba(64, 158, 255, 0.2)",
- textStyle: {
- color: "#ffffff",
- },
- formatter: (params) => {
- // 自定义tooltip格式
- let result = params[0].name + "<br/>"
- params.forEach((item) => {
- result +=
- item.marker +
- item.seriesName +
- ": " +
- Math.abs(item.value) +
- "<br/>"
- })
- return result
- },
- },
- grid: {
- left: "1%",
- right: "10%",
- bottom: "5%",
- top: "5%",
- containLabel: true,
- },
- xAxis: {
- type: "value",
- axisLabel: {
- formatter: (value) => {
- if (value < 0) return -value //这里是针对取负值
- else return value
- },
- },
- show: false, // 隐藏x轴
- },
- yAxis: [
- {
- type: "category",
- data: this.chartData.categories,
- axisLine: {
- show: false,
- },
- axisTick: {
- show: false,
- },
- axisLabel: {
- show: true,
- color: "#a0b3d6",
- fontSize: 14,
- align: "right",
- },
- },
- ],
- series: [
- {
- name: "本月",
- type: "bar",
- // barMinHeight: 20,
- data: thisMonthData,
- itemStyle: {
- // 右侧渐变色:从#1620D4到#3AB3E3
- color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
- { offset: 0, color: "#3AB3E3" },
- { offset: 1, color: "#1620D4" },
- ]),
- borderRadius: [0, 20, 20, 0],
- },
- label: {
- show: true,
- position: "right",
- formatter: "{c}",
- color: "#ffffff",
- fontSize: 12,
- },
- },
- ],
- }
- this.chartInstanceA.setOption(optionA)
- this.chartInstanceA.resize()
- this.chartInstanceB.setOption(optionB)
- this.chartInstanceB.resize()
- },
- // 处理窗口大小变化
- handleResize() {
- if (this.chartInstanceA) {
- this.chartInstanceA.resize()
- }
- if (this.chartInstanceB) {
- this.chartInstanceB.resize()
- }
- },
- },
- }
- </script>
- <style lang="scss" scoped>
- @import "@/assets/css/theme.scss";
- .market-container {
- background: #0d1629;
- border-radius: halfW(4);
- height: 100%;
- display: flex;
- flex-direction: column;
- background: var(--content-bg);
- }
- .market-title {
- border-radius: halfW(4);
- background: var(--title-bg);
- padding: halfH(4) halfW(10);
- font-size: halfW(16);
- color: var(--title-primary);
- display: flex;
- align-items: center;
- }
- .legend {
- display: flex;
- justify-content: space-around;
- align-items: center;
- padding: halfH(10) halfW(20);
- font-size: halfW(14);
- font-weight: 500;
- color: #fff;
- border-bottom: 1px solid rgba(64, 158, 255, 0.2);
- }
- .chart-wrap {
- display: flex;
- align-items: center;
- width: 100%;
- height: 100%;
- .chart {
- width: 40%;
- height: 100%;
- background: var(--content-bg);
- }
- .chart2 {
- width: 60%;
- height: 100%;
- background: var(--content-bg);
- }
- }
- </style>
|