import { Injectable } from '@angular/core'; import * as _ from 'lodash'; import * as color from '../../config/colors' import * as background from '../../config/backgroundColor' 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 brush from '../../config/brush'; import * as datazoom from '../../config/datazoom'; import { columnData, axisLabelRotate, legendPosition } from './basic-bar-interface' import { CommonService } from '../../service/common.service' @Injectable({ providedIn: 'root' }) export class BasicBarService { constructor( private commonService: CommonService ) { } // 柱状图 // getColumnChartOption(data: columnData, bcacChartShowMenu: boolean = true, legendPosition: legendPosition) { // let toolboxType = this.getToolbox(bcacChartShowMenu); // let legendType: { legend: any, grid: any } = this.getLegendType(legendPosition, this.commonService.changeFontSize()) // // console.log(legendType) // let option = { // backgroundColor: background.backgroundColor, // color: color.defaultColors, // grid: legendType.grid, // legend: legendType.legend, // toolbox: toolboxType, // tooltip: tooltip.getCommonTooltipOfAxis(data.tooltipUnit, data.tooltipFormatter), // xAxis: xAxis.getCategoryXAxis(data.xData), // yAxis: yAxis.getValueYAxis(data.yAxisName), // series: series.getColumnChartSeries(data) // } // return option; // } // 获取柱状图默认option getColumnChartDefaultOpiton() { let style = this.commonService.changeFontSize() let option = { backgroundColor: background.backgroundColor, color: color.defaultColors, grid: style.interval, legend: legend.getLegendBottomScroll(style), toolbox: toolbox.getToolboxBasicMenu(), // brush: brush.getLineXBrush(), tooltip: tooltip.getCommonTooltipOfAxis(), xAxis: xAxis.getCategoryXAxis(style), yAxis: yAxis.getValueYAxis(style), series: series.getColumnChartSeries(style) } return option; } // 更改图例显示状态 changeLegendShow(chartOpt, bcacLegendShow: boolean) { let option = _.cloneDeep(chartOpt); option.legend.show = bcacLegendShow; return option; } // 更改图例位置 changeLegendPosition(chartOpt, position: legendPosition) { let style = this.commonService.changeFontSize(); switch (position) { case 'bottom': chartOpt.legend = legend.getLegendBottomScroll(style) chartOpt.grid = style.interval break; case 'above': chartOpt.legend = legend.getAboveBlock(style) chartOpt.grid = style.interval break; case 'right': chartOpt.legend = legend.getLegendRightScroll(style) chartOpt.grid = style.rightLegendGrid break; } } // 更改toolbox显示状态 changeToolboxShow(chartOpt, bcacLegendShow: boolean) { chartOpt.toolbox.show = bcacLegendShow; } // 更改brush开关状态 changeBrushStatus(chartOpt, style, callback?: ($evetn, ...arg: any[]) => void) { if (callback) { chartOpt.brush = brush.getLineXBrush() chartOpt.toolbox = toolbox.getToolboxIncludeCalc(style, callback); } } // 更改数据 changeChartData(chartOpt, data: columnData) { let style = this.commonService.changeFontSize() chartOpt.xAxis.data = data.xData; chartOpt.series = series.getColumnChartSeries(style, data) } // 更改tooltipUnit changeTooltipUnit(chartOpt, tooltipUnit: string | string[]) { chartOpt.tooltip = tooltip.getCommonTooltipOfAxis(tooltipUnit) } // 更改XAxisName changeXAxisName(chartOpt, xAxisName: string) { chartOpt.xAxis.name = xAxisName; } // 更改yAxisName changeYAxisName(chartOpt, yAxisName: string) { chartOpt.yAxis.name = yAxisName; } // 更改yAxisNameGap changeYAxisNameGap(chartOpt, yAxisNameGap: number) { chartOpt.yAxis.nameGap = yAxisNameGap; } // 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的旋转 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; } } }