Android 经过美化的柱状图....

产品跟我说,要做一个柱形图,我去github上找了一下,发现了一个,功能挺不错,我把使用方法
来说明一下
github上的网址
MPAndroidChart
强大的图表绘制工具,支持折线图、面积图、散点图、时间图、柱状图、条图、饼图、气泡图、圆环图、范围(高至低)条形图、网状图及各种图的结合;支持图的拖拽缩放;支持 Android 2.2 以上,支持横纵轴缩放,多指缩放,展现动画、高亮、保存到 sdcard、从文件读取图表
项目地址:https://github.com/PhilJay/MPAndroidChart
Demo地址:https://play.google.com/store/apps/details?id=com.xxmassdeveloper.mpchartexample
Demo项目:https://github.com/PhilJay/MPAndroidChart/tree/master/MPChartExample

柱状图的效果,
1初始化的时候,柱子是动画递增的
2每个柱子 颜色不同,而且可以通过放大缩小图片那样,伸缩柱状图

http://img.blog.csdn.net/20150722185145945?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

注:要用 AndroidStudio (使用eclipse的同学导一下library)
具体实现方法
1:打开app 下的build.gradle
添加如下代码
repositories {
maven { url “https://jitpack.io” }
}
dependencies {
compile ‘com.github.PhilJay:MPAndroidChart:v2.1.0’
}
然后点击 AndroidStudio菜单栏 Build –> Rebuild Project
等下方的进度条读完,配置完后就可以引用库类了
以下是关键代码

/**
* A simple {@link Fragment} subclass.
* Use the {@link AnotherBarGraphFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class AnotherBarGraphFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = “param1”;
private static final String ARG_PARAM2 = “param2”;

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

//这是柱状图对象
private BarChart mChart;
private Context context;

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment AnotherBarGraphFragment.
 */
// TODO: Rename and change types and number of parameters
public static AnotherBarGraphFragment newInstance(String param1, String param2) {
    AnotherBarGraphFragment fragment = new AnotherBarGraphFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

public AnotherBarGraphFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
    context = getActivity();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_another_bar_graph, container, false);
    mChart = ((BarChart) view.findViewById(R.id.barchart_newBar));
    mChart.setDescription("");

    mChart.setMaxVisibleValueCount(60);

    mChart.setPinchZoom(false);
    mChart.setDrawBarShadow(false);
    mChart.setDrawGridBackground(false);
    XAxis xAxis = mChart.getXAxis();
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setSpaceBetweenLabels(0);
    xAxis.setDrawGridLines(false);
    mChart.getAxisLeft().setDrawGridLines(false);
    mChart.animateY(2500);

    mChart.getLegend().setEnabled(false);

//———————————–注意这里是关键代码——————-//
//相当于要添加的数据,以下添加的数据为: 十个柱子,从10到19
ArrayList yVals1 = new ArrayList();
for (int i = 0; i < 10; i++) {
float val1 = i + 10;
yVals1.add(new BarEntry((int) val1, i));
}

    ArrayList<String> xVals = new ArrayList<String>();
    for (int i = 0; i < 10; i++) {
        xVals.add((int) yVals1.get(i).getVal() + "");
    }
    BarDataSet set1 = new BarDataSet(yVals1, "Data Set");
    set1.setColors(ColorTemplate.VORDIPLOM_COLORS);
    set1.setDrawValues(false);

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

    BarData data = new BarData(xVals, dataSets);

    mChart.setData(data);
    mChart.invalidate();

    return view;
}

}
布局文件为下

你可能感兴趣的:(github,android)