private void previewY() {
   Viewport tempViewport = new Viewport(recordChart.getMaximumViewport());
   float dy = (tempViewport.height() + 0.2f) / 4;
   tempViewport.inset(0, dy);
   recordPreviewChart.setCurrentViewportWithAnimation(tempViewport);
   recordPreviewChart.setZoomType(ZoomType.VERTICAL);
 }
    @Override
    public void onViewportChanged(Viewport newViewport) {
      if (!updatingPreviewViewport) {
        updatingChartViewport = true;
        previewChart.setZoomType(ZoomType.HORIZONTAL);
        previewChart.setCurrentViewport(newViewport);
        updatingChartViewport = false;

        if (iobcobFragmentObject.getView() != null) { // Fragment is loaded
          LineChartView iobcobPastChart = (LineChartView) findViewById(R.id.iobcobPast);
          Viewport iobv =
              new Viewport(
                  chart
                      .getMaximumViewport()); // Update the IOB COB Line Chart Viewport to stay
                                              // inline with the preview
          iobv.left = newViewport.left;
          iobv.right = newViewport.right;
          iobv.top = extendedGraphBuilder.yCOBMax.floatValue();
          iobv.bottom = extendedGraphBuilder.yCOBMin.floatValue();
          iobcobPastChart.setCurrentViewport(iobv);
        }
        if (basalvsTempBasalObject.getView() != null) {
          LineChartView bvbChart = (LineChartView) findViewById(R.id.basalvsTempBasal_LineChart);
          Viewport bvbv = new Viewport(chart.getMaximumViewport());
          bvbv.left = newViewport.left;
          bvbv.right = newViewport.right;
          bvbv.top = extendedGraphBuilder.maxBasal.floatValue();
          bvbv.bottom = -4; // TODO: 14/09/2015 how to make this negative of maxBolus?
          bvbChart.setCurrentViewport(bvbv);
        }
      }
    }
 private void previewXY() {
   // Better to not modify viewport of any recordChart directly so create a copy.
   Viewport tempViewport = new Viewport(recordChart.getMaximumViewport());
   // Make temp viewport smaller.
   float dx = tempViewport.width() / 4;
   float dy = tempViewport.height() / 4;
   tempViewport.inset(dx, dy);
   recordPreviewChart.setCurrentViewportWithAnimation(tempViewport);
 }
  private void setUpChartArea() {
    Viewport currentViewport = new Viewport(chart.getMaximumViewport());

    currentViewport.left = (float) (chart.getMaximumViewport().left * 1.9);
    currentViewport.right = (float) (chart.getMaximumViewport().right * 0.1);

    chart.setCurrentViewport(currentViewport);

    chart.setZoomEnabled(false);
  }
 ///////// VIEWPORT RELATED//////////////
 public Viewport advanceViewport(Chart chart, Chart previewChart) {
   viewport = new Viewport(previewChart.getMaximumViewport());
   viewport.inset((float) ((86400000 / 2.5) / FUZZER), 0);
   double distance_to_move =
       ((new Date().getTime()) / FUZZER)
           - viewport.left
           - (((viewport.right - viewport.left) / 2));
   viewport.offset((float) distance_to_move, 0);
   return viewport;
 }
 private void previewX(boolean animate) {
   Viewport tempViewport = new Viewport(chart.getMaximumViewport());
   float dx = tempViewport.width() / 4;
   tempViewport.inset(dx, 0);
   if (animate) {
     previewChart.setCurrentViewportWithAnimation(tempViewport);
   } else {
     previewChart.setCurrentViewport(tempViewport);
   }
   previewChart.setZoomType(ZoomType.HORIZONTAL);
 }
 @Override
 protected void onResume() {
   super.onResume();
   checkEula();
   _broadcastReceiver =
       new BroadcastReceiver() {
         @Override
         public void onReceive(Context ctx, Intent intent) {
           if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0) {
             updateCurrentBgInfo();
           }
         }
       };
   newDataReceiver =
       new BroadcastReceiver() {
         @Override
         public void onReceive(Context ctx, Intent intent) {
           holdViewport.set(0, 0, 0, 0);
           setupCharts();
           updateCurrentBgInfo();
         }
       };
   registerReceiver(_broadcastReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
   registerReceiver(newDataReceiver, new IntentFilter(Intents.ACTION_NEW_BG_ESTIMATE_NO_DATA));
   holdViewport.set(0, 0, 0, 0);
   setupCharts();
   updateCurrentBgInfo();
 }
  @Override
  protected void onResume() {
    super.onResume();

    // xdrip start
    extendedGraphBuilder = new ExtendedGraphBuilder(getApplicationContext());

    _broadcastReceiver =
        new BroadcastReceiver() {
          @Override
          public void onReceive(Context ctx, Intent intent) {
            if (intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0) {
              setupCharts();
              displayCurrentInfo();
              holdViewport.set(0, 0, 0, 0);
            }
          }
        };
    newDataReceiver =
        new BroadcastReceiver() {
          @Override
          public void onReceive(Context ctx, Intent intent) {
            setupCharts();
            displayCurrentInfo();
            holdViewport.set(0, 0, 0, 0);
          }
        };
    registerReceiver(_broadcastReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
    registerReceiver(newDataReceiver, new IntentFilter(Intents.ACTION_NEW_BG));
    setupCharts();
    displayCurrentInfo();
    holdViewport.set(0, 0, 0, 0);
    // xdrip end
  }
    public void setupChart() {
      basalvsTempBasalChart.setZoomType(ZoomType.HORIZONTAL);
      basalvsTempBasalChart.setLineChartData(extendedGraphBuilder.basalvsTempBasalData());

      Viewport iobv =
          new Viewport(
              basalvsTempBasalChart
                  .getMaximumViewport()); // Sets the min and max for Top and Bottom of the
                                          // viewpoint
      iobv.top = extendedGraphBuilder.maxBasal.floatValue();
      iobv.bottom = -(extendedGraphBuilder.maxBasal.floatValue() - 1);
      basalvsTempBasalChart.setMaximumViewport(iobv);
      iobv.left = previewChart.getCurrentViewport().left;
      iobv.right = previewChart.getCurrentViewport().right;
      basalvsTempBasalChart.setCurrentViewport(iobv);

      basalvsTempBasalChart.setViewportCalculationEnabled(true);
      // iobcobPastChart.setViewportChangeListener(new ChartViewPortListener());
      // //causes a crash, no idea why #// TODO: 28/08/2015
    }
    public void setupChart() {

      iobcobPastChart.setZoomType(ZoomType.HORIZONTAL);
      iobcobPastChart.setLineChartData(extendedGraphBuilder.iobcobPastLineData());

      Viewport iobv =
          new Viewport(
              iobcobPastChart
                  .getMaximumViewport()); // Sets the min and max for Top and Bottom of the
                                          // viewpoint
      iobv.top = Float.parseFloat(extendedGraphBuilder.yCOBMax.toString());
      iobv.bottom = Float.parseFloat(extendedGraphBuilder.yCOBMin.toString());
      iobcobPastChart.setMaximumViewport(iobv);
      iobv.left = previewChart.getCurrentViewport().left;
      iobv.right = previewChart.getCurrentViewport().right;
      iobcobPastChart.setCurrentViewport(iobv);

      iobcobPastChart.setViewportCalculationEnabled(true);
      // iobcobPastChart.setViewportChangeListener(new ChartViewPortListener());
      // //causes a crash, no idea why #// TODO: 28/08/2015
    }
    @Override
    public void onViewportChanged(Viewport newViewport) {
      if (!updatingChartViewport) {
        updatingPreviewViewport = true;
        chart.setZoomType(ZoomType.HORIZONTAL);
        chart.setCurrentViewport(newViewport);
        tempViewport = newViewport;
        updatingPreviewViewport = false;

        if (iobcobFragmentObject.getView() != null) { // Fragment is loaded
          LineChartView iobcobPastChart = (LineChartView) findViewById(R.id.iobcobPast);
          iobcobPastChart.setZoomType(ZoomType.HORIZONTAL);
          iobcobPastChart.setCurrentViewport(newViewport);
          Viewport iobv =
              new Viewport(
                  iobcobPastChart
                      .getMaximumViewport()); // Update the IOB COB Line Chart Viewport to stay
                                              // inline with the preview
          iobv.left = newViewport.left;
          iobv.right = newViewport.right;
          iobcobPastChart.setCurrentViewport(iobv);
        }
        if (basalvsTempBasalObject.getView() != null) {
          LineChartView bvbChart = (LineChartView) findViewById(R.id.basalvsTempBasal_LineChart);
          bvbChart.setZoomType(ZoomType.HORIZONTAL);
          bvbChart.setCurrentViewport(newViewport);
          Viewport bvbv = new Viewport(bvbChart.getMaximumViewport());
          bvbv.left = newViewport.left;
          bvbv.right = newViewport.right;
          bvbChart.setCurrentViewport(bvbv);
        }
      }
      if (updateStuff == true) {
        holdViewport.set(newViewport.left, newViewport.top, newViewport.right, newViewport.bottom);
      }
    }
  private void updateGraph(Cursor cursor) {
    // set up the goal line
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    int goalCalories = Integer.parseInt(prefs.getString("preference_calorie_goal", "2000"));
    List<PointValue> goalValues = new ArrayList<>();

    // set up the data
    List<PointValue> values = new ArrayList<>();
    cursor.moveToFirst();

    if (!cursor.isAfterLast()) {
      goalValues.add(new PointValue(getTimeFromRow(cursor), goalCalories));
    }

    while (cursor.isAfterLast() == false) {
      float value = cursor.getFloat(cursor.getColumnIndex("total"));
      // Log.d("graph", "adding " + value + " on " + sdf.format(getTimeFromRow(cursor)));
      values.add(new PointValue(getTimeFromRow(cursor), value));
      cursor.moveToNext();
    }

    cursor.moveToLast();
    goalValues.add(new PointValue(getTimeFromRow(cursor), goalCalories));

    List<Line> lines = new ArrayList<Line>();

    Line goalLine = new Line(goalValues).setColor(Color.parseColor("#ff0000"));
    lines.add(goalLine);

    Line line = new Line(values).setColor(Color.parseColor("#44a134")).setCubic(true);
    // line.setFilled(true);
    // line.setHasLabels(true);

    lines.add(line);

    LineChartData data = new LineChartData();
    data.setLines(lines);

    Axis timeAxis = new Axis();
    // timeAxis.setName("");
    // timeAxis.setTextColor(ChartUtils.COLOR_ORANGE);
    timeAxis.setMaxLabelChars(5);
    timeAxis.setFormatter(new TimestampAxisFormatter());
    timeAxis.setHasLines(true);
    timeAxis.setHasTiltedLabels(true);
    data.setAxisXBottom(timeAxis);

    Axis calorieAxis = new Axis();
    // weightAxis.setName("");
    // weightAxis.setTextColor(ChartUtils.COLOR_GREEN);
    calorieAxis.setMaxLabelChars(7);
    calorieAxis.setFormatter(new SimpleAxisValueFormatter().setAppendedText(" C".toCharArray()));
    calorieAxis.setHasLines(true);
    calorieAxis.setHasTiltedLabels(true);
    data.setAxisYLeft(calorieAxis);

    hellochart.setLineChartData(data);
    hellochart.setZoomType(ZoomType.HORIZONTAL);
    hellochart.setOnValueTouchListener(new ValueTouchListener());

    // set up zoom so we can see 0 calories
    Viewport viewport = new Viewport(hellochart.getMaximumViewport());
    viewport.bottom = 0;
    hellochart.setMaximumViewport(viewport);
    hellochart.setCurrentViewport(viewport);

    hellochart.invalidate();
  }
    @Override
    public View onCreateView(
        LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.fragment_view_pager_charts, container, false);
      RelativeLayout layout = (RelativeLayout) rootView;
      int sectionNum = getArguments().getInt(ARG_SECTION_NUMBER);
      switch (sectionNum) {
        case 1:
          LineChartView lineChartView = new LineChartView(getActivity());
          lineChartView.setLineChartData(generateLineChartData());
          lineChartView.setZoomType(ZoomType.HORIZONTAL);

          /** Note: Chart is within ViewPager so enable container scroll mode. * */
          lineChartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);

          layout.addView(lineChartView);
          break;
        case 2:
          ColumnChartView columnChartView = new ColumnChartView(getActivity());
          columnChartView.setColumnChartData(generateColumnChartData());
          columnChartView.setZoomType(ZoomType.HORIZONTAL);

          /** Note: Chart is within ViewPager so enable container scroll mode. * */
          columnChartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);

          layout.addView(columnChartView);
          break;
        case 3:
          BubbleChartView bubbleChartView = new BubbleChartView(getActivity());
          bubbleChartView.setBubbleChartData(generateBubbleChartData());
          bubbleChartView.setZoomType(ZoomType.HORIZONTAL_AND_VERTICAL);

          /** Note: Chart is within ViewPager so enable container scroll mode. * */
          bubbleChartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);

          layout.addView(bubbleChartView);
          break;
        case 4:
          PreviewLineChartView previewLineChartView = new PreviewLineChartView(getActivity());
          previewLineChartView.setLineChartData(generatePreviewLineChartData());

          /** Note: Chart is within ViewPager so enable container scroll mode. * */
          previewLineChartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);

          Viewport tempViewport = new Viewport(previewLineChartView.getMaximumViewport());
          float dx = tempViewport.width() / 6;
          tempViewport.inset(dx, 0);
          previewLineChartView.setCurrentViewport(tempViewport);
          previewLineChartView.setZoomType(ZoomType.HORIZONTAL);

          layout.addView(previewLineChartView);
          break;
        case 5:
          PieChartView pieChartView = new PieChartView(getActivity());
          pieChartView.setPieChartData(generatePieChartData());

          /** Note: Chart is within ViewPager so enable container scroll mode. * */
          pieChartView.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);

          layout.addView(pieChartView);
          break;
      }

      return rootView;
    }