Search in sources :

Example 11 with IAxisValueFormatter

use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project MPAndroidChart by PhilJay.

the class BarChartActivityMultiDataset 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_barchart);
    setTitle("BarChartActivityMultiDataset");
    tvX = findViewById(R.id.tvXMax);
    tvX.setTextSize(10);
    tvY = findViewById(R.id.tvYMax);
    seekBarX = findViewById(R.id.seekBar1);
    seekBarX.setMax(50);
    seekBarX.setOnSeekBarChangeListener(this);
    seekBarY = findViewById(R.id.seekBar2);
    seekBarY.setOnSeekBarChangeListener(this);
    chart = findViewById(R.id.chart1);
    chart.setOnChartValueSelectedListener(this);
    chart.getDescription().setEnabled(false);
    // chart.setDrawBorders(true);
    // scaling can now only be done on x- and y-axis separately
    chart.setPinchZoom(false);
    chart.setDrawBarShadow(false);
    chart.setDrawGridBackground(false);
    // create a custom MarkerView (extend MarkerView) and specify the layout
    // to use for it
    MyMarkerView mv = new MyMarkerView(this, R.layout.custom_marker_view);
    // For bounds control
    mv.setChartView(chart);
    // Set the marker to the chart
    chart.setMarker(mv);
    seekBarX.setProgress(10);
    seekBarY.setProgress(100);
    Legend l = chart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(true);
    l.setTypeface(tfLight);
    l.setYOffset(0f);
    l.setXOffset(10f);
    l.setYEntrySpace(0f);
    l.setTextSize(8f);
    XAxis xAxis = chart.getXAxis();
    xAxis.setTypeface(tfLight);
    xAxis.setGranularity(1f);
    xAxis.setCenterAxisLabels(true);
    xAxis.setValueFormatter(new IAxisValueFormatter() {

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return String.valueOf((int) value);
        }
    });
    YAxis leftAxis = chart.getAxisLeft();
    leftAxis.setTypeface(tfLight);
    leftAxis.setValueFormatter(new LargeValueFormatter());
    leftAxis.setDrawGridLines(false);
    leftAxis.setSpaceTop(35f);
    // this replaces setStartAtZero(true)
    leftAxis.setAxisMinimum(0f);
    chart.getAxisRight().setEnabled(false);
}
Also used : Legend(com.github.mikephil.charting.components.Legend) LargeValueFormatter(com.github.mikephil.charting.formatter.LargeValueFormatter) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) AxisBase(com.github.mikephil.charting.components.AxisBase) MyMarkerView(com.xxmassdeveloper.mpchartexample.custom.MyMarkerView) XAxis(com.github.mikephil.charting.components.XAxis) YAxis(com.github.mikephil.charting.components.YAxis)

Example 12 with IAxisValueFormatter

use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project kcanotify by antest1.

the class KcaResoureLogFragment method drawChart.

public void drawChart(View v) {
    View chart_area = v.findViewById(R.id.reslog_chart_area);
    if (ResourceLogActivity.getChartHiddenState()) {
        chart_area.setVisibility(View.GONE);
        return;
    } else {
        chart_area.setVisibility(View.VISIBLE);
    }
    if (resourceData.size() <= 0)
        return;
    LineChart chart = v.findViewById(R.id.reslog_chart);
    final int[] color_table = color_data[position];
    final String[] key_list = data_key[position];
    int interval_value = interval[position];
    int y_count = 7;
    List<ILineDataSet> lines = new ArrayList<>();
    int max_value = 0;
    int min_value = maximum[position];
    for (int k = 0; k < 4; k++) {
        String key = key_list[k];
        List<Entry> data_entries = new ArrayList<>();
        for (JsonObject data : resourceData) {
            long time = data.get("timestamp").getAsLong();
            int value = data.get(key).getAsInt();
            data_entries.add(new Entry(time, value));
            if (max_value < value)
                max_value = value;
            if (min_value > value)
                min_value = value;
        }
        Collections.sort(data_entries, new EntryXComparator());
        LineDataSet dataset = new LineDataSet(data_entries, "data1");
        dataset.setDrawValues(false);
        int color = ContextCompat.getColor(getContext(), color_table[k]);
        dataset.setColor(color);
        dataset.setCircleColor(color);
        dataset.setVisible(is_draw_enabled[k]);
        dataset.setHighlightEnabled(false);
        lines.add(dataset);
    }
    max_value = (int) Math.ceil(max_value / (float) interval_value) * interval_value;
    min_value = (int) Math.floor(min_value / (float) interval_value) * interval_value;
    int range = max_value - min_value;
    while (range % (y_count - 1) != 0) y_count -= 1;
    Log.e("KCA", KcaUtils.format("%d~%d / %d", min_value, max_value, range));
    IAxisValueFormatter formatter = new IAxisValueFormatter() {

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(xaxis_format, Locale.US);
            return dateFormat.format(new Date((long) value));
        }
    };
    LineData data = new LineData(lines);
    chart.setData(data);
    XAxis xAxis = chart.getXAxis();
    xAxis.setValueFormatter(formatter);
    // xAxis.setGranularity(xaxis_interval);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    // xAxis.setLabelCount(8, true);
    setChartYRange(chart, max_value, min_value, y_count, interval_value);
    chart.getDescription().setEnabled(false);
    chart.getLegend().setEnabled(false);
    chart.notifyDataSetChanged();
    chart.invalidate();
}
Also used : EntryXComparator(com.github.mikephil.charting.utils.EntryXComparator) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) LineDataSet(com.github.mikephil.charting.data.LineDataSet) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) AxisBase(com.github.mikephil.charting.components.AxisBase) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) Date(java.util.Date) XAxis(com.github.mikephil.charting.components.XAxis) Entry(com.github.mikephil.charting.data.Entry) LineData(com.github.mikephil.charting.data.LineData) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) SimpleDateFormat(java.text.SimpleDateFormat) LineChart(com.github.mikephil.charting.charts.LineChart)

