Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. private int year = 2015;
  2. SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss.SSS");
  3. private int[] mColors = {Color.BLUE,Color.YELLOW,Color.CYAN,Color.MAGENTA,Color.GREEN};
  4. private int mCurrentColorIndex = 0;
  5.  
  6. private synchronized void addEntry(String id, float value) {
  7.  
  8. LineData data = mChart.getData();
  9.  
  10. if (data != null) {
  11.  
  12. ILineDataSet set = data.getDataSetByLabel(id,true);
  13. //ILineDataSet set1 = data.getDataSetByIndex(0);
  14.  
  15.  
  16. if (set == null) {
  17. set = createSet(id, mColors[(mCurrentColorIndex++)%mColors.length ]);
  18. data.addDataSet(set);
  19. }
  20.  
  21.  
  22. String xValue = sdf.format(new Date());
  23.  
  24. // add a new x-value first
  25. data.addXValue(xValue);
  26. set.addEntry(new Entry(value, set.getEntryCount(), 0));
  27.  
  28.  
  29. // let the chart know it's data has changed
  30. mChart.notifyDataSetChanged();
  31.  
  32. // limit the number of visible entries
  33. mChart.setVisibleXRangeMaximum(30);
  34. // mChart.setVisibleYRange(30, AxisDependency.LEFT);
  35.  
  36. // move to the latest entry
  37. mChart.moveViewToX(data.getXValCount() - 31);
  38.  
  39. // this automatically refreshes the chart (calls invalidate())
  40. // mChart.moveViewTo(data.getXValCount()-7, 55f,
  41. // AxisDependency.LEFT);
  42. }
  43. }
  44.  
  45. private LineDataSet createSet(String label, int color) {
  46.  
  47. LineDataSet set = new LineDataSet(null, label);
  48. set.setAxisDependency(AxisDependency.LEFT);
  49. set.setColor(color);
  50. set.setCircleColor(Color.WHITE);
  51. set.setLineWidth(2f);
  52. set.setCircleRadius(4f);
  53. set.setFillAlpha(65);
  54. set.setFillColor(color);
  55. set.setHighLightColor(Color.rgb(244, 117, 117));
  56. set.setValueTextColor(Color.WHITE);
  57. set.setValueTextSize(9f);
  58. set.setDrawValues(false);
  59. return set;
  60. }
  61.  
  62. private void feedMultiple() {
  63.  
  64. new Thread(new Runnable() {
  65.  
  66. @Override
  67. public void run() {
  68. for(int i = 0; i < 50; i++) {
  69.  
  70. runOnUiThread(new Runnable() {
  71.  
  72. @Override
  73. public void run() {
  74. addEntry("name1", (float)(Math.random() * 40) + 30f);
  75. }
  76. });
  77. try {
  78. Thread.sleep(35);
  79. } catch (InterruptedException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. }
  84. }).start();
  85.  
  86. new Thread(new Runnable() {
  87.  
  88. @Override
  89. public void run() {
  90. for(int i = 0; i < 100; i++) {
  91.  
  92. runOnUiThread(new Runnable() {
  93.  
  94. @Override
  95. public void run() {
  96. addEntry("name2", (float)(Math.random() * 40) + 30f);
  97. }
  98. });
  99. try {
  100. Thread.sleep(35);
  101. } catch (InterruptedException e) {
  102. e.printStackTrace();
  103. }
  104. }
  105. }
  106. }).start();
  107.  
  108. }
  109.  
  110. //set.addEntry(new Entry(value, set.getEntryCount(), 0)); //incorrect
  111. set.addEntry(new Entry(value, data.getXValCount(), 0)); //correct
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement