Ejemplo n.º 1
0
  private void createChart(View v) {
    BarChart chart = (BarChart) v.findViewById(R.id.barChart);
    chart.clear();
    PSDatePickerView datePickerView = (PSDatePickerView) v.findViewById(R.id.date_picker_view);
    PSUserDataCollection dataCollection = PSDataStore.getInstance().getUserDataCollection();
    ArrayList<BarEntry> entriesAgg = new ArrayList<BarEntry>();
    ArrayList<String> xVals = new ArrayList<String>();

    int idx = 0;
    for (Calendar cur = (Calendar) datePickerView.getStartDate().clone();
        PSUtil.beforeCalender(cur, datePickerView.getEndDate());
        cur.add(Calendar.DATE, 1)) {
      PSDailyData dailyData = dataCollection.getDailyData(cur.getTimeInMillis());
      BarEntry ent = null;
      if (dailyData != null) {
        ent = new BarEntry(dailyData.getFormula(), idx, dailyData);
      }
      if (ent != null) {
        entriesAgg.add(ent);
      }
      xVals.add(PSUtil.weekDays[idx]);

      idx++;
    }

    BarDataSet setAgg = new BarDataSet(entriesAgg, PSDataStore.getInstance().getUser().getName());
    setAgg.setAxisDependency(YAxis.AxisDependency.LEFT);
    setAgg.setColor(getResources().getColor(R.color.ps_blue_sky));

    ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
    dataSets.add(setAgg);

    // BarData
    BarData data = new BarData(xVals, dataSets);
    chart.setData(data);
    // Config chart
    chart.setDescription("");
    chart.setDrawHighlightArrow(true);
    // Config axises
    YAxis leftAxis = chart.getAxisLeft();
    YAxis rightAxis = chart.getAxisRight();
    leftAxis.setDrawGridLines(false);
    rightAxis.setDrawGridLines(false);
    // Config limit line
    LimitLine ll = new LimitLine(60f);
    ll.setLineColor(Color.CYAN);
    ll.setLineWidth(1f);
    leftAxis.removeAllLimitLines();
    leftAxis.addLimitLine(ll);
    // Config marker
    PSDailyDataMarkerView markerView =
        new PSDailyDataMarkerView(v.getContext(), R.layout.ps_daily_data_marker_view);
    chart.setMarkerView(markerView);
    // Legend
    Legend l = chart.getLegend();
    l.setEnabled(false);
    // Show data
    chart.animateY(600);
  }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootview = inflater.inflate(R.layout.fragment_graph, container, false);
    chart = (BarChart) rootview.findViewById(R.id.chart);

    String url =
        "http://codez.savonia.fi/etp4301_2015_r1/Test.php?key=SK1-tekuenergy&Tags=4096&obj=OPI-JKL02&Date=2015-10-31&measuresInHour=12&graphScaleInHours=24&dataPrecision=hour&roundPrecision=2";
    // String url =
    // "http://codez.savonia.fi/etp4301_2015_r1/Test.php?key=SK101-kuopioenergy&Tags=666&Date=2014-9-23&obj=KPOE&measuresInHour=1&graphScaleInHours=24&dataPrecision=hour&roundPrecision=2";
    WebService request = new WebService(GraphActivityFragment.this, "GET");

    Measurements = new ArrayList<BarEntry>();

    dataSets = new ArrayList<BarDataSet>();
    BarDataSet setComp1 = new BarDataSet(Measurements, "Sähkötolpat");
    setComp1.setAxisDependency(YAxis.AxisDependency.LEFT);

    chart.setAutoScaleMinMaxEnabled(true);
    chart.setDrawValueAboveBar(false);

    xVals = new ArrayList<String>();
    xVals.add("00:00");
    xVals.add("01:00");
    xVals.add("02:00");
    xVals.add("03:00");
    xVals.add("04:00");
    xVals.add("05:00");
    xVals.add("06:00");
    xVals.add("07:00");
    xVals.add("08:00");
    xVals.add("09:00");
    xVals.add("10:00");
    xVals.add("11:00");
    xVals.add("12:00");
    xVals.add("13:00");
    xVals.add("14:00");
    xVals.add("15:00");
    xVals.add("16:00");
    xVals.add("17:00");
    xVals.add("18:00");
    xVals.add("19:00");
    xVals.add("20:00");
    xVals.add("21:00");
    xVals.add("22:00");
    xVals.add("23:00");
    xVals.add("24:00");

    request.execute(url);
    return rootview;
  }
  @Override
  public void HttpResponse(String response) {

    Date xPoint = new Date();
    String yPoint = "";
    try {
      JSONObject reader = new JSONObject(response);
      String xString = reader.getString("Results");

      String[] test = xString.split(",");
      for (String s : test) {
        s = s.replace('{', ' ');
        s = s.replace('}', ' ');
        s = s.replace('"', ' ');
        String[] mark = s.split(":");
        Integer intTest;

        if (s.substring(12, 13).equals("0")) {
          // Log.d("paiva1",s.substring(13, 14) );
          intTest = Integer.parseInt(s.substring(13, 14));
        } else {
          // Log.d("paiva2",s.substring(12, 14).replace(" ", "") );
          intTest = Integer.parseInt(s.substring(12, 14).replace(" ", ""));
        }

        // String str = "22:00";
        // SimpleDateFormat ft = new SimpleDateFormat ("HH:mm");

        // xPoint = ft.parse(str);
        yPoint = mark[2];

        BarEntry measure = new BarEntry(Float.parseFloat(yPoint), intTest); //
        Measurements.add(measure);
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }

    BarDataSet setComp1 = new BarDataSet(Measurements, "Sähkötolpat");
    setComp1.setAxisDependency(YAxis.AxisDependency.LEFT);

    dataSets.add(setComp1);
    BarData data = new BarData(xVals, dataSets);
    chart.setData(data);
    chart.invalidate();
  }