javascript - angular-echarts扩展指令datapoint值与series属性关系

查看:153
本文介绍了javascript - angular-echarts扩展指令datapoint值与series属性关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

echarts以前用过,用指令封装进angular的主要工作约为 指定config与data,watch并更新data;
echarts首页推荐三个angular的扩展指令我跳了这个坑,对于作者自定义的datapoint实在是不理解,所以求助大家,下附作者datapoint转series代码
源码地址https://github.com/wangshijun...

exampleData = {
        name: 'page.load',
        type: 'bar',
        datapoints: [
            { x: 2001, y: 1012 },
            { x: 2002, y: 1023 },
            { x: 2003, y: 1045 },
            { x: 2004, y: 1062 },
            { x: 2005, y: 1032 },
            { x: 2006, y: 1040 },
            { x: 2007, y: 1023 },
            { x: 2008, y: 1090 },
            { x: 2009, y: 1012 },
            { x: 2010, y: 1012 },
        ]
    };

function getSeries(data, config, type) {
        var series = [];
        angular.forEach(data, function (serie) {
            // datapoints for line, area, bar chart
            var datapoints = [];
            angular.forEach(serie.datapoints, function (datapoint) {
                datapoints.push(datapoint.y);
            });

            var conf = {
                type: type || 'line',
                name: serie.name,
                data: datapoints,
            };

            // area chart is actually line chart with special itemStyle
            if (type === 'area') {
                conf.type = 'line';
                conf.itemStyle = {
                    normal: { areaStyle: { type: 'default'}}
                };
            }

            // gauge chart need many special config
            if (type === 'gauge') {
                conf = angular.extend(conf, {
                    splitNumber: 10,       // 分割段数,默认为5
                    axisLine: {            // 坐标轴线
                        lineStyle: {       // 属性lineStyle控制线条样式
                            color: [[0.2, '#228b22'], [0.8, '#48b'], [1, '#ff4500']],
                            width: 8
                        }
                    },
                    axisTick: {            // 坐标轴小标记
                        splitNumber: 10,   // 每份split细分多少段
                        length :12,        // 属性length控制线长
                        lineStyle: {       // 属性lineStyle控制线条样式
                            color: 'auto'
                        }
                    },
                    axisLabel: {           // 坐标轴文本标签,详见axis.axisLabel
                        textStyle: {       // 其余属性默认使用全局文本样式,详见TEXTSTYLE
                            color: 'auto'
                        }
                    },
                    splitLine: {           // 分隔线
                        show: true,        // 默认显示,属性show控制显示与否
                        length :30,         // 属性length控制线长
                        lineStyle: {       // 属性lineStyle(详见lineStyle)控制线条样式
                            color: 'auto'
                        }
                    },
                    pointer: {
                        width: 5
                    },
                    title: {
                        show: true,
                        offsetCenter: [0, '-40%'],       // x, y,单位px
                        textStyle: {       // 其余属性默认使用全局文本样式,详见TEXTSTYLE
                            fontWeight: 'bolder'
                        }
                    },
                    detail: {
                        formatter: '{value}%',
                        textStyle: {       // 其余属性默认使用全局文本样式,详见TEXTSTYLE
                            color: 'auto',
                            fontWeight: 'bolder'
                        }
                    },
                }, config.gauge || {});
            }

            // datapoints for pie chart and gauges are different
            if (!isAxisChart(type)) {
                conf.data = [];
                angular.forEach(serie.datapoints, function (datapoint) {
                    conf.data.push({value: datapoint.y, name: datapoint.x });
                });
            }

            if (isPieChart(type)) {
                // donut charts are actually pie charts
                conf.type = 'pie';

                // pie chart need special radius, center config
                conf.center = config.center || ['40%', '50%'];
                conf.radius = config.radius || '60%';

                // donut chart require special itemStyle
                if (type === 'donut') {
                    conf.radius = config.radius || ['50%', '70%'];
                    conf = angular.extend(conf, {
                        itemStyle: {
                            normal: {
                                label: {
                                    show: false
                                },
                                labelLine: {
                                    show: false
                                }
                            },
                            emphasis: {
                                label: {
                                    show: true,
                                    position: 'center',
                                    textStyle: {
                                        fontSize: '50',
                                        fontWeight: 'bold'
                                    }
                                }
                            }
                        }
                    }, config.donut || {});
                } else if (type === 'pie') {
                    conf = angular.extend(conf, {
                        itemStyle: {
                            normal : {
                                label : {
                                    position : 'inner',
                                    formatter : function (item) {
                                        return (+item.percent).toFixed() + '%';
                                    }
                                },
                                labelLine : {
                                    show : false
                                }
                            },
                            emphasis : {
                                label : {
                                show : true,
                                    formatter : '{b}\n{d}%'
                                }
                            }
                        }
                    }, config.pie || {});
                }
            }

            if (isMapChart(type)) {
                conf.type = 'map';
                conf = angular.extend(conf, {}, config.map || {});
            }

            // if stack set to true
            if (config.stack) {
                conf.stack = 'total';
            }

            if (type === 'radar') {
                conf.data = serie.data;
            }

            series.push(conf);
        });

        return series;
    }

echarts本身的data为何要使用datapoint的方式传入然后再转化为data格式,请解惑。

解决方案

封装的代码?type不同data不同 if (!isAxisChart(type))conf.data.push({value: datapoint.y, name: datapoint.x });

这篇关于javascript - angular-echarts扩展指令datapoint值与series属性关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