import { Injectable } from '@angular/core'; import * as _ from 'lodash'; import { stackColumnData } from './stack-column-interface'; import * as legend from '../../config/legend' import * as series from '../../config/series' import * as toolbox from '../../config/toolbox' import * as tooltip from '../../config/tootip' import * as xAxis from '../../config/xAxis' import * as yAxis from '../../config/yAxis' import * as color from '../../config/colors'; import * as backgroundColor from '../../config/backgroundColor'; import * as datazoom from '../../config/datazoom'; import { CommonService } from '../../service/common.service' import { axisLabelRotate } from './stack-column.component' @Injectable({ providedIn: 'root' }) export class StackColumnService { constructor( private commonService: CommonService ) { } getStackBarChartDefaultOption() { let style = this.commonService.changeFontSize(); let option = { backgroundColor: backgroundColor.backgroundColor, color: color.defaultColors, grid: style.interval, legend: legend.getHorizontalLegend(style), toolbox: toolbox.getToolboxBasicMenu(), tooltip: tooltip.getCommonTooltipOfItem(), xAxis: xAxis.getCategoryXAxis(style), yAxis: yAxis.getValueYAxis(style), series: series.getStackBarChartSeries() } return option; } /** * 更改toolbox显示状态 * @param options echart配置 * @param show 显示开关 */ changeToolboxShow(options, show: boolean) { options.toolbox.show = show; } /** * 更改图例是否显示 * @param options echarts配置 * @param bcacLegendShow 图例显示开关 */ changeLegendShow(options, bcacLegendShow: boolean) { options.legend.show = bcacLegendShow; } /** * 更改图例位置 * @param options 配置 * @param positon 位置 */ changeLegendPosition(options, positon: string) { let style = this.commonService.changeFontSize(); switch (positon) { case 'bottom': options.legend = legend.getHorizontalLegend(style) options.grid = this.commonService.changeFontSize().interval break; case 'right': options.legend = legend.getLegendRightScroll(style) options.grid = this.commonService.changeFontSize().rightLegendGrid break; } } /** * 更改tooltip单位 * @param options echartsOption * @param tooltipUnit tooltip单位 */ _tooltipUnit: string = "" changeTooltipUnit(options, tooltipUnit: string) { this._tooltipUnit = tooltipUnit; // options.tooltip = tooltip.getCommonTooltipOfItem(tooltipUnit) } /** * 更改x轴名称 * @param options ECharts配置 * @param xAxisName x轴名称 */ changeXAxisName(options, xAxisName: string) { options.xAxis.name = xAxisName; } /** * 更改y轴名称 * @param options ECharts配置 * @param yAxisName y轴名称 */ changeYAxisName(options, yAxisName: string) { options.yAxis.name = yAxisName; } /** * 更改y轴name距离 * @param options ECharts配置 * @param yAxisNameGap y轴name距离 */ changeYAxisNameGap(options, yAxisNameGap: number) { options.yAxis.nameGap = yAxisNameGap; } /** * 更改x轴label的旋转状态 * @param chartOpt ECharts图options * @param xAxisLabelRotate x轴label的旋转 */ changeXAxisLabelRotate(chartOpt, xAxisLabelRotate: axisLabelRotate) { let rotate = 0; switch (xAxisLabelRotate) { case 'leftSmallAngle': rotate = 30 break; case 'leftBigAngle': rotate = 60 break; case 'rightSmallAngle': rotate = -30 break; case 'rightBigAngle': rotate = -60 break } chartOpt.xAxis.axisLabel.rotate = rotate; chartOpt.xAxis.axisLabel.formatter = (value: string): string => { let valueText = ''; if (value.length > 5) { valueText = value.substring(0, 5) + '...' } else { valueText = value } return valueText; } } /** * 更改y轴刻度label旋转状态 * @param chartOpt ECharts图配置 * @param yAxisLabelRotate y轴刻度label旋转 */ changeYAxisLabelRotate(chartOpt, yAxisLabelRotate: axisLabelRotate) { let rotate = 0; switch (yAxisLabelRotate) { case 'leftSmallAngle': rotate = 30 break; case 'leftBigAngle': rotate = 60 break; case 'rightSmallAngle': rotate = -30 break; case 'rightBigAngle': rotate = -60 break } chartOpt.yAxis.axisLabel.rotate = rotate; chartOpt.yAxis.axisLabel.formatter = (value: string): string => { let valueText = ''; if (value.length > 5) { valueText = value.substring(0, 5) + '...' } else { valueText = value } return valueText; } } /** * 更改图表数据 * @param options 图表配置 * @param data 数据 */ changeChartData(options, data: stackColumnData) { options.tooltip = tooltip.getCommonTooltipOfItem(this._tooltipUnit, data.tooltipFormatter) options.xAxis.data = data.xData; options.series = series.getStackBarChartSeries(data); } }