Create example for XYMarkerView (#2029)
This commit is contained in:
parent
e000c42744
commit
3ea1db57ab
3 changed files with 58 additions and 4 deletions
|
@ -37,6 +37,7 @@ import com.github.mikephil.charting.utils.ColorTemplate;
|
|||
import com.github.mikephil.charting.utils.MPPointF;
|
||||
import com.xxmassdeveloper.mpchartexample.custom.DayAxisValueFormatter;
|
||||
import com.xxmassdeveloper.mpchartexample.custom.MyAxisValueFormatter;
|
||||
import com.xxmassdeveloper.mpchartexample.custom.XYMarkerView;
|
||||
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -79,13 +80,15 @@ public class BarChartActivity extends DemoBase implements OnSeekBarChangeListene
|
|||
mChart.setDrawGridBackground(false);
|
||||
// mChart.setDrawYLabels(false);
|
||||
|
||||
AxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(mChart);
|
||||
|
||||
XAxis xAxis = mChart.getXAxis();
|
||||
xAxis.setPosition(XAxisPosition.BOTTOM);
|
||||
xAxis.setTypeface(mTfLight);
|
||||
xAxis.setDrawGridLines(false);
|
||||
xAxis.setGranularity(1f); // only intervals of 1 day
|
||||
xAxis.setLabelCount(7);
|
||||
xAxis.setValueFormatter(new DayAxisValueFormatter(mChart));
|
||||
xAxis.setValueFormatter(xAxisFormatter);
|
||||
|
||||
AxisValueFormatter custom = new MyAxisValueFormatter();
|
||||
|
||||
|
@ -116,6 +119,8 @@ public class BarChartActivity extends DemoBase implements OnSeekBarChangeListene
|
|||
// l.setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
|
||||
// "def", "ghj", "ikl", "mno" });
|
||||
|
||||
mChart.setMarkerView(new XYMarkerView(this, xAxisFormatter));
|
||||
|
||||
setData(12, 50);
|
||||
|
||||
// setting data
|
||||
|
|
|
@ -32,7 +32,7 @@ public class DayAxisValueFormatter implements AxisValueFormatter {
|
|||
String monthName = mMonths[month % mMonths.length];
|
||||
String yearName = String.valueOf(year);
|
||||
|
||||
if (chart.getVisibleXRange() > 30 * axis.getLabelCount()) {
|
||||
if (chart.getVisibleXRange() > 30 * 6) {
|
||||
|
||||
return monthName + " " + yearName;
|
||||
} else {
|
||||
|
|
|
@ -1,7 +1,56 @@
|
|||
|
||||
package com.xxmassdeveloper.mpchartexample.custom;
|
||||
|
||||
import android.content.Context;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.github.mikephil.charting.components.MarkerView;
|
||||
import com.github.mikephil.charting.data.CandleEntry;
|
||||
import com.github.mikephil.charting.data.Entry;
|
||||
import com.github.mikephil.charting.formatter.AxisValueFormatter;
|
||||
import com.github.mikephil.charting.highlight.Highlight;
|
||||
import com.github.mikephil.charting.utils.Utils;
|
||||
import com.xxmassdeveloper.mpchartexample.R;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
/**
|
||||
* Created by philipp on 16/07/16.
|
||||
* Custom implementation of the MarkerView.
|
||||
*
|
||||
* @author Philipp Jahoda
|
||||
*/
|
||||
public class XYMarkerView {
|
||||
public class XYMarkerView extends MarkerView {
|
||||
|
||||
private TextView tvContent;
|
||||
private AxisValueFormatter xAxisValueFormatter;
|
||||
|
||||
private DecimalFormat format;
|
||||
|
||||
public XYMarkerView(Context context, AxisValueFormatter xAxisValueFormatter) {
|
||||
super(context, R.layout.custom_marker_view);
|
||||
|
||||
this.xAxisValueFormatter = xAxisValueFormatter;
|
||||
tvContent = (TextView) findViewById(R.id.tvContent);
|
||||
format = new DecimalFormat("###.0");
|
||||
}
|
||||
|
||||
// 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("x: " + xAxisValueFormatter.getFormattedValue(e.getX(), null) + ", y: " + format.format(e.getY()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getXOffset(float xpos) {
|
||||
// this will center the marker-view horizontally
|
||||
return -(getWidth() / 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getYOffset(float ypos) {
|
||||
// this will cause the marker-view to be above the selected value
|
||||
return -getHeight();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue