Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private int year = 2015;
- SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss.SSS");
- private int[] mColors = {Color.BLUE,Color.YELLOW,Color.CYAN,Color.MAGENTA,Color.GREEN};
- private int mCurrentColorIndex = 0;
- private synchronized void addEntry(String id, float value) {
- LineData data = mChart.getData();
- if (data != null) {
- ILineDataSet set = data.getDataSetByLabel(id,true);
- //ILineDataSet set1 = data.getDataSetByIndex(0);
- if (set == null) {
- set = createSet(id, mColors[(mCurrentColorIndex++)%mColors.length ]);
- data.addDataSet(set);
- }
- String xValue = sdf.format(new Date());
- // add a new x-value first
- data.addXValue(xValue);
- set.addEntry(new Entry(value, set.getEntryCount(), 0));
- // let the chart know it's data has changed
- mChart.notifyDataSetChanged();
- // limit the number of visible entries
- mChart.setVisibleXRangeMaximum(30);
- // mChart.setVisibleYRange(30, AxisDependency.LEFT);
- // move to the latest entry
- mChart.moveViewToX(data.getXValCount() - 31);
- // this automatically refreshes the chart (calls invalidate())
- // mChart.moveViewTo(data.getXValCount()-7, 55f,
- // AxisDependency.LEFT);
- }
- }
- private LineDataSet createSet(String label, int color) {
- LineDataSet set = new LineDataSet(null, label);
- set.setAxisDependency(AxisDependency.LEFT);
- set.setColor(color);
- set.setCircleColor(Color.WHITE);
- set.setLineWidth(2f);
- set.setCircleRadius(4f);
- set.setFillAlpha(65);
- set.setFillColor(color);
- set.setHighLightColor(Color.rgb(244, 117, 117));
- set.setValueTextColor(Color.WHITE);
- set.setValueTextSize(9f);
- set.setDrawValues(false);
- return set;
- }
- private void feedMultiple() {
- new Thread(new Runnable() {
- @Override
- public void run() {
- for(int i = 0; i < 50; i++) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- addEntry("name1", (float)(Math.random() * 40) + 30f);
- }
- });
- try {
- Thread.sleep(35);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).start();
- new Thread(new Runnable() {
- @Override
- public void run() {
- for(int i = 0; i < 100; i++) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- addEntry("name2", (float)(Math.random() * 40) + 30f);
- }
- });
- try {
- Thread.sleep(35);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).start();
- }
- //set.addEntry(new Entry(value, set.getEntryCount(), 0)); //incorrect
- set.addEntry(new Entry(value, data.getXValCount(), 0)); //correct
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement