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);
  }
    private void generateColumnData() {

      int numSubcolumns = 1;
      int numColumns = months.length;

      List<AxisValue> axisValues = new ArrayList<AxisValue>();
      List<Column> columns = new ArrayList<Column>();
      List<SubcolumnValue> values;
      for (int i = 0; i < numColumns; ++i) {

        values = new ArrayList<SubcolumnValue>();
        for (int j = 0; j < numSubcolumns; ++j) {
          values.add(new SubcolumnValue((float) Math.random() * 50f + 5, ChartUtils.pickColor()));
        }

        axisValues.add(new AxisValue(i).setLabel(months[i]));

        columns.add(new Column(values).setHasLabelsOnlyForSelected(true));
      }

      columnData = new ColumnChartData(columns);

      columnData.setAxisXBottom(new Axis(axisValues).setHasLines(true));
      columnData.setAxisYLeft(new Axis().setHasLines(true).setMaxLabelChars(2));

      chartBottom.setColumnChartData(columnData);

      // Set value touch listener that will trigger changes for chartTop.
      chartBottom.setOnValueTouchListener(new ValueTouchListener());

      // Set selection mode to keep selected month column highlighted.
      chartBottom.setValueSelectionEnabled(true);

      chartBottom.setZoomType(ZoomType.HORIZONTAL);

      // chartBottom.setOnClickListener(new View.OnClickListener() {
      //
      // @Override
      // public void onClick(View v) {
      // SelectedValue sv = chartBottom.getSelectedValue();
      // if (!sv.isSet()) {
      // generateInitialLineData();
      // }
      //
      // }
      // });

    }
 @Override
 public void onViewCreated(View v, Bundle savedInstanceState) {
   autonTotes = (ColumnChartView) v.findViewById(R.id.chart_auton);
   autonTotes.setZoomEnabled(false);
   team = ((TeamActivity) getActivity()).getTeam();
   loadNumStacks();
 }
    // Updates Stats
    public static JSONObject updateChart(Activity a) {

      JSONObject reply = new JSONObject();
      List<Stats> statList = Stats.updateActiveBarChart(a.getBaseContext());

      if (iobcobChart != null || statList != null || statList.size() > 0) {
        // reloads charts with Treatment data
        iobcobChart.setColumnChartData(extendedGraphBuilder.iobcobFutureChart(statList));
        try {
          reply.put("iob", String.format(Locale.ENGLISH, "%.2f", statList.get(0).iob));
          reply.put("cob", String.format(Locale.ENGLISH, "%.2f", statList.get(0).cob));
        } catch (JSONException e) {
          Crashlytics.logException(e);
          e.printStackTrace();
        }
        return reply;
      } else {
        try {
          reply.put("iob", String.format(Locale.ENGLISH, "%.2f", 0.00));
          reply.put("cob", String.format(Locale.ENGLISH, "%.2f", 0.00));
        } catch (JSONException e) {
          Crashlytics.logException(e);
          e.printStackTrace();
        }
        return reply;
      }
    }
  private void updateChart(int total) {
    int numSubcolumns = 60;
    int numColumns = 1000;
    List<float[]> flist = new ArrayList<>();
    //        if (!Constants.Queue_DrawRealtimewaterfall.isEmpty()) {
    //            flist = Constants.Queue_DrawRealtimewaterfall.poll();
    if (!Constants.Queue_DrawRealtimeSpectrum.isEmpty()) {
      flist = Constants.Queue_DrawRealtimeSpectrum.poll();
      int realsize = Constants.Queue_DrawRealtimeSpectrum.size();
      if (realsize >= 5) Constants.Queue_DrawRealtimeSpectrum.clear();
      List<Column> columns = new ArrayList<Column>();
      List<SubcolumnValue> values;
      if (flist.size() != total) {
        // 没收到整段就返回
        return;
      }
      float[] ff1 = selectData(flist);
      if (Xvalues.size() > 0) {
        int size = Xvalues.size();
        for (int i = 0; i < size; i++) {
          values = new ArrayList<SubcolumnValue>();
          values = Xvalues.get(i);
          for (int j = numSubcolumns - 1; j > 0; j--) {
            values.set(j, values.get(j - 1));
          }
          // int color=ChartUtils.pickColor();
          int index = (int) ((ff1[i] + 150) / 8);
          int color = 0;
          if (index >= 20) {
            color = colors[19];
          } else if (index < 0) {
            color = colors[0];
          } else {
            color = colors[(int) ((ff1[i] + 150) / 8)];
          }
          values.set(0, new SubcolumnValue(1, color));

          Column column = new Column(values);
          columns.add(column);
        }
      }
      data = new ColumnChartData(columns);
      data.setStacked(true);
      data.setFillRatio(1);
      Axis axisX = new Axis(Lvalues);
      Axis axisY = Axis.generateAxisFromRange(0, 60, 2);
      axisY.setLineColor(Color.GREEN);
      axisX.setHasLines(false);
      axisX.setHasSeparationLine(false);
      axisY.setHasSeparationLine(false);
      axisY.setHasLines(false);

      axisX.setName("Axis X");
      axisY.setName("Axis Y");
      data.setAxisXBottom(axisX);
      data.setAxisYLeft(axisY);
      chart.setColumnChartData(data);
    }
  }
  private void setUpChartColumns() {
    List<Column> chartColumns = getChartColumns();
    List<AxisValue> chartAxisPoints = getChartAxisPoints();
    List<AxisValue> chartAxisValues = getChartAxisValues();

    ColumnChartData chartData = new ColumnChartData().setColumns(chartColumns);
    chartData.setAxisXBottom(new Axis().setValues(chartAxisPoints));
    chartData.setAxisYLeft(new Axis().setValues(chartAxisValues));

    chart.setColumnChartData(chartData);
  }
  public void loadNumStacks() {
    List<Column> cols = new ArrayList<>();
    for (MatchData m : team.getMatchData()) {
      List<SubcolumnValue> subCols = new ArrayList<>();
      SubcolumnValue auton = new SubcolumnValue(m.autonYellowTotes).setLabel("m" + m.matchNo);
      auton.setColor(getResources().getColor(R.color.accent));
      subCols.add(auton);
      cols.add(new Column(subCols).setHasLabels(true));
    }

    ColumnChartData data = new ColumnChartData(cols).setBaseValue(0).setStacked(true);
    data.setAxisYLeft(new Axis().setHasLines(true).setName("Yellow totes stacked"));
    data.setAxisXBottom(new Axis().setName("Match"));

    autonTotes.setColumnChartData(data);
  }
  /** Generates columns with stacked subcolumns. */
  private void generateStackedData(int start, int end) {
    int numSubcolumns = 60;
    int numColumns = 1000;
    List<Column> columns = new ArrayList<Column>();
    List<SubcolumnValue> values;
    Lvalues = new ArrayList<>();
    Xvalues = new ArrayList<List<SubcolumnValue>>();
    for (int i = 0; i < numColumns; ++i) {
      values = new ArrayList<SubcolumnValue>();
      if (i == 0) {
        Lvalues.add(new AxisValue(i).setLabel(String.valueOf(start)));
      } else if (i == numColumns - 30) {
        Lvalues.add(new AxisValue(i).setLabel(String.valueOf(end)));
      }
      for (int j = 1; j <= numSubcolumns; ++j) {
        values.add(new SubcolumnValue(1, Color.WHITE));
      }

      Column column = new Column(values);
      column.setHasLabelsOnlyForSelected(hasLabelForSelected);
      columns.add(column);

      Xvalues.add(values);
    }
    data = new ColumnChartData(columns);

    data.setStacked(true);
    data.setFillRatio(1);

    Axis axisX = new Axis(Lvalues);
    axisX.setHasSeparationLine(false);
    axisX.setHasLines(false);
    Axis axisY = Axis.generateAxisFromRange(0, 60, 1);
    axisY.setHasSeparationLine(false);
    axisY.setHasLines(false);
    axisY.setLineColor(Color.GREEN);

    axisX.setName("Axis X");
    axisY.setName("Axis Y");
    data.setAxisXBottom(axisX);
    data.setAxisYLeft(axisY);
    chart.setColumnChartData(data);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.chart_waterfall);
    chart = (ColumnChartView) findViewById(R.id.chart);
    chart.setOnValueTouchListener(new ValueTouchListener());
    generateStackedData(70, 95);

    MyTopBar topBar = (MyTopBar) findViewById(R.id.topbar_chartwater);
    topBar.setOnTopBarClickListener(
        new MyTopBar.TopBarClickListener() {
          @Override
          public void leftclick() {
            Chart_waterfall.this.finish();
          }

          @Override
          public void rightclick() {}
        });
  }
    @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;
    }