android - How to set String value of xAxis in MPAndroidChart? -


i want make line chart graph, have problem show value string in xaxis, im used library github mpandroidchart linechart. please me how add string value , question want ask

private void drawlinechartline(){          private float[] ydatal = {40, 60, 70, 80};         private string[] xdatal = {"week 1", "week 1" , "week 3" , "week 4"};           arraylist<entry> yentrys = new arraylist<>();          final arraylist<string> xentrys = new arraylist<>();          for(int = 0; < ydatal.length; i++){             yentrys.add(new entry(ydatal[i] ,i));         }          for(int = 1; < xdatal.length; i++){             xentrys.add(xdatal[i]);         }          //create data set         linedataset linedataset = new linedataset(yentrys, "assa");          xaxis xaxis = linechart.getxaxis();         xaxis.setposition(xaxis.xaxisposition.bottom);         xaxis.setdrawgridlines(false);           xaxis.setvalueformatter(new iaxisvalueformatter() {             @override             public string getformattedvalue(float value, axisbase axis) {                 return xentrys.get((int) value);             }         });          linedata linedata = new linedata(linedataset);         linechart.setdata(linedata);         linechart.invalidate(); } 

i got error

invalid index 40, size 6

in code

xaxis.setvalueformatter(new iaxisvalueformatter() {             @override             public string getformattedvalue(float value, axisbase axis) {                 return xentrys.get((int) value);             }         }); 

you're accessing arraylist index way out of bounds, because trying use value index. need find index of value return it.

xaxis.setvalueformatter(new iaxisvalueformatter() {     @override     public string getformattedvalue(float value, axisbase axis) {        (int = 0 ; < yentrys.size(); ++i) {            if (yentrys.get(i).equals(value)) {                return xentrys.get(i);            }        }        return null;     } }); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -