Search in sources :

Example 41 with BarEntry

use of com.github.mikephil.charting.data.BarEntry in project carat by amplab.

the class BarChart method drawData.

@Override
protected void drawData() {
    ArrayList<BarDataSet> dataSets = mData.getDataSets();
    int setCount = mData.getDataSetCount();
    // the space between bar-groups
    float space = mData.getGroupSpace();
    // 2D drawing
    for (int i = 0; i < setCount; i++) {
        BarDataSet dataSet = dataSets.get(i);
        boolean noStacks = dataSet.getStackSize() == 1 ? true : false;
        ArrayList<BarEntry> entries = dataSet.getYVals();
        // do the drawing
        for (int j = 0; j < dataSet.getEntryCount() * mPhaseX; j++) {
            BarEntry e = entries.get(j);
            // calculate the x-position, depending on datasetcount
            float x = e.getXIndex() + j * (setCount - 1) + i + space * j + space / 2f;
            float y = e.getVal();
            // no stacks
            if (noStacks) {
                prepareBar(x, y, dataSet.getBarSpace());
                // avoid drawing outofbounds values
                if (isOffContentRight(mBarRect.left))
                    break;
                if (isOffContentLeft(mBarRect.right)) {
                    continue;
                }
                // if drawing the bar shadow is enabled
                if (mDrawBarShadow) {
                    mRenderPaint.setColor(dataSet.getBarShadowColor());
                    mDrawCanvas.drawRect(mBarShadow, mRenderPaint);
                }
                // Set the color for the currently drawn value. If the index
                // is
                // out of bounds, reuse colors.
                mRenderPaint.setColor(dataSet.getColor(j));
                mDrawCanvas.drawRect(mBarRect, mRenderPaint);
            } else {
                // stacked bars
                float[] vals = e.getVals();
                // in between
                if (vals == null) {
                    prepareBar(x, y, dataSet.getBarSpace());
                    // if drawing the bar shadow is enabled
                    if (mDrawBarShadow) {
                        mRenderPaint.setColor(dataSet.getBarShadowColor());
                        mDrawCanvas.drawRect(mBarShadow, mRenderPaint);
                    }
                    mRenderPaint.setColor(dataSet.getColor(0));
                    mDrawCanvas.drawRect(mBarRect, mRenderPaint);
                } else {
                    float all = e.getVal();
                    // if drawing the bar shadow is enabled
                    if (mDrawBarShadow) {
                        prepareBar(x, y, dataSet.getBarSpace());
                        mRenderPaint.setColor(dataSet.getBarShadowColor());
                        mDrawCanvas.drawRect(mBarShadow, mRenderPaint);
                    }
                    // draw the stack
                    for (int k = 0; k < vals.length; k++) {
                        all -= vals[k];
                        prepareBar(x, vals[k] + all, dataSet.getBarSpace());
                        mRenderPaint.setColor(dataSet.getColor(k));
                        mDrawCanvas.drawRect(mBarRect, mRenderPaint);
                    }
                }
                // avoid drawing outofbounds values
                if (isOffContentRight(mBarRect.left))
                    break;
            }
        }
    }
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) BarEntry(com.github.mikephil.charting.data.BarEntry) Paint(android.graphics.Paint)

Example 42 with BarEntry

use of com.github.mikephil.charting.data.BarEntry in project carat by amplab.

the class FileUtils method loadEntriesFromAssets.

/**
 * Loads an array of Entries from a textfile from the assets folder.
 *
 * @param am
 * @param path the name of the file in the assets folder (+ path if needed)
 * @return
 */
public static ArrayList<Entry> loadEntriesFromAssets(AssetManager am, String path) {
    ArrayList<Entry> entries = new ArrayList<Entry>();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(am.open(path), "UTF-8"));
        String line = reader.readLine();
        while (line != null) {
            // process line
            String[] split = line.split("#");
            if (split.length <= 2) {
                entries.add(new Entry(Float.parseFloat(split[0]), Integer.parseInt(split[1])));
            } else {
                float[] vals = new float[split.length - 1];
                for (int i = 0; i < vals.length; i++) {
                    vals[i] = Float.parseFloat(split[i]);
                }
                entries.add(new BarEntry(vals, Integer.parseInt(split[split.length - 1])));
            }
            line = reader.readLine();
        }
    } catch (IOException e) {
        Log.e(LOG, e.toString());
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                Log.e(LOG, e.toString());
            }
        }
    }
    return entries;
// String label = null;
// ArrayList<Entry> entries = new ArrayList<Entry>();
// 
// BufferedReader reader = null;
// try {
// reader = new BufferedReader(
// new InputStreamReader(am.open(path), "UTF-8"));
// 
// // do reading, usually loop until end of file reading
// label = reader.readLine();
// String line = reader.readLine();
// 
// while (line != null) {
// // process line
// String[] split = line.split("#");
// entries.add(new Entry(Float.parseFloat(split[0]),
// Integer.parseInt(split[1])));
// line = reader.readLine();
// }
// } catch (IOException e) {
// Log.e(LOG, e.toString());
// 
// } finally {
// 
// if (reader != null) {
// try {
// reader.close();
// } catch (IOException e) {
// Log.e(LOG, e.toString());
// }
// }
// }
// 
// DataSet ds = new DataSet(entries, label);
// return ds;
}
Also used : Entry(com.github.mikephil.charting.data.Entry) BarEntry(com.github.mikephil.charting.data.BarEntry) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 43 with BarEntry

use of com.github.mikephil.charting.data.BarEntry in project carat by amplab.

the class FileUtils method loadEntriesFromFile.

/**
 * Loads a an Array of Entries from a textfile from the sd-card.
 *
 * @param path the name of the file on the sd-card (+ path if needed)
 * @return
 */
public static ArrayList<Entry> loadEntriesFromFile(String path) {
    File sdcard = Environment.getExternalStorageDirectory();
    // Get the text file
    File file = new File(sdcard, path);
    ArrayList<Entry> entries = new ArrayList<Entry>();
    try {
        @SuppressWarnings("resource") BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            String[] split = line.split("#");
            if (split.length <= 2) {
                entries.add(new Entry(Float.parseFloat(split[0]), Integer.parseInt(split[1])));
            } else {
                float[] vals = new float[split.length - 1];
                for (int i = 0; i < vals.length; i++) {
                    vals[i] = Float.parseFloat(split[i]);
                }
                entries.add(new BarEntry(vals, Integer.parseInt(split[split.length - 1])));
            }
        }
    } catch (IOException e) {
        Log.e(LOG, e.toString());
    }
    return entries;
// File sdcard = Environment.getExternalStorageDirectory();
// 
// // Get the text file
// File file = new File(sdcard, path);
// 
// ArrayList<Entry> entries = new ArrayList<Entry>();
// String label = "";
// 
// try {
// @SuppressWarnings("resource")
// BufferedReader br = new BufferedReader(new FileReader(file));
// String line = br.readLine();
// 
// // firstline is the label
// label = line;
// 
// while ((line = br.readLine()) != null) {
// String[] split = line.split("#");
// entries.add(new Entry(Float.parseFloat(split[0]),
// Integer.parseInt(split[1])));
// }
// } catch (IOException e) {
// Log.e(LOG, e.toString());
// }
// 
// DataSet ds = new DataSet(entries, label);
// return ds;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) BarEntry(com.github.mikephil.charting.data.BarEntry) Entry(com.github.mikephil.charting.data.Entry) BarEntry(com.github.mikephil.charting.data.BarEntry) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File)

Example 44 with BarEntry

use of com.github.mikephil.charting.data.BarEntry in project HAR-Android by linw7.

the class BarActivity method get_bar_set_2.

private BarDataSet get_bar_set_2() {
    List<BarEntry> entry_list = new ArrayList<>();
    BarEntry entry_sit = new BarEntry(sit_y, 0);
    BarEntry entry_stand = new BarEntry(stand_y, 1);
    BarEntry entry_upstairs = new BarEntry(upstairs_y, 2);
    BarEntry entry_downstairs = new BarEntry(downstairs_y, 3);
    BarEntry entry_walk = new BarEntry(walk_y, 4);
    BarEntry entry_jog = new BarEntry(jog_y, 5);
    entry_list.add(entry_sit);
    entry_list.add(entry_stand);
    entry_list.add(entry_upstairs);
    entry_list.add(entry_downstairs);
    entry_list.add(entry_walk);
    entry_list.add(entry_jog);
    BarDataSet barSet = new BarDataSet(entry_list, "前一日数据");
    barSet.setColor(Color.rgb(255, 21, 226));
    barSet.setDrawValues(true);
    return barSet;
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) ArrayList(java.util.ArrayList) BarEntry(com.github.mikephil.charting.data.BarEntry)

Example 45 with BarEntry

use of com.github.mikephil.charting.data.BarEntry in project HAR-Android by linw7.

the class BarActivity method get_bar_set_3.

private BarDataSet get_bar_set_3() {
    List<BarEntry> entry_list = new ArrayList<>();
    BarEntry entry_sit = new BarEntry(sit_t, 0);
    BarEntry entry_stand = new BarEntry(stand_t, 1);
    BarEntry entry_upstairs = new BarEntry(upstairs_t, 2);
    BarEntry entry_downstairs = new BarEntry(downstairs_t, 3);
    BarEntry entry_walk = new BarEntry(walk_t, 4);
    BarEntry entry_jog = new BarEntry(jog_t, 5);
    entry_list.add(entry_sit);
    entry_list.add(entry_stand);
    entry_list.add(entry_upstairs);
    entry_list.add(entry_downstairs);
    entry_list.add(entry_walk);
    entry_list.add(entry_jog);
    BarDataSet barSet = new BarDataSet(entry_list, "今日数据");
    barSet.setColor(Color.rgb(155, 241, 226));
    barSet.setDrawValues(true);
    return barSet;
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) IBarDataSet(com.github.mikephil.charting.interfaces.datasets.IBarDataSet) ArrayList(java.util.ArrayList) BarEntry(com.github.mikephil.charting.data.BarEntry)

Aggregations

BarEntry (com.github.mikephil.charting.data.BarEntry)46 ArrayList (java.util.ArrayList)32 BarData (com.github.mikephil.charting.data.BarData)27 BarDataSet (com.github.mikephil.charting.data.BarDataSet)27 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)18 Transformer (com.github.mikephil.charting.utils.Transformer)6 Paint (android.graphics.Paint)5 BarBuffer (com.github.mikephil.charting.buffer.BarBuffer)5 Entry (com.github.mikephil.charting.data.Entry)5 BufferedReader (java.io.BufferedReader)5 IOException (java.io.IOException)5 Drawable (android.graphics.drawable.Drawable)3 IValueFormatter (com.github.mikephil.charting.formatter.IValueFormatter)3 MPPointF (com.github.mikephil.charting.utils.MPPointF)3 InputStreamReader (java.io.InputStreamReader)3 SuppressLint (android.annotation.SuppressLint)2 HorizontalBarBuffer (com.github.mikephil.charting.buffer.HorizontalBarBuffer)2 XAxis (com.github.mikephil.charting.components.XAxis)2 IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)2 File (java.io.File)2