MPAndroidChart開源圖表庫(三)之直條圖

來源:互聯網
上載者:User

標籤: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.components.Legend;import com.github.mikephil.charting.components.Legend.LegendForm;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;@Overrideprotected 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);// 顏色                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;}}

如下:


MPAndroidChart開源圖表庫(三)之直條圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.