• MPAndroidChart开源图表库(三)之柱形图


    承接上一篇文章。请參考 http://blog.csdn.net/shineflowers/article/details/44704723

    1. 将mpandroidchartlibrary-2-0-8.jar包copy到项目的libs中

    2. 定义xml文件


    3. 主要Java逻辑代码例如以下。凝视已经都加入上了。

    package com.jackie.mpandoidbarchart;
    
    import java.util.ArrayList;
    
    import com.github.mikephil.charting.charts.BarChart;
    import com.github.mikephil.charting.charts.LineChart;
    import com.github.mikephil.charting.components.Legend;
    import com.github.mikephil.charting.components.Legend.LegendForm;
    import com.github.mikephil.charting.components.XAxis;
    import com.github.mikephil.charting.components.XAxis.XAxisPosition;
    import com.github.mikephil.charting.data.BarData;
    import com.github.mikephil.charting.data.BarDataSet;
    import com.github.mikephil.charting.data.BarEntry;
    
    import android.support.v7.app.ActionBarActivity;
    import android.graphics.Color;
    import android.os.Bundle;
    
    public class MainActivity extends ActionBarActivity {
    
    	private BarChart mBarChart;
    	private BarData mBarData;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		
    		mBarChart = (BarChart) findViewById(R.id.spread_bar_chart);
    		mBarData = getBarData(4, 100);
    		showBarChart(mBarChart, mBarData);
    	}
    	
    	private void showBarChart(BarChart barChart, BarData barData) {
    		barChart.setDrawBorders(false);  ////是否在折线图上加入边框 
    	      
    		barChart.setDescription("");// 数据描写叙述    
    		
            // 假设没有数据的时候,会显示这个,相似ListView的EmptyView    
    		barChart.setNoDataTextDescription("You need to provide data for the chart.");    
                   
    		barChart.setDrawGridBackground(false); // 是否显示表格颜色    
    		barChart.setGridBackgroundColor(Color.WHITE & 0x70FFFFFF); // 表格的的颜色,在这里是是给颜色设置一个透明度    
          
    		barChart.setTouchEnabled(true); // 设置能否够触摸    
         
    		barChart.setDragEnabled(true);// 能否够拖拽    
    		barChart.setScaleEnabled(true);// 能否够缩放    
        
    		barChart.setPinchZoom(false);//     
        
    //		barChart.setBackgroundColor();// 设置背景    
    		
    		barChart.setDrawBarShadow(true);
           
    		barChart.setData(barData); // 设置数据    
    
    		Legend mLegend = barChart.getLegend(); // 设置比例图标示
        
            mLegend.setForm(LegendForm.CIRCLE);// 样式    
            mLegend.setFormSize(6f);// 字体    
            mLegend.setTextColor(Color.BLACK);// 颜色    
            
    //      X轴设定
    //      XAxis xAxis = barChart.getXAxis();
    //      xAxis.setPosition(XAxisPosition.BOTTOM);
        
            barChart.animateX(2500); // 马上运行的动画,x轴 	
    	}
    
    	private BarData getBarData(int count, float range) {
    		ArrayList<String> xValues = new ArrayList<String>();
    		for (int i = 0; i < count; i++) {
    			xValues.add("第" + (i + 1) + "季度");
    		}
    		
    		ArrayList<BarEntry> yValues = new ArrayList<BarEntry>();
            
    		for (int i = 0; i < count; i++) {    
                float value = (float) (Math.random() * range/*100以内的随机数*/) + 3;
                yValues.add(new BarEntry(value, i));    
            }
    		
    		// y轴的数据集合    
            BarDataSet barDataSet = new BarDataSet(yValues, "測试饼状图"); 
            
            barDataSet.setColor(Color.rgb(114, 188, 223));
        
            ArrayList<BarDataSet> barDataSets = new ArrayList<BarDataSet>();    
            barDataSets.add(barDataSet); // add the datasets    
        
            BarData barData = new BarData(xValues, barDataSets);
    		
    		return barData;
    	}
    }

    效果图例如以下:


  • 相关阅读:
    一、ThinkPHP的介绍
    一、ThinkPHP的介绍
    perl 爬虫两个技巧
    perl 爬虫两个技巧
    perl 爬取上市公司业绩预告
    perl lwp get uft-8和gbk
    perl lwp get uft-8和gbk
    perl 爬取同花顺数据
    perl 爬取同花顺数据
    php 接口示例
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6869290.html
Copyright © 2020-2023  润新知