Example 13 with IAxisValueFormatter

use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project Osmand by osmandapp.

the class GpxUiHelper method createGPXElevationDataSet.

public static OrderedLineDataSet createGPXElevationDataSet(OsmandApplication ctx, LineChart mChart, GPXTrackAnalysis analysis, GPXDataSetAxisType axisType, boolean useRightAxis, boolean drawFilled) {
    OsmandSettings settings = ctx.getSettings();
    OsmandSettings.MetricsConstants mc = settings.METRIC_SYSTEM.get();
    boolean useFeet = (mc == OsmandSettings.MetricsConstants.MILES_AND_FEET) || (mc == OsmandSettings.MetricsConstants.MILES_AND_YARDS);
    boolean light = settings.isLightContent();
    final float convEle = useFeet ? 3.28084f : 1.0f;
    float divX;
    XAxis xAxis = mChart.getXAxis();
    if (axisType == GPXDataSetAxisType.TIME && analysis.isTimeSpecified()) {
        divX = setupXAxisTime(xAxis, analysis.timeSpan);
    } else {
        divX = setupXAxisDistance(ctx, xAxis, analysis.totalDistance);
    }
    final String mainUnitY = useFeet ? ctx.getString(R.string.foot) : ctx.getString(R.string.m);
    YAxis yAxis;
    if (useRightAxis) {
        yAxis = mChart.getAxisRight();
        yAxis.setEnabled(true);
    } else {
        yAxis = mChart.getAxisLeft();
    }
    yAxis.setTextColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_blue_label));
    yAxis.setGridColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_blue_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 = calculateElevationArray(analysis, axisType, divX, convEle);
    OrderedLineDataSet dataSet = new OrderedLineDataSet(values, "", GPXDataSetType.ALTITUDE, axisType);
    dataSet.priority = (float) (analysis.avgElevation - analysis.minElevation) * convEle;
    dataSet.divX = divX;
    dataSet.mulY = convEle;
    dataSet.divY = Float.NaN;
    dataSet.units = mainUnitY;
    dataSet.setColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_blue));
    dataSet.setLineWidth(1f);
    if (drawFilled) {
        dataSet.setFillAlpha(128);
        dataSet.setFillColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_blue));
        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.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
    dataSet.setFillFormatter(new IFillFormatter() {

        @Override
        public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
            return dataProvider.getYChartMin();
        }
    });
    if (useRightAxis) {
        dataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
    }
    return dataSet;
}
Also used : IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) SpannableString(android.text.SpannableString) AxisBase(com.github.mikephil.charting.components.AxisBase) IFillFormatter(com.github.mikephil.charting.formatter.IFillFormatter) OsmandSettings(net.osmand.plus.OsmandSettings) XAxis(com.github.mikephil.charting.components.XAxis) Entry(com.github.mikephil.charting.data.Entry) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) LineDataProvider(com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider) YAxis(com.github.mikephil.charting.components.YAxis)

Example 14 with IAxisValueFormatter

use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project Osmand by osmandapp.

the class GpxUiHelper method createGPXSpeedDataSet.

