Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mpchart//How to add value of X label #3269

Open
ddlow opened this issue Jul 18, 2017 · 0 comments · May be fixed by #5249
Open

Mpchart//How to add value of X label #3269

ddlow opened this issue Jul 18, 2017 · 0 comments · May be fixed by #5249

Comments

@ddlow
Copy link

ddlow commented Jul 18, 2017

My all data in a array and I want to add the value on X Axis label
For example : The first value is 0.2 ,second value is 0.4 and third 0.8 until array.length.
Actually I'm makeing a ECG Grid.........LOL
any solution can help me?

i'm sorry that my english is so poor and this is my first time use the issue system

this my code :
package com.example.mpchart;
/-------------------------code--------------------------------------------/
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.icu.text.SimpleDateFormat;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.text.Layout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.components.YAxis.AxisDependency;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

import cz.msebera.android.httpclient.Header;

public class MainActivity extends Activity implements View.OnClickListener{
String [] splitarray;
private LineChart mainchart;
private LineChart mChart;

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	final LinearLayout LLV = (LinearLayout) findViewById(R.id.LLV);
	Button btnget = (Button) findViewById(R.id.button);
	assert btnget != null;
	btnget.setOnClickListener(this);



	Toast toast =Toast.makeText(MainActivity.this,"輸入欲查詢資料",Toast.LENGTH_LONG);
	toast.show();
	LLV.setBackgroundColor(Color.BLACK);

	mainchart = (LineChart) findViewById(R.id.mainchart);
	//create line chart
	mChart = new LineChart(this);
	//add to main layout
	mainchart.addView(mChart);
	
	//custom line chart
	mChart.setDescription(" ");
	mChart.setNoDataText("No for the moment");
	
	//enable value highlighting
	mChart.setTouchEnabled(true);
	
	//we want also enable scaling and dragging
	mChart.setDragEnabled(true);
	mChart.setScaleEnabled(true);
	//mChart.setScaleXEnabled(true);
	mChart.setDrawGridBackground(false);



	//enable pinch zoom to void scaling x and y axis separately
	mChart.setPinchZoom(true);
	
	//放大/縮小的手勢就可以 Zoom In/Out,但你也可以透過動態的方式來操作
	//mChart.zoomIn();
	//mChart.zoomOut();
	mChart.setHighlightPerTapEnabled(false);
	//alternative background color
	mChart.setBackgroundColor(Color.BLACK);
	
	//now we work on data
	LineData data= new LineData();
	data.setValueTextColor(Color.WHITE);

	//add data to line chart
	mChart.setData(data);



	//get legend object
	Legend l = mChart.getLegend();
	
	//custom
	l.setEnabled(false);
	l.setForm(LegendForm.LINE);
	l.setTextColor(Color.WHITE);
	l.setXEntrySpace(2f);

	//================================X======================================

	XAxis xl = mChart.getXAxis();
	//xl.setLabelsToSkip(0);
	xl.getTypeface();
	xl.setSpaceBetweenLabels(5);
	xl.setDrawAxisLine(true);
	xl.setAxisMinValue(0);
	xl.setTextColor(Color.WHITE);
	xl.setDrawGridLines(true);
	xl.setAvoidFirstLastClipping(false);
	xl.resetLabelsToSkip();
	xl.setEnabled(true);
	xl.setPosition(XAxis.XAxisPosition.BOTTOM);

	//===========================Y============================================
	YAxis yl = mChart.getAxisLeft();
	yl.setTextColor(Color.WHITE);
	yl.setAxisMinValue(0);
	yl.setAxisMaxValue(5);
	yl.setDrawGridLines(true);
	
	YAxis y12 = mChart.getAxisRight();
	y12.setEnabled(false);

		
}

@Override
protected void onResume() {
	// TODO Auto-generated method stub
	super.onResume();
	//now we're going to simulate real time addition

	new Thread(new Runnable() {
		
		@Override
		public void run() {
			// add 100 entries
			for (int i = 0; i < 1000; i++){
				runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						//addEntry(ss);// chart is notified of update in add Entry method

					}
				});
				//pause between adds
				try {
					Thread.sleep(2000);
					
				}catch (InterruptedException e) {
					//manage error ...
				}
			}
		}
	}).start();
}

//we need to create method to add entry to the line chart
@RequiresApi(api = Build.VERSION_CODES.N)
private void addEntry(Float aa) {
	LineData data = mChart.getData();
	
	if (data !=null){
		LineDataSet set = (LineDataSet) data.getDataSetByIndex (0);
		
		if (set == null) {
			//creation if null
			set = createSet();
			data.addDataSet(set);
		}
		//add a new random value
			/*	SimpleDateFormat sdt = new SimpleDateFormat();
				int value =0;
			Calendar cal = Calendar.getInstance();
			cal.setTimeInMillis(value);*/

				data.addXValue("");            //X座標 標籤


			data.addEntry(
					new Entry((Float) aa, set
							.getEntryCount()), 0);



		
		// notify chart data have changed
		mChart.notifyDataSetChanged();
		
		//limit number of visible entries
		mChart.setVisibleXRange(100,100);
		
		//scroll to the last entry
	//	mChart.moveViewToX(data.getXValCount() - 7);
	}
}

//method to create set
@SuppressWarnings("deprecation")
private LineDataSet createSet() {
	LineDataSet set = new LineDataSet(null, "");
	set.setDrawValues(false);
//	set.setDrawCubic(false);
	set.setDrawCircles(false);
	set.setCubicIntensity(0.2f);
	set.setAxisDependency(AxisDependency.LEFT);
	set.setColor(ColorTemplate.getHoloBlue());
	set.setCircleColor(ColorTemplate.getHoloBlue());
	set.setLineWidth(2f);
	//set.setCircleSize(4f);
	set.setFillAlpha(65);
	set.setHighLightColor(Color.rgb(244, 117, 177));
	set.setValueTextColor(Color.WHITE);
	set.setValueTextSize(10f);
			
	return set;
	
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.main, menu);
	return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
	// Handle action bar item clicks here. The action bar will
	// automatically handle clicks on the Home/Up button, so long
	// as you specify a parent activity in AndroidManifest.xml.
	int id = item.getItemId();
	if (id == R.id.action_settings) {
		return true;
	}
	return super.onOptionsItemSelected(item);
}

@Override
public void onClick(final View view) {

	EditText in_id = (EditText) findViewById(R.id.editText);

	String getdb_name="";
	getdb_name = in_id.getText().toString();

	AsyncHttpClient client = new AsyncHttpClient();
	view.setEnabled(false);
	client.get("http://admin:12345@203.64.102.75:5984/ecg/"+getdb_name, new AsyncHttpResponseHandler() {
		@RequiresApi(api = Build.VERSION_CODES.N)
		@Override
		public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

				if (responseBody != null) {

					//TextView txv = (TextView) findViewById(R.id.textView);
					String add = "";

					try {

						//===========解析方法============================//
						String tmp = new String(responseBody);
						JSONTokener j = new JSONTokener(tmp);
						JSONObject kk = (JSONObject) j.nextValue();
						String id = kk.getString("ecg_data");
						splitarray = id.split(",");//字串切割


						for (int i = 0; i <= splitarray.length; i++) {


							//txv.setText(add);
							float ss = Float.parseFloat(splitarray[i]);
							addEntry(ss);
						}


					} catch (Exception e) {
						e.printStackTrace();

					}

				}
				view.setEnabled(true);

				//Toast.makeText(MainActivity.this,"輸入不正確",Toast.LENGTH_LONG);



		}

		@Override
		public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
			view.setEnabled(true);
		}
	});

}

}

@yaugenka yaugenka linked a pull request Jan 19, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant