当前位置:   article > 正文

Android自定义散点图View_android 自定义散点图

android 自定义散点图
  1. import android.app.Activity;
  2. import android.graphics.Paint;
  3. import android.graphics.Paint.Style;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Path;
  7. import android.graphics.Point;
  8. import android.graphics.Rect;
  9. import android.graphics.RectF;
  10. import android.graphics.Typeface;
  11. import android.view.View;
  12. import android.widget.RelativeLayout;
  13. import com.axeac.android.sdk.R;
  14. import com.axeac.android.sdk.utils.CommonUtil;
  15. import com.axeac.android.sdk.utils.StaticObject;
  16. import java.util.ArrayList;
  17. import java.util.LinkedHashMap;
  18. import java.util.List;
  19. public class ScatterChart extends View {
  20. private static final int DEFAULT_PADDING_LENGTH = 25;
  21. private static final int DEFAULT_DATAAXIS_NGRID = 4;
  22. private static final float DEFAULT_EMPTYPX = 5;
  23. private Activity ctx;
  24. private RectF rect;
  25. private int dataMaxXValue = 0;
  26. private int dataMaxYValue = 0;
  27. private int titleXPx = 0;
  28. private int titleYPx = 0;
  29. /**
  30. * 网格横线间隙
  31. * */
  32. private float xAxisGridGap;
  33. /**
  34. * 网格竖线间隙
  35. * */
  36. private float yAxisGridGap;
  37. /**
  38. * 坐标系内网格横线数量
  39. * */
  40. private int xAxisNGrid;
  41. /**
  42. * 坐标系内网格竖线数量
  43. * */
  44. private int yAxisNGrid;
  45. /**
  46. * 标题文本
  47. * <br>默认值为空
  48. * */
  49. private String title = "";
  50. /**
  51. * 标题字体
  52. * <br>默认值为font-size:30px;color:255255255;style:bold
  53. * */
  54. private String titleFont = "font-size:30px;color:051051051;style:bold";
  55. /**
  56. * 副标题文本
  57. * <br>默认值为空
  58. * */
  59. private String subTitle = "";
  60. /**
  61. * 副标题字体
  62. * <br>默认值为font-size:22px;color:255255255;style:bold
  63. * */
  64. private String subTitleFont = "font-size:22px;color:051051051;style:bold";
  65. private String titleX = "";
  66. /**
  67. * 坐标轴横轴文字尺寸
  68. * */
  69. private String titleXFont;
  70. private String titleY = "";
  71. /**
  72. * 坐标轴纵轴文字尺寸
  73. * */
  74. private String titleYFont;
  75. private String dataTitleFont;
  76. private int symbolSize;
  77. private ArrayList<Integer> colors;
  78. private LinkedHashMap<String, ArrayList<String[]>> datas = new LinkedHashMap<String, ArrayList<String[]>>();
  79. public ScatterChart(Activity ctx) {
  80. super(ctx);
  81. this.ctx = ctx;
  82. int height = 0;
  83. Rect frame = new Rect();
  84. ctx.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
  85. height = frame.top;
  86. height += ctx.findViewById(R.id.toolbar).getHeight();
  87. height += ctx.findViewById(R.id.layout_bottom).getHeight();
  88. this.setLayoutParams(new RelativeLayout.LayoutParams(
  89. RelativeLayout.LayoutParams.MATCH_PARENT,
  90. (int) (StaticObject.deviceWidth*0.618)));
  91. this.setBackgroundColor(getResources().getColor(R.color.background));
  92. this.getBackground().setAlpha(180);
  93. }
  94. public void setTitle(String title){
  95. this.title = title;
  96. }
  97. public void setTitleFont(String titleFont){
  98. this.titleFont = titleFont;
  99. }
  100. public void setSubTitle(String subTitle){
  101. this.subTitle = subTitle;
  102. }
  103. public void setSubTitleFont(String subTitleFont){
  104. this.subTitleFont = subTitleFont;
  105. }
  106. public void setTitleX(String titleX){
  107. this.titleX = titleX;
  108. }
  109. public void setTitleXFont(String titleXFont){
  110. this.titleXFont = titleXFont;
  111. }
  112. public void setTitleY(String titleY){
  113. this.titleY = titleY;
  114. }
  115. public void setTitleYFont(String titleYFont){
  116. this.titleYFont = titleYFont;
  117. }
  118. public void setDataTitleFont(String dataTitleFont) {
  119. this.dataTitleFont = dataTitleFont;
  120. }
  121. public void setSymbolSize(int symbolSize){
  122. this.symbolSize = symbolSize;
  123. }
  124. public void setColor(ArrayList<Integer> colors) {
  125. this.colors = colors;
  126. }
  127. public void setDataList(LinkedHashMap<String, ArrayList<String[]>> datas){
  128. this.datas = datas;
  129. }
  130. @Override
  131. protected void onDraw(Canvas canvas) {
  132. super.onDraw(canvas);
  133. drawChart(canvas);
  134. }
  135. /**
  136. * 绘制散点图
  137. * @param canvas
  138. * Canvas对象
  139. * */
  140. private void drawChart(Canvas canvas) {
  141. Rect leftRect = drawTitle(canvas);
  142. Rect rightRect = drawDataTitle(canvas, leftRect);
  143. int titleHeight = leftRect.bottom > rightRect.bottom ? leftRect.bottom : rightRect.bottom;
  144. rect = new RectF(0, titleHeight, this.getWidth(), this.getHeight());
  145. initChartDatas();
  146. if (!titleX.equals("")) {
  147. drawTitleX(canvas);
  148. }
  149. if (!titleY.equals("")) {
  150. drawTitleY(canvas);
  151. }
  152. drawXAxisScaleLine(canvas);
  153. drawYAxisScaleLine(canvas);
  154. drawXAxisLabel(canvas);
  155. drawYAxisLabel(canvas);
  156. drawDiagram(canvas);
  157. drawXAxisLine(canvas);
  158. drawYAxisLine(canvas);
  159. }
  160. /**
  161. * 绘制主副标题
  162. * @param canvas
  163. * Canvas对象
  164. * */
  165. private Rect drawTitle(Canvas canvas) {
  166. Paint paint = new Paint();
  167. float titleTextSize = 30;
  168. if (titleFont != null && !"".equals(titleFont)) {
  169. if (titleFont.indexOf(";") != -1) {
  170. String[] strs = titleFont.split(";");
  171. for (String str : strs) {
  172. if (str.startsWith("font-size")) {
  173. int index = str.indexOf(":");
  174. if (index == -1)
  175. continue;
  176. String s = str.substring(index + 1).trim();
  177. paint.setTextSize(Float.parseFloat(s.replace("px", "").trim()));
  178. titleTextSize = Float.parseFloat(s.replace("px", "").trim());
  179. } else if(str.startsWith("style")) {
  180. int index = str.indexOf(":");
  181. if (index == -1)
  182. continue;
  183. String s = str.substring(index + 1).trim();
  184. if ("bold".equals(s)){
  185. paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
  186. } else if("italic".equals(s)) {
  187. paint.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
  188. } else {
  189. if (s.indexOf(",") != -1) {
  190. if ("bold".equals(s.split(",")[0]) && "italic".equals(s.split(",")[1])) {
  191. paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
  192. }
  193. if ("bold".equals(s.split(",")[1]) && "italic".equals(s.split(",")[0])) {
  194. paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
  195. }
  196. }
  197. }
  198. } else if(str.startsWith("color")) {
  199. int index = str.indexOf(":");
  200. if (index == -1)
  201. continue;
  202. String s = str.substring(index + 1).trim();
  203. if (CommonUtil.validRGBColor(s)) {
  204. int r = Integer.parseInt(s.substring(0, 3));
  205. int g = Integer.parseInt(s.substring(3, 6));
  206. int b = Integer.parseInt(s.substring(6, 9));
  207. paint.setColor(Color.rgb(r, g, b));
  208. } else {
  209. paint.setColor(Color.WHITE);
  210. }
  211. }
  212. }
  213. }
  214. }
  215. paint.setStyle(Style.STROKE);
  216. paint.setAntiAlias(true);
  217. canvas.drawText(title, DEFAULT_PADDING_LENGTH, paint.getFontMetrics().bottom - paint.getFontMetrics().top, paint);
  218. int titleWidth = (int) paint.measureText(title) + DEFAULT_PADDING_LENGTH * 2;
  219. int titleHeight = (int) (paint.getFontMetrics().bottom - paint.getFontMetrics().top + titleTextSize * 0.75);
  220. paint = new Paint();
  221. float subTitleTextSize = 23;
  222. if (subTitleFont != null && !"".equals(subTitleFont)) {
  223. if (subTitleFont.indexOf(";") != -1) {
  224. String[] strs = subTitleFont.split(";");
  225. for (String str : strs) {
  226. if (str.startsWith("font-size")) {
  227. int index = str.indexOf(":");
  228. if (index == -1) continue;
  229. String s = str.substring(index + 1).trim();
  230. paint.setTextSize(Float.parseFloat(s.replace("px", "").trim()));
  231. subTitleTextSize = Float.parseFloat(s.replace("px", "").trim());
  232. } else if(str.startsWith("style")) {
  233. int index = str.indexOf(":");
  234. if (index == -1)
  235. continue;
  236. String s = str.substring(index + 1).trim();
  237. if ("bold".equals(s)){
  238. paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
  239. } else if("italic".equals(s)) {
  240. paint.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
  241. } else {
  242. if (s.indexOf(",") != -1) {
  243. if ("bold".equals(s.split(",")[0]) && "italic".equals(s.split(",")[1])) {
  244. paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
  245. }
  246. if ("bold".equals(s.split(",")[1]) && "italic".equals(s.split(",")[0])) {
  247. paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
  248. }
  249. }
  250. }
  251. } else if(str.startsWith("color")) {
  252. int index = str.indexOf(":");
  253. if (index == -1)
  254. continue;
  255. String s = str.substring(index + 1).trim();
  256. if (CommonUtil.validRGBColor(s)) {
  257. int r = Integer.parseInt(s.substring(0, 3));
  258. int g = Integer.parseInt(s.substring(3, 6));
  259. int b = Integer.parseInt(s.substring(6, 9));
  260. paint.setColor(Color.rgb(r, g, b));
  261. } else {
  262. paint.setColor(Color.WHITE);
  263. }
  264. }
  265. }
  266. }
  267. }
  268. paint.setStyle(Style.STROKE);
  269. paint.setAntiAlias(true);
  270. canvas.drawText(subTitle, DEFAULT_PADDING_LENGTH, paint.getFontMetrics().bottom - paint.getFontMetrics().top + titleHeight, paint);
  271. int subTitleWidth = (int) paint.measureText(subTitle) + DEFAULT_PADDING_LENGTH * 2;
  272. int subTitleHeight = (int) (paint.getFontMetrics().bottom - paint.getFontMetrics().top + subTitleTextSize * 0.75);
  273. int width = titleWidth > subTitleWidth ? titleWidth : subTitleWidth;
  274. int height = titleHeight + subTitleHeight;
  275. return new Rect(0, 0, width, height);
  276. }
  277. private Rect drawDataTitle(Canvas canvas, Rect rectF) {
  278. Paint paint = new Paint();
  279. float dataTitleTextSize = 23;
  280. if (dataTitleFont != null && !"".equals(dataTitleFont)) {
  281. if (dataTitleFont.indexOf(";") != -1) {
  282. String[] strs = dataTitleFont.split(";");
  283. for (String str : strs) {
  284. if (str.startsWith("font-size")) {
  285. int index = str.indexOf(":");
  286. if (index == -1) continue;
  287. String s = str.substring(index + 1).trim();
  288. paint.setTextSize(Float.parseFloat(s.replace("px", "").trim()));
  289. dataTitleTextSize = Float.parseFloat(s.replace("px", "").trim());
  290. } else if(str.startsWith("style")) {
  291. int index = str.indexOf(":");
  292. if (index == -1)
  293. continue;
  294. String s = str.substring(index + 1).trim();
  295. if ("bold".equals(s)){
  296. paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
  297. } else if("italic".equals(s)) {
  298. paint.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
  299. } else {
  300. if (s.indexOf(",") != -1) {
  301. if ("bold".equals(s.split(",")[0]) && "italic".equals(s.split(",")[1])) {
  302. paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
  303. }
  304. if ("bold".equals(s.split(",")[1]) && "italic".equals(s.split(",")[0])) {
  305. paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
  306. }
  307. }
  308. }
  309. } else if(str.startsWith("color")) {
  310. int index = str.indexOf(":");
  311. if (index == -1)
  312. continue;
  313. String s = str.substring(index + 1).trim();
  314. if (CommonUtil.validRGBColor(s)) {
  315. int r = Integer.parseInt(s.substring(0, 3));
  316. int g = Integer.parseInt(s.substring(3, 6));
  317. int b = Integer.parseInt(s.substring(6, 9));
  318. paint.setColor(Color.rgb(r, g, b));
  319. } else {
  320. paint.setColor(Color.WHITE);
  321. }
  322. }
  323. }
  324. }
  325. }
  326. paint.setStyle(Style.STROKE);
  327. paint.setAntiAlias(true);
  328. Rect rect = new Rect(rectF);
  329. rect.set(rect.right + DEFAULT_PADDING_LENGTH, rect.top + 15, this.getWidth() - DEFAULT_PADDING_LENGTH, rect.bottom);
  330. if (datas.size() > 0) {
  331. String[] list = datas.keySet().toArray(new String[0]);
  332. Integer[] lengths = new Integer[list.length];
  333. for (int i = 0; i < list.length; i++) {
  334. lengths[i] = (int) paint.measureText(list[i]);
  335. }
  336. lengths = CommonUtil.sortDesc(lengths);
  337. int itemWidth = lengths[0];
  338. int itemHeight = (int) (paint.getFontMetrics().bottom - paint.getFontMetrics().top);
  339. int lineHeight = (int) (paint.getFontMetrics().bottom - paint.getFontMetrics().top + dataTitleTextSize * 0.75);
  340. int colsCount = rect.width() / (itemWidth + DEFAULT_PADDING_LENGTH * 2);
  341. int rowsCount = list.length / colsCount + (list.length % colsCount > 0 ? 1 : 0);
  342. rect.set(rect.right - (itemWidth + DEFAULT_PADDING_LENGTH * 2) * colsCount, rect.top, rect.right, rect.top + lineHeight * rowsCount);
  343. for (int i = 0; i < rowsCount; i++) {
  344. if (i < rowsCount - 1) {
  345. for (int j = 0; j < colsCount; j++) {
  346. canvas.drawText(list[i * colsCount + j], rect.left + (itemWidth + DEFAULT_PADDING_LENGTH * 2) * j + DEFAULT_PADDING_LENGTH * 2, rect.top + lineHeight * i + itemHeight, paint);
  347. Paint p = new Paint();
  348. p.setColor(colors.get(i * colsCount + j));
  349. p.setStyle(Style.FILL_AND_STROKE);
  350. p.setAntiAlias(true);
  351. canvas.drawCircle(rect.left + (itemWidth + DEFAULT_PADDING_LENGTH * 2) * j + DEFAULT_PADDING_LENGTH, rect.top + lineHeight * i + lineHeight / 2, 13, p);
  352. }
  353. } else {
  354. int index = list.length - (rowsCount - 1) * colsCount;
  355. for (int j = 0; j < index; j++) {
  356. canvas.drawText(list[i * colsCount + j], rect.left + (itemWidth + DEFAULT_PADDING_LENGTH * 2) * (j + colsCount - index) + DEFAULT_PADDING_LENGTH * 2, rect.top + lineHeight * i + itemHeight, paint);
  357. Paint p = new Paint();
  358. p.setColor(colors.get(i * colsCount + j));
  359. p.setStyle(Style.FILL_AND_STROKE);
  360. p.setAntiAlias(true);
  361. canvas.drawCircle(rect.left + (itemWidth + DEFAULT_PADDING_LENGTH * 2) * (j + colsCount - index) + DEFAULT_PADDING_LENGTH, rect.top + lineHeight * i + lineHeight / 2, 13, p);
  362. }
  363. }
  364. }
  365. }
  366. return rect;
  367. }
  368. /**
  369. * 初始化操作
  370. * */
  371. private void initChartDatas() {
  372. obtainDataMaxValue();
  373. titleXPx = obtainPaintXYHeight(titleXFont);
  374. titleYPx = obtainPaintXYHeight(titleYFont);
  375. float originX = 0;
  376. float originY = 0;
  377. if (titleY.equals("")) {
  378. originX = rect.left + titleYPx + DEFAULT_EMPTYPX * 2;
  379. } else {
  380. originX = rect.left + (titleYPx + DEFAULT_EMPTYPX) * 2;
  381. }
  382. if (titleX.equals("")) {
  383. originY = rect.bottom - titleXPx - DEFAULT_EMPTYPX * 2;
  384. } else {
  385. originY = rect.bottom - (titleXPx + DEFAULT_EMPTYPX) * 2;
  386. }
  387. rect = new RectF(originX, rect.top + DEFAULT_EMPTYPX * 2, rect.right - DEFAULT_EMPTYPX * 4, originY);
  388. xAxisNGrid = DEFAULT_DATAAXIS_NGRID;
  389. yAxisNGrid = DEFAULT_DATAAXIS_NGRID;
  390. xAxisGridGap = rect.width() / xAxisNGrid;
  391. yAxisGridGap = rect.height() / yAxisNGrid;
  392. }
  393. /**
  394. * 设置XY轴坐标点最大数值
  395. * */
  396. private void obtainDataMaxValue() {
  397. float maxX = 0;
  398. float maxY = 0;
  399. String[] ids = datas.keySet().toArray(new String[0]);
  400. for (String id : ids) {
  401. ArrayList<String[]> list = datas.get(id);
  402. for (int i = 0; i < list.size(); i++) {
  403. float countX = Float.parseFloat(list.get(i)[2]);
  404. maxX = maxX > countX ? maxX : countX;
  405. float countY = Float.parseFloat(list.get(i)[3]);
  406. maxY = maxY > countY ? maxY : countY;
  407. }
  408. }
  409. dataMaxXValue = CommonUtil.obtainMaxData(Math.round(maxX), DEFAULT_DATAAXIS_NGRID);
  410. dataMaxYValue = CommonUtil.obtainMaxData(Math.round(maxY), DEFAULT_DATAAXIS_NGRID);
  411. }
  412. /**
  413. * 绘制散点图点
  414. * @param canvas
  415. * Canvas对象
  416. * */
  417. private void drawDiagram(Canvas canvas) {
  418. Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  419. paint.setStyle(Style.FILL_AND_STROKE);
  420. String[] ids = datas.keySet().toArray(new String[0]);
  421. for (int i = 0; i < ids.length; i++) {
  422. List<String[]> list = datas.get(ids[i]);
  423. paint.setColor(colors.get(i));
  424. for (int j = 0; j < list.size(); j++) {
  425. int x = (int) (rect.left + Float.parseFloat(list.get(j)[2]) / dataMaxXValue * rect.width());
  426. int y = (int) (rect.bottom - Float.parseFloat(list.get(j)[3]) / dataMaxYValue * rect.height());
  427. Point point = new Point(x, y);
  428. paint.setAlpha(200);
  429. float pointSize = symbolSize/5*3;
  430. canvas.drawCircle(point.x, point.y, pointSize, paint);
  431. }
  432. }
  433. }
  434. /**
  435. * 绘制坐标轴横轴文字标题
  436. * @param canvas
  437. * Canvas对象
  438. * */
  439. private void drawTitleX(Canvas canvas) {
  440. Paint paint = obtainPaintXYPaint(titleXFont);
  441. canvas.drawText(titleX, rect.left + (rect.width() - paint.measureText(titleX)) / 2, rect.bottom + titleXPx * 2, paint);
  442. }
  443. /**
  444. * 绘制坐标轴竖轴文字标题
  445. * @param canvas
  446. * Canvas对象
  447. * */
  448. private void drawTitleY(Canvas canvas) {
  449. Paint paint = obtainPaintXYPaint(titleYFont);
  450. Path path = new Path();
  451. path.moveTo(rect.left - titleYPx * 2, rect.top + (rect.height() - paint.measureText(titleY)) / 2);
  452. path.lineTo(rect.left - titleYPx * 2, rect.bottom - (rect.height() - paint.measureText(titleY)) / 2);
  453. canvas.drawTextOnPath(titleY, path, 0, 0, paint);
  454. }
  455. /**
  456. * 绘制坐标轴内网格小横线(存在数据的点)
  457. * @param canvas
  458. * Canvas对象
  459. * */
  460. private void drawXAxisScaleLine(Canvas canvas) {
  461. Paint paint = new Paint();
  462. paint.setColor(Color.BLACK);
  463. for (int i = 1; i <= xAxisNGrid * 2; i++) {
  464. canvas.drawLine(rect.left + i * xAxisGridGap / 2, rect.bottom,
  465. rect.left + i * xAxisGridGap / 2, rect.bottom + DEFAULT_EMPTYPX, paint);
  466. }
  467. }
  468. /**
  469. * 绘制坐标轴内网格小竖线(存在数据的点)
  470. * @param canvas
  471. * Canvas对象
  472. * */
  473. private void drawYAxisScaleLine(Canvas canvas) {
  474. Paint paint = new Paint();
  475. paint.setColor(Color.BLACK);
  476. for (int i = 1; i <= yAxisNGrid; i++)
  477. canvas.drawLine(rect.left, rect.bottom - i * yAxisGridGap,
  478. rect.left - DEFAULT_EMPTYPX, rect.bottom - i * yAxisGridGap, paint);
  479. }
  480. /**
  481. * 绘制横轴标签文字
  482. * @param canvas
  483. * Canvas对象
  484. * */
  485. private void drawXAxisLabel(Canvas canvas) {
  486. Paint paint = obtainPaintXYPaint(titleXFont);
  487. float textStartPx = 0;
  488. String text;
  489. for (int i = 0; i <= xAxisNGrid; i++) {
  490. int singleVal = dataMaxXValue / DEFAULT_DATAAXIS_NGRID;
  491. text = String.valueOf(singleVal * i);
  492. textStartPx = paint.measureText(text) / 2;
  493. canvas.drawText(text, rect.left - textStartPx + i * xAxisGridGap, rect.bottom + titleXPx, paint);
  494. }
  495. }
  496. /**
  497. * 绘制纵轴标签文字
  498. * @param canvas
  499. * Canvas对象
  500. * */
  501. private void drawYAxisLabel(Canvas canvas) {
  502. Paint paint = obtainPaintXYPaint(titleYFont);
  503. String text;
  504. for (int i = 0; i <= yAxisNGrid; i++) {
  505. int singleVal = dataMaxYValue / DEFAULT_DATAAXIS_NGRID;
  506. text = String.valueOf(singleVal * i);
  507. Path path = new Path();
  508. path.moveTo(rect.left - titleYPx, rect.bottom - i * yAxisGridGap - paint.measureText(text) / 2);
  509. path.lineTo(rect.left - titleYPx, rect.bottom - i * yAxisGridGap + paint.measureText(text) / 2);
  510. canvas.drawTextOnPath(text, path, 0, 0, paint);
  511. }
  512. }
  513. private int obtainPaintXYHeight(String font) {
  514. Paint paint = obtainPaintXYPaint(font);
  515. return (int) (paint.getFontMetrics().bottom - paint.getFontMetrics().top);
  516. }
  517. /**
  518. * 返回绘制XY坐标点文字的Paint对象
  519. * @param font
  520. * 文字尺寸
  521. * @return
  522. * Paint对象
  523. * */
  524. private Paint obtainPaintXYPaint(String font) {
  525. Paint paint = new Paint();
  526. if (font != null && !"".equals(font)) {
  527. if (font.indexOf(";") != -1) {
  528. String[] strs = font.split(";");
  529. for (String str : strs) {
  530. if (str.startsWith("font-size")) {
  531. int index = str.indexOf(":");
  532. if (index == -1)
  533. continue;
  534. String s = str.substring(index + 1).trim();
  535. paint.setTextSize(Float.parseFloat(s.replace("px", "").trim()));
  536. } else if(str.startsWith("style")) {
  537. int index = str.indexOf(":");
  538. if (index == -1)
  539. continue;
  540. String s = str.substring(index + 1).trim();
  541. if ("bold".equals(s)){
  542. paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
  543. } else if("italic".equals(s)) {
  544. paint.setTypeface(Typeface.defaultFromStyle(Typeface.ITALIC));
  545. } else {
  546. if (s.indexOf(",") != -1) {
  547. if ("bold".equals(s.split(",")[0]) && "italic".equals(s.split(",")[1])) {
  548. paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
  549. }
  550. if ("bold".equals(s.split(",")[1]) && "italic".equals(s.split(",")[0])) {
  551. paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD_ITALIC));
  552. }
  553. }
  554. }
  555. } else if(str.startsWith("color")) {
  556. int index = str.indexOf(":");
  557. if (index == -1)
  558. continue;
  559. String s = str.substring(index + 1).trim();
  560. if (CommonUtil.validRGBColor(s)) {
  561. int r = Integer.parseInt(s.substring(0, 3));
  562. int g = Integer.parseInt(s.substring(3, 6));
  563. int b = Integer.parseInt(s.substring(6, 9));
  564. paint.setColor(Color.rgb(r, g, b));
  565. } else {
  566. paint.setColor(Color.WHITE);
  567. }
  568. }
  569. }
  570. }
  571. }
  572. paint.setStyle(Style.FILL_AND_STROKE);
  573. paint.setAntiAlias(true);
  574. return paint;
  575. }
  576. /**
  577. * 绘制X轴
  578. * @param canvas
  579. * Canvas对象
  580. * */
  581. private void drawXAxisLine(Canvas canvas) {
  582. Paint paint = new Paint();
  583. paint.setColor(Color.BLACK);
  584. canvas.drawLine(rect.left, rect.bottom, rect.right, rect.bottom, paint);
  585. }
  586. /**
  587. * 绘制Y轴
  588. * @param canvas
  589. * Canvas对象
  590. * */
  591. private void drawYAxisLine(Canvas canvas) {
  592. Paint paint = new Paint();
  593. paint.setColor(Color.BLACK);
  594. canvas.drawLine(rect.left, rect.bottom, rect.left, rect.top, paint);
  595. }
  596. @Override
  597. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  598. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  599. this.setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
  600. }
  601. private int measureWidth(int measureSpec) {
  602. int result = 0;
  603. int specMode = MeasureSpec.getMode(measureSpec);
  604. int specSize = MeasureSpec.getSize(measureSpec);
  605. if (specMode == MeasureSpec.EXACTLY) {
  606. result = specSize;
  607. } else if (specMode == MeasureSpec.AT_MOST) {
  608. result = Math.min(result, specSize);
  609. }
  610. return result;
  611. }
  612. private int measureHeight(int measureSpec) {
  613. int result = 0;
  614. int specMode = MeasureSpec.getMode(measureSpec);
  615. int specSize = MeasureSpec.getSize(measureSpec);
  616. if (specMode == MeasureSpec.EXACTLY) {
  617. result = specSize;
  618. } else if (specMode == MeasureSpec.AT_MOST) {
  619. result = Math.min(result, specSize);
  620. }
  621. return result;
  622. }
  623. public View getView(){
  624. return this;
  625. }
  626. }

其中散点图数据格式为:id||type||x||y" 

id:不同数据项的id

type:散点类别

x:散点对应x轴数据

y:散点对应y轴数据

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/254626?site
推荐阅读
相关标签
  

闽ICP备14008679号