public static OrderedLineDataSet createGPXSpeedDataSet(OsmandApplication ctx, LineChart mChart, GPXTrackAnalysis analysis, GPXDataSetAxisType axisType, boolean useRightAxis, boolean drawFilled) {
    OsmandSettings settings = ctx.getSettings();
    boolean light = settings.isLightContent();
    float divX;
    XAxis xAxis = mChart.getXAxis();
    if (axisType == GPXDataSetAxisType.TIME && analysis.isTimeSpecified()) {
        divX = setupXAxisTime(xAxis, analysis.timeSpan);
    } else {
        divX = setupXAxisDistance(ctx, xAxis, analysis.totalDistance);
    }
    OsmandSettings.SpeedConstants sps = settings.SPEED_SYSTEM.get();
    float mulSpeed = Float.NaN;
    float divSpeed = Float.NaN;
    final String mainUnitY = sps.toShortString(ctx);
    if (sps == OsmandSettings.SpeedConstants.KILOMETERS_PER_HOUR) {
        mulSpeed = 3.6f;
    } else if (sps == OsmandSettings.SpeedConstants.MILES_PER_HOUR) {
        mulSpeed = 3.6f * METERS_IN_KILOMETER / METERS_IN_ONE_MILE;
    } else if (sps == OsmandSettings.SpeedConstants.NAUTICALMILES_PER_HOUR) {
        mulSpeed = 3.6f * METERS_IN_KILOMETER / METERS_IN_ONE_NAUTICALMILE;
    } else if (sps == OsmandSettings.SpeedConstants.MINUTES_PER_KILOMETER) {
        divSpeed = METERS_IN_KILOMETER / 60;
    } else if (sps == OsmandSettings.SpeedConstants.MINUTES_PER_MILE) {
        divSpeed = METERS_IN_ONE_MILE / 60;
    } else {
        mulSpeed = 1f;
    }
    YAxis yAxis;
    if (useRightAxis) {
        yAxis = mChart.getAxisRight();
        yAxis.setEnabled(true);
    } else {
        yAxis = mChart.getAxisLeft();
    }
    if (analysis.hasSpeedInTrack()) {
        yAxis.setTextColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_orange_label));
        yAxis.setGridColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_orange_grid));
    } else {
        yAxis.setTextColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_red_label));
        yAxis.setGridColor(ActivityCompat.getColor(mChart.getContext(), R.color.gpx_chart_red_grid));
    }
    yAxis.setAxisMinimum(0f);
    ArrayList<Entry> values = new ArrayList<>();
    List<Speed> speedData = analysis.speedData;
    float nextX = 0;
    float nextY;
    float x;
    for (Speed s : speedData) {
        x = axisType == GPXDataSetAxisType.TIME ? s.time : s.distance;
        if (x > 0) {
            if (axisType == GPXDataSetAxisType.TIME && x > 60) {
                values.add(new Entry(nextX + 1, 0));
                values.add(new Entry(nextX + x - 1, 0));
            }
            nextX += x / divX;
            if (Float.isNaN(divSpeed)) {
                nextY = s.speed * mulSpeed;
            } else {
                nextY = divSpeed / s.speed;
            }
            if (nextY < 0 || Float.isInfinite(nextY)) {
                nextY = 0;
            }
            values.add(new Entry(nextX, nextY));
        }
    }
    OrderedLineDataSet dataSet = new OrderedLineDataSet(values, "", GPXDataSetType.SPEED, axisType);
    String format = null;
    if (dataSet.getYMax() < 3) {
        format = "{0,number,0.#} ";
    }
    final String formatY = format;
    yAxis.setValueFormatter(new IAxisValueFormatter() {

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            if (!Algorithms.isEmpty(formatY)) {
                return MessageFormat.format(formatY + mainUnitY, value);
            } else {
                return (int) value + " " + mainUnitY;
            }
        }
    });
    if (Float.isNaN(divSpeed)) {
        dataSet.priority = analysis.avgSpeed * mulSpeed;
    } else {
        dataSet.priority = divSpeed / analysis.avgSpeed;
    }
    dataSet.divX = divX;
    if (Float.isNaN(divSpeed)) {
        dataSet.mulY = mulSpeed;
        dataSet.divY = Float.NaN;
    } else {
        dataSet.divY = divSpeed;
        dataSet.mulY = Float.NaN;
    }
    dataSet.units = mainUnitY;
    if (analysis.hasSpeedInTrack()) {
        dataSet.setColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_orange));
    } else {
        dataSet.setColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_red));
    }
    dataSet.setLineWidth(1f);
    if (drawFilled) {
        dataSet.setFillAlpha(128);
        if (analysis.hasSpeedInTrack()) {
            dataSet.setFillColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_orange));
        } else {
            dataSet.setFillColor(ContextCompat.getColor(mChart.getContext(), R.color.gpx_chart_red));
        }
        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));
    if (useRightAxis) {
        dataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
    }
    return dataSet;
}
Also used : Speed(net.osmand.plus.GPXUtilities.Speed) ArrayList(java.util.ArrayList) IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) SpannableString(android.text.SpannableString) AxisBase(com.github.mikephil.charting.components.AxisBase) OsmandSettings(net.osmand.plus.OsmandSettings) XAxis(com.github.mikephil.charting.components.XAxis) Entry(com.github.mikephil.charting.data.Entry) YAxis(com.github.mikephil.charting.components.YAxis)

Example 15 with IAxisValueFormatter

use of com.github.mikephil.charting.formatter.IAxisValueFormatter in project Osmand by osmandapp.

the class GpxUiHelper method setupXAxisDistance.

private static float setupXAxisDistance(OsmandApplication ctx, XAxis xAxis, float meters) {
    OsmandSettings settings = ctx.getSettings();
    OsmandSettings.MetricsConstants mc = settings.METRIC_SYSTEM.get();
    float divX;
    String format1 = "{0,number,0.#} ";
    String format2 = "{0,number,0.##} ";
    String fmt = null;
    float granularity = 1f;
    int mainUnitStr;
    float mainUnitInMeters;
    if (mc == OsmandSettings.MetricsConstants.KILOMETERS_AND_METERS) {
        mainUnitStr = R.string.km;
        mainUnitInMeters = METERS_IN_KILOMETER;
    } else if (mc == OsmandSettings.MetricsConstants.NAUTICAL_MILES) {
        mainUnitStr = R.string.nm;
        mainUnitInMeters = METERS_IN_ONE_NAUTICALMILE;
    } else {
        mainUnitStr = R.string.mile;
        mainUnitInMeters = METERS_IN_ONE_MILE;
    }
    if (meters > 9.99f * mainUnitInMeters) {
        fmt = format1;
        granularity = .1f;
    }
    if (meters >= 100 * mainUnitInMeters || meters > 9.99f * mainUnitInMeters || meters > 0.999f * mainUnitInMeters || mc == OsmandSettings.MetricsConstants.MILES_AND_FEET && meters > 0.249f * mainUnitInMeters || mc == OsmandSettings.MetricsConstants.MILES_AND_METERS && meters > 0.249f * mainUnitInMeters || mc == OsmandSettings.MetricsConstants.MILES_AND_YARDS && meters > 0.249f * mainUnitInMeters || mc == OsmandSettings.MetricsConstants.NAUTICAL_MILES && meters > 0.99f * mainUnitInMeters) {
        divX = mainUnitInMeters;
        if (fmt == null) {
            fmt = format2;
            granularity = .01f;
        }
    } else {
        fmt = null;
        granularity = 1f;
        if (mc == OsmandSettings.MetricsConstants.KILOMETERS_AND_METERS || mc == OsmandSettings.MetricsConstants.MILES_AND_METERS) {
            divX = 1f;
            mainUnitStr = R.string.m;
        } else if (mc == OsmandSettings.MetricsConstants.MILES_AND_FEET) {
            divX = 1f / FEET_IN_ONE_METER;
            mainUnitStr = R.string.foot;
        } else if (mc == OsmandSettings.MetricsConstants.MILES_AND_YARDS) {
            divX = 1f / YARDS_IN_ONE_METER;
            mainUnitStr = R.string.yard;
        } else {
            divX = 1f;
            mainUnitStr = R.string.m;
        }
    }
    final String formatX = fmt;
    final String mainUnitX = ctx.getString(mainUnitStr);
    xAxis.setGranularity(granularity);
    xAxis.setValueFormatter(new IAxisValueFormatter() {

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            if (!Algorithms.isEmpty(formatX)) {
                return MessageFormat.format(formatX + mainUnitX, value);
            } else {
                return (int) value + " " + mainUnitX;
            }
        }
    });
    return divX;
}
Also used : IAxisValueFormatter(com.github.mikephil.charting.formatter.IAxisValueFormatter) SpannableString(android.text.SpannableString) AxisBase(com.github.mikephil.charting.components.AxisBase) OsmandSettings(net.osmand.plus.OsmandSettings) SuppressLint(android.annotation.SuppressLint)

Aggregations

IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)16 AxisBase (com.github.mikephil.charting.components.AxisBase)15 XAxis (com.github.mikephil.charting.components.XAxis)13 YAxis (com.github.mikephil.charting.components.YAxis)11 Legend (com.github.mikephil.charting.components.Legend)7 ArrayList (java.util.ArrayList)7 SpannableString (android.text.SpannableString)5 Entry (com.github.mikephil.charting.data.Entry)5 OsmandSettings (net.osmand.plus.OsmandSettings)4 BarData (com.github.mikephil.charting.data.BarData)3 LineData (com.github.mikephil.charting.data.LineData)3 ILineDataSet (com.github.mikephil.charting.interfaces.datasets.ILineDataSet)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 SuppressLint (android.annotation.SuppressLint)2 TextView (android.widget.TextView)2 MarkerView (com.github.mikephil.charting.components.MarkerView)2 LineDataSet (com.github.mikephil.charting.data.LineDataSet)2 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)2 RadarMarkerView (com.xxmassdeveloper.mpchartexample.custom.RadarMarkerView)2