Java Code Examples for com.github.mikephil.charting.components.XAxis#setValueFormatter()

The following examples show how to use com.github.mikephil.charting.components.XAxis#setValueFormatter() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DailyBarChart.java    From AndroidApp with GNU Affero General Public License v3.0 6 votes vote down vote up
private void setFormatting() {
    barChart.setDrawGridBackground(false);
    barChart.getLegend().setEnabled(false);
    barChart.getAxisLeft().setEnabled(false);
    barChart.getAxisRight().setEnabled(false);
    barChart.setHardwareAccelerationEnabled(true);
    barChart.getDescription().setEnabled(false);
    barChart.setNoDataText("");
    barChart.setTouchEnabled(false);
    barChart.setExtraBottomOffset(2);

    XAxis xAxis2 = barChart.getXAxis();
    xAxis2.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis2.setTextColor(ContextCompat.getColor(context, R.color.lightGrey));
    xAxis2.setTextSize(context.getResources().getInteger(R.integer.chartValueTextSize));
    xAxis2.setDrawGridLines(false);
    xAxis2.setDrawAxisLine(false);
    xAxis2.setValueFormatter(new LabelAxisFormatter(chartLabels));
}
 
Example 2
Source File: PFAChart.java    From privacy-friendly-shopping-list with Apache License 2.0 6 votes vote down vote up
private void setXlabels(List<String> labelList)
{
    int valuesSelectedItemPos = cache.getValuesSpinner().getSelectedItemPosition();
    String[] labels = new String[ labelList.size() ];
    labelList.toArray(labels);

    PFAXAxisLabels xFormatter = new PFAXAxisLabels(labels);
    chart.setMarkerView(new PFAMarkerView(context, xFormatter, valuesSelectedItemPos, cache.getNumberScale()));

    XAxis xAxis = chart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawGridLines(false);
    xAxis.setGranularity(1f);
    xAxis.setLabelCount(5);
    xAxis.setValueFormatter(xFormatter);
}
 
Example 3
Source File: DailyBarChart.java    From AndroidApp with GNU Affero General Public License v3.0 6 votes vote down vote up
public void refreshChart(int start) {

        BarDataSet dataSet = (BarDataSet) barData.getDataSetByLabel("kWh", true);
        dataSet.clear();
        if (chartBarColours != null) {
            dataSet.setColors(chartBarColours);
        }

        for (int i = start; i < chartLabels.size(); i++) {
            barData.addEntry(new BarEntry(i, chartValues.get(i).floatValue()), 0);
        }
        XAxis xAxis2 = barChart.getXAxis();
        xAxis2.setValueFormatter(new LabelAxisFormatter(chartLabels));
        barChart.getXAxis().setLabelCount(chartLabels.size());
        notifyDataChanged();

    }
 
Example 4
Source File: RecordingsAdapter.java    From go-bees with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Setup chart (axis, grid, etc.).
 *
 * @param lineChart      chart to setup.
 * @param data           chart with the data.
 * @param firstTimestamp seconds timestamp of the first record (used as initial reference).
 */
private void setupChart(LineChart lineChart, LineData data, long firstTimestamp) {
    // General setup
    lineChart.setDrawGridBackground(false);
    lineChart.setDrawBorders(false);
    lineChart.setViewPortOffsets(50, 0, 50, 50);
    lineChart.getDescription().setEnabled(false);
    lineChart.getLegend().setEnabled(false);
    lineChart.setTouchEnabled(false);
    lineChart.setNoDataText(context.getString(R.string.no_flight_act_data_available));
    // X axis setup
    IAxisValueFormatter xAxisFormatter = new HourAxisValueFormatter(firstTimestamp);
    XAxis xAxis = lineChart.getXAxis();
    xAxis.setValueFormatter(xAxisFormatter);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(false);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setCenterAxisLabels(false);
    xAxis.setTextColor(ContextCompat.getColor(context, R.color.colorIcons));
    // Y axis setup
    YAxis yAxis = lineChart.getAxisLeft();
    yAxis.setAxisMaximum(40);
    yAxis.setAxisMinimum(0);
    yAxis.setDrawLabels(false);
    yAxis.setDrawAxisLine(false);
    yAxis.setDrawGridLines(true);
    lineChart.getAxisRight().setEnabled(false);
    // Add data
    lineChart.setData(data);
}
 
Example 5
Source File: BarGraph.java    From fastnfitness with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void draw(List<BarEntry> entries, ArrayList<String> xAxisLabel) {
    mChart.clear();
    if (entries.isEmpty()) {
        return;
    }

    XAxis xAxis = this.mChart.getXAxis();
    xAxis.setValueFormatter(new IndexAxisValueFormatter(xAxisLabel));

    Collections.sort(entries, new EntryXComparator());

    BarDataSet set1 = new BarDataSet(entries, mChartName);
    set1.setColor(mContext.getResources().getColor(R.color.toolbar_background));

    // Create a data object with the datasets
    BarData data = new BarData(set1);

    data.setValueTextSize(12);
    data.setValueFormatter(new IValueFormatter() {
        private DecimalFormat mFormat = new DecimalFormat("#.## kg");

        @Override
        public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
            return mFormat.format(value);
        }
    });

    // Set data
    mChart.setData(data);

    mChart.getAxisLeft().setAxisMinimum(0f);

    mChart.invalidate();
}
 
Example 6
Source File: PowerChart.java    From AndroidApp with GNU Affero General Public License v3.0 5 votes vote down vote up
private void setFormatting() {
    powerChart.setDrawGridBackground(false);
    powerChart.getLegend().setEnabled(false);
    powerChart.getAxisRight().setEnabled(false);
    powerChart.getDescription().setEnabled(false);
    powerChart.setNoDataText("");
    powerChart.setHardwareAccelerationEnabled(true);

    YAxis yAxis = powerChart.getAxisLeft();
    yAxis.setEnabled(true);
    yAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
    yAxis.setDrawTopYLabelEntry(false);
    yAxis.setDrawGridLines(false);
    yAxis.setDrawAxisLine(false);
    yAxis.setTextColor(ContextCompat.getColor(context, R.color.lightGrey));
    yAxis.setTextSize(context.getResources().getInteger(R.integer.chartDateTextSize));
    yAxis.setValueFormatter(new IntegerYAxisValueFormatter());

    XAxis xAxis = powerChart.getXAxis();
    xAxis.setDrawAxisLine(false);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawLabels(true);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setTextColor(ContextCompat.getColor(context, R.color.lightGrey));
    xAxis.setValueFormatter(new HoursMinutesXAxisValueFormatter(chartLabels));

    xAxis.setTextSize(context.getResources().getInteger(R.integer.chartDateTextSize));
}
 
Example 7
Source File: LineChartExtensions.java    From exchange-rates-mvvm with Apache License 2.0 5 votes vote down vote up
private static void formatXAxisLabels(final LineChart view, final List<SingleValue> items) {
    final XAxis xAxis = view.getXAxis();
    xAxis.setValueFormatter((value, axis) -> {
        int index = (int) value;
        if (index >= items.size()) {
            return "";
        }
        return mDateFormatter.format(items.get(index).getDate());
    });
}
 
Example 8
Source File: AnalysisFragment.java    From outlay with Apache License 2.0 5 votes vote down vote up
private void initChart() {
    barChart.setDrawBarShadow(false);
    barChart.setDrawValueAboveBar(true);
    barChart.getDescription().setEnabled(false);
    barChart.setPinchZoom(false);
    barChart.setMaxVisibleValueCount(60);
    barChart.getLegend().setEnabled(false);
    barChart.setDrawGridBackground(false);
    barChart.getAxisRight().setEnabled(false);
    barChart.setScaleYEnabled(false);

    dayAxisValueFormatter = new DayAxisValueFormatter();
    XAxis xAxis = barChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setLabelRotationAngle(270);
    xAxis.setDrawGridLines(false);
    xAxis.setGranularity(1f);
    xAxis.setTextColor(getOutlayTheme().secondaryTextColor);
    xAxis.setValueFormatter(dayAxisValueFormatter);


    YAxis leftAxis = barChart.getAxisLeft();
    leftAxis.setValueFormatter(new AmountValueFormatter());
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    leftAxis.setTextColor(getOutlayTheme().secondaryTextColor);
    leftAxis.setSpaceTop(15f);
    leftAxis.setAxisMinimum(0f);
}
 
Example 9
Source File: TestSuiteFragment.java    From SQLite-Performance with The Unlicense 5 votes vote down vote up
private void setupXAxis(BarLineChartBase chart, IAxisValueFormatter formatter) {
    XAxis x = chart.getXAxis();
    x.setGranularity(1.0f);
    x.setDrawGridLines(false);
    x.setPosition(XAxis.XAxisPosition.BOTTOM);
    x.setDrawAxisLine(false);
    x.setCenterAxisLabels(true);
    x.setAxisMinimum(2.0f);
    x.setAxisMaximum(5.0f);
    x.setValueFormatter(formatter);
}
 
Example 10
Source File: RecordingFragment.java    From go-bees with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Configure bees chart and the data.
 *
 * @param recordsList list of records.
 */
private void setupBeesChart(List<Record> recordsList) {
    // Setup data
    referenceTimestamp = recordsList.get(0).getTimestamp().getTime() / 1000;
    Record[] records = recordsList.toArray(new Record[recordsList.size()]);
    List<Entry> entries = new ArrayList<>();
    int maxNumBees = 0;
    for (Record record : records) {
        // Convert timestamp to seconds and relative to first timestamp
        long timestamp = record.getTimestamp().getTime() / 1000 - referenceTimestamp;
        int numBees = record.getNumBees();
        entries.add(new Entry(timestamp, numBees));
        // Get max num of bees
        if (numBees > maxNumBees) {
            maxNumBees = numBees;
        }
    }
    lastTimestamp = (long) entries.get(entries.size() - 1).getX();
    // Style char lines (type, color, etc.)
    LineDataSet lineDataSet = new LineDataSet(entries, getString(R.string.num_bees));
    lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
    lineDataSet.setCubicIntensity(0.2f);
    lineDataSet.setDrawValues(false);
    lineDataSet.setDrawCircles(false);
    lineDataSet.setLineWidth(1.8f);
    lineDataSet.setColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
    // General setup
    beesChart.setDrawGridBackground(false);
    beesChart.setDrawBorders(false);
    beesChart.setViewPortOffsets(80, 40, 80, 50);
    beesChart.getDescription().setEnabled(false);
    beesChart.getLegend().setEnabled(false);
    beesChart.setTouchEnabled(true);
    beesChart.setDragEnabled(false);
    beesChart.setScaleEnabled(false);
    beesChart.setPinchZoom(false);
    BeesMarkerView mv = new BeesMarkerView(getContext(), R.layout.recording_bees_marker_vew);
    mv.setChartView(beesChart);
    beesChart.setMarker(mv);
    beesChart.setNoDataText(getString(R.string.no_flight_act_data_available));
    // X axis setup
    IAxisValueFormatter xAxisFormatter = new HourAxisValueFormatter(referenceTimestamp);
    XAxis xAxis = beesChart.getXAxis();
    xAxis.setValueFormatter(xAxisFormatter);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(false);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setTextColor(Color.BLACK);
    // Y axis setup
    YAxis leftAxis = beesChart.getAxisLeft();
    leftAxis.setAxisMaximum(maxNumBees > 40 ? maxNumBees + 2 : 40);
    leftAxis.setAxisMinimum(0);
    leftAxis.setDrawGridLines(true);
    leftAxis.setDrawAxisLine(false);
    YAxis rightAxis = beesChart.getAxisRight();
    rightAxis.setAxisMaximum(maxNumBees > 40 ? maxNumBees + 2 : 40);
    rightAxis.setAxisMinimum(0);
    rightAxis.setDrawGridLines(true);
    rightAxis.setDrawAxisLine(false);
    // Add data
    beesChart.setData(new LineData(lineDataSet));
}
 
