HelloCharts 와 MPAndroidChart 의 사용

11477 단어 android
프레임 주소
GITHUB  MPAndroid :https://github.com/PhilJay/MPAndroidChart
  HelloCharts  https://github.com/lecho/hellocharts-android
여기에 핵심 코드 를 붙 여 원본 업로드 에 필요 한 다운로드 시청 저 자 는 계속 업 데 이 트 됩 니 다.
DEMO 주소 http://download.csdn.net/detail/jerry872235631/9684707
1. 막대 그래프
 
 public void showColumnView(List reportOrderList) {
        mReportOrderList = new ArrayList<>();
        mReportOrderList.addAll(reportOrderList);
        //       
        int numColumns = WEEK_ARRAY.length;
        //X   
        List axisValues = new ArrayList<>();
        //      
        List columns = new ArrayList<>();
        //       
        List values;
        //         
        for (int i = 0; i < numColumns; ++i) {
            values = new ArrayList<>();
            //       ( >0     , <0,    ),       
            values.add(new SubcolumnValue(reportOrderList.get(i).orderCount, COLOR[i]));
            //X        
            axisValues.add(new AxisValue(i).setLabel(reportOrderList.get(i).date));
            //        
            Column column = new Column(values);
            //               , setHasLabels()     ,setHasLabels()  
            column.setHasLabelsOnlyForSelected(true);
//            column.setHasLabels(false);
            columns.add(column);
        }
        //       
        ColumnChartData columnData = new ColumnChartData(columns);
        //   X   
        Axis axisX = new Axis();
        axisX.setHasLines(false);//       
        axisX.setTextColor(getResources().getColor(R.color.colorSkyBlue));
        axisX.setValues(axisValues);
        //   Y 
        Axis axisY = new Axis();
        axisY.setHasLines(true);
        axisY.setTextColor(getResources().getColor(R.color.colorSkyBlue));
        axisY.setHasSeparationLine(true);//   
        axisY.setLineColor(getResources().getColor(R.color.colorSkyBlue));
//        axisY.setMaxLabelChars(4);
        columnData.setAxisXBottom(axisX);
        columnData.setAxisYLeft(axisY);
        columnData.setValueLabelBackgroundAuto(false);//               
        columnData.setValueLabelBackgroundEnabled(false);//         
        columnData.setValueLabelsTextColor(getResources().getColor(R.color.colorDeepGray));

//        Viewport viewport = new Viewport();
//        viewport.left = 0;
//        viewport.top = mColumnChartView.getHeight();
//        viewport.bottom = 0;
//        viewport.right = 10;
//        mColumnChartView.setMaximumViewport(viewport);
//        mColumnChartView.setCurrentViewportWithAnimation(viewport);

        mColumnChartView.setColumnChartData(columnData);
        mColumnChartView.setValueSelectionEnabled(true);
        mColumnChartView.setInteractive(true);//     
        mColumnChartView.setZoomEnabled(false);//      
//        mColumnChartView.setZoomType(ZoomType.HORIZONTAL_AND_VERTICAL);//    
//        mColumnChartView.setRotation(30);//     
        mColumnChartView.startDataAnimation(500);//    
    }

2. 접선 도
 
 
 private void initData() {
        int number = months.length;
        List lines = new ArrayList<>();
        List values = new ArrayList<>();
        //X   
        List axisValues = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            values.add(new PointValue(i, (int) (Math.random() * 50)));
            //X        
            axisValues.add(new AxisValue(i).setValue(i).setLabel(months[i]));
        }

        Line line = new Line(values);
        line.setColor(ChartUtils.pickColor());
        line.setStrokeWidth(1);//      
        line.setHasLines(true);

        line.setFilled(false);   //      
        line.setHasPoints(true);//      
        line.setPointColor(Color.GREEN);//    
        line.setPointRadius(3);//    
        line.setHasLabels(true);//        

        line.setShape(ValueShape.DIAMOND);//        DIAMOND  、SQUARE  、CIRCLE  
        line.setCubic(true);//         
        lines.add(line);

        //   X   
        Axis axisX = new Axis();
        axisX.setHasLines(true);//     X    
        axisX.setHasSeparationLine(true);//        
        axisX.setTextColor(Color.BLACK);
        axisX.setLineColor(getResources().getColor(R.color.colorGreen));
        axisX.setTextSize(14);
        axisX.setTypeface(Typeface.DEFAULT);//      
        axisX.setHasTiltedLabels(true);//  X       45 
        axisX.setName("   ");
        axisX.setValues(axisValues);// X             
        //   Y 
        Axis axisY = new Axis();
        axisY.setHasLines(true);
        axisY.setTextColor(Color.BLACK);
//        axisY.setName("    ");
        //       
        axisY.setMaxLabelChars(4);

        LineChartData data = new LineChartData();
        data.setLines(lines);
        data.setAxisXBottom(axisX);
        data.setAxisYLeft(axisY);
        data.setBaseValue(Float.NEGATIVE_INFINITY);//           
        data.setValueLabelBackgroundAuto(false);//               
        data.setValueLabelBackgroundColor(ChartUtils.pickColor());//        
        data.setValueLabelBackgroundEnabled(false);//            
        data.setValueLabelsTextColor(Color.BLUE);//        
        mLineCharView.setLineChartData(data);
        //             
        mLineCharView.setInteractive(true);
        //        
        mLineCharView.setZoomEnabled(true);
        Animation animation = new AlphaAnimation(0.3f, 1.0f);
        animation.setDuration(1000);
        mLineCharView.startAnimation(animation);
    }

3. 떡 그림
 
HelloCharts
 
 public void showPieChartView(int columnIndex, List reportOrderList, String date) {
        List values = new ArrayList<>();
        String[] orderStatusArray = getResources().getStringArray(R.array.order_status_array);
        String[] amountTypeArray = getResources().getStringArray(R.array.amount_array);
        for (int i = 0; i < reportOrderList.size(); i++) {
            float value = (float) reportOrderList.get(i).amountOrder;
            SliceValue sliceValue = new SliceValue(value, COLOR[i]);
            String amountType = amountTypeArray[i];
            Log.i(TAG, "---value:" + value);
            if (value > 0.01f) {
                String label = amountType + "¥" + ((int) value);
                sliceValue.setLabel(label);
                sliceValue.setSliceSpacing(1);
                values.add(sliceValue);
            }
        }
        Viewport viewport = new Viewport();
        viewport.left = 0;
        viewport.top = 100;
        viewport.bottom = 0;
        viewport.right = 10;
        mPieChartView.setMaximumViewport(viewport);
        mPieChartView.setCurrentViewportWithAnimation(viewport);

        PieChartData data = new PieChartData(values);
        data.setHasLabels(true);//    
        data.setHasLabelsOnlyForSelected(false);//         
        data.setHasLabelsOutside(true);//           
        data.setHasCenterCircle(true);//       
        data.setValueLabelBackgroundEnabled(false);//            
        data.setValueLabelBackgroundAuto(true);//                   
//        data.setValueLabelBackgroundColor(Color.GREEN);
        data.setValueLabelsTextColor(getResources().getColor(R.color.colorDeepGray));//        
        data.setSlicesSpacing(0);//      
//        data.setValueLabelTextSize(14);//        
        data.setCenterCircleScale(0.6f);//    
        data.setCenterText1(date + " " + WEEK_ARRAY[columnIndex]);
        // Get roboto-italic font.
        Typeface typeface1 = Typeface.createFromAsset(getActivity().getAssets(), "Roboto-Italic.ttf");
        data.setCenterText1Typeface(typeface1);
        // Get font size from dimens.xml and convert it to sp(library uses sp values).
        data.setCenterText1FontSize(16);
        data.setCenterText2(orderStatusArray[status] + "  ");
        data.setCenterText2Color(getResources().getColor(R.color.colorSkyBlue));
        Typeface typeface2 = Typeface.createFromAsset(getActivity().getAssets(), "Roboto-Italic.ttf");
        data.setCenterText2Typeface(typeface2);
        data.setCenterText2FontSize(22);
        mPieChartView.setPieChartData(data);//       
        mPieChartView.setCircleFillRatio(0.7f);//         
        mPieChartView.setViewportCalculationEnabled(true);//          
        mPieChartView.setValueSelectionEnabled(true);//          
        mPieChartView.setAlpha(0.8f);//     
//        mPieChartView.setChartRotation(360, true);//        ,      
        mPieChartView.setChartRotationEnabled(true);//            
//        mPieChartView.moveToWithAnimation(1, 2);//       
        mPieChartView.startDataAnimation(1000);//      
    }

MPAndroidChart
 
 
private void initPieChart(PieChart pieChart) {
    pieChart.setUsePercentValues(true);//      
    pieChart.setDescription("BarChart Test");
    pieChart.setOffsets(5, 10, 5, 5);//      
    pieChart.setDrawHoleEnabled(true);//      
    pieChart.setTransparentCircleRadius(55f); //     
    pieChart.setHoleRadius(50f);  //    
    pieChart.setDrawCenterText(true);  //           
    pieChart.setRotationAngle(90); //       
    pieChart.setRotationEnabled(true); //       
    pieChart.setCenterText("PieChart");  //        
    //    
    PieData pieData = getPieData(6);
    pieChart.setData(pieData);
    Legend mLegend = pieChart.getLegend();  //     
    mLegend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART_CENTER);  //     
    mLegend.setForm(Legend.LegendForm.SQUARE);  //        ,      SQUARE
    mLegend.setXEntrySpace(7f);
    mLegend.setYEntrySpace(5f);
    pieChart.animateXY(1000, 1000);  //    
    pieChart.invalidate();
}

private PieData getPieData(int count) {
    int[] yy = {121, 12, 18, 20, 28, 10};
    ArrayList xValues = new ArrayList<>();  //xVals            
    for (int i = 0; i < count; i++) {
        //      PieChart1, PieChart2, PieChart3, PieChart4,PieChart5,PieChart6
        xValues.add("PieChart" + (i + 1));
    }
    /**
     *            ,           12:12:18:20:28:10
     *    12        12%
     *          ,          list   
     */
    ArrayList yValues = new ArrayList();  //yVals               
    for (int i = 0; i < count; i++) {
        yValues.add(new Entry(yy[i], i));
    }
    //y    
    PieDataSet pieDataSet = new PieDataSet(yValues, "PieChart Revenue 2014");
    pieDataSet.setSliceSpace(0f); //           
    //     
    ArrayList colors = new ArrayList();
    colors.add(Color.rgb(205, 205, 205));
    colors.add(Color.rgb(114, 188, 223));
    colors.add(Color.rgb(255, 123, 124));
    colors.add(Color.rgb(57, 135, 200));
    colors.add(Color.rgb(30, 20, 200));
    colors.add(Color.rgb(80, 60, 150));
    pieDataSet.setColors(colors);
    //dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    float px = 5 * (metrics.densityDpi / 160f);
    pieDataSet.setSelectionShift(px); //         
    return new PieData(xValues, pieDataSet);
}

 
 

좋은 웹페이지 즐겨찾기