Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.powermonitor.epitech.Charts; import com.powermonitor.epitech.Models.Module; import com.powermonitor.epitech.R; import android.os.Bundle; import android.support.v4.app.Fragment; import android.text.format.DateFormat; import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.github.mikephil.charting.charts.LineChart; import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineDataSet; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import android.R.color; import android.widget.TextView; /** * * @author jourda_c */ public class LineChartFragment extends Fragment { private LineChart chart; private ArrayList<LineDataSet> sets; private ArrayList<String> xVals; private SparseArray<String> dates; private SparseArray<LineDataSet> setsSave; private LineData data; private double maxY = 0; private int[] colors; private int currentColorId = 0; private TextView title; private String titleStr = ""; @Override public void onCreate(Bundle savedInstanceBundle) { super.onCreate(savedInstanceBundle); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) { View v = inflater.inflate(R.layout.linechart, container, false); title = (TextView) v.findViewById(R.id.title); if (!titleStr.isEmpty()) title.setText(titleStr); this.chart = (LineChart) v.findViewById(R.Charts.LineChart); sets = new ArrayList<LineDataSet>(); xVals = new ArrayList<String>(); dates = new SparseArray<String>(); data = new LineData(xVals, sets); setsSave = new SparseArray<LineDataSet>(); colors = new int[] { color.holo_blue_dark, color.holo_green_dark, color.holo_orange_dark, color.holo_purple, color.holo_red_dark }; this.chart.setData(data); this.chart.getXAxis().setPosition(XAxis.XAxisPosition.TOP); this.chart.setDescription(""); this.chart.getAxisRight().setEnabled(false); return v; } public void setTitle(String t) { if (title != null) title.setText(t); titleStr = t; } public void empty() { this.sets.clear(); this.xVals.clear(); this.dates.clear(); this.setsSave.clear(); this.data.clearValues(); this.chart.clear(); this.chart.setData(data); this.maxY = 0; this.currentColorId = 0; } public synchronized void addData(String label, int date, float y) { if (this.setsSave.get(label.hashCode(), null) == null) { LineDataSet s = new LineDataSet(new ArrayList<Entry>(), label); s.setDrawValues(false); s.setDrawCircles(false); s.setLineWidth(2f); s.setColor(getResources().getColor(this.colors[currentColorId])); currentColorId++; if (currentColorId >= this.colors.length) currentColorId = 0; this.data.addDataSet(s); this.setsSave.append(label.hashCode(), s); } LineDataSet set = this.setsSave.get(label.hashCode()); if (dates.get(date, null) == null) { long ms = (long) date * 1000; Date d = new Date(ms); SimpleDateFormat format = new SimpleDateFormat("dd-MM"); dates.append(date, format.format(d)); xVals.add(dates.get(date)); } set.addEntry(new Entry(y, dates.indexOfKey(date))); if (y > maxY) { maxY = y + 1; chart.getAxis(YAxis.AxisDependency.LEFT).setAxisMaxValue((int) maxY); } chart.notifyDataSetChanged(); chart.invalidate(); } }