Example 11
Source File: FragmentPrice.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
private void setupChart(LineChart chart, LineData data, int color) {
    ((LineDataSet) data.getDataSetByIndex(0)).setCircleColorHole(color);
    chart.getDescription().setEnabled(false);
    chart.setDrawGridBackground(false);
    chart.setTouchEnabled(false);
    chart.setDragEnabled(false);
    chart.setScaleEnabled(true);
    chart.setPinchZoom(false);
    chart.setBackgroundColor(color);
    chart.setViewPortOffsets(0, 23, 0, 0);
    chart.setData(data);
    Legend l = chart.getLegend();
    l.setEnabled(false);

    chart.getAxisLeft().setEnabled(true);
    chart.getAxisLeft().setDrawGridLines(false);
    chart.getAxisLeft().setDrawAxisLine(false);
    chart.getAxisLeft().setSpaceTop(10);
    chart.getAxisLeft().setSpaceBottom(30);
    chart.getAxisLeft().setAxisLineColor(0xFFFFFF);
    chart.getAxisLeft().setTextColor(0xFFFFFF);
    chart.getAxisLeft().setDrawTopYLabelEntry(true);
    chart.getAxisLeft().setLabelCount(10);

    chart.getXAxis().setEnabled(true);
    chart.getXAxis().setDrawGridLines(false);
    chart.getXAxis().setDrawAxisLine(false);
    chart.getXAxis().setAxisLineColor(0xFFFFFF);
    chart.getXAxis().setTextColor(0xFFFFFF);

    Typeface tf = Typeface.DEFAULT;

    // X Axis
    XAxis xAxis = chart.getXAxis();
    xAxis.setTypeface(tf);
    xAxis.removeAllLimitLines();

    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);

    xAxis.setTextColor(Color.argb(150, 255, 255, 255));

    if (displayType == 1 || displayType == 2) // Week and Month
        xAxis.setValueFormatter(new WeekXFormatter());
    else if (displayType == 0) //  Day
        xAxis.setValueFormatter(new HourXFormatter());
    else
        xAxis.setValueFormatter(new YearXFormatter()); // Year

    // Y Axis
    YAxis leftAxis = chart.getAxisLeft();
    leftAxis.removeAllLimitLines();
    leftAxis.setTypeface(tf);
    leftAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
    leftAxis.setTextColor(Color.argb(150, 255, 255, 255));
    leftAxis.setValueFormatter(new DontShowNegativeFormatter(displayInUsd));
    chart.getAxisRight().setEnabled(false); // Deactivates horizontal lines

    chart.animateX(1300);
    chart.notifyDataSetChanged();
}
 
Example 12
Source File: DateGraph.java    From fastnfitness with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public DateGraph(Context context, LineChart chart, String name) {
    mChart = chart;
    mChartName = name;
    mChart.setDoubleTapToZoomEnabled(true);
    mChart.setHorizontalScrollBarEnabled(true);
    mChart.setVerticalScrollBarEnabled(true);
    mChart.setAutoScaleMinMaxEnabled(true);
    mChart.setDrawBorders(true);
    mChart.setNoDataText(context.getString(R.string.no_chart_data_available));

    IMarker marker = new DateGraphMarkerView(mChart.getContext(), R.layout.graph_markerview, mChart);
    mChart.setMarker(marker);

    mContext = context;
    // get the legend (only possible after setting data)
    Legend l = mChart.getLegend();
    l.setEnabled(false);

    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setTextColor(ColorTemplate.getHoloBlue());
    xAxis.setDrawAxisLine(true);
    xAxis.setDrawGridLines(true);
    xAxis.setCenterAxisLabels(false);
    xAxis.setGranularity(1); // 1 jour
    xAxis.setValueFormatter(new IAxisValueFormatter() {

        private SimpleDateFormat mFormat = new SimpleDateFormat("dd-MMM"); // HH:mm:ss

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            //long millis = TimeUnit.HOURS.toMillis((long) value);
            mFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
            Date tmpDate = new Date((long) DateConverter.nbMilliseconds(value)); // Convert days in milliseconds
            return mFormat.format(tmpDate);
        }
    });

    YAxis leftAxis = mChart.getAxisLeft();
    leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    leftAxis.setTextColor(ColorTemplate.getHoloBlue());
    leftAxis.setDrawGridLines(true);
    leftAxis.setGranularityEnabled(true);
    leftAxis.setGranularity((float) 0.5);
    leftAxis.resetAxisMinimum();

    mChart.getAxisRight().setEnabled(false);
}
 
Example 13
Source File: HistogramChart.java    From walt with Apache License 2.0 4 votes vote down vote up
public HistogramChart(Context context, AttributeSet attrs) {
    super(context, attrs);
    inflate(getContext(), R.layout.histogram, this);

    barChart = (BarChart) findViewById(R.id.bar_chart);
    findViewById(R.id.button_close_bar_chart).setOnClickListener(this);

    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HistogramChart);
    final String descString;
    final int numDataSets;
    final float binWidth;
    try {
        descString = a.getString(R.styleable.HistogramChart_description);
        numDataSets = a.getInteger(R.styleable.HistogramChart_numDataSets, 1);
        binWidth = a.getFloat(R.styleable.HistogramChart_binWidth, 5f);
    } finally {
        a.recycle();
    }

    ArrayList<IBarDataSet> dataSets = new ArrayList<>(numDataSets);
    for (int i = 0; i < numDataSets; i++) {
        final BarDataSet dataSet = new BarDataSet(new ArrayList<BarEntry>(), "");
        dataSet.setColor(ColorTemplate.MATERIAL_COLORS[i]);
        dataSets.add(dataSet);
    }

    BarData barData = new BarData(dataSets);
    barData.setBarWidth((1f - GROUP_SPACE)/numDataSets);
    barChart.setData(barData);
    histogramData = new HistogramData(numDataSets, binWidth);
    groupBars(barData);
    final Description desc = new Description();
    desc.setText(descString);
    desc.setTextSize(12f);
    barChart.setDescription(desc);

    XAxis xAxis = barChart.getXAxis();
    xAxis.setGranularityEnabled(true);
    xAxis.setGranularity(1);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setValueFormatter(new IAxisValueFormatter() {
        DecimalFormat df = new DecimalFormat("#.##");

        @Override
        public String getFormattedValue(float value, AxisBase axis) {
            return df.format(histogramData.getDisplayValue(value));
        }
    });

    barChart.setFitBars(true);
    barChart.invalidate();
}
 
Example 14
Source File: BarChartPositiveNegative.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_barchart_noseekbar);

    setTitle("BarChartPositiveNegative");

    chart = findViewById(R.id.chart1);
    chart.setBackgroundColor(Color.WHITE);
    chart.setExtraTopOffset(-30f);
    chart.setExtraBottomOffset(10f);
    chart.setExtraLeftOffset(70f);
    chart.setExtraRightOffset(70f);

    chart.setDrawBarShadow(false);
    chart.setDrawValueAboveBar(true);

    chart.getDescription().setEnabled(false);

    // scaling can now only be done on x- and y-axis separately
    chart.setPinchZoom(false);

    chart.setDrawGridBackground(false);

    XAxis xAxis = chart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTTOM);
    xAxis.setTypeface(tfRegular);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(false);
    xAxis.setTextColor(Color.LTGRAY);
    xAxis.setTextSize(13f);
    xAxis.setLabelCount(5);
    xAxis.setCenterAxisLabels(true);
    xAxis.setGranularity(1f);

    YAxis left = chart.getAxisLeft();
    left.setDrawLabels(false);
    left.setSpaceTop(25f);
    left.setSpaceBottom(25f);
    left.setDrawAxisLine(false);
    left.setDrawGridLines(false);
    left.setDrawZeroLine(true); // draw a zero line
    left.setZeroLineColor(Color.GRAY);
    left.setZeroLineWidth(0.7f);
    chart.getAxisRight().setEnabled(false);
    chart.getLegend().setEnabled(false);

    // THIS IS THE ORIGINAL DATA YOU WANT TO PLOT
    final List<Data> data = new ArrayList<>();
    data.add(new Data(0f, -224.1f, "12-29"));
    data.add(new Data(1f, 238.5f, "12-30"));
    data.add(new Data(2f, 1280.1f, "12-31"));
    data.add(new Data(3f, -442.3f, "01-01"));
    data.add(new Data(4f, -2280.1f, "01-02"));

    xAxis.setValueFormatter(new ValueFormatter() {
        @Override
        public String getFormattedValue(float value) {
            return data.get(Math.min(Math.max((int) value, 0), data.size()-1)).xAxisValue;
        }
    });

    setData(data);
}
 
Example 15
Source File: LineChartTime.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_linechart_time);

    setTitle("LineChartTime");

    tvX = findViewById(R.id.tvXMax);
    seekBarX = findViewById(R.id.seekBar1);
    seekBarX.setOnSeekBarChangeListener(this);

    chart = findViewById(R.id.chart1);

    // no description text
    chart.getDescription().setEnabled(false);

    // enable touch gestures
    chart.setTouchEnabled(true);

    chart.setDragDecelerationFrictionCoef(0.9f);

    // enable scaling and dragging
    chart.setDragEnabled(true);
    chart.setScaleEnabled(true);
    chart.setDrawGridBackground(false);
    chart.setHighlightPerDragEnabled(true);

    // set an alternative background color
    chart.setBackgroundColor(Color.WHITE);
    chart.setViewPortOffsets(0f, 0f, 0f, 0f);

    // add data
    seekBarX.setProgress(100);

    // get the legend (only possible after setting data)
    Legend l = chart.getLegend();
    l.setEnabled(false);

    XAxis xAxis = chart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.TOP_INSIDE);
    xAxis.setTypeface(tfLight);
    xAxis.setTextSize(10f);
    xAxis.setTextColor(Color.WHITE);
    xAxis.setDrawAxisLine(false);
    xAxis.setDrawGridLines(true);
    xAxis.setTextColor(Color.rgb(255, 192, 56));
    xAxis.setCenterAxisLabels(true);
    xAxis.setGranularity(1f); // one hour
    xAxis.setValueFormatter(new ValueFormatter() {

        private final SimpleDateFormat mFormat = new SimpleDateFormat("dd MMM HH:mm", Locale.ENGLISH);

        @Override
        public String getFormattedValue(float value) {

            long millis = TimeUnit.HOURS.toMillis((long) value);
            return mFormat.format(new Date(millis));
        }
    });

    YAxis leftAxis = chart.getAxisLeft();
    leftAxis.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
    leftAxis.setTypeface(tfLight);
    leftAxis.setTextColor(ColorTemplate.getHoloBlue());
    leftAxis.setDrawGridLines(true);
    leftAxis.setGranularityEnabled(true);
    leftAxis.setAxisMinimum(0f);
    leftAxis.setAxisMaximum(170f);
    leftAxis.setYOffset(-9f);
    leftAxis.setTextColor(Color.rgb(255, 192, 56));

    YAxis rightAxis = chart.getAxisRight();
    rightAxis.setEnabled(false);
}
 
Example 16
Source File: BarChartActivityMultiDataset.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
@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);
        mv.setChartView(chart); // For bounds control
        chart.setMarker(mv); // Set the marker to the chart

        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 ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return String.valueOf((int) value);
            }
        });

        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setTypeface(tfLight);
        leftAxis.setValueFormatter(new LargeValueFormatter());
        leftAxis.setDrawGridLines(false);
        leftAxis.setSpaceTop(35f);
        leftAxis.setAxisMinimum(0f); // this replaces setStartAtZero(true)

        chart.getAxisRight().setEnabled(false);
    }
 
