Exemplo n.º 1
0
  private void initChart() {
    barChart.setDrawBorders(true);
    barChart.setBorderWidth(1);
    barChart.setBorderColor(getResources().getColor(R.color.minute_grayLine));
    barChart.setDescription("");
    barChart.setDragEnabled(true);
    barChart.setScaleYEnabled(false);

    Legend barChartLegend = barChart.getLegend();
    barChartLegend.setEnabled(false);

    // BarYAxisFormatter  barYAxisFormatter=new BarYAxisFormatter();
    // bar x y轴
    xAxisBar = barChart.getXAxis();
    xAxisBar.setDrawLabels(true);
    xAxisBar.setDrawGridLines(false);
    xAxisBar.setDrawAxisLine(false);
    xAxisBar.setTextColor(getResources().getColor(R.color.minute_zhoutv));
    xAxisBar.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxisBar.setGridColor(getResources().getColor(R.color.minute_grayLine));

    axisLeftBar = barChart.getAxisLeft();
    axisLeftBar.setAxisMinValue(0);
    axisLeftBar.setDrawGridLines(false);
    axisLeftBar.setDrawAxisLine(false);
    axisLeftBar.setTextColor(getResources().getColor(R.color.minute_zhoutv));
    axisLeftBar.setDrawLabels(true);
    axisLeftBar.setSpaceTop(0);
    axisLeftBar.setShowOnlyMinMax(true);
    axisRightBar = barChart.getAxisRight();
    axisRightBar.setDrawLabels(false);
    axisRightBar.setDrawGridLines(false);
    axisRightBar.setDrawAxisLine(false);
    /** ************************************************************* */
    combinedchart.setDrawBorders(true);
    combinedchart.setBorderWidth(1);
    combinedchart.setBorderColor(getResources().getColor(R.color.minute_grayLine));
    combinedchart.setDescription("");
    combinedchart.setDragEnabled(true);
    combinedchart.setScaleYEnabled(false);

    Legend combinedchartLegend = combinedchart.getLegend();
    combinedchartLegend.setEnabled(false);
    // bar x y轴
    xAxisK = combinedchart.getXAxis();
    xAxisK.setDrawLabels(true);
    xAxisK.setDrawGridLines(false);
    xAxisK.setDrawAxisLine(false);
    xAxisK.setTextColor(getResources().getColor(R.color.minute_zhoutv));
    xAxisK.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxisK.setGridColor(getResources().getColor(R.color.minute_grayLine));

    axisLeftK = combinedchart.getAxisLeft();
    axisLeftK.setDrawGridLines(true);
    axisLeftK.setDrawAxisLine(false);
    axisLeftK.setDrawLabels(true);
    axisLeftK.setTextColor(getResources().getColor(R.color.minute_zhoutv));
    axisLeftK.setGridColor(getResources().getColor(R.color.minute_grayLine));
    axisLeftK.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
    axisRightK = combinedchart.getAxisRight();
    axisRightK.setDrawLabels(false);
    axisRightK.setDrawGridLines(true);
    axisRightK.setDrawAxisLine(false);
    axisRightK.setGridColor(getResources().getColor(R.color.minute_grayLine));
    combinedchart.setDragDecelerationEnabled(true);
    barChart.setDragDecelerationEnabled(true);
    combinedchart.setDragDecelerationFrictionCoef(0.2f);
    barChart.setDragDecelerationFrictionCoef(0.2f);

    // 将K线控的滑动事件传递给交易量控件
    combinedchart.setOnChartGestureListener(
        new CoupleChartGestureListener(combinedchart, new Chart[] {barChart}));
    // 将交易量控件的滑动事件传递给K线控件
    barChart.setOnChartGestureListener(
        new CoupleChartGestureListener(barChart, new Chart[] {combinedchart}));
    barChart.setOnChartValueSelectedListener(
        new OnChartValueSelectedListener() {
          @Override
          public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
            Log.e("%%%%", h.getXIndex() + "");
            combinedchart.highlightValues(new Highlight[] {h});
          }

          @Override
          public void onNothingSelected() {
            combinedchart.highlightValue(null);
          }
        });
    combinedchart.setOnChartValueSelectedListener(
        new OnChartValueSelectedListener() {
          @Override
          public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {

            barChart.highlightValues(new Highlight[] {h});
          }

          @Override
          public void onNothingSelected() {
            barChart.highlightValue(null);
          }
        });
  }
  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();
  }