dkahgld

AAOptionsConstructor.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // AAColumnAndBarAndSoOnOptions.m
  3. // AAChartKit
  4. //
  5. // Created by An An on 17/1/9.
  6. // Copyright © 2017年 An An. All rights reserved.
  7. // source code ----*** https://github.com/AAChartModel/AAChartKit ***--- source code
  8. //
  9. /*
  10. * -------------------------------------------------------------------------------
  11. *
  12. * ❀❀❀ WARM TIPS!!! ❀❀❀
  13. *
  14. * Please contact me on GitHub,if there are any problems encountered in use.
  15. * GitHub Issues : https://github.com/AAChartModel/AAChartKit/issues
  16. * -------------------------------------------------------------------------------
  17. * And if you want to contribute for this project, please contact me as well
  18. * GitHub : https://github.com/AAChartModel
  19. * StackOverflow : https://stackoverflow.com/users/7842508/codeforu
  20. * JianShu : http://www.jianshu.com/u/f1e6753d4254
  21. * SegmentFault : https://segmentfault.com/u/huanghunbieguan
  22. *
  23. * -------------------------------------------------------------------------------
  24. */
  25. #import "AAOptionsConstructor.h"
  26. @implementation AAOptionsConstructor
  27. + (AAOptions *)configureChartOptionsWithAAChartModel:(AAChartModel *)chartModel {
  28. AAChart *chart = AAObject(AAChart)
  29. .typeSet(chartModel.chartType)//绘图类型
  30. .invertedSet(chartModel.inverted)//设置是否反转坐标轴,使X轴垂直,Y轴水平。 如果值为 true,则 x 轴默认是 倒置 的。 如果图表中出现条形图系列,则会自动反转
  31. .backgroundColorSet(@"rgba(0,0,0,0)")//设置图表的背景色(包含透明度的设置)
  32. .zoomTypeSet(chartModel.zoomType)//设置手势缩放方向
  33. .panningSet(true)//设置手势缩放后是否可平移
  34. .polarSet(chartModel.polar);
  35. if (chartModel.options3dEnable == true) {
  36. chart.options3d = (AAObject(AAOptions3d)
  37. .enabledSet(chartModel.options3dEnable)
  38. .alphaSet(@-15)
  39. );
  40. }
  41. AATitle *title = AAObject(AATitle)
  42. .textSet(chartModel.title)//标题文本内容
  43. .styleSet(AAObject(AAStyle)
  44. .colorSet(@"#000000")//标题颜色
  45. .fontSizeSet(@"12px")//标题字体大小
  46. );
  47. AASubtitle *subtitle = AAObject(AASubtitle)
  48. .textSet(chartModel.subtitle)//副标题内容
  49. .alignSet(chartModel.subtitleAlign)//图表副标题文本水平对齐方式。可选的值有 “left”,”center“和“right”。 默认是:center.
  50. .styleSet(AAObject(AAStyle)
  51. .colorSet(@"#000000")
  52. .fontSizeSet(@"9px")
  53. );
  54. AAXAxis *xAxis = AAObject(AAXAxis)
  55. .labelsSet(AAObject(AALabels)
  56. .enabledSet(chartModel.xAxisLabelsEnabled)//设置 x 轴是否显示文字
  57. )
  58. .reversedSet(chartModel.xAxisReversed)
  59. .gridLineWidthSet(chartModel.xAxisGridLineWidth)//x轴网格线宽度
  60. .categoriesSet(chartModel.categories);
  61. AAYAxis *yAxis = AAObject(AAYAxis)
  62. .labelsSet(AAObject(AALabels)
  63. .enabledSet(chartModel.yAxisLabelsEnabled)//设置 y 轴是否显示数字
  64. )
  65. .minSet(chartModel.yMin)//设置 y 轴最小值,最小值等于零就不能显示负值了
  66. .maxSet(chartModel.yMax)//y轴最大值
  67. .tickPositionsSet(chartModel.yTickPositions)//自定义Y轴坐标
  68. .allowDecimalsSet(chartModel.yAllowDecimals)//是否允许显示小数
  69. .plotLinesSet(chartModel.yPlotLines) //标示线设置
  70. .reversedSet(chartModel.yAxisReversed)
  71. .gridLineWidthSet(chartModel.yAxisGridLineWidth)//y轴网格线宽度
  72. .titleSet(AAObject(AATitle)
  73. .textSet(chartModel.yAxisTitle))//y 轴标题
  74. .lineWidthSet(@0);
  75. AATooltip *tooltip = AAObject(AATooltip)
  76. .enabledSet(true)//启用浮动提示框
  77. .sharedSet(true)//多组数据公享一个浮动提示框
  78. .crosshairsSet(chartModel.crosshairs);
  79. // .useHTMLSet(true)
  80. // .valueSuffixSet(@"摄氏度");//浮动提示框的单位名称后缀
  81. AAPlotOptions *plotOptions = AAObject(AAPlotOptions)
  82. .seriesSet(AAObject(AASeries)
  83. // .colorByPointSet(false)//决定了图表是否给每个数据列或每个点分配一个颜色,默认值是 false, 即默认是给每个数据类分配颜色,
  84. .stackingSet(chartModel.stacking)//设置是否百分比堆叠显示图形
  85. // .animationSet(AAObject(AAAnimation)
  86. // .easingSet(chartAnimationType)
  87. // .durationSet(chartModel.animationDuration)
  88. // )
  89. );
  90. if (chartModel.animationType != 0) {
  91. NSString *chartAnimationType = [self configureTheEasingAnimationType:chartModel.animationType];
  92. plotOptions.series.animation = (AAObject(AAAnimation)
  93. .easingSet(chartAnimationType)
  94. .durationSet(chartModel.animationDuration)
  95. );
  96. }
  97. //数据点标记相关配置,只有线性图(折线图、曲线图、折线区域填充图、曲线区域填充图)才有数据点标记
  98. if ( [chartModel.chartType isEqualToString:AAChartTypeArea]
  99. || [chartModel.chartType isEqualToString:AAChartTypeAreaspline]
  100. || [chartModel.chartType isEqualToString:AAChartTypeLine]
  101. || [chartModel.chartType isEqualToString:AAChartTypeSpline]) {
  102. AAMarker *marker = AAObject(AAMarker)
  103. .radiusSet(chartModel.markerRadius)//曲线连接点半径,默认是4
  104. .symbolSet(chartModel.symbol);//曲线点类型:"circle", "square", "diamond", "triangle","triangle-down",默认是"circle"
  105. if (chartModel.symbolStyle == AAChartSymbolStyleTypeInnerBlank) {
  106. marker.fillColorSet(@"#ffffff")//点的填充色(用来设置折线连接点的填充色)
  107. .lineWidthSet(@2)//外沿线的宽度(用来设置折线连接点的轮廓描边的宽度)
  108. .lineColorSet(@"");//外沿线的颜色(用来设置折线连接点的轮廓描边颜色,当值为空字符串时,默认取数据点或数据列的颜色)
  109. } else if (chartModel.symbolStyle == AAChartSymbolStyleTypeBorderBlank) {
  110. marker.lineWidthSet(@2)
  111. .lineColorSet(@"#ffffff");
  112. }
  113. AASeries *series = plotOptions.series;
  114. series.connectNulls = chartModel.connectNulls;
  115. series.marker = marker;
  116. }
  117. plotOptions = [self configureTheAAPlotOptionsWithPlotOptions:plotOptions chartModel:chartModel];
  118. // plotOptions.series.events = @{@"click":@"hahaha"};
  119. AALegend *legend = AAObject(AALegend)
  120. .enabledSet(chartModel.legendEnabled)//是否显示 legend
  121. .layoutSet(AALegendLayoutTypeHorizontal)//图例数据项的布局。布局类型: "horizontal" 或 "vertical" 即水平布局和垂直布局 默认是:horizontal.
  122. .alignSet(AALegendAlignTypeCenter)//设定图例在图表区中的水平对齐方式,合法值有left,center 和 right。
  123. .verticalAlignSet(AALegendVerticalAlignTypeBottom)//设定图例在图表区中的垂直对齐方式,合法值有 top,middle 和 bottom。垂直位置可以通过 y 选项做进一步设定。
  124. .itemMarginTopSet(@0);//图例的每一项的顶部外边距,单位px。 默认是:0.
  125. AAOptions *options = AAObject(AAOptions)
  126. .chartSet(chart)
  127. .titleSet(title)
  128. .subtitleSet(subtitle)
  129. .xAxisSet(xAxis)
  130. .yAxisSet(yAxis)
  131. .tooltipSet(tooltip)
  132. .plotOptionsSet(plotOptions)
  133. .legendSet(legend)
  134. .seriesSet(chartModel.series)
  135. .colorsSet(chartModel.colorsTheme)//设置颜色主题
  136. .gradientColorEnableSet(chartModel.gradientColorEnable);//设置主题颜色是否为渐变色
  137. // + (nullable NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;
  138. //NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:options options:NSJSONReadingMutableLeaves error:nil];
  139. // options.plotOptions.pie.dataLabels.formatSet(@"你一生的故事<br/>你一生的故事<br/>你一生的故事<br/>");
  140. return options;
  141. }
  142. + (NSString *)configureTheEasingAnimationType:(AAChartAnimation)animationType {
  143. NSArray *animationTypeArr = @[
  144. @"linear",
  145. @"easeInQuad",
  146. @"easeOutQuad",
  147. @"easeInOutQuad",
  148. @"easeInCubic",
  149. @"easeOutCubic",
  150. @"easeInOutCubic",
  151. @"easeInQuart",
  152. @"easeOutQuart",
  153. @"easeInOutQuart",
  154. @"easeInQuint",
  155. @"easeOutQuint",
  156. @"easeInOutQuint",
  157. @"easeInSine",
  158. @"easeOutSine",
  159. @"easeInOutSine",
  160. @"easeInExpo",
  161. @"easeOutExpo",
  162. @"easeInOutExpo",
  163. @"easeInCirc",
  164. @"easeOutCirc",
  165. @"easeInOutCirc",
  166. @"easeOutBounce",
  167. @"easeInBack",
  168. @"easeOutBack",
  169. @"easeInOutBack",
  170. @"elastic",
  171. @"swingFromTo",
  172. @"swingFrom",
  173. @"swingTo",
  174. @"bounce",
  175. @"bouncePast",
  176. @"easeFromTo",
  177. @"easeFrom",
  178. @"easeTo",
  179. ] ;
  180. return animationTypeArr[animationType];
  181. }
  182. + (AAPlotOptions *)configureTheAAPlotOptionsWithPlotOptions:(AAPlotOptions *)plotOptions
  183. chartModel:(AAChartModel *)chartModel {
  184. //数据点标记相关配置,只有线性图才有数据点标记
  185. if ([chartModel.chartType isEqualToString:AAChartTypeColumn]) {
  186. plotOptions.columnSet(AAObject(AAColumn)
  187. .pointPaddingSet(@0.2)
  188. .borderWidthSet(@0)
  189. .borderRadiusSet(chartModel.borderRadius)
  190. .dataLabelsSet(AAObject(AADataLabels)
  191. .enabledSet(chartModel.dataLabelEnabled)
  192. )
  193. );
  194. } else if ([chartModel.chartType isEqualToString:AAChartTypeBar]) {
  195. plotOptions.barSet(AAObject(AABar)
  196. .pointPaddingSet(@0.2)
  197. .borderWidthSet(@0)
  198. .colorByPointSet(false)
  199. .borderRadiusSet(chartModel.borderRadius)
  200. .dataLabelsSet(AAObject(AADataLabels)
  201. .enabledSet(chartModel.dataLabelEnabled)
  202. )
  203. );
  204. } else if ([chartModel.chartType isEqualToString:AAChartTypeArea]) {
  205. plotOptions.areaSet(AAObject(AAArea)
  206. .dataLabelsSet(AAObject(AADataLabels)
  207. .enabledSet(chartModel.dataLabelEnabled)
  208. )
  209. );
  210. } else if ([chartModel.chartType isEqualToString:AAChartTypeAreaspline]) {
  211. plotOptions.areasplineSet(AAObject(AAAreaspline)
  212. .dataLabelsSet(AAObject(AADataLabels)
  213. .enabledSet(chartModel.dataLabelEnabled)
  214. )
  215. );
  216. } else if ([chartModel.chartType isEqualToString:AAChartTypeLine]) {
  217. plotOptions.lineSet(AAObject(AALine)
  218. .dataLabelsSet(AAObject(AADataLabels)
  219. .enabledSet(chartModel.dataLabelEnabled)
  220. )
  221. );
  222. } else if ([chartModel.chartType isEqualToString:AAChartTypeSpline]) {
  223. plotOptions.splineSet(AAObject(AASpline)
  224. .dataLabelsSet(AAObject(AADataLabels)
  225. .enabledSet(chartModel.dataLabelEnabled))
  226. );
  227. } else if ([chartModel.chartType isEqualToString:AAChartTypePie]) {
  228. plotOptions.pieSet(AAObject(AAPie)
  229. .allowPointSelectSet(true)
  230. .cursorSet(@"pointer")
  231. .dataLabelsSet(AAObject(AADataLabels)
  232. .enabledSet(chartModel.dataLabelEnabled)
  233. .formatSet(@"{point.percentage:.1f}%")
  234. .styleSet(AAObject(AAStyle)
  235. .colorSet(@"black")
  236. )
  237. )
  238. .showInLegendSet(true)
  239. );
  240. if (chartModel.options3dEnable ==true) {
  241. plotOptions.pie.depth = chartModel.options3dDepth;//设置3d 图形阴影深度
  242. }
  243. // plotOptions.series.colorByPoint = true;
  244. }
  245. return plotOptions;
  246. }
  247. @end