Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPAndroidChart Bar Chart with Various Bar Widths

I need to create a chart that has time along the x-axis and an area along the y-axis. The program is going to record data over a certain time differential for an activity and display the data in a graph, kind of like this -> https://i.sstatic.net/s4MiC.png but where every other bar would be alternating colors to correspond to a separate activity. I haven't been able to find a way to do this using MPAndroidChart yet, each entry seems to have a fixed width. If someone either knows how this can be achieved using MPAndroidChart or another open source library help would be appreciated, thanks!

EDIT:

This is the code I've been trying to work with:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data_report);  

    BarChart barChart = (BarChart) findViewById(R.id.barChart);
    List<BarEntry> entries = generateBarEntries();
    BarDataSet set = new BarDataSet(entries, "BarDataSet");
    BarData data = new BarData(set);
    barChart.setData(data);
}

private List<BarEntry> generateBarEntries() {
    List<BarEntry> entries = new ArrayList<>();
    for (int i = 0; i < timeValues.size(); i++) {
        float x = timeValues.get(i);
        float y = areaValues.get(i);
        entries.add(new BarEntry(x, y));
    }
    return entries;
}

timeValues is an ArrayList with values in seconds such as [3, 9, 21, 26, 33] and areaValues is also an ArrayList with the same size as timeValues with each value being 30 for now (i.e. [30, 30, 30, 30, 30]).

like image 584
Tommy Jackson Avatar asked Apr 23 '26 17:04

Tommy Jackson


1 Answers

Okay so I figured out how to go about this. Instead of creating a bar chart, I just created a line chart with horizontal lines and filled the area underneath each line.

LineChart timeChart = (LineChart) findViewById(R.id.timeChart);
List<ILineDataSet> dataSets = generateLineData();
LineData data = new LineData(dataSets);
timeChart.setData(data);
timeChart.invalidate();

private List<ILineDataSet> generateLineData() {
    List<ILineDataSet> dataSets = new ArrayList<>();
    for (int i = 0; i < areaValues.size(); i++) {
        List<Entry> barPoints = new ArrayList<>();
        Entry barPoint1;
        Entry barPoint2;
        LineDataSet dataSet;
        if (areaValues.get(i) >= 0) {
            barPoint1 = new Entry(timeValues.get(i), areaValues.get(i));
            barPoint2 = new Entry(timeValues.get(i+1), areaValues.get(i));
            barPoints.add(barPoint1);
            barPoints.add(barPoint2);
            dataSet = new LineDataSet(barPoints, "");
            dataSet.setColor(getResources().getColor(R.color.green));
            dataSet.setFillDrawable(ContextCompat.getDrawable(this, R.color.green));
        }
        dataSet.setDrawCircles(false);
        dataSet.setDrawFilled(true);
        dataSet.setDrawValues(false);
        dataSets.add(dataSet);
    }
    return dataSets;
}

Works fantastically.

like image 71
Tommy Jackson Avatar answered Apr 26 '26 07:04

Tommy Jackson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!