use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project MPAndroidChart by PhilJay.
the class RadarChartActivitry method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_radarchart_noseekbar);
TextView tv = (TextView) findViewById(R.id.textView);
tv.setTypeface(mTfLight);
tv.setTextColor(Color.WHITE);
tv.setBackgroundColor(Color.rgb(60, 65, 82));
mChart = (RadarChart) findViewById(R.id.chart1);
mChart.setBackgroundColor(Color.rgb(60, 65, 82));
mChart.getDescription().setEnabled(false);
mChart.setWebLineWidth(1f);
mChart.setWebColor(Color.LTGRAY);
mChart.setWebLineWidthInner(1f);
mChart.setWebColorInner(Color.LTGRAY);
mChart.setWebAlpha(100);
// create a custom MarkerView (extend MarkerView) and specify the layout
// to use for it
MarkerView mv = new RadarMarkerView(this, R.layout.radar_markerview);
// For bounds control
mv.setChartView(mChart);
// Set the marker to the chart
mChart.setMarker(mv);
setData();
mChart.animateXY(1400, 1400, Easing.EasingOption.EaseInOutQuad, Easing.EasingOption.EaseInOutQuad);
XAxis xAxis = mChart.getXAxis();
xAxis.setTypeface(mTfLight);
xAxis.setTextSize(9f);
xAxis.setYOffset(0f);
xAxis.setXOffset(0f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
private String[] mActivities = new String[] { "Burger", "Steak", "Salad", "Pasta", "Pizza" };
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mActivities[(int) value % mActivities.length];
}
});
xAxis.setTextColor(Color.WHITE);
YAxis yAxis = mChart.getYAxis();
yAxis.setTypeface(mTfLight);
yAxis.setLabelCount(5, false);
yAxis.setTextSize(9f);
yAxis.setAxisMinimum(0f);
yAxis.setAxisMaximum(80f);
yAxis.setDrawLabels(false);
Legend l = mChart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(false);
l.setTypeface(mTfLight);
l.setXEntrySpace(7f);
l.setYEntrySpace(5f);
l.setTextColor(Color.WHITE);
}
use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project MPAndroidChart by PhilJay.
the class RealmWikiExample method setData.
private void setData() {
// LINE-CHART
final RealmResults<Score> results = mRealm.where(Score.class).findAll();
IAxisValueFormatter formatter = new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return results.get((int) value).getPlayerName();
}
};
lineChart.getXAxis().setValueFormatter(formatter);
barChart.getXAxis().setValueFormatter(formatter);
RealmLineDataSet<Score> lineDataSet = new RealmLineDataSet<Score>(results, "scoreNr", "totalScore");
lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
lineDataSet.setLabel("Result Scores");
lineDataSet.setDrawCircleHole(false);
lineDataSet.setColor(ColorTemplate.rgb("#FF5722"));
lineDataSet.setCircleColor(ColorTemplate.rgb("#FF5722"));
lineDataSet.setLineWidth(1.8f);
lineDataSet.setCircleRadius(3.6f);
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(lineDataSet);
LineData lineData = new LineData(dataSets);
styleData(lineData);
// set data
lineChart.setData(lineData);
lineChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
// BAR-CHART
RealmBarDataSet<Score> barDataSet = new RealmBarDataSet<Score>(results, "scoreNr", "totalScore");
barDataSet.setColors(new int[] { ColorTemplate.rgb("#FF5722"), ColorTemplate.rgb("#03A9F4") });
barDataSet.setLabel("Realm BarDataSet");
ArrayList<IBarDataSet> barDataSets = new ArrayList<IBarDataSet>();
barDataSets.add(barDataSet);
BarData barData = new BarData(barDataSets);
styleData(barData);
barChart.setData(barData);
barChart.setFitBars(true);
barChart.animateY(1400, Easing.EasingOption.EaseInOutQuart);
}
use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project AmazMod by edotassi.
the class BatteryChartCard method updateChart.
private void updateChart() {
final List<Entry> yValues = new ArrayList<Entry>();
final List<Integer> colors = new ArrayList<>();
final int days = PreferenceManager.getInt(getContext(), Constants.PREFERENCE_BATTERY_CHART_RANGE, 5);
Calendar calendar = Calendar.getInstance();
long highX = calendar.getTimeInMillis();
calendar.add(Calendar.DATE, -1 * days);
long lowX = calendar.getTimeInMillis();
List<BatteryRead> batteryReadList = SQLite.select().from(BatteryRead.class).where(BatteryRead_Table.date.greaterThan(lowX)).queryList();
BatteryRead prevRead = null;
for (int i = 0; i < batteryReadList.size(); i++) {
BatteryRead read = batteryReadList.get(i);
if ((read.getLevel() > 0) && ((prevRead == null) || (read.getLevel() != prevRead.getLevel()))) {
Entry entry = new Entry(read.getDate(), read.getLevel());
yValues.add(entry);
colors.add(Color.parseColor(read.isCharging() ? "#00E676" : "#3F51B5"));
}
prevRead = read;
}
if (yValues.size() == 0) {
return;
}
LineDataSet lineDataSet = new LineDataSet(yValues, "Battery");
lineDataSet.setLineWidth(1.5f);
lineDataSet.setDrawCircleHole(false);
lineDataSet.setDrawCircles(false);
lineDataSet.setDrawValues(false);
Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.fade_blue_battery);
lineDataSet.setDrawFilled(true);
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet.setFillDrawable(drawable);
lineDataSet.setColors(colors);
lineDataSet.setMode(LineDataSet.Mode.LINEAR);
lineDataSet.setCubicIntensity(0.05f);
Description description = new Description();
description.setText("");
chart.setDescription(description);
XAxis xAxis = chart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setLabelRotationAngle(-45);
xAxis.setTextSize(8);
xAxis.setAxisMinimum(lowX);
xAxis.setAxisMaximum(highX);
final Calendar now = Calendar.getInstance();
final SimpleDateFormat simpleDateFormatHours = new SimpleDateFormat("HH");
final SimpleDateFormat simpleDateFormatHoursMinutes = new SimpleDateFormat("HH:mm");
final SimpleDateFormat simpleDateFormatDateMonth = new SimpleDateFormat("dd/MM");
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis((long) value);
Date date = calendar.getTime();
if ((days > 1) || (now.get(Calendar.DATE) != calendar.get(Calendar.DATE))) {
int minutes = calendar.get(Calendar.MINUTE);
if (minutes > 30) {
calendar.add(Calendar.HOUR, 1);
}
return simpleDateFormatHours.format(date) + "\n" + simpleDateFormatDateMonth.format(date);
} else {
return simpleDateFormatHoursMinutes.format(calendar.getTime()) + "\n" + simpleDateFormatDateMonth.format(calendar.getTime());
}
}
});
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setDrawAxisLine(true);
leftAxis.setDrawZeroLine(false);
leftAxis.setDrawGridLines(true);
leftAxis.setAxisMinimum(0);
leftAxis.setAxisMaximum(100);
chart.getAxisRight().setEnabled(false);
chart.getLegend().setEnabled(false);
chart.setTouchEnabled(false);
chart.setXAxisRenderer(new CustomXAxisRenderer(chart.getViewPortHandler(), chart.getXAxis(), chart.getTransformer(YAxis.AxisDependency.LEFT)));
LineData lineData = new LineData(lineDataSet);
chart.setData(lineData);
chart.invalidate();
}
use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project Osmand by osmandapp.
the class GpxUiHelper method createGPXSlopeDataSet.
public static OrderedLineDataSet createGPXSlopeDataSet(OsmandApplication ctx, LineChart mChart, GPXTrackAnalysis analysis, GPXDataSetAxisType axisType, List<Entry> eleValues, boolean useRightAxis, boolean drawFilled) {
if (axisType == GPXDataSetAxisType.TIME) {
return null;
}
OsmandSettings settings = ctx.getSettings();
boolean light = settings.isLightContent();
OsmandSettings.MetricsConstants mc = settings.METRIC_SYSTEM.get();
boolean useFeet = (mc == OsmandSettings.MetricsConstants.MILES_AND_FEET) || (mc == OsmandSettings.MetricsConstants.MILES_AND_YARDS);
final float convEle = useFeet ? 3.28084f : 1.0f;
final float totalDistance = analysis.totalDistance;
XAxis xAxis = mChart.getXAxis();
float divX = setupXAxisDistance(ctx, xAxis, analysis.totalDistance);
final String mainUnitY = "%";
YAxis yAxis;
if (useRightAxis) {
yAxis = mChart.getAxisRight();
yAxis.setEnabled(true);
} else {
yAxis = mChart.getAxisLeft();
}
yAxis.setTextColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_green_label));
yAxis.setGridColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_green_grid));
yAxis.setGranularity(1f);
yAxis.resetAxisMinimum();
yAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
return (int) value + " " + mainUnitY;
}
});
List<Entry> values;
if (eleValues == null) {
values = calculateElevationArray(analysis, GPXDataSetAxisType.DISTANCE, 1f, 1f);
} else {
values = new ArrayList<>(eleValues.size());
for (Entry e : eleValues) {
values.add(new Entry(e.getX() * divX, e.getY() / convEle));
}
}
if (values == null || values.size() == 0) {
if (useRightAxis) {
yAxis.setEnabled(false);
}
return null;
}
int lastIndex = values.size() - 1;
double STEP = 5;
double[] calculatedDist = new double[(int) (totalDistance / STEP) + 1];
double[] calculatedH = new double[(int) (totalDistance / STEP) + 1];
int nextW = 0;
for (int k = 0; k < calculatedDist.length; k++) {
if (k > 0) {
calculatedDist[k] = calculatedDist[k - 1] + STEP;
}
while (nextW < lastIndex && calculatedDist[k] > values.get(nextW).getX()) {
nextW++;
}
double pd = nextW == 0 ? 0 : values.get(nextW - 1).getX();
double ph = nextW == 0 ? values.get(0).getY() : values.get(nextW - 1).getY();
calculatedH[k] = ph + (values.get(nextW).getY() - ph) / (values.get(nextW).getX() - pd) * (calculatedDist[k] - pd);
}
double SLOPE_PROXIMITY = 100;
if (totalDistance - SLOPE_PROXIMITY < 0) {
if (useRightAxis) {
yAxis.setEnabled(false);
}
return null;
}
double[] calculatedSlopeDist = new double[(int) ((totalDistance - SLOPE_PROXIMITY) / STEP) + 1];
double[] calculatedSlope = new double[(int) ((totalDistance - SLOPE_PROXIMITY) / STEP) + 1];
int index = (int) ((SLOPE_PROXIMITY / STEP) / 2);
for (int k = 0; k < calculatedSlopeDist.length; k++) {
calculatedSlopeDist[k] = calculatedDist[index + k];
calculatedSlope[k] = (calculatedH[2 * index + k] - calculatedH[k]) * 100 / SLOPE_PROXIMITY;
if (Double.isNaN(calculatedSlope[k])) {
calculatedSlope[k] = 0;
}
}
List<Entry> slopeValues = new ArrayList<>(calculatedSlopeDist.length);
float prevSlope = -80000;
float slope;
float x;
float lastXSameY = 0;
boolean hasSameY = false;
Entry lastEntry = null;
lastIndex = calculatedSlopeDist.length - 1;
for (int i = 0; i < calculatedSlopeDist.length; i++) {
x = (float) calculatedSlopeDist[i] / divX;
slope = (float) calculatedSlope[i];
if (prevSlope != -80000) {
if (prevSlope == slope && i < lastIndex) {
hasSameY = true;
lastXSameY = x;
continue;
}
if (hasSameY) {
slopeValues.add(new Entry(lastXSameY, lastEntry.getY()));
}
hasSameY = false;
}
prevSlope = slope;
lastEntry = new Entry(x, slope);
slopeValues.add(lastEntry);
}
OrderedLineDataSet dataSet = new OrderedLineDataSet(slopeValues, "", GPXDataSetType.SLOPE, axisType);
dataSet.divX = divX;
dataSet.units = mainUnitY;
dataSet.setColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_green));
dataSet.setLineWidth(1f);
if (drawFilled) {
dataSet.setFillAlpha(128);
dataSet.setFillColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_green));
dataSet.setDrawFilled(true);
} else {
dataSet.setDrawFilled(false);
}
dataSet.setDrawValues(false);
dataSet.setValueTextSize(9f);
dataSet.setFormLineWidth(1f);
dataSet.setFormSize(15.f);
dataSet.setDrawCircles(false);
dataSet.setDrawCircleHole(false);
dataSet.setHighlightEnabled(true);
dataSet.setDrawVerticalHighlightIndicator(true);
dataSet.setDrawHorizontalHighlightIndicator(false);
dataSet.setHighLightColor(light ? mChart.getResources().getColor(R.color.secondary_text_light) : mChart.getResources().getColor(R.color.secondary_text_dark));
/*
dataSet.setFillFormatter(new IFillFormatter() {
@Override
public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
return dataProvider.getYChartMin();
}
});
*/
if (useRightAxis) {
dataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
}
return dataSet;
}
use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project Osmand by osmandapp.
the class GpxUiHelper method setupXAxisTime.
private static float setupXAxisTime(XAxis xAxis, long timeSpan) {
final boolean useHours = timeSpan / 3600000 > 0;
xAxis.setGranularity(1f);
xAxis.setValueFormatter(new IAxisValueFormatter() {
@Override
public String getFormattedValue(float value, AxisBase axis) {
int seconds = (int) value;
if (useHours) {
int hours = seconds / (60 * 60);
int minutes = (seconds / 60) % 60;
int sec = seconds % 60;
return hours + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (sec < 10 ? "0" + sec : sec);
} else {
int minutes = (seconds / 60) % 60;
int sec = seconds % 60;
return (minutes < 10 ? "0" + minutes : minutes) + ":" + (sec < 10 ? "0" + sec : sec);
}
}
});
return 1f;
}
Aggregations