示例#1
0
  private void addData() {
    ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();

    for (int i = 0; i < yData.length; i++) yVals1.add(new BarEntry(yData[i], i));

    ArrayList<String> xVals = new ArrayList<String>();

    for (int i = 0; i < xData.length; i++) xVals.add(xData[i]);

    // create pie data set
    BarDataSet dataSet = new BarDataSet(yVals1, "Stock");
    //	dataSet.setSliceSpace(5);
    //	dataSet.setSelectionShift(5);

    // add many colors
    ArrayList<Integer> colors = new ArrayList<Integer>();
    colors.add(Color.rgb(0, 204, 68));
    colors.add(Color.rgb(230, 0, 0));

    dataSet.setColors(colors);

    // instantiate pie data object
    BarData data = new BarData(xVals, dataSet);

    data.setValueTextSize(16);
    data.setValueTextColor(Color.BLACK);

    mChart.setData(data);

    // undo highlights
    mChart.highlightValue(null);

    // update pie chart
    mChart.invalidate();
  }
  private void setChartData(ClimbingArea ClimbingArea) {

    HashMap<Integer, Integer> difficultyMap = new HashMap<>();
    for (int i = AppConstants.MIN_UIAA_LEVEL; i <= AppConstants.MAX_UIAA_LEVEL; ++i) {
      difficultyMap.put(i, 0);
    }

    for (ClimbingRoute route : ClimbingArea.getRoutes()) {
      String difficulty = route.getUIAARank();
      int routeDifficulty = Utils.trimUIAARank(difficulty);
      if (difficultyMap.containsKey(routeDifficulty)) {
        difficultyMap.put(routeDifficulty, difficultyMap.get(routeDifficulty) + 1);
      }
    }

    ArrayList<BarEntry> routeDifficulties = new ArrayList<BarEntry>();

    String[] xVals = new String[difficultyMap.size()];
    List<Integer> orderedList = new ArrayList<>(difficultyMap.keySet());
    Collections.sort(orderedList);
    int indexCounter = 0;
    for (Integer difficulty : orderedList) {
      xVals[indexCounter] = difficulty + "";
      // BarEntry entry = new BarEntry(difficultyMap.get(difficulty), indexCounter++ , difficulty
      // +"");
      BarEntry entry = new BarEntry(difficulty, difficultyMap.get(difficulty), indexCounter++);
      routeDifficulties.add(entry);
    }
    mChart.getXAxis().setLabelCount(xVals.length);

    BarDataSet set1 = new BarDataSet(routeDifficulties, "Schwierigkeiten");
    // set1.setColor(getResources().getColor(R.color.primary_700));
    int[] uiaaChartColors = new int[AppConstants.UIAA_CHART_COLORS.length];
    for (int i = 0; i < uiaaChartColors.length; ++i) {
      uiaaChartColors[i] = getResources().getColor(AppConstants.UIAA_CHART_COLORS[i]);
    }
    set1.setColors(uiaaChartColors);

    ArrayList<IBarDataSet> dataSets = new ArrayList<>();
    dataSets.add(set1);

    BarData data = new BarData(dataSets);
    data.setValueFormatter(new MyValueFormatter());
    data.setValueTextSize(10f);

    mChart.setData(data);

    mChart.animateX(1000);
    mChart.animateY(1000);
  }
  private ArrayList<BarDataSet> getDataSet() {
    ArrayList<BarDataSet> dataSets = null;

    ArrayList<BarEntry> valueSet1 = new ArrayList<BarEntry>();
    BarEntry v1e1 = new BarEntry(110.000f, 0); // Jan
    valueSet1.add(v1e1);
    BarEntry v1e2 = new BarEntry(40.000f, 1); // Feb
    valueSet1.add(v1e2);
    BarEntry v1e3 = new BarEntry(60.000f, 2); // Mar
    valueSet1.add(v1e3);
    BarEntry v1e4 = new BarEntry(30.000f, 3); // Apr
    valueSet1.add(v1e4);
    BarEntry v1e5 = new BarEntry(90.000f, 4); // May
    valueSet1.add(v1e5);
    BarEntry v1e6 = new BarEntry(100.000f, 5); // Jun
    valueSet1.add(v1e6);

    ArrayList<BarEntry> valueSet2 = new ArrayList<BarEntry>();
    BarEntry v2e1 = new BarEntry(150.000f, 0); // Jan
    valueSet2.add(v2e1);
    BarEntry v2e2 = new BarEntry(90.000f, 1); // Feb
    valueSet2.add(v2e2);
    BarEntry v2e3 = new BarEntry(120.000f, 2); // Mar
    valueSet2.add(v2e3);
    BarEntry v2e4 = new BarEntry(60.000f, 3); // Apr
    valueSet2.add(v2e4);
    BarEntry v2e5 = new BarEntry(20.000f, 4); // May
    valueSet2.add(v2e5);
    BarEntry v2e6 = new BarEntry(80.000f, 5); // Jun
    valueSet2.add(v2e6);

    BarDataSet barDataSet1 = new BarDataSet(valueSet1, "Brand 1");
    barDataSet1.setColor(Color.rgb(0, 155, 0));
    BarDataSet barDataSet2 = new BarDataSet(valueSet2, "Brand 2");
    barDataSet2.setColors(ColorTemplate.COLORFUL_COLORS);

    dataSets = new ArrayList<BarDataSet>();
    dataSets.add(barDataSet1);
    dataSets.add(barDataSet2);
    return dataSets;
  }
  private void setChart(BarChart barChart, double[][] values, String[] labels) {

    int[] colors = new int[5];
    colors[0] = Color.argb(255, 11, 55, 144);
    colors[1] = Color.argb(255, 48, 120, 254);
    colors[2] = Color.argb(255, 123, 179, 254);
    colors[3] = Color.argb(255, 230, 230, 230);
    colors[4] = Color.argb(0, 255, 255, 255);

    ArrayList<BarEntry> dataEntries = new ArrayList<BarEntry>();

    for (int i = 0; i < values.length; ++i) {
      float[] fList = new float[values[i].length];

      for (int j = 0; j < values[i].length; ++j) {
        fList[j] = (float) values[i][j];
      }

      dataEntries.add(new BarEntry(fList, i));
    }
    BarDataSet dataSet = new BarDataSet(dataEntries, "");
    dataSet.setBarSpacePercent(2f);
    dataSet.setStackLabels(new String[] {"浅睡眠", "深睡眠", "活动"});
    dataSet.setColors(new int[] {colors[0], colors[1], colors[2]});

    dataSet.setDrawValues(false);
    ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
    dataSets.add(dataSet);
    BarData barData = new BarData(labels, dataSets);
    barChart.setData(barData);

    YAxis leftAxis = barChart.getAxisLeft();
    leftAxis.setDrawGridLines(true);
    leftAxis.setDrawLabels(false);
    leftAxis.setGridColor(colors[3]);
    leftAxis.setDrawAxisLine(true);
    leftAxis.setAxisLineColor(colors[3]);
    leftAxis.setAxisLineWidth(0.5f);
    YAxis rightAxis = barChart.getAxisRight();
    rightAxis.setEnabled(true);
    rightAxis.setDrawLabels(false);
    rightAxis.setDrawGridLines(false);
    rightAxis.setAxisLineColor(colors[3]);
    rightAxis.setAxisLineWidth(0.5f);

    XAxis xAxis = barChart.getXAxis();
    xAxis.setPosition(XAxisPosition.BOTTOM);
    xAxis.setDrawLabels(false);
    xAxis.setDrawGridLines(true);
    xAxis.setGridColor(colors[3]);
    xAxis.setAxisLineWidth(0.5f);
    xAxis.setAxisLineColor(colors[3]);

    barChart.setBackgroundColor(colors[4]);
    barChart.setGridBackgroundColor(colors[4]);
    barChart.getLegend().setEnabled(false);
    barChart.setScaleEnabled(false);
    barChart.setDescription("");
    barChart.animateXY(2000, 2000);
    barChart.notifyDataSetChanged();
  }