我使用MPAndroidChart
创建了一个图表,我希望看看是否可以为markerview
设置固定位置。我的图表正在fragment
内使用。我希望markerview
始终位于X-Axis
中心的图表顶部。我使用的是Override Draw
方法,但除非我用ints
指定,否则我无法让它坐在我想要的位置。我试图按照另一个指南覆盖getXoffset()
,但我不能在我的班级中使用此方法。这可能吗?
@Override
public void draw(Canvas canvas, float posX, float posY) {
posX = 400;
posY = -30;
canvas.translate(posX,posY);
draw(canvas);
canvas.translate(-posX,-posY);
}
}
完整碎片代码
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);
}
}
答案 0 :(得分:0)
也许您可以使用:
posX=Resources.getSystem().getDisplayMetrics().widthPixels*ratio1
posY=Resources.getSystem().getDisplayMetrics().heightPixels*ratio2
Resources.getSystem().getDisplayMetrics()
将为您提供屏幕的大小,然后您可以选择任何比例来设置位置。