• 使用HelloCharts绘制柱状图


    首先下载依赖库 ,有现成的jar包:hellocharts-library-1.5.8.jar

    在需要的布局中直接使用:

    <lecho.lib.hellocharts.view.ColumnChartView
    android:id="@+id/columnchart"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

    例如绘制一年的数据:

    伪代码:

    //声明所需变量
    public
    final static String[] months = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", }; ColumnChartView columnChart; ColumnChartData columnData; List<Column> lsColumn = new ArrayList<Column>(); List<SubcolumnValue> lsValue;

    //初始化数据并显示在图表上

    private void dataInit() {

    int numSubcolumns = 1;
    int numColumns = months.length;

    List<AxisValue> axisValues = new ArrayList<AxisValue>();
    List<Column> columns = new ArrayList<Column>();
    List<SubcolumnValue> values;
    for (int i = 0; i < numColumns; ++i) {

    values = new ArrayList<SubcolumnValue>();
    for (int j = 0; j < numSubcolumns; ++j) {
    values.add(new SubcolumnValue((float) Math.random() * 50f + 5,
    ChartUtils.pickColor()));
    }
    // 点击柱状图就展示数据量
    axisValues.add(new AxisValue(i).setLabel(months[i]));

    columns.add(new Column(values).setHasLabelsOnlyForSelected(true));
    }

    columnData = new ColumnChartData(columns);

    columnData.setAxisXBottom(new Axis(axisValues).setHasLines(true)
    .setTextColor(Color.BLACK));
    columnData.setAxisYLeft(new Axis().setHasLines(true)
    .setTextColor(Color.BLACK).setMaxLabelChars(2));

    columnChart.setColumnChartData(columnData);

    // Set value touch listener that will trigger changes for chartTop.
    columnChart.setOnValueTouchListener(new ValueTouchListener());

    // Set selection mode to keep selected month column highlighted.
    columnChart.setValueSelectionEnabled(true);

    columnChart.setZoomType(ZoomType.HORIZONTAL);

    }

    /**
    * 柱状图监听器
    *
    * @author 1017
    *
    */
    private class ValueTouchListener implements
    ColumnChartOnValueSelectListener {

    @Override
    public void onValueSelected(int columnIndex, int subcolumnIndex,
    SubcolumnValue value) {
    // generateLineData(value.getColor(), 100);
    }

    @Override
    public void onValueDeselected() {

    // generateLineData(ChartUtils.COLOR_GREEN, 0);

    }
    }

     最后出来的效果:

    点击对应的柱状图就会显示当前项的数据;

  • 相关阅读:
    Solaris 11 system package 安装与更新(如:assembler)
    JS实现页面内跳转
    使用Eclipse进行嵌入式软件开发
    Eclipse下C++标准库报错::mbstate_t has not been declared
    嵌入式ARM开发环境搭建
    CCS内存数据转成图片
    RESET
    Android 圆角研究
    Java 重写必须满足的条件
    android 社会化分享集成
  • 原文地址:https://www.cnblogs.com/yjpjy/p/5242164.html
Copyright © 2020-2023  润新知