Highcharts双饼图使用实例

这次实践了Highcharts的双饼图,确实比普通饼图复杂多了,关键相关数据 多不能继续用简单基本数据类型Map,list了,单独建了个VO存放要用到的数据,不多说,贴代码!

JS:

/**查看机器占比(按产品线) 2015/8*/
function loadMachineRate(){
	var chart;
	$(document).ready(function(){
		
		chart = new Highcharts.Chart({
			//常规图表选项设置
            chart: {
                renderTo: 'machineRate',        //在哪个区域呈现,对应HTML中的一个元素ID
                plotBackgroundColor: null,    //绘图区的背景颜色
                plotBorderWidth: null,        //绘图区边框宽度
                plotShadow: false            //绘图区是否显示阴影            
            },
            //图表的主标题
            title: {
                text: '按产品线统计机器占比'
            },
            yAxis: {
                title: {
                    text: 'Total percent market share'
                }
      
            },
            //当鼠标经过时的提示设置
            tooltip: {
                formatter: function() {
                   return  this.point.name +': '+ '<b>'+Highcharts.numberFormat(this.percentage, 2) +'% </b><br/>总量: '+
                   		'<b>'+ Highcharts.numberFormat(this.y, 0, ',') +' 台</b>';
                }
             },
          //每种图表类型属性设置
            plotOptions: {
                pie: {
                    shadow: false,
                    center: ['50%', '50%']
                }
            },
            //图表要展现的数据
            series: [{
                name: 'productLine',
                size: '60%',
                type:'pie',
                dataLabels: {
                    color: 'white',
                    distance: -30
                }
            }, {
                name: 'machineStatus',
                type:'pie',
                size: '80%',
                innerSize: '60%',
                dataLabels: {
                  
                }
            }]
            
		});
	});
	$.ajax({
		type : "GET",
		/*url : "machine/getStaticMachineRateByProductLine",*/
		url : "machine/getMachineRateVOByProductline",
		success : function(data){
	        var allMachineCount = 0;//所有机器总数
	        for(i in data){
	            allMachineCount += data[i].allMachine;
	        }
	        console.log("所有机器"+allMachineCount);
	        var colors = Highcharts.getOptions().colors,
	        categories = ['网页', '下载', '点播', '视频'],
	        data = [{
	            y: data[0].allMachine,
	            color: colors[0],
	            drilldown: {
	                name: '机器状态',
	                categories: ['网页投产未使用', '网页投产使用', '网页投产无IP', '网页待下架','网页上架完毕','网页故障','网页下架中'],
	                data: [data[0].freeMachineCount,data[0].workMachineCount,data[0].noIp,data[0].waitShelfCount,data[0].onShelfCount,data[0].bugCount,data[0].offShelfCount],
	            }
	        }, {
	            y: data[1].allMachine,
	            color: colors[1],
	            drilldown: {
	                name: '机器状态',
	                categories: ['下载投产未使用', '下载投产使用', '下载投产无IP', '下载待下架','下载上架完毕','下载故障','下载下架中'],
	                data: [data[1].freeMachineCount,data[1].workMachineCount,data[1].noIp,data[1].waitShelfCount,data[1].onShelfCount,data[1].bugCount,data[1].offShelfCount],
	            }
	        }, {
	            y: data[2].allMachine,
	            color: colors[2],
	            drilldown: {
	                name: '机器状态',
	                categories: ['点播投产未使用', '点播投产使用', '点播投产无IP', '点播待下架','点播上架完毕','点播故障','点播下架中'],
	                data: [data[2].freeMachineCount,data[2].workMachineCount,data[2].noIp,data[2].waitShelfCount,data[2].onShelfCount,data[2].bugCount,data[2].offShelfCount],
	            }
	        }, {
	            y: data[3].allMachine,
	            color: colors[3],
	            drilldown: {
	                name: '机器状态',
	                categories: ['视频投产未使用', '视频投产使用', '视频投产无IP', '视频待下架','视频上架完毕','视频故障','视频下架中'],
	                data: [data[3].freeMachineCount,data[3].workMachineCount,data[3].noIp,data[3].waitShelfCount,data[3].onShelfCount, data[3].bugCount,data[3].offShelfCount],
	            }
	        }],
	        productlineData = [],
	        statusData = [],
	        i,
	        j,
	        dataLen = data.length,
	        drillDataLen,
	        brightness;
	        
	        // Build the data arrays
	        for (i = 0; i < dataLen; i += 1) {
	            // add productline data
	            productlineData.push({
	                name: categories[i],
	                y: data[i].y,
	                color: data[i].color
	            });
	            // add status data
	            drillDataLen = data[i].drilldown.data.length;
	            for (j = 0; j < drillDataLen; j += 1) {
	                brightness = 0.22 - (j / drillDataLen)/4;
	                statusData.push({
	                    name: data[i].drilldown.categories[j],
	                    y: data[i].drilldown.data[j],
	                    color: Highcharts.Color(data[i].color).brighten(brightness).get()
	                });
	            }
	        }
	        console.log(productlineData);
	        console.log(statusData);
	        chart.series[0].setData(productlineData);  
	        chart.series[1].setData(statusData);  
		},
		error : function(e){
			/*alert(e);*/
		}
	});
}
Controller:

/**
 * 获取产品线下不同机器状态的机器数量
 */
@RequestMapping("/getMachineRateVOByProductline")
@ResponseBody
public List<MachineRateVO> getMachineRateVOByProductline(){
	List<MachineRateVO> machineRateVOs = platformMachineService.getMachineRateVOByProductline();
	return machineRateVOs;
}
Service:

/**
 * 获取产品线下不同机器状态的机器数量
 */
@Override
public List<MachineRateVO> getMachineRateVOByProductline() {
	List<MachineRateVO> machineRateVOs = new ArrayList<MachineRateVO>();
	//根据产品线统计机器占比(饼图)
	List<Map<String, Integer>> productlineMaps = this.platformMachineMapper.getStaticMachineRateByProductLine();
	
	//循环每一条产品线 
	for (Map<String, Integer> productlineMap : productlineMaps) {
		Iterator it = productlineMap.entrySet().iterator();
		while(it.hasNext()){
			Map.Entry entry =  (Map.Entry) it.next(); 
			Object key = entry.getKey(); 
			Object val = entry.getValue(); 
			if (key.toString().equals("businessType")) {
				List<com.dnion.platform.dao.mybatis.entity.PlatformMachine> platformMachines = this.platformMachineMapper.getPlatformMachinesByProductline(val.toString());
				MachineRateVO machineRateVO = wrapMachineRateVO(val.toString(),platformMachines);
				machineRateVOs.add(machineRateVO);
			}
		}
	}
	return machineRateVOs;
}
Method:

/**
 * 封装platformMachine为MachineRateVO
 */
@SuppressWarnings("null")
private MachineRateVO wrapMachineRateVO(String businessType,List<com.dnion.platform.dao.mybatis.entity.PlatformMachine> platformMachines) {
	int waitShelfCount = 0;//待下架
	int offShelfCount = 0;//下架中
	int onShelfCount = 0;//上架完毕
	int bugCount = 0;//故障
	int operationCount = 0;//投产
	int freeMachineCount = 0;//未使用
	int workMachineCount = 0;//使用
	int noIp = 0;//无ip
	
	MachineRateVO machineRateVO = new MachineRateVO();
	for(com.dnion.platform.dao.mybatis.entity.PlatformMachine platformMachine : platformMachines){
		if (platformMachine.getMcRunStatus().equals("OPERATION")) {//投产 
			operationCount += 1;
			List<PlatformMachineIp> platformMachineIps = platformMachine.getPlatformMachineIps();
			if (platformMachineIps.size() != 0) { //有ip
				Set<Integer> mcIpStatus = new HashSet<Integer>();
				for(PlatformMachineIp platformMachineIp : platformMachineIps){
					mcIpStatus.add((int)platformMachineIp.getMcIpStatus());
				}
				if (mcIpStatus.contains(5)) {//有空闲ip 则为空闲机器
					freeMachineCount += 1;
				}else {
					workMachineCount += 1;
				}
				
			}else {//无ip
				noIp += 1;
			}
		}else if (platformMachine.getMcRunStatus().equals("OFFSHELF")) {
			offShelfCount += 1;
		}else if (platformMachine.getMcRunStatus().equals("ONSHELF")) {
			onShelfCount += 1;
		}else if (platformMachine.getMcRunStatus().equals("BUG")) {
			bugCount += 1;
		}else if (platformMachine.getMcRunStatus().equals("WAITSHELF")) {
			waitShelfCount += 1;
		}
	}
	machineRateVO.setWaitShelfCount(waitShelfCount);
	machineRateVO.setOffShelfCount(offShelfCount);
	machineRateVO.setOnShelfCount(onShelfCount);
	machineRateVO.setBugCount(bugCount);
	machineRateVO.setOperationCount(operationCount);
	machineRateVO.setFreeMachineCount(freeMachineCount);
	machineRateVO.setWorkMachineCount(workMachineCount);
	machineRateVO.setBusinessType(businessType);
	machineRateVO.setNoIp(noIp);
	machineRateVO.setAllMachine(platformMachines.size());
	return machineRateVO;
}
VO:

