apex图表使用饼图居中_salesforce 零基础学习(二十六)自定义图表chart简单介绍(使用apex和VF实现)...

chart在报表中经常使用到,他可以使报表结果更加直观的展现给用户。salesforce支持VF和apex代码来更好的展示chart。

chart分类:常用的图表样式有饼状图,柱状图,折线图,条形图,表盘图,雷达图,及线性系列图表等。

图表根据样式不同显示的内容不同,大概包含以下部分:

1. X,Y坐标;

2. 标题;

3. 内容及所含数量(data);

4.移入上面显示的相关提示信息(tip);

5.说明(legend)。

注:这里只是总结大概的部分,显示的部分因图表样式而有相应的差距,chart是可以高度自定义的,可以通过vf标签的属性对内容进行显示或者隐藏。

这里主要对饼状图进行描述,其他图形可以参看Page Developer的描述以及相关标签的属性介绍来自行练习。我们要实现的大概效果如下图所示。

还是以Goods__c表为例,其中一个字段为GoodsBrand__c,类型为PickList,主要有四种可选择产品类型,华为,小米,魅族以及联想。我们要做的chart为展示数据表中各个品牌所占的比例。

对此饼状图大概分析:制作此饼状图需要有数据,说明(legend),提示信息(tip)。

其中,我们使用标签来封装数据,使用来显示数据。有这样两个属性,labelField展示图表上面显示的内容信息,此信息与legend的标题信息相同且legend不可以自己定义,dataField用来展示比例,即这个值占据总量的多少。默认情况下tip信息由key,value队组成,显示的key为labelField值,显示的value为dataField的值。

制作步骤:

1.Apex用于制作数据

1 public classPieChartController {2 public Map goodsBrandNumberMap = new Map();3 public SetgoodsBrand{get;set;}4 publicPieChartController() {5 Map goodsBrandValuesMap = PickListValuesUtil.getPicklistValues('Goods__c','GoodsBrand__c');6 goodsBrand =goodsBrandValuesMap.keySet();7 List goodsList = [select Id,GoodsBrand__c from Goods__c where GoodsBrand__c <> null];8 for(Goods__c currentGoods : goodsList) {9 if(goodsBrand.contains(currentGoods.GoodsBrand__c)) {10 if(goodsBrandNumberMap.keySet().contains(currentGoods.GoodsBrand__c)) {11 goodsBrandNumberMap.put(currentGoods.GoodsBrand__c,goodsBrandNumberMap.get(currentGoods.GoodsBrand__c) + 1);12 } else{13 goodsBrandNumberMap.put(currentGoods.GoodsBrand__c,1);14 }15 }16 }17 }18

19 public ListgetGoodsBrandPieData() {20 List pieWdegeData = new List();21 for(String goodsBrandName : goodsBrandNumberMap.keySet()) {22 Data data = new Data(goodsBrandName + '\n' +goodsBrandNumberMap.get(goodsBrandName),goodsBrandNumberMap.get(goodsBrandName),goodsBrandName);23 pieWdegeData.add(data);24 }25

26 returnpieWdegeData;27 }28

29 //Wrapper class

30 public classData {31 //label

32 publicString chartLabel { get; set; }33 //the value of chart label

34 publicDecimal valueOfChartLabel { get; set; }35 //tip customize

36 publicString tipOfChartLabel {get;set;}37 //tip customize

38 publicDecimal tipOfLabelValue{get;set;}39

40 publicData(String chartLabel,Decimal valueOfChartLabel) {41 this.chartLabel =chartLabel;42 this.valueOfChartLabel =valueOfChartLabel;43 }44

45 //if the label of tip need to customize,choose this

46 publicData(String chartLabel,Decimal valueOfChartLabel,String tipOfChartLabel) {47 this(chartLabel,valueOfChartLabel);48 this.tipOfChartLabel =tipOfChartLabel;49 }50

51 //if both the label of tip and the value of tip need to customize ,choose this

52 publicData(String chartLabel,Decimal valueOfChartLabel,String tipOfChartLabel,Decimal tipOfLabelValue) {53 this(chartLabel,valueOfChartLabel,tipOfChartLabel);54 this.tipOfLabelValue =tipOfLabelValue;55 }56 }57 }

由于展现的legend需要自己定义,所以我们在构造Data时要考虑图表的label以及对应的value和label提示信息以及相对应的value。Data相当于一个Property,前台通过数据绑定后在使用便相当于迭代器,将列表中每个Data元素取出。

2.Page页面显示数据

1

2

3

4

5

6

7

8

9

10

11

12

通过上述代码便可以实现上图所示的chart。

我们把Page页面做些调整,页面代码如下所示:

1

2

3

4

5

6

7

8

9

显示效果如下图所示

可以看出legend和label显示样式相同,如果需要进行相关的定制化,详见PDF中所使用标签的属性。

总结:自定义图表简单的说便是先提供数据进行绑定,然后通过需要的图表样式进行解析即可,如果需要定制一些特殊需要,详见使用的标签属性,legend标签无value等自定义的值,其值与label绑定,所以如果需要legend显示特殊的值,需要在设置Data时考虑相关的信息,在label绑定时,绑定legend需要显示的值,在对label进行自定义操作。如果篇中有错误地方欢迎指出,如果有问题欢迎留言。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值