MPAndroidChart icon indicating copy to clipboard operation
MPAndroidChart copied to clipboard

How to show string values for xaxis scatter chart in android.

Open fabritronixdesignlabs opened this issue 5 years ago • 0 comments

Hello All,

I'm doing medical related application. In my application want to show the scatter chart by using reading values as y axis and days are x axis . When I try to show string values for x axis in scatter chart. But x axis not displaying the all values. However the y axis values displaying fine. Ans also I'm giving the filter option like Fasting, Pre meal,Post Meal,Random.

This is my code snippet

                try {
                    JSONArray readingArray = new JSONArray(reading_data);

                    for(int i = readingArray.length()-1; i >= 0;i--){

                        JSONObject jsonObject = readingArray.getJSONObject(i);

                        String created = jsonObject.getString("created_at");

                        int value = jsonObject.getInt("value");

                        int r_type = jsonObject.getInt("reading_type");

                        String[] dateSplit = created.split("-");

                        String monthSplit = dateSplit[1];

                        String lastDate = dateSplit[0]+dateSplit[1]+dateSplit[2];

                        if(monthSplit.equalsIgnoreCase("01")){
                            month = dateSplit[2]+"JAN";
                        }else if(monthSplit.equalsIgnoreCase("02")){
                            month = dateSplit[2]+"FEB";
                        }else if(monthSplit.equalsIgnoreCase("03")){
                            month = dateSplit[2]+"MAR";
                        }else if(monthSplit.equalsIgnoreCase("04")){
                            month = dateSplit[2]+"APR";
                        }else if(monthSplit.equalsIgnoreCase("05")){
                            month = dateSplit[2]+"MAY";
                        }else if(monthSplit.equalsIgnoreCase("06")){
                            month = dateSplit[2]+"JUN";
                        }else if(monthSplit.equalsIgnoreCase("07")){
                            month = dateSplit[2]+"JLY";
                        }else if(monthSplit.equalsIgnoreCase("08")){
                            month = dateSplit[2]+"AUG";
                        }else if(monthSplit.equalsIgnoreCase("09")){
                            month = dateSplit[2]+"SEP";
                        }else if(monthSplit.equalsIgnoreCase("10")){
                            month = dateSplit[2]+"OCT";
                        }else if(monthSplit.equalsIgnoreCase("11")){
                            month = dateSplit[2]+"NOV";
                        }else if(monthSplit.equalsIgnoreCase("12")){
                            month = dateSplit[2]+"DEC";
                        }

                        int compareDate = Integer.parseInt(lastDate);

                        if(compareDate > Integer.parseInt(lastWeekDate) && compareDate < Integer.parseInt(currentDate)){

                            GraphModel graphModel = new GraphModel();
                            graphModel.setValue(value);
                            graphModel.setCreated_at(created);
                            graphModel.setType(r_type);
                            graphModelList.add(graphModel);
                            Log.d(TAG,"Glucose Value :"+value+"\n"+"Created At :"+created+"\n"+"Reading Type :"+r_type);
                            if(value <=70){
                                entryList1.add(new BarEntry(i,value));
                                daysList.add(month);
                            }else if(value >= 70 && value <= 180){
                                entryList2.add(new BarEntry(i,value));
                                daysList.add(month);
                            }else{
                                entryList3.add(new BarEntry(i,value));
                                daysList.add(month);
                            }
                        }


                        //initialize graph
                        if(i == 0){

                            Log.d(TAG,"days are ::::::"+daysList.toString()+"\n"+"all count :"+daysList.size());
                            int allCount = daysList.size();

                            xaxisView.setText(daysList.toString());

                            XAxis xAxis = scatterChart.getXAxis();

                            //xAxis.setLabelCount(allCount);
                            xAxis.setValueFormatter(new IndexAxisValueFormatter(daysList){
                                @Override
                                public String getFormattedValue(float value) {
                                    int x = (int)value / 10;
                                    if(x >= 0 && x < daysList.size()){
                                        Log.d(TAG,"days are :::"+daysList.get(x));
                                        return daysList.get(x);
                                    }else {
                                        return "";
                                    }
                                }
                            });
                            xAxis.setAvoidFirstLastClipping(true);
                            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
                            xAxis.setDrawGridLines(false);
                            xAxis.setDrawAxisLine(false);
                            xAxis.setGranularity(1f);
                            xAxis.setGranularityEnabled(true);
                            xAxis.setCenterAxisLabels(true);
                            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

                            //Y axis
                            YAxis yAxisRight = scatterChart.getAxisRight();
                            yAxisRight.setEnabled(false);
                            YAxis yAxisLeft = scatterChart.getAxisLeft();
                            yAxisLeft.setLabelCount(6,true);
                            yAxisLeft.setDrawAxisLine(false);
                            yAxisLeft.setAxisMinimum(0);
                            yAxisLeft.setAxisMaximum((500));



                            //Collections.sort(daysList);
                            Collections.sort(entryList1,new EntryXComparator());
                            Collections.sort(entryList2,new EntryXComparator());
                            Collections.sort(entryList3,new EntryXComparator());

                            scatterDataSet1 = new ScatterDataSet(entryList1, "");
                            scatterDataSet1.setColors(ContextCompat.getColor(getActivity(),R.color.orange_color));

                            scatterDataSet2 = new ScatterDataSet(entryList2,"");
                            scatterDataSet2.setColor(ContextCompat.getColor(getActivity(),R.color.colorGreen));

                            scatterDataSet3 = new ScatterDataSet(entryList3,"");
                            scatterDataSet3.setColor(ContextCompat.getColor(getActivity(),R.color.colorRed));

                            ArrayList<IScatterDataSet> dataSets = new ArrayList<>();
                            dataSets.add(scatterDataSet1); // add the data sets
                            dataSets.add(scatterDataSet2);
                            dataSets.add(scatterDataSet3);

                            scatterData = new ScatterData(dataSets);
                            scatterChart.setData(scatterData);
                            scatterChart.setVisibleXRangeMaximum(5);
                            scatterChart.notifyDataSetChanged();
                            scatterChart.invalidate();
                            scatterChart.animateXY(2000,2500);


                            scatterDataSet1.setValueTextColor(Color.BLACK);
                            scatterDataSet1.setValueTextSize(10f);
                            scatterDataSet1.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
                            scatterDataSet1.setDrawValues(false);


                            scatterDataSet2.setValueTextColor(Color.BLACK);
                            scatterDataSet2.setValueTextSize(10f);
                            scatterDataSet2.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
                            scatterDataSet2.setDrawValues(false);

                            scatterDataSet3.setValueTextColor(Color.BLACK);
                            scatterDataSet3.setValueTextSize(10f);
                            scatterDataSet3.setScatterShape(ScatterChart.ScatterShape.CIRCLE);
                            scatterDataSet3.setDrawValues(false);
                        }
                    }

                } catch (JSONException | NegativeArraySizeException e) {
                    e.printStackTrace();
                }

Can any one help me how to display correct values in scatter x axis.Please help me,trying to do from past one week. Thanks in Advance.

fabritronixdesignlabs avatar Feb 26 '21 12:02 fabritronixdesignlabs