Search in sources :

Example 1 with FinanceFormatter

use of ru.sash0k.thriftbox.charting.FinanceFormatter in project Thrift-box by Sash0k.

the class ChartsFragment method onCreateView.

// ============================================================================
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.charts_fragment, container, false);
    TextView month = (TextView) v.findViewById(R.id.current_month);
    long ts = getArguments().getLong(DB.TIMESTAMP);
    month.setText(SDF_MONTH.format(new Date(ts * 1000)));
    // отображение статистики за текущий месяц
    BarChart barChart = (BarChart) v.findViewById(R.id.bar_chart);
    barChart.setDescription("");
    Paint mInfoPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mInfoPaint.setColor(getResources().getColor(R.color.primary));
    mInfoPaint.setTextAlign(Paint.Align.CENTER);
    mInfoPaint.setTextSize(com.github.mikephil.charting.utils.Utils.convertDpToPixel(16f));
    barChart.setPaint(mInfoPaint, Chart.PAINT_INFO);
    barChart.setNoDataText(getString(R.string.charts_no_data));
    barChart.setDrawBarShadow(false);
    barChart.setDrawGridBackground(false);
    barChart.setDrawValueAboveBar(false);
    barChart.setPinchZoom(false);
    barChart.setScaleEnabled(false);
    barChart.setDoubleTapToZoomEnabled(false);
    barChart.getXAxis().disableGridDashedLine();
    barChart.getXAxis().setDrawGridLines(false);
    barChart.getXAxis().setEnabled(false);
    barChart.getAxisLeft().setDrawGridLines(false);
    barChart.getAxisLeft().setEnabled(false);
    barChart.getAxisRight().setDrawGridLines(false);
    barChart.getAxisRight().removeAllLimitLines();
    barChart.getAxisRight().setValueFormatter(new FinanceFormatter());
    barChart.getLegend().setEnabled(false);
    barChart.setData(generateMonthlyData());
    // если данных нет, не показывать ось
    barChart.getAxisRight().setEnabled(!barChart.isEmpty());
    MyMarkerView mv = new MyMarkerView(getActivity(), R.layout.custom_marker_view);
    mv.setX(mv.getMeasuredWidth());
    barChart.setMarkerView(mv);
    Utils.log("getYMin = " + barChart.getBarData().getYMin());
    Utils.log("getYMax = " + barChart.getBarData().getYMax());
    barChart.animateY(ANIMATION_TIME);
    return v;
}
Also used : BarChart(com.github.mikephil.charting.charts.BarChart) TextView(android.widget.TextView) Paint(android.graphics.Paint) TextView(android.widget.TextView) MyMarkerView(ru.sash0k.thriftbox.charting.MyMarkerView) View(android.view.View) Date(java.util.Date) MyMarkerView(ru.sash0k.thriftbox.charting.MyMarkerView) FinanceFormatter(ru.sash0k.thriftbox.charting.FinanceFormatter)

Example 2 with FinanceFormatter

use of ru.sash0k.thriftbox.charting.FinanceFormatter in project Thrift-box by Sash0k.

the class ChartsFragment method generateMonthlyData.

// ============================================================================
/**
 * Заполнение графика данными
 */
private BarData generateMonthlyData() {
    final Context ctx = getActivity();
    // получение категорий
    final String[] bars = ctx.getResources().getStringArray(R.array.categories);
    // получение данных
    final long month = getArguments().getLong(DB.TIMESTAMP);
    // Статистика за текущий месяц
    List<Float> stats = new DB(ctx).getStatData(ctx, month, bars.length);
    final int count = stats.size();
    ArrayList<BarEntry> entries = new ArrayList<>();
    final ArrayList<String> usedBars = new ArrayList<>();
    int j = 0;
    for (int i = 0; i < count; i++) {
        float value = stats.get(i);
        if (value > 0f) {
            usedBars.add(bars[i]);
            entries.add(new BarEntry(value, j, bars[i]));
            j++;
        }
    }
    // данные графика
    BarDataSet ds = new BarDataSet(entries, null);
    ds.setDrawValues(true);
    ds.setColor(getResources().getColor(R.color.accent));
    // подписи к линиям графика
    BarData d = new BarData(usedBars, ds);
    d.setValueTextColor(getResources().getColor(R.color.primary_text));
    d.setValueTextSize(14f);
    d.setValueFormatter(new FinanceFormatter());
    return d;
}
Also used : Context(android.content.Context) BarDataSet(com.github.mikephil.charting.data.BarDataSet) ArrayList(java.util.ArrayList) BarEntry(com.github.mikephil.charting.data.BarEntry) Paint(android.graphics.Paint) FinanceFormatter(ru.sash0k.thriftbox.charting.FinanceFormatter) BarData(com.github.mikephil.charting.data.BarData) DB(ru.sash0k.thriftbox.database.DB)

Aggregations

Paint (android.graphics.Paint)2 FinanceFormatter (ru.sash0k.thriftbox.charting.FinanceFormatter)2 Context (android.content.Context)1 View (android.view.View)1 TextView (android.widget.TextView)1 BarChart (com.github.mikephil.charting.charts.BarChart)1 BarData (com.github.mikephil.charting.data.BarData)1 BarDataSet (com.github.mikephil.charting.data.BarDataSet)1 BarEntry (com.github.mikephil.charting.data.BarEntry)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 MyMarkerView (ru.sash0k.thriftbox.charting.MyMarkerView)1 DB (ru.sash0k.thriftbox.database.DB)1