/**
 * 产品线机器占比页面视图
 */
public class MachineRateVO {
	
	/** 主要元素 下面的数量都是对应于该产品线 */
	private String businessType;//产品线类型 主要元素 下面的数量都是对应于该产品线
	private Integer allMachine;//产品线下的所有机器
	private Integer waitShelfCount;//待下架机器数量
	private Integer offShelfCount;//下架中机器数量
	private Integer onShelfCount;//上架完毕机器数量
	private Integer bugCount;//故障机器数量
	private Integer operationCount;//投产机器数量
	
	//freeMachineCount+workMachineCount=operationCount
	private Integer freeMachineCount;//未使用机器数量
	private Integer workMachineCount;//使用的机器数量
	private Integer noIp;//投产但无ip
	
	public MachineRateVO(){
	}
	
	getter and setter...
}

基本上就是以上代码了,下面是效果图:




  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以按照以下步骤在Vue中使用Highcharts来绘制3D饼图: 1. 在你的项目的main.js文件中引入Highchartshighcharts-3d插件: ```javascript import Highcharts from 'highcharts' import Highcharts3d from 'highcharts/highcharts-3d' Highcharts3d(Highcharts) ``` 2. 在你的组件中创建一个容器来显示图表,并引入Highcharts库: ```vue <template> <div class="container"> <div :id="id" :option="option" class="chart-container"></div> </div> </template> <script> import Highcharts from 'highcharts' export default { props: { id: { type: String }, // 用于区分多个图表的唯一标识符 option: { type: Object } // 图表的配置选项 }, data() { return { chart: null } }, mounted() { // 在组件加载完成后初始化图表 this.setOption() }, methods: { setOption() { // 销毁之前的图表实例 if (this.chart) { this.chart.destroy() } // 创建一个新的图表实例并渲染到指定容器中 this.chart = Highcharts.chart(this.id, this.option) // 重新调整图表大小,以适应容器 this.chart.reflow() } } } </script> <style scoped> .container { width: 100%; height: 100%; background: #043b8c; .chart-container { width: 100%; height: 100%; } /* 去除水印 */ .highcharts-credits { display: none; } } </style> ``` 3. 在你的父组件中,使用刚才创建的组件,并传入相应的id和配置选项: ```vue <template> <div> <pie-chart id="chart1" :option="chartOptions"></pie-chart> </div> </template> <script> import PieChart from './PieChart.vue' export default { components: { PieChart }, data() { return { chartOptions: { chart: { type: 'pie', options3d: { enabled: true, alpha: 45, beta: 0 } }, title: { text: '3D饼图' }, series: [{ name: 'Brands', data: [ ['Chrome', 61.41], ['Internet Explorer', 11.84], ['Firefox', 10.85], ['Edge', 4.67], ['Safari', 4.18], ['Other', 7.05] ] }] } } } } </script> ``` 这样,你就可以在Vue中使用Highcharts来绘制3D饼图了。记得根据你的需求修改配置选项和数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值