• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Bar graph creation problem using JSON

I try to create a Bar Graph using JSON data. The problem is when try to save a specific data in an Entry object, with the mention that i save each value in a ArrayList for iteration needed to put each value as an Entry in the graph(Y AXIS).
This is the request and plot of the graph part:
Java:
try {
                       JSONObject jsonObject = new JSONObject(response);

                       JSONObject open = jsonObject.getJSONObject("open");


                       for (int i = 0; i < open.length(); i++) {

                           int mon = open.getInt("hour");
                           int tue = open.getInt("day");
                           int wen = open.getInt("week");
                           int thu = open.getInt("month");
                           int fri = open.getInt("month_3");
                           int sat = open.getInt("month_6");
                           int sun = open.getInt("year");

                           //        dates = new ArrayList<>();
                           //        dates.add("Mon");
                           //        dates.add("Tue");
                           //        dates.add("Wen");          THIS IS THE ARRAY FOR THE X VALUES
                           //        dates.add("Thu");
                           //        dates.add("Fri");
                           //        dates.add("Sat");
                           //        dates.add("Sun");

                           xAxis1.add(dates.indexOf(0));

                           valoriAxaY.add(mon);
                           valoriAxaY.add(tue);
                           valoriAxaY.add(wen);
                           valoriAxaY.add(thu);            // THIS IS THE ARRAY FOR THE Y VALUES
                           valoriAxaY.add(fri);          
                           valoriAxaY.add(sat);
                           valoriAxaY.add(sun);

                           values = new BarEntry(valoriAxaY.indexOf(i), i); //THIS IS WHERE THE PROBLEM APPEARS
                                               // ALSO I NEED TO MENTION THAT I GET NO ERROR'S

                           yValues.add(values);

                       }

                   } catch (Exception e) {
                       e.printStackTrace();
                   }

                   BarDataSet barDataSet = new BarDataSet(yValues, "Prices");

                   yAxis = new ArrayList<>();
                   yAxis.add(barDataSet);
                   data = new BarData(dates, yAxis);
                   barChart.setData(data);

                   barDataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
                   barChart.setDescription("CryptoCurrency");
                   barChart.invalidate();
}

And this is the result :
 

Attachments

  • 35737606_2126912840715244_4929127497951346688_n.jpg
    35737606_2126912840715244_4929127497951346688_n.jpg
    22.5 KB · Views: 101
Last edited:
I try to create a Bar Graph using JSON data. The problem is when try to save a specific data in an Entry object, with the mention that i save each value in a ArrayList for iteration needed to put each value as an Entry in the graph(Y AXIS).
This is the request and plot of the graph part:

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'><tt>&#160; </tt></li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>try { </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;JSONObject jsonObject = new JSONObject (response ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;JSONObject open = jsonObject. getJSONObject ( 'open' ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; for ( int i = 0 ; i &lt; open. length ( ) ; i ++ ) { </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; int mon = open. getInt ( 'hour' ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; int tue = open. getInt ( 'day' ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; int wen = open. getInt ( 'week' ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; int thu = open. getInt ( 'month' ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; int fri = open. getInt ( 'month_3' ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; int sat = open. getInt ( 'month_6' ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; int sun = open. getInt ( 'year' ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; // &#160; &#160; &#160; &#160;dates = new ArrayList&lt;&gt;(); </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; // &#160; &#160; &#160; &#160;dates.add('Mon'); </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; // &#160; &#160; &#160; &#160;dates.add('Tue'); </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; // &#160; &#160; &#160; &#160;dates.add('Wen'); &#160; &#160; &#160; &#160; &#160;THIS IS THE ARRAY FOR THE X VALUES </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; // &#160; &#160; &#160; &#160;dates.add('Thu'); </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; // &#160; &#160; &#160; &#160;dates.add('Fri'); </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; // &#160; &#160; &#160; &#160;dates.add('Sat'); </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; // &#160; &#160; &#160; &#160;dates.add('Sun'); </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;xAxis1. add (dates. indexOf ( 0 ) ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;valoriAxaY. add (mon ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;valoriAxaY. add (tue ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;valoriAxaY. add (wen ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;valoriAxaY. add (thu ) ; &#160; &#160; &#160; &#160; &#160; &#160; // THIS IS THE ARRAY FOR THE Y VALUES </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;valoriAxaY. add (fri ) ; &#160; &#160; &#160; &#160; &#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;valoriAxaY. add (sat ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;valoriAxaY. add (sun ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;values = new BarEntry (valoriAxaY. indexOf (i ), i ) ; //THIS IS WHERE THE PROBLEM APPEARS </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; // ALSO I NEED TO MENTION THAT I GET NO ERROR'S </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;yValues. add (values ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; } </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; } catch ( Exception e ) { </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;e. printStackTrace ( ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; } </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;BarDataSet barDataSet = new BarDataSet (yValues, 'Prices' ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;yAxis = new ArrayList &lt;&gt; ( ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;yAxis. add (barDataSet ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;data = new BarData (dates, yAxis ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;barChart. setData (data ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;barDataSet. setColors (ColorTemplate. VORDIPLOM_COLORS ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;barChart. setDescription ( 'CryptoCurrency' ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160;barChart. invalidate ( ) ; </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>} </li>
</ul>

<ul style='margin-top:0; margin-bottom:0;'>
<li dir='ltr'>[/LIST] And this is the result :
</li>
</ul>
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones