1
有沒有辦法給頂部添加填充\偏移量?看屏幕。數字是裁剪如果LimitLine
是在頂部:MPAndroidChart - 添加偏移頂部
編輯:源代碼
public class ChartManager implements
OnChartGestureListener, OnChartValueSelectedListener {
private LineChart mChart;
private Context mContext;
private float lastYValue;
int blueColor;
int tealColor;
int whiteColor;
public ChartManager(Context context, LineChart mChart) {
this.mContext = context;
tealColor = mContext.getResources().getColor(R.color.graph_stroke);
blueColor = mContext.getResources().getColor(R.color.primary_blue);
whiteColor = mContext.getResources().getColor(R.color.primary_white);
mChart.setTop(50);
mChart.setOnChartGestureListener(this);
mChart.setOnChartValueSelectedListener(this);
mChart.setDrawGridBackground(false);
mChart.setDescription(null);
mChart.setNoDataTextDescription("You need to provide data for the chart.");
mChart.setTouchEnabled(false);
mChart.setDragEnabled(false);
mChart.setScaleEnabled(false);
mChart.setPinchZoom(true);
mChart.setBorderColor(blueColor);
mChart.getLegend().setEnabled(false);
XAxis xAxis = mChart.getXAxis();
xAxis.setAxisLineColor(blueColor);
xAxis.setGridColor(blueColor);
xAxis.setTextColor(blueColor);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
mChart.getAxisRight().setEnabled(true);
mChart.getAxisLeft().setEnabled(false);
this.mChart = mChart;
}
public void setLimitLine(){
YAxis yAxis = mChart.getAxisRight();
yAxis.removeAllLimitLines();
yAxis.setAxisLineColor(blueColor);
yAxis.setGridColor(blueColor);
yAxis.setTextColor(blueColor);
LimitLine ll = new LimitLine(lastYValue, SingleAsset.round(lastYValue, 2).toString());
ll.setLineColor(tealColor);
ll.setLabelPosition(LimitLine.LimitLabelPosition.RIGHT_TOP);
ll.setLineWidth(2f);
ll.setTextColor(whiteColor);
ll.setTextSize(16f);
yAxis.addLimitLine(ll);
Legend l = mChart.getLegend();
l.setForm(Legend.LegendForm.LINE);
}
public void removeAllLimitLines(){
YAxis yAxis = mChart.getAxisRight();
yAxis.removeAllLimitLines();
}
public String parseHours(long millis){
return new SimpleDateFormat("hh:mm").format(new Date(millis));
}
public void setData(LineChart mChart, List<SingleAsset.Timestamp> timestamps) {
ArrayList<String> xVals = new ArrayList<>();
for (SingleAsset.Timestamp timestamp : timestamps){
xVals.add(parseHours(timestamp.getTimestamp()));
}
ArrayList<Entry> yVals = new ArrayList<>();
for (int i = 0; i < timestamps.size(); i++){
float value = (float) timestamps.get(i).getValue();
yVals.add(new Entry(value, i));
lastYValue = value;
}
// create a dataset and give it a type
LineDataSet lineDataSet = new LineDataSet(yVals, null);
lineDataSet.enableDashedHighlightLine(10f, 5f, 0f);
lineDataSet.setColor(mContext.getResources().getColor(R.color.primary_green));
lineDataSet.setLineWidth(1f);
lineDataSet.setCircleRadius(3f);
lineDataSet.setDrawCircleHole(false);
lineDataSet.setValueTextSize(9f);
Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.fade_green);
lineDataSet.setFillDrawable(drawable);
lineDataSet.setDrawFilled(true);
lineDataSet.setDrawValues(false);
lineDataSet.setDrawCircles(false);
lineDataSet.setDrawCubic(false);
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(lineDataSet); // update the datasets
// create a data object with the datasets
LineData lineData = new LineData(xVals, dataSets);
// set data
mChart.setData(lineData);
}
@Override
public void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
Log.i("Gesture", "START, x: " + me.getX() + ", y: " + me.getY());
}
@Override
public void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture) {
Log.i("Gesture", "END, lastGesture: " + lastPerformedGesture);
// un-highlight values after the gesture is finished and no single-tap
if (lastPerformedGesture != ChartTouchListener.ChartGesture.SINGLE_TAP)
mChart.highlightValues(null); // or highlightTouch(null) for callback to onNothingSelected(...)
}
@Override
public void onChartLongPressed(MotionEvent me) {
Log.i("LongPress", "Chart longpressed.");
}
@Override
public void onChartDoubleTapped(MotionEvent me) {
Log.i("DoubleTap", "Chart double-tapped.");
}
@Override
public void onChartSingleTapped(MotionEvent me) {
Log.i("SingleTap", "Chart single-tapped.");
}
@Override
public void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY) {
Log.i("Fling", "Chart flinged. VeloX: " + velocityX + ", VeloY: " + velocityY);
}
@Override
public void onChartScale(MotionEvent me, float scaleX, float scaleY) {
Log.i("Scale/Zoom", "ScaleX: " + scaleX + ", ScaleY: " + scaleY);
}
@Override
public void onChartTranslate(MotionEvent me, float dX, float dY) {
Log.i("Translate/Move", "dX: " + dX + ", dY: " + dY);
}
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
Log.i("Entry selected", e.toString());
Log.i("LOWHIGH", "low: " + mChart.getLowestVisibleXIndex() + ", high: " + mChart.getHighestVisibleXIndex());
Log.i("MIN MAX", "xmin: " + mChart.getXChartMin() + ", xmax: " + mChart.getXChartMax() + ", ymin: " + mChart.getYChartMin() + ", ymax: " + mChart.getYChartMax());
}
@Override
public void onNothingSelected() {
Log.i("Nothing selected", "Nothing selected.");
}
}
能否請您發表您的碼? – Dhruv
@Dhruv,對不起。請看編輯 – AnZ
http://stackoverflow.com/q/28563051/3817374你可以參考這個 – Beena