説明なし

AAOptions.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // AAOptions.m
  3. // AAChartKit
  4. //
  5. // Created by An An on 17/1/4.
  6. // Copyright © 2017年 An An. All rights reserved.
  7. //*************** ...... SOURCE CODE ...... ***************
  8. //***...................................................***
  9. //*** https://github.com/AAChartModel/AAChartKit ***
  10. //*** https://github.com/AAChartModel/AAChartKit-Swift ***
  11. //***...................................................***
  12. //*************** ...... SOURCE CODE ...... ***************
  13. /*
  14. * -------------------------------------------------------------------------------
  15. *
  16. * 🌕 🌖 🌗 🌘 ❀❀❀ WARM TIPS!!! ❀❀❀ 🌑 🌒 🌓 🌔
  17. *
  18. * Please contact me on GitHub,if there are any problems encountered in use.
  19. * GitHub Issues : https://github.com/AAChartModel/AAChartKit/issues
  20. * -------------------------------------------------------------------------------
  21. * And if you want to contribute for this project, please contact me as well
  22. * GitHub : https://github.com/AAChartModel
  23. * StackOverflow : https://stackoverflow.com/users/7842508/codeforu
  24. * JianShu : http://www.jianshu.com/u/f1e6753d4254
  25. * SegmentFault : https://segmentfault.com/u/huanghunbieguan
  26. *
  27. * -------------------------------------------------------------------------------
  28. */
  29. #import "AAOptions.h"
  30. @implementation AAOptions
  31. AAPropSetFuncImplementation(AAOptions, AAChart *, chart);
  32. AAPropSetFuncImplementation(AAOptions, AATitle *, title);
  33. AAPropSetFuncImplementation(AAOptions, AASubtitle *, subtitle);
  34. AAPropSetFuncImplementation(AAOptions, AAXAxis *, xAxis);
  35. AAPropSetFuncImplementation(AAOptions, AAYAxis *, yAxis);
  36. AAPropSetFuncImplementation(AAOptions, AATooltip *, tooltip);
  37. AAPropSetFuncImplementation(AAOptions, AAPlotOptions *, plotOptions);
  38. AAPropSetFuncImplementation(AAOptions, NSArray *, series);
  39. AAPropSetFuncImplementation(AAOptions, AALegend *, legend);
  40. AAPropSetFuncImplementation(AAOptions, NSArray *, colors);
  41. AAPropSetFuncImplementation(AAOptions, BOOL, gradientColorEnabled);
  42. AAPropSetFuncImplementation(AAOptions, NSString *, zoomResetButtonText); //String to display in 'zoom reset button"
  43. @end
  44. #define AAFontSizeFormat(fontSize) [NSString stringWithFormat:@"%@%@", fontSize, @"px"]
  45. @implementation AAOptionsConstructor
  46. + (AAOptions *)configureChartOptionsWithAAChartModel:(AAChartModel *)aaChartModel {
  47. AAChart *aaChart = AAObject(AAChart)
  48. .typeSet(aaChartModel.chartType)//绘图类型
  49. .invertedSet(aaChartModel.inverted)//设置是否反转坐标轴,使X轴垂直,Y轴水平。 如果值为 true,则 x 轴默认是 倒置 的。 如果图表中出现条形图系列,则会自动反转
  50. .backgroundColorSet(aaChartModel.backgroundColor)//设置图表的背景色(包含透明度的设置)
  51. .pinchTypeSet(aaChartModel.zoomType)//设置手势缩放方向
  52. .panningSet(true)//设置手势缩放后是否可平移
  53. .polarSet(aaChartModel.polar);
  54. AATitle *aaTitle = AAObject(AATitle)
  55. .textSet(aaChartModel.title)//标题文本内容
  56. .styleSet(AAObject(AAStyle)
  57. .colorSet(aaChartModel.titleFontColor)//Title font color
  58. .fontSizeSet(AAFontSizeFormat(aaChartModel.titleFontSize))//Title font size
  59. .fontWeightSet(aaChartModel.titleFontWeight)//Title font weight
  60. );
  61. AASubtitle *aaSubtitle = AAObject(AASubtitle)
  62. .textSet(aaChartModel.subtitle)//副标题内容
  63. .alignSet(aaChartModel.subtitleAlign)//图表副标题文本水平对齐方式。可选的值有 “left”,”center“和“right”。 默认是:center.
  64. .styleSet(AAObject(AAStyle)
  65. .colorSet(aaChartModel.subtitleFontColor)//Subtitle font color
  66. .fontSizeSet(AAFontSizeFormat(aaChartModel.subtitleFontSize))//Subtitle font size
  67. .fontWeightSet(aaChartModel.subtitleFontWeight)//Subtitle font weight
  68. );
  69. AATooltip *aaTooltip = AAObject(AATooltip)
  70. .enabledSet(aaChartModel.tooltipEnabled)//启用浮动提示框
  71. .sharedSet(aaChartModel.tooltipShared)//多组数据共享一个浮动提示框
  72. .crosshairsSet(true)//启用准星线
  73. //.pointFormatSet(aaChartModel.tooltipValueString)//Tooltip value point format string
  74. .valueSuffixSet(aaChartModel.tooltipValueSuffix);//浮动提示框的单位名称后缀
  75. AAPlotOptions *aaPlotOptions = AAObject(AAPlotOptions)
  76. .seriesSet(AAObject(AASeries)
  77. //.colorByPointSet(false)//决定了图表是否给每个数据列或每个点分配一个颜色,默认值是 false, 即默认是给每个数据类分配颜色,
  78. .stackingSet(aaChartModel.stacking)//设置是否百分比堆叠显示图形
  79. );
  80. if (aaChartModel.animationType != 0) {
  81. aaPlotOptions.series.animation = (AAObject(AAAnimation)
  82. .easingSet(aaChartModel.animationType)
  83. .durationSet(aaChartModel.animationDuration)
  84. );
  85. }
  86. [self configureTheStyleOfConnectNodeWithChartModel:aaChartModel plotOptions:aaPlotOptions];
  87. aaPlotOptions = [self configureTheAAPlotOptionsWithPlotOptions:aaPlotOptions chartModel:aaChartModel];
  88. // aaPlotOptions.series.events = @{@"click":@"hahaha"};
  89. AALegend *aaLegend = AAObject(AALegend)
  90. .enabledSet(aaChartModel.legendEnabled);//是否显示 legend
  91. AAOptions *aaOptions = AAObject(AAOptions)
  92. .chartSet(aaChart)
  93. .titleSet(aaTitle)
  94. .subtitleSet(aaSubtitle)
  95. .tooltipSet(aaTooltip)
  96. .plotOptionsSet(aaPlotOptions)
  97. .legendSet(aaLegend)
  98. .seriesSet(aaChartModel.series)
  99. .colorsSet(aaChartModel.colorsTheme)//设置颜色主题
  100. .gradientColorEnabledSet(aaChartModel.gradientColorEnabled)//设置主题颜色是否为渐变色
  101. .zoomResetButtonTextSet(aaChartModel.zoomResetButtonText);//设置重置缩放按钮的默认标题
  102. if ( aaChartModel.chartType != AAChartTypePie
  103. && aaChartModel.chartType != AAChartTypePyramid
  104. && aaChartModel.chartType != AAChartTypeFunnel) {
  105. [self configureAxisContentAndStyleWithAAOptions:aaOptions AAChartModel:aaChartModel];
  106. }
  107. return aaOptions;
  108. }
  109. + (void)configureAxisContentAndStyleWithAAOptions:(AAOptions *)aaOptions AAChartModel:(AAChartModel *)aaChartModel {
  110. AAXAxis *aaXAxis = AAObject(AAXAxis)
  111. .labelsSet(AAObject(AALabels)
  112. .enabledSet(aaChartModel.xAxisLabelsEnabled)//设置 x 轴是否显示文字
  113. .styleSet(AAObject(AAStyle)
  114. .colorSet(aaChartModel.xAxisLabelsFontColor)//xAxis Label font color
  115. .fontSizeSet(AAFontSizeFormat(aaChartModel.xAxisLabelsFontSize))//xAxis Label font size
  116. .fontWeightSet(aaChartModel.xAxisLabelsFontWeight)//xAxis Label font weight
  117. )
  118. )
  119. .reversedSet(aaChartModel.xAxisReversed)
  120. .gridLineWidthSet(aaChartModel.xAxisGridLineWidth)//x轴网格线宽度
  121. .categoriesSet(aaChartModel.categories)
  122. .visibleSet(aaChartModel.xAxisVisible)//x轴是否可见
  123. .tickIntervalSet(aaChartModel.xAxisTickInterval);//x轴坐标点间隔数
  124. if ([aaChartModel.xAxisCrosshairWidth floatValue]>0) {
  125. aaXAxis.crosshairSet(AAObject(AACrosshair)
  126. .widthSet(aaChartModel.xAxisCrosshairWidth)
  127. .colorSet(aaChartModel.xAxisCrosshairColor)
  128. .dashStyleSet(aaChartModel.xAxisCrosshairDashStyleType)
  129. );
  130. }
  131. AAYAxis *aaYAxis = AAObject(AAYAxis)
  132. .labelsSet(AAObject(AALabels)
  133. .enabledSet(aaChartModel.yAxisLabelsEnabled)//设置 y 轴是否显示数字
  134. .styleSet(AAObject(AAStyle)
  135. .colorSet(aaChartModel.yAxisLabelsFontColor)//yAxis Label font color
  136. .fontSizeSet(AAFontSizeFormat(aaChartModel.yAxisLabelsFontSize))//yAxis Label font size
  137. .fontWeightSet(aaChartModel.yAxisLabelsFontWeight)//yAxis Label font weight
  138. )
  139. .formatSet(@"{value:.,0f}")//让y轴的值完整显示 而不是100000显示为100k
  140. )
  141. .minSet(aaChartModel.yAxisMin)//设置 y 轴最小值,最小值等于零就不能显示负值了
  142. .maxSet(aaChartModel.yAxisMax)//y轴最大值
  143. .tickPositionsSet(aaChartModel.yAxisTickPositions)//自定义Y轴坐标
  144. .allowDecimalsSet(aaChartModel.yAxisAllowDecimals)//是否允许显示小数
  145. .plotLinesSet(aaChartModel.yAxisPlotLines) //标示线设置
  146. .reversedSet(aaChartModel.yAxisReversed)
  147. .gridLineWidthSet(aaChartModel.yAxisGridLineWidth)//y轴网格线宽度
  148. .titleSet(AAObject(AATitle)
  149. .textSet(aaChartModel.yAxisTitle))//y 轴标题
  150. .lineWidthSet(aaChartModel.yAxisLineWidth)//设置 y轴轴线的宽度,为0即是隐藏 y轴轴线
  151. .visibleSet(aaChartModel.yAxisVisible)
  152. .tickIntervalSet(aaChartModel.yAxisTickInterval);
  153. if ([aaChartModel.yAxisCrosshairWidth floatValue]>0) {
  154. aaYAxis.crosshairSet(AAObject(AACrosshair)
  155. .widthSet(aaChartModel.yAxisCrosshairWidth)
  156. .colorSet(aaChartModel.yAxisCrosshairColor)
  157. .dashStyleSet(aaChartModel.yAxisCrosshairDashStyleType)
  158. );
  159. }
  160. aaOptions.xAxis = aaXAxis;
  161. aaOptions.yAxis = aaYAxis;
  162. }
  163. + (void)configureTheStyleOfConnectNodeWithChartModel:(AAChartModel *)aaChartModel plotOptions:(AAPlotOptions *)aaPlotOptions {
  164. //数据点标记相关配置,只有线性图(折线图、曲线图、折线区域填充图、曲线区域填充图)才有数据点标记
  165. if ( aaChartModel.chartType == AAChartTypeArea
  166. || aaChartModel.chartType == AAChartTypeAreaspline
  167. || aaChartModel.chartType == AAChartTypeLine
  168. || aaChartModel.chartType == AAChartTypeSpline) {
  169. AAMarker *aaMarker = AAObject(AAMarker)
  170. .radiusSet(aaChartModel.markerRadius)//曲线连接点半径,默认是4
  171. .symbolSet(aaChartModel.symbol);//曲线点类型:"circle", "square", "diamond", "triangle","triangle-down",默认是"circle"
  172. if (aaChartModel.symbolStyle == AAChartSymbolStyleTypeInnerBlank) {
  173. aaMarker.fillColorSet(@"#ffffff")//点的填充色(用来设置折线连接点的填充色)
  174. .lineWidthSet(@2)//外沿线的宽度(用来设置折线连接点的轮廓描边的宽度)
  175. .lineColorSet(@"");//外沿线的颜色(用来设置折线连接点的轮廓描边颜色,当值为空字符串时,默认取数据点或数据列的颜色)
  176. } else if (aaChartModel.symbolStyle == AAChartSymbolStyleTypeBorderBlank) {
  177. aaMarker.lineWidthSet(@2)
  178. .lineColorSet(aaChartModel.backgroundColor);
  179. }
  180. AASeries *aaSeries = aaPlotOptions.series;
  181. aaSeries.connectNulls = aaChartModel.connectNulls;
  182. aaSeries.marker = aaMarker;
  183. }
  184. }
  185. + (AAPlotOptions *)configureTheAAPlotOptionsWithPlotOptions:(AAPlotOptions *)aaPlotOptions chartModel:(AAChartModel *)aaChartModel {
  186. AAChartType chartType = aaChartModel.chartType;
  187. AADataLabels *aaDataLabels;
  188. if (aaChartModel.dataLabelEnabled == true) {
  189. aaDataLabels = (AAObject(AADataLabels)
  190. .enabledSet(aaChartModel.dataLabelEnabled)
  191. .styleSet(AAObject(AAStyle)
  192. .colorSet(aaChartModel.dataLabelFontColor)
  193. .fontSizeSet(AAFontSizeFormat(aaChartModel.dataLabelFontSize))
  194. .fontWeightSet(aaChartModel.dataLabelFontWeight)
  195. )
  196. );
  197. }
  198. if (chartType == AAChartTypeColumn) {
  199. AAColumn *aaColumn = (AAObject(AAColumn)
  200. .borderWidthSet(@0)
  201. //.groupPaddingSet(@0.05)
  202. .borderRadiusSet(aaChartModel.borderRadius)
  203. .dataLabelsSet(aaDataLabels));
  204. if (aaChartModel.polar == YES) {
  205. aaColumn.pointPaddingSet(@0)
  206. .groupPaddingSet(@0.005);
  207. }
  208. aaPlotOptions.columnSet(aaColumn);
  209. } else if (chartType == AAChartTypeBar) {
  210. AABar *aaBar = (AAObject(AABar)
  211. .borderWidthSet(@0)
  212. //.groupPaddingSet(@0.1)
  213. .borderRadiusSet(aaChartModel.borderRadius)
  214. .dataLabelsSet(aaDataLabels));
  215. if (aaChartModel.polar == YES) {
  216. aaBar.pointPaddingSet(@0)
  217. .groupPaddingSet(@0.005);
  218. }
  219. aaPlotOptions.barSet(aaBar);
  220. } else if (chartType == AAChartTypeArea) {
  221. aaPlotOptions.areaSet(AAObject(AAArea)
  222. .dataLabelsSet(aaDataLabels));
  223. } else if (chartType == AAChartTypeAreaspline) {
  224. aaPlotOptions.areasplineSet(AAObject(AAAreaspline)
  225. .dataLabelsSet(aaDataLabels));
  226. } else if (chartType == AAChartTypeLine) {
  227. aaPlotOptions.lineSet(AAObject(AALine)
  228. .dataLabelsSet(aaDataLabels));
  229. } else if (chartType == AAChartTypeSpline) {
  230. aaPlotOptions.splineSet(AAObject(AASpline)
  231. .dataLabelsSet(aaDataLabels));
  232. } else if (chartType == AAChartTypePie) {
  233. AAPie *aaPie = (AAObject(AAPie)
  234. .allowPointSelectSet(true)
  235. .cursorSet(@"pointer")
  236. .showInLegendSet(true)
  237. );
  238. if (aaChartModel.dataLabelEnabled == true) {
  239. aaPie.dataLabelsSet(aaDataLabels
  240. .formatSet(@"<b>{point.name}</b>: {point.percentage:.1f} %")
  241. );
  242. } else {
  243. aaPie.dataLabelsSet(AAObject(AADataLabels)
  244. .enabledSet(false)
  245. );
  246. }
  247. aaPlotOptions.pieSet(aaPie);
  248. } else if (chartType == AAChartTypeColumnrange) {
  249. NSMutableDictionary *columnRangeDic = [[NSMutableDictionary alloc]init];
  250. [columnRangeDic setValue:@0 forKey:@"borderRadius"];//The color of the border surrounding each column or bar
  251. [columnRangeDic setValue:@0 forKey:@"borderWidth"];//The corner radius of the border surrounding each column or bar. default:0.
  252. [columnRangeDic setValue:aaDataLabels forKey:@"dataLabels"];
  253. aaPlotOptions.columnrangeSet(columnRangeDic);
  254. } else if (chartType == AAChartTypeArearange) {
  255. NSDictionary *areaRangeDic = [[NSMutableDictionary alloc]init];
  256. [areaRangeDic setValue:aaDataLabels forKey:@"dataLabels"];
  257. aaPlotOptions.arearangeSet(areaRangeDic);
  258. }
  259. return aaPlotOptions;
  260. }
  261. @end