Android 图表开源框架HelloCharts 之 柱状图、折线图、饼状图,Fragment

    xiaoxiao2021-03-25  89

    在网上找到一些开源的框架使用,不是MPAndroidChart,也不是AChartEngine ,而是HelloCharts。

    开源地 址:https://github.com/lecho/hellocharts-android

    主要想记录一下ViewPager与Fragment 结合实现;

     水平滚动,实现fragment 贴换 显示不同的 图表,

    第1个页面 显示  “折线图”;

    第2个页面 显示  “柱形图”;

    第3个页面 显示  “饼状图”;

    HelloCharts支持以下chart类型

    Line chart(cubic lines, filled lines, scattered points)

    Column chart(grouped, stacked, negative values)

    Pie chart

    Bubble chart

    Combo chart(columns/lines)

    Preview charts(for column chart and line chart)

    此外还具有以下特点

    支持缩放、滑动以及平移。Zoom(pinch to zoom, double tap zoom), scroll and fling

    支持自定义坐标轴(比如坐标轴位置:上下左右内部),支持自动生成坐标轴。Custom and auto-generated axes(top, bottom, left, right, inside)

    动画(Animations)

    支持预览,即在chart下面会有一个坐标密度更细的附属chart,当选中附属chart的某一区域,附属chart上面的chart会显示选中区域的更详细情况

    下面是一些效果截图

     每一种chart都可以在xml中定义:

    ==============================XML=======================

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     tools:context="com.example.testchart.MainActivity$PlaceholderFragment"      android:background="#fff"     >    <lecho.lib.hellocharts.view.LineChartView         android:id="@+id/chart"         android:layout_width="match_parent"         android:layout_height="match_parent" >     </lecho.lib.hellocharts.view.LineChartView>      </RelativeLayout>

    =========================    Fragment =========================================

    public static class PlaceholderFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; Context context=null; LineChartView chart=null; boolean hasAxes=true; boolean hasAxesNames=true; /** * Returns a new instance of this fragment for the given section number. */ public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); context=getActivity().getApplicationContext(); chart=(LineChartView) rootView.findViewById(R.id.chart); //      List<AxisValue> mAxisValues=new ArrayList<AxisValue>();     for (int i = 1; i < 8 ; i++) {          String xItemStr=i+"月份";         mAxisValues.add((new AxisValue(i)).setLabel(xItemStr)); //为每个对应的i设置相应的label(显示在X轴)           List<PointValue> values = new ArrayList<PointValue>();    values.add(new PointValue(1, 13));    values.add(new PointValue(2, 9));    values.add(new PointValue(3, 2));    values.add(new PointValue(4, 1));    values.add(new PointValue(5, 5));    values.add(new PointValue(6, 29.82f));        //In most cased you can call data model methods in builder-pattern-like manner.    Line line = new Line(values).setColor(Color.BLUE).setCubic(false);    List<Line> lines = new ArrayList<Line>();    lines.add(line);        LineChartData data = new LineChartData();    data.setLines(lines);        if (hasAxes) {                Axis axisX = new Axis().setHasTiltedLabels(true);//有 lable                Axis axisY = new Axis().setHasLines(true);                if (hasAxesNames) {                    axisX.setName("\r\n\r"); //                    axisY.setName("数量");                    axisX.setMaxLabelChars(1);  //                    axisX.setValues(mAxisValues);                                    }                                data.setAxisXBottom(axisX);                data.setAxisYLeft(axisY);            } else {                data.setAxisXBottom(null);                data.setAxisYLeft(null);            }                data.setBaseValue(Float.NEGATIVE_INFINITY);                chart.setInteractive(true);              chart.setZoomType(ZoomType.HORIZONTAL);              chart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);            chart.setLineChartData(data);     return rootView; }

    }

    项目源码 资源:http://download.csdn.net/detail/u013758456/9775716

    转载请注明原文地址: https://ju.6miu.com/read-13766.html

    最新回复(0)