🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
路径:D:\ireport365\ireport365.war\js\enduser\designer\vs-component-echarts.js 添加组件对象 ``` { name: "", type: "boxplot", coverImage: contextPath + "/images/componenttypes/" + locale + "/echarts/zh_world.png", coverImageWidth: "50%", tip: "盒须图" } ``` 添加箱线图模板对象 ``` // 箱线图对象 var boxplotOptionTemplate = { animationDuration: 500, title: { show:false }, tooltip: { show: true, trigger: "item", axisPointer: { type: 'shadow' } }, toolbox: { //可视化的工具箱 show: false, feature: { dataView: { //数据视图 show: true }, restore: { //重置 show: true }, saveAsImage: {//保存图片 show: true } } }, dataZoom: { show: false, realtime: true, showDetail: true, handleSize: 12, height: 30, type: 'slider', backgroundColor:"rgba(47,69,84,0)", //组件的背景颜色 fillerColor:"rgba(167,183,204,0.6)", //选中范围的填充颜色。 borderColor:"#ddd", //边框颜色。 filterMode: 'filter', throttle:100, left:"center", //组件离容器左侧的距离,'left', 'center', 'right','20%' top:"bottom", //组件离容器上侧的距离,'top', 'middle', 'bottom','20%' right:"auto", //组件离容器右侧的距离,'20%' bottom:"0%", //组件离容器下侧的距离,'20%' }, grid: { x: 42, y: 30, x2: 11, y2: 50, borderWidth: 0, borderColor: "#f0f0f0" }, xAxis: { type: 'category', data: ['支付宝','微信','银联','闪付','京东','移动支付'], nameTextStyle: { color: '#3259B8', fontSize: 14, }, axisTick:{ show:false, }, axisLine: { lineStyle: { color: '#3259B8', } }, splitLine: { show: false } }, yAxis: { type: 'value', nameTextStyle: { color: '#3259B8', fontSize: 14, }, axisLabel:{ formatter:'{value}', }, axisLine: { lineStyle: { color: '#3259B8', }}, splitLine: { lineStyle: { color: '#A7BAFA', }, } }, series: [{ name: 'boxplot', type: 'boxplot', data: '', tooltip: { formatter: function (param) { return [ 'grade: ' + param.name, 'upper: ' + param.data[4], 'Q3: ' + param.data[3], 'median: ' + param.data[2], 'Q1: ' + param.data[1], 'lower: ' + param.data[0] ].join('<br/>') } }, itemStyle: { normal:{ borderColor: { type: 'linear', x: 0, y: 0, x2: 0, y2: 1, colorStops: [{ offset: 0, color: '#F02FC2' // 0% 处的颜色 }, { offset: 1, color: '#3EACE5' // 100% 处的颜色 }], globalCoord: false // 缺省为 false }, borderWidth:2, color: { type: 'linear', x: 0, y: 0, x2: 0, y2: 1, colorStops: [{ offset: 0, color: 'rgba(240,47,194,0.7)' // 0% 处的颜色 }, { offset: 1, color: 'rgba(62,172,299,0.7)' // 100% 处的颜色 }], globalCoord: false // 缺省为 false }, } }, }, ] } // end ``` 核心处理数据函数 ``` var internalRefreshBoxplotModelData = function () { var option = component.config.chartConfig; var dimensions = component.config.datasourceConfig.dimensions; var measures = component.config.datasourceConfig.measures; var data = component.context.data; var chartData = []; var chartDataMap = {}; var measureIdx = 0; console.log(data) /** * 箱体图需要两个维度 一个度量 * 生成图形,需要将data按最后一个维度将度量进行分组 */ // x轴数据 var xAxisData = []; // 箱体数据需要的数组 var seriesData = []; // 箱体数据名称的数组,主要是为了显示异常值的维度名称 var seriesDataName = []; // 根据倒数第二个维度,获取X轴数组 for (var i = 0; i < data.length; i++) { var value = data[i][dimensions[dimensions.length - 2].name]; var isAdd = true; for (var d = 0; d < xAxisData.length; d++) { if (xAxisData[d] == value) { isAdd = false; break; } } if (isAdd) { xAxisData[xAxisData.length] = value; } } // 根据倒数第二和倒数第一个维度 + 倒数第一个度量 获取度量值 组成箱体数据需要的数组 for (var d = 0; d < xAxisData.length; d++) { var xv = xAxisData[d]; var seriesGroupData = []; var seriesGroupDataName = []; for (var i = 0; i < data.length; i++) { var dimensions1 = data[i][dimensions[dimensions.length - 2].name]; if (xv == dimensions1) { seriesGroupData[seriesGroupData.length] = data[i][measures[measures.length - 1].name]; seriesGroupDataName[seriesGroupDataName.length] = { name: data[i][dimensions[dimensions.length - 1].name], value: data[i][measures[measures.length - 1].name]}; } } seriesData[seriesData.length] = seriesGroupData; seriesDataName[seriesDataName.length] = seriesGroupDataName; } // 根据箱体需要的数组调用echart生成 箱体数据 因为原生的生成方法 异常值没有维度名称,此处修改为把维度名称传进去并在异常值中显示维度名称 var seriesLoadData = echarts.dataTool.prepareBoxplotData(seriesData, null, seriesDataName); option.xAxis.data = xAxisData; // 异常值对象 此处动态添加 var outliers = { name: '异常值', type: 'scatter', itemStyle: { normal: { color: 'red' } }, symbolSize:5, data: seriesLoadData.outliers, label: { show: true, formatter: "{@[2]}", position: 'right', } } var series = []; series[0] = option.series[0]; series[0].data = seriesLoadData.boxData; series[1] = outliers; option.series = series; component.context.chart.setOption(option, true); }; ``` 添加调用数据处理的函数 ``` case "boxplot": internalRefreshBoxplotModelData(); break; ``` 设置2个维度 如图的位置 搜索 _buildGaugeThresholdDescription ![](https://box.kancloud.cn/fe670ee1a5a7bc0d0847ca1f45e5c99f_927x730.png) ``` // 添加箱体图支持两个维度 switch (component.type) { case "boxplot": scope.component.config.chartDimensionCount = 2; break; } ``` 在树图配置项下面添加 ``` case "boxplot": chartCategory.groups.push({ // 图例 title: { text: "基本配置" }, elements: [ { title: "工具箱", type: "switch", bind: "showRoam", on: vsLang.on, off: vsLang.off }] }); // 箱线图轴线配置 chartCategory.groups.push({ // 图例 title: { text: "轴线配置" }, elements: [{ // x轴颜色 title: "x轴颜色", type: "colorpicker", bind: "boxplotColorX" },{ // y轴颜色 title: "y轴颜色", type: "colorpicker", bind: "boxplotColorY" },{ // 分割线颜色 title: "分割线颜色", type: "colorpicker", bind: "boxplotColorXf" },{ // 箱线宽度 title: "箱线宽度", type: "configSlide", bind: "boxplotBorder", config: { slideEnd: 20 } }] }); // 盒须图渐变色 chartCategory.groups.push({ // 图例 title: { text: "渐变色配置" }, elements: [{ // 箱体颜色一 title: "箱体颜色一", type: "colorpicker", bind: "boxplotColorOne" },{ // 箱体颜色二 title: "箱体颜色二", type: "colorpicker", bind: "boxplotColorTow" },{ // 箱线颜色一 title: "箱线颜色一", type: "colorpicker", bind: "boxplotColorOneLine" },{ // 箱线颜色二 title: "箱线颜色二", type: "colorpicker", bind: "boxplotColorTowLine" }] }); break; ``` 树图监听下面 加 箱线图的监听 ``` // 监听箱线图 case "boxplot": scope.$watch("component.config.showRoam", function (newValue) { if (newValue != null) { // console.log(newValue) var option = component.config.chartConfig; option.toolbox.show = newValue; scope.component.context.chart.setOption(option, true) } }); // x轴颜色 scope.$watch("component.config.boxplotColorX", function (newValue) { if (newValue != null) { // console.log(newValue) var option = component.config.chartConfig; option.xAxis.axisLine.lineStyle.color = newValue; scope.component.context.chart.setOption(option, true) } }); // y轴颜色 scope.$watch("component.config.boxplotColorY", function (newValue) { if (newValue != null) { // console.log(newValue) var option = component.config.chartConfig; option.yAxis.axisLine.lineStyle.color = newValue; scope.component.context.chart.setOption(option, true) } }); // 分割轴颜色 scope.$watch("component.config.boxplotColorXf", function (newValue) { if (newValue != null) { // console.log(newValue) var option = component.config.chartConfig; option.yAxis.splitLine.lineStyle.color = newValue; scope.component.context.chart.setOption(option, true) } }); // 渐变色1 scope.$watch("component.config.boxplotColorOne", function (newValue) { if (newValue != null) { // console.log(newValue) var option = component.config.chartConfig; option.series[0].itemStyle.normal.color.colorStops[0].color = newValue; scope.component.context.chart.setOption(option, true) } }); // 渐变色2 scope.$watch("component.config.boxplotColorTow", function (newValue) { if (newValue != null) { // console.log(newValue) var option = component.config.chartConfig; option.series[0].itemStyle.normal.color.colorStops[1].color = newValue; scope.component.context.chart.setOption(option, true) } });// 渐变色1 scope.$watch("component.config.boxplotColorOneLine", function (newValue) { if (newValue != null) { // console.log(newValue) var option = component.config.chartConfig; option.series[0].itemStyle.normal.borderColor.colorStops[0].color = newValue; scope.component.context.chart.setOption(option, true) } }); // 渐变色2 scope.$watch("component.config.boxplotColorTowLine", function (newValue) { if (newValue != null) { // console.log(newValue) var option = component.config.chartConfig; option.series[0].itemStyle.normal.borderColor.colorStops[1].color = newValue; scope.component.context.chart.setOption(option, true) } }); // 箱线宽度 scope.$watch("component.config.boxplotBorder", function (newValue) { if (newValue != null) { // console.log(newValue) var option = component.config.chartConfig; option.series[0].itemStyle.normal.borderWidth = newValue; scope.component.context.chart.setOption(option, true) } }); break; ``` 在配置项及监听提示窗属性里 添加case "boxplot": 如图 ![](https://box.kancloud.cn/666c5598595797936735f9ef8bab9433_607x442.png) ![](https://box.kancloud.cn/53a4d4e416cf0a232b1405786d9aadd8_526x414.png) 在配置项及监听拖动小组件里 添加case "boxplot": 如图 ![](https://box.kancloud.cn/35caf8deb3f87288eb90a65f5e7bec14_658x453.png) ![](https://box.kancloud.cn/55a01d3984beaf3275e354969f2204d5_633x425.png) 深拷贝对象 angular.copy ``` case "boxplot": option = angular.copy(boxplotOptionTemplate); var data = echarts.dataTool.prepareBoxplotData([[30645,53490,66640.5,89123,159949,],[19464,46454,59139,83479,19464,],[16704,46041,60155,86818,159980,],[21543,41619.75,58819.5,87540,159978,],[15202,35757,44721,59916.5,159825,],[22158,34754.5,49718,71637,139972,],]); option.series[0].data = data.boxData; break; ``` 分享页 D:\ireport365\ireport365.war\WEB-INF\classes\report-resource\design.js 核心数据处理函数 ``` var internalRefreshBoxplotModelData = function () { var option = component.config.chartConfig; var dimensions = component.config.datasourceConfig.dimensions; var measures = component.config.datasourceConfig.measures; var data = component.context.data; var chartData = []; var chartDataMap = {}; var measureIdx = 0; console.log(data) /** * 箱体图需要两个维度 一个度量 * 生成图形,需要将data按最后一个维度将度量进行分组 */ // x轴数据 var xAxisData = []; // 箱体数据需要的数组 var seriesData = []; // 箱体数据名称的数组,主要是为了显示异常值的维度名称 var seriesDataName = []; // 根据倒数第二个维度,获取X轴数组 for (var i = 0; i < data.length; i++) { var value = data[i][dimensions[dimensions.length - 2].name]; var isAdd = true; for (var d = 0; d < xAxisData.length; d++) { if (xAxisData[d] == value) { isAdd = false; break; } } if (isAdd) { xAxisData[xAxisData.length] = value; } } // 根据倒数第二和倒数第一个维度 + 倒数第一个度量 获取度量值 组成箱体数据需要的数组 for (var d = 0; d < xAxisData.length; d++) { var xv = xAxisData[d]; var seriesGroupData = []; var seriesGroupDataName = []; for (var i = 0; i < data.length; i++) { var dimensions1 = data[i][dimensions[dimensions.length - 2].name]; if (xv == dimensions1) { seriesGroupData[seriesGroupData.length] = data[i][measures[measures.length - 1].name]; seriesGroupDataName[seriesGroupDataName.length] = { name: data[i][dimensions[dimensions.length - 1].name], value: data[i][measures[measures.length - 1].name]}; } } seriesData[seriesData.length] = seriesGroupData; seriesDataName[seriesDataName.length] = seriesGroupDataName; } // 根据箱体需要的数组调用echart生成 箱体数据 因为原生的生成方法 异常值没有维度名称,此处修改为把维度名称传进去并在异常值中显示维度名称 var seriesLoadData = echarts.dataTool.prepareBoxplotData(seriesData, null, seriesDataName); option.xAxis.data = xAxisData; // 异常值对象 此处动态添加 var outliers = { name: '异常值', type: 'scatter', itemStyle: { normal: { color: 'red' } }, symbolSize:5, data: seriesLoadData.outliers, label: { show: true, formatter: "{@[2]}", position: 'right', } } var series = []; series[0] = option.series[0]; series[0].data = seriesLoadData.boxData; series[1] = outliers; option.series = series; component.context.chart.setOption(option, true); }; ``` 调用数据处理方法 ``` case "boxplot": internalRefreshBoxplotModelData(); break; ``` 发起联动? ``` case "boxplot": component.context.chart.on("mouseover", function (param) { if (param.dataIndex < 0) { return } if (component.context.tooltipName != null && "" + component.context.tooltipName === "" + param.name) { return } component.context.tooltipName = param.name; scope.notifyDimensionValueChange(null, scope.getLastDimension(), param.name) }); break; ``` 接收联动? ``` case "boxplot": var dataIndex = -1; var option = component.config.chartConfig; for (var i = 0; i < component.config.chartConfig.series[0].data.length; i++) { if ("" + component.config.chartConfig.series[0].data[i].name === "" + event.source.value) { dataIndex = i; break } } if (dataIndex < 0) { component.context.chart.dispatchAction({ type: "hideTip" }); return } if (dataIndex > -1) { component.context.chart.dispatchAction({ type: "showTip", name: event.source.value, seriesIndex: 0 }) } break; ```