Echart的使用与将图标形成指令

完成后如下图所示

image

1. Echart的使用

官网地址

官网教程引入,根据教程可以很简单的完成实例的引入,我主要写关于百分率样式的配置

代码入下所示

var option = {
                    title: {
                        text: {}
                    },
                    color: ['#62CB31'],
                    tooltip: {
                        trigger: 'item',
                        formatter: '{b}:\n{c}%'
                    },
                    xAxis: [{
                        type: '',
                        data: [],
                        axisTick: {
                            alignWithLabel: true
                        }
                    }],
                    yAxis: {
                        type: 'value',
                        min: '',
                        max: '',
                        axisLabel: {
                            show: true,
                            interval: 'auto',
                            formatter: '{value} %'
                        }
                    },
                    series: [{
                        name: '',
                        type: 'bar',
                        data: [],
                        itemStyle: {
                            normal: {
                                color: function(params) {
                                   var colorList = CommonService.getChartBarColor();
                                    return colorList[params.dataIndex % colorList.length];
                                },
                                label: {
                                    show: true,
                                    position: 'top',
                                    formatter: '{b}\n{c}%'
                                }
                            }
                        },
                        axisLabel: {
                            show: true,
                            interval: 'auto',
                            formatter: '{value} %'
                        },
                        barWidth: 70
                    }]
                };

1.1 y轴的百分比显示

axisLabel: {
               show: true,
               interval: 'auto',
               formatter: '{value} %'
}

1.2 柱形上的显示与多颜色的加入

 itemStyle: {
                            normal: {
                                color: function(params) {
                                   var colorList = CommonService.getChartBarColor();
                                    return colorList[params.dataIndex % colorList.length];
                                },
                                label: {
                                    show: true,
                                    position: 'top',
                                    formatter: '{b}\n{c}%'
                                }
                            }
                        }

2. 指令的使用

<yunzhi-chart-bar data-chart-name = "chartName" data-chart-xdata="chartXData" data-chart-ymax="chartYMax" data-chart-ymin="chartYMin" data-chart-dar="chartDarData"></yunzhi-chart-bar>

外部数据:图表名称,图标x轴,图表y轴,图表bar内容(数据)

在写指令是遇到问题是$watch的多监听

解决如下

scope.$watch('[chartXdata, chartDar, chartName, chartYmin, chartYmax]', function(newValue) {
                    if (scope.chartDar) {  // 防止第一次数据为空时加载图表
                        $timeout(function() {
                            self.setChart();
                        }, 1000);
                    }
                }, true);

把要监听的对象放入数组中,进行多对监听

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容