Example 17
Source File: StackedBarActivityNegative.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_age_distribution);

    setTitle("StackedBarActivityNegative");

    chart = findViewById(R.id.chart1);
    chart.setOnChartValueSelectedListener(this);
    chart.setDrawGridBackground(false);
    chart.getDescription().setEnabled(false);

    // scaling can now only be done on x- and y-axis separately
    chart.setPinchZoom(false);

    chart.setDrawBarShadow(false);
    chart.setDrawValueAboveBar(true);
    chart.setHighlightFullBarEnabled(false);

    chart.getAxisLeft().setEnabled(false);
    chart.getAxisRight().setAxisMaximum(25f);
    chart.getAxisRight().setAxisMinimum(-25f);
    chart.getAxisRight().setDrawGridLines(false);
    chart.getAxisRight().setDrawZeroLine(true);
    chart.getAxisRight().setLabelCount(7, false);
    chart.getAxisRight().setValueFormatter(new CustomFormatter());
    chart.getAxisRight().setTextSize(9f);

    XAxis xAxis = chart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTH_SIDED);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(false);
    xAxis.setTextSize(9f);
    xAxis.setAxisMinimum(0f);
    xAxis.setAxisMaximum(110f);
    xAxis.setCenterAxisLabels(true);
    xAxis.setLabelCount(12);
    xAxis.setGranularity(10f);
    xAxis.setValueFormatter(new ValueFormatter() {

        private final DecimalFormat format = new DecimalFormat("###");

        @Override
        public String getFormattedValue(float value) {
            return format.format(value) + "-" + format.format(value + 10);
        }
    });

    Legend l = chart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(false);
    l.setFormSize(8f);
    l.setFormToTextSpace(4f);
    l.setXEntrySpace(6f);

    // IMPORTANT: When using negative values in stacked bars, always make sure the negative values are in the array first
    ArrayList<BarEntry> values = new ArrayList<>();
    values.add(new BarEntry(5, new float[]{ -10, 10 }));
    values.add(new BarEntry(15, new float[]{ -12, 13 }));
    values.add(new BarEntry(25, new float[]{ -15, 15 }));
    values.add(new BarEntry(35, new float[]{ -17, 17 }));
    values.add(new BarEntry(45, new float[]{ -19, 20 }));
    values.add(new BarEntry(45, new float[]{ -19, 20 }, getResources().getDrawable(R.drawable.star)));
    values.add(new BarEntry(55, new float[]{ -19, 19 }));
    values.add(new BarEntry(65, new float[]{ -16, 16 }));
    values.add(new BarEntry(75, new float[]{ -13, 14 }));
    values.add(new BarEntry(85, new float[]{ -10, 11 }));
    values.add(new BarEntry(95, new float[]{ -5, 6 }));
    values.add(new BarEntry(105, new float[]{ -1, 2 }));

    BarDataSet set = new BarDataSet(values, "Age Distribution");
    set.setDrawIcons(false);
    set.setValueFormatter(new CustomFormatter());
    set.setValueTextSize(7f);
    set.setAxisDependency(YAxis.AxisDependency.RIGHT);
    set.setColors(Color.rgb(67,67,72), Color.rgb(124,181,236));
    set.setStackLabels(new String[]{
            "Men", "Women"
    });

    BarData data = new BarData(set);
    data.setBarWidth(8.5f);
    chart.setData(data);
    chart.invalidate();
}
 
Example 18
Source File: RadarChartActivity.java    From StockChart-MPAndroidChart with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_radarchart);

    setTitle("RadarChartActivity");

    chart = findViewById(R.id.chart1);
    chart.setBackgroundColor(Color.rgb(60, 65, 82));

    chart.getDescription().setEnabled(false);

    chart.setWebLineWidth(1f);
    chart.setWebColor(Color.LTGRAY);
    chart.setWebLineWidthInner(1f);
    chart.setWebColorInner(Color.LTGRAY);
    chart.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);
    mv.setChartView(chart); // For bounds control
    chart.setMarker(mv); // Set the marker to the chart

    setData();

    chart.animateXY(1400, 1400, Easing.EaseInOutQuad);

    XAxis xAxis = chart.getXAxis();
    xAxis.setTypeface(tfLight);
    xAxis.setTextSize(9f);
    xAxis.setYOffset(0f);
    xAxis.setXOffset(0f);
    xAxis.setValueFormatter(new ValueFormatter() {

        private final String[] mActivities = new String[]{"Burger", "Steak", "Salad", "Pasta", "Pizza"};

        @Override
        public String getFormattedValue(float value) {
            return mActivities[(int) value % mActivities.length];
        }
    });
    xAxis.setTextColor(Color.WHITE);

    YAxis yAxis = chart.getYAxis();
    yAxis.setTypeface(tfLight);
    yAxis.setLabelCount(5, false);
    yAxis.setTextSize(9f);
    yAxis.setAxisMinimum(0f);
    yAxis.setAxisMaximum(80f);
    yAxis.setDrawLabels(false);

    Legend l = chart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
    l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
    l.setDrawInside(false);
    l.setTypeface(tfLight);
    l.setXEntrySpace(7f);
    l.setYEntrySpace(5f);
    l.setTextColor(Color.WHITE);
}
 
Example 19
Source File: FreeHandView.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    BarData exampleData;

    switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN :
            touchStart(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE :
            touchMove(x, y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP :
            touchUp();
            //toGrayscale(mBitmap);
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(mBitmap, mClassifier.getImageSizeX(), mClassifier.getImageSizeY(), true);
            Random rng = new Random();

            try {
                File mFile;
                mFile = this.getContext().getExternalFilesDir(String.valueOf(rng.nextLong() + ".png"));
                FileOutputStream pngFile = new FileOutputStream(mFile);
            }
            catch (Exception e){
            }
            //scaledBitmap.compress(Bitmap.CompressFormat.PNG, 90, pngFile);
            Float prediction = mClassifier.classifyFrame(scaledBitmap);
            exampleData = updateBarEntry();
            barChart.animateY(1000, Easing.EasingOption.EaseOutQuad);
            XAxis xAxis = barChart.getXAxis();
            xAxis.setValueFormatter(new IAxisValueFormatter() {
                @Override
                public String getFormattedValue(float value, AxisBase axis) {
                    return xAxisLabel.get((int) value);
                }
            });
            barChart.setData(exampleData);
            exampleData.notifyDataChanged(); // let the data know a dataSet changed
            barChart.notifyDataSetChanged(); // let the chart know it's data changed
            break;
    }

    return true;
}
 
Example 20
Source File: GraphUtils.java    From your-local-weather with GNU General Public License v3.0 4 votes vote down vote up
public static void setupXAxis(XAxis x,
                              List<DetailedWeatherForecast> weatherForecastList,
                              int textColorId,
                              Float textSize,
                              AppPreference.GraphGridColors gridColor,
                              Locale locale) {
    x.removeAllLimitLines();
    Map<Integer, Long> hourIndexes = new HashMap<>();

    int lastDayOflimitLine = 0;
    for (int i = 0; i < weatherForecastList.size(); i++) {
        hourIndexes.put(i, weatherForecastList.get(i).getDateTime());
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(weatherForecastList.get(i).getDateTime() * 1000);
        if (cal.get(Calendar.DAY_OF_YEAR) != lastDayOflimitLine) {
            Calendar calOfPreviousRecord = Calendar.getInstance();
            int previousRecordHour = 24;
            if (i > 0) {
                calOfPreviousRecord.setTimeInMillis(weatherForecastList.get(i - 1).getDateTime() * 1000);
                previousRecordHour = calOfPreviousRecord.get(Calendar.HOUR_OF_DAY);
            }
            int currentHour = cal.get(Calendar.HOUR_OF_DAY);
            float timeSpan = (24 - previousRecordHour) + currentHour;
            float dayLine = currentHour / timeSpan;
            float midnight = i - dayLine;
            float hour6 = midnight + (6 / timeSpan);
            float hour12 = midnight + (12 / timeSpan);
            float hour18 = midnight + (18 / timeSpan);
            LimitLine limitLine = new LimitLine(midnight);
            limitLine.setLineColor(gridColor.getMainGridColor());
            limitLine.setLineWidth(0.5f);
            x.addLimitLine(limitLine);
            /*LimitLine limitLine6 = new LimitLine(hour6, "");
            limitLine6.setLineColor(Color.LTGRAY);
            limitLine6.setLineWidth(0.5f);
            x.addLimitLine(limitLine6);*/
            LimitLine limitLine12 = new LimitLine(hour12);
            limitLine12.setLineColor(gridColor.getSecondaryGridColor());
            limitLine12.setLineWidth(0.5f);
            x.addLimitLine(limitLine12);
            /*LimitLine limitLine18 = new LimitLine(hour18, "");
            limitLine18.setLineColor(Color.LTGRAY);
            limitLine18.setLineWidth(0.5f);
            x.addLimitLine(limitLine18);*/
            lastDayOflimitLine = cal.get(Calendar.DAY_OF_YEAR);
        }
    }

    x.setEnabled(true);
    x.setPosition(XAxis.XAxisPosition.BOTTOM);
    x.setDrawGridLines(false);
    x.setLabelCount(25, true);
    x.setTextColor(textColorId);
    x.setValueFormatter(new XAxisValueFormatter(hourIndexes, locale));
    x.setDrawLimitLinesBehindData(true);

    if (textSize != null) {
        x.setTextSize(textSize);
    }
}