/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer
 * Software" and other relevant administrative regulations and international treaties. Without
 * the written permission of the Company, no person may use (including but not limited to the
 * illegal copy, distribute, display, image, upload, and download) and disclose the above
 * technical documents to any third party. Otherwise, any infringer shall afford the legal
 * liability to the company.
 */
/**
 * Created by huangdeyun on 2015/9/15 11:29.
 */
define(['echarts', 'echarts/chart/pie'], function (ECharts, Pie) {
    return {
        init: function (myCharts_, mainNode, title, subTitle, names, values) {
            if (names.length === 0 || values.length === 0) {
                return myCharts_;
            }
            var myCharts = myCharts_;
            if (!myCharts) {
                myCharts = ECharts.init(mainNode);
            }
            myCharts.clear();

            /* //测试数据
             var title = "访问来源";
             var subTitle = "";
             var names = ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎'];
             var values = [335, 310, 234, 135, 1548];*/
            var datas = [];
            var count = 0;
            for (var i = 0; i < names.length; i++) {
                datas.push({value: values[i], name: names[i]});
                count += values[i];
            }
            var legends = [];
            for (i = 0; i < values.length; i++) {
                legends.push((values[i] * 100 / count).toFixed(2) + "%");
            }

            var option = {
                title: {
                    text: title === null ? "" : title,
                    subtext: subTitle === null ? "" : subTitle,
                    x: 'center',
                    y: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'horizontal',//'vertical',
                    x: 'center',
                    y: '80%',
                    data: names,
                    selectedMode: false,//图例开关，默认开启
                    formatter: function (name) {
                        return name + ":" + legends[names.indexOf(name)];
                    }
                },
                toolbox: {
                    show: false
                },
                calculable: false,
                series: [
                    {
                        name: title,
                        type: 'pie',
                        radius: ['45%', '70%'],
                        center: ['50%', '43%'],
                        itemStyle: {
                            normal: {
                                label: {
                                    show: false
                                },
                                labelLine: {
                                    show: false
                                }
                            },
                            emphasis: {
                                label: {
                                    show: true,
                                    position: 'center',
                                    textStyle: {
                                        fontSize: '30',
                                        fontWeight: 'bold'
                                    }
                                }
                            }
                        },
                        data: datas
                    }
                ]
            };
            myCharts.setOption(option);
            return myCharts;
        }
    };

});