private void loadData(boolean drawLast) {
    ChartCollection collection =
        fromDevice ? fileManager.getCollection() : fileManager.getPcaCollection();
    if (collection != null) {
      int size = collection.size();
      if (size > 0) {
        List<Entry> entries = new ArrayList<>();
        List<String> xVals = new ArrayList<>();

        ChartData data = collection.getCharData(size - 1);
        Map<Double, Double> values = data.getData();
        List<Double> keyList = new ArrayList<>();
        keyList.addAll(values.keySet());
        Collections.sort(keyList);

        for (Double x : keyList) {
          Number numX = x;
          int intX = numX.intValue();
          xVals.add(String.valueOf(x));

          double value = values.get(x);

          Number num = value;
          float val = num.floatValue();
          Entry entry = new Entry(val, intX);
          entries.add(entry);
        }

        LineDataSet dataSet = new LineDataSet(entries, "Dados");
        dataSet.setDrawCircleHole(false);
        dataSet.setDrawCircles(false);
        LineData chartData = new LineData(xVals, dataSet);
        chart.setData(chartData);

        totalGraphs = size;
        if (drawLast) {
          currentGraph = totalGraphs;
        } else {
          if (currentGraph < 1 || currentGraph > size) {
            currentGraph = totalGraphs;
          }
        }

        mainHandler.post(
            new Runnable() {
              @Override
              public void run() {
                chart.notifyDataSetChanged();
                chart.invalidate();

                txtTotal.setText(String.valueOf(totalGraphs));
                txtCurrent.setText(String.valueOf(currentGraph));
              }
            });
      }
    }
  }
    @Override
    public Map<String, ChartCollection> loadInBackground() {
      ChartCollection collection = new ChartCollection("PCA");

      File defaultFolder = mFileManager.getDefaultFolder();
      Uri uriDefaultFolder = Uri.parse("file://" + defaultFolder.getAbsolutePath());

      for (String filename : mFiles) {
        Uri uriFile = uriDefaultFolder.buildUpon().appendPath(filename).build();
        ChartCollection fileCollection = mFileManager.readFileForPca(uriFile);
        if (fileCollection != null) {
          for (ChartData chartData : fileCollection) {
            collection.addChartData(chartData);
          }
        }
      }

      if (mNumComponents > collection.size()) {
        mShowComponentsMsg = true;
        mNumComponents = collection.size();
      }

      double[][] array = new double[collection.size()][2048];
      for (int i = 0; i < collection.size(); i++) {
        for (int j = 0; j < 2048; j++) {
          array[i][j] = collection.getCharData(i).getData().get((double) j);
        }
      }
      Matrix matrix = new Matrix(array);

      //            SingularValueDecomposition svd = matrix.svd();
      //            SingularValueDecomposition svd = new SingularValueDecomposition(matrix);
      Pca pca = new Pca(matrix, mNumComponents);

      StringWriter strWriter = new StringWriter();
      PrintWriter writer = new PrintWriter(strWriter);
      matrix.print(writer, 10, 0);
      Log.d("Visio", "Matrix = " + strWriter.toString());

      Map<String, ChartCollection> pcaCollections = new HashMap<>(2);

      ChartCollection lCollection = new ChartCollection("PCA - L");

      Matrix lMatrix = pca.getL();
      double[][] lArray = lMatrix.getArray();
      for (int i = 0; i < lMatrix.getRowDimension(); i++) {
        Map<Double, Double> data = new HashMap<>();

        for (int j = 0; j < lMatrix.getColumnDimension(); j++) {
          data.put((double) j, lArray[i][j]);
        }

        ChartData chartData = new ChartData("PCA", "PCA", new Date().getTime(), data);
        lCollection.addChartData(chartData);
      }
      pcaCollections.put("L", lCollection);

      ChartCollection tCollection = new ChartCollection("PCA - T");

      Matrix tMatrix = pca.getT();
      double[][] tArray = tMatrix.getArray();
      for (int i = 0; i < tMatrix.getRowDimension(); i++) {
        Map<Double, Double> data = new HashMap<>();

        for (int j = 0; j < tMatrix.getColumnDimension(); j++) {
          data.put((double) j, tArray[i][j]);
        }

        ChartData chartData = new ChartData("PCA", "PCA", new Date().getTime(), data);
        tCollection.addChartData(chartData);
      }
      pcaCollections.put("T", tCollection);

      return pcaCollections;
    }