Reputation: 57
I have created a chart using MPAndroidChart
and I am looking to see if it is possible to set a fixed position for the markerview
. My chart is being used inside a fragment
. I want the markerview
to always stay at the top of chart in the center of the X-Axis
. I am using the Override Draw
method but I can not get it to sit in the position I want it unless I specify it with ints
. I tried to follow another guide where I override the getXoffset()
but it I can not use this method in my class. Is this possible to do?
@Override
public void draw(Canvas canvas, float posX, float posY) {
posX = 400;
posY = -30;
canvas.translate(posX,posY);
draw(canvas);
canvas.translate(-posX,-posY);
}
}
Full Fragment Code
public View onCreateView(LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.graph1_fragment, container, false);
mChart = (LineChart) view.findViewById(R.id.chart1);
mChart.setNoDataText("Getting Data From Server");
mChart.setNoDataTextColor(Color.BLACK);
makeChart();
return view;
}
private void makeChart() {
StringRequest req = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
try {
jsonResponse = "";
JSONObject jObject = new JSONObject(response);
JSONArray jsonArray = jObject.getJSONArray("Data");
for (int i = 0; i <= jsonArray.length(); i++) {
JSONObject o = jsonArray.getJSONObject(i);
time = o.getString("time");
open = o.getString("close");
float val = Float.parseFloat(open);
yVals1.add(new Entry(i, val));
long unixSeconds = Long.parseLong(time);
Date date = new Date(unixSeconds * 1000L);
SimpleDateFormat sdf = new
SimpleDateFormat("HH:MM");
sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
String formattedDate = sdf.format(date);
xValues.add(formattedDate);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
Graph_AppController.getInstance().addToRequestQueue(req);
handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
mChart.setBackgroundColor(Color.WHITE);
mChart.setGridBackgroundColor(Color.WHITE);
mChart.setDrawGridBackground(true);
mChart.setDrawBorders(true);
Legend l = mChart.getLegend();
l.setEnabled(false);
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setAxisLineColor(Color.BLACK);
xAxis.setTextColor(Color.BLACK);
xAxis.setValueFormatter(new DefaultAxisValueFormatter(0) {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return xValues.get((int) value % xValues.size());
}
@Override
public int getDecimalDigits() {
return 0;
}
});
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTextColor(Color.BLACK);
leftAxis.setDrawAxisLine(true);
leftAxis.setDrawZeroLine(false);
leftAxis.setDrawGridLines(false);
leftAxis.setGridColor(Color.WHITE);
leftAxis.setAxisLineColor(Color.BLACK);
mChart.getAxisRight().setEnabled(false);
LineDataSet set1;
set1 = new LineDataSet(yVals1, "DataSet 1");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
set1.setColor(Color.parseColor("#008000"));
set1.setDrawCircles(true);
set1.setLineWidth(3f);
set1.setCircleRadius(1f);
set1.setCircleColor(Color.parseColor("#008000"));
set1.setFillAlpha(50);
set1.setDrawFilled(true);
set1.setFillColor(Color.parseColor("#008000"));
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);
dataSets.add(set1);
LineData datab = new LineData(dataSets);
datab.setDrawValues(false);
mChart.setData(datab);
mChart.setDrawMarkers(true);
IMarker marker = new
YourMarkerView(getContext(),R.layout.custom_marker);
mChart.setMarker(marker);
}
}, 3000);
}
public class YourMarkerView extends MarkerView {
private TextView tvContent;
public YourMarkerView(Context context, int layoutResource) {
super(context, layoutResource);
// find your layout components
tvContent = (TextView) findViewById(R.id.tvContent);
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight) {
tvContent.setText("$" + e.getY());
// this will perform necessary layouting
super.refreshContent(e, highlight);
}
private MPPointF mOffset;
@Override
public MPPointF getOffset() {
if(mOffset == null) {
// center the marker horizontally and vertically
mOffset = new MPPointF(-(getWidth() / 2), -getHeight());
}
return mOffset;
}
@Override
public void draw(Canvas canvas, float posX, float posY) {
posX = 400;
posY = -30;
canvas.translate(posX,posY);
draw(canvas);
canvas.translate(-posX,-posY);
}
}
Upvotes: 1
Views: 1069
Reputation: 59
Maybe you could use this :
posX=Resources.getSystem().getDisplayMetrics().widthPixels*ratio1
posY=Resources.getSystem().getDisplayMetrics().heightPixels*ratio2
Resources.getSystem().getDisplayMetrics()
will give you the sizes of your screen and then you can choose any ratio you want in order to set the position.
Upvotes: 0