Andrey1101

Работа с графиками

Jun 17th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.81 KB | None | 0 0
  1. package com.krabd.klient;
  2.  
  3. import android.app.Activity;
  4. import android.graphics.Color;
  5. import android.graphics.Typeface;
  6. import android.graphics.drawable.Drawable;
  7. import android.os.AsyncTask;
  8. import android.os.Bundle;
  9. import android.support.v4.content.ContextCompat;
  10. import android.util.Log;
  11. import android.view.MotionEvent;
  12. import android.view.View;
  13. import android.widget.SimpleCursorAdapter;
  14. import android.widget.Toast;
  15.  
  16. import com.github.mikephil.charting.charts.LineChart;
  17. import com.github.mikephil.charting.components.Legend;
  18. import com.github.mikephil.charting.components.XAxis;
  19. import com.github.mikephil.charting.components.YAxis;
  20. import com.github.mikephil.charting.data.Entry;
  21. import com.github.mikephil.charting.data.LineData;
  22. import com.github.mikephil.charting.data.LineDataSet;
  23. import com.github.mikephil.charting.highlight.Highlight;
  24. import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
  25. import com.github.mikephil.charting.listener.ChartTouchListener;
  26. import com.github.mikephil.charting.listener.OnChartGestureListener;
  27. import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
  28. import com.github.mikephil.charting.utils.Utils;
  29.  
  30. import org.apache.http.NameValuePair;
  31. import org.apache.http.message.BasicNameValuePair;
  32.  
  33. import java.util.ArrayList;
  34. import java.util.HashMap;
  35. import java.util.List;
  36.  
  37. public class StudentsGraph extends Activity {
  38.     LineChart mChart;
  39.     final int SUM=0;
  40.     final int COUNT=1;
  41.     @Override
  42.     protected void onCreate(Bundle savedInstanceState) {
  43.         super.onCreate(savedInstanceState);
  44.         setContentView(R.layout.activity_students_graph);
  45.  
  46.  
  47.         mChart = (LineChart) findViewById(R.id.chart1);
  48.         mChart.setDrawGridBackground(false);
  49.         //------
  50.         mChart.setOnClickListener(new View.OnClickListener() {
  51.             @Override
  52.             public void onClick(View v) {
  53.                 Toast.makeText(getApplication(),"Олллололол",Toast.LENGTH_SHORT).show();
  54.             }
  55.         });
  56.         //-----
  57.         // no description text
  58.         mChart.setDescription("");
  59.         mChart.setNoDataTextDescription("You need to provide data for the chart.");
  60.  
  61.         // enable touch gestures
  62.         mChart.setTouchEnabled(true);
  63.         mChart.setDragEnabled(true);
  64.         mChart.setScaleEnabled(true);
  65.         mChart.setPinchZoom(true);
  66.  
  67.         YAxis leftAxis = mChart.getAxisLeft();
  68.         leftAxis.removeAllLimitLines();
  69.         leftAxis.setAxisMaxValue(220f);
  70.         leftAxis.setAxisMinValue(0);
  71.         leftAxis.enableGridDashedLine(10f, 10f, 0f);
  72.         leftAxis.setDrawZeroLine(false);
  73.  
  74.         leftAxis.setDrawLimitLinesBehindData(true);
  75.  
  76.         mChart.getAxisRight().setEnabled(false);
  77.  
  78.         Legend l = mChart.getLegend();
  79.         l.setForm(Legend.LegendForm.LINE);
  80.         new GetAllProgress().execute();
  81.     }
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.     private class GetAllProgress extends AsyncTask<String, Integer, List<String[]>> {//получает результаты за  testы
  89.         @Override
  90.         protected List<String[]> doInBackground(String... params) {
  91.             // TODO Auto-generated method stub
  92.             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
  93.             Variable.stringresponse_stud = POSTRequest.POST_Data(
  94.                     nameValuePairs, "http://kushelev.ru/action/getAllMarksForTest.php");
  95.             return ParseJSON.parseStatistAll(Variable.stringresponse_stud,getApplicationContext());
  96.         }
  97.  
  98.         protected void onPostExecute(List<String[]> result) {
  99.             List<String[]> strings=result;
  100.  
  101.             HashMap<String,AvgMark> hashMap=new HashMap<>();
  102.             ArrayList<String> xVals = new ArrayList<>();// тут номера групп
  103.             AvgMark avgMark;
  104.             for (String[] res:result){
  105.                 if(hashMap.containsKey(res[0])){//если уже группу записали, то добаляем к сумме
  106.                     hashMap.put(res[0], (hashMap.get(res[0])).add(Float.valueOf(res[1])));
  107.                 }
  108.                 else{
  109.                     avgMark=new AvgMark(Float.valueOf(res[1]));
  110.                     hashMap.put(res[0],avgMark);//пишем сюда элемент средней оценки
  111.                     xVals.add(res[0]);//пишем номер группы в массив
  112.                 }
  113.             }
  114.  
  115.             ArrayList<Entry> yVals = new ArrayList<Entry>(xVals.size());//тут их оценки
  116.  
  117.             for (int i = 0; i < hashMap.size(); i++) {
  118.                 float val = hashMap.get(xVals.get(i)).getAvg();//отдаём среднее значение
  119.                 yVals.add(new Entry(val, i));// значение, позиция
  120.             }
  121.  
  122.             LineDataSet set1;
  123.  
  124.  
  125.             if (mChart.getData() != null &&
  126.                     mChart.getData().getDataSetCount() > 0) {
  127.                 set1 = (LineDataSet)mChart.getData().getDataSetByIndex(0);
  128.                 set1.setYVals(yVals);
  129.                 mChart.getData().setXVals(xVals);
  130.                 mChart.getData().notifyDataChanged();
  131.                 mChart.notifyDataSetChanged();
  132.             } else {
  133.                 set1 = new LineDataSet(yVals, "DataSet 1");
  134.                 set1.enableDashedLine(10f, 5f, 0f);
  135.                 set1.enableDashedHighlightLine(10f, 5f, 0f);
  136.                 set1.setColor(Color.BLACK);
  137.                 set1.setCircleColor(Color.BLACK);
  138.                 set1.setLineWidth(1f);
  139.                 set1.setCircleRadius(3f);
  140.                 set1.setDrawCircleHole(false);
  141.                 set1.setValueTextSize(9f);
  142.                 set1.setDrawFilled(true);
  143.                     set1.setFillColor(Color.BLACK);
  144.                 ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
  145.                 dataSets.add(set1); // add the datasets
  146.                 // create a data object with the datasets
  147.                 LineData data = new LineData(xVals, dataSets);
  148.                 // set data
  149.                 mChart.setData(data);
  150.                 mChart.invalidate();
  151.                 Log.e("TAG","Данные готовы");
  152.                 getActionBar().setTitle(getActionBar().getTitle()+" График успеваемости:");
  153.  
  154.  
  155.         }
  156.  
  157.  
  158.     }
  159.     }
  160.     public static class AvgMark{//обёртка для среднего результат
  161.         Float sum;
  162.         Float count;
  163.         public AvgMark(Float element){
  164.             this.sum=element;
  165.             this.count=1F;
  166.         }
  167.         AvgMark add(Float element){//добавить один элемент
  168.             this.sum+=element;
  169.             this.count++;//увеличим количство элеметов
  170.             return this;//класс отдаёт себя
  171.         }
  172.         Float getAvg(){
  173.             return sum/count;
  174.         }
  175.     }
  176. }
Add Comment
Please, Sign In to add comment