精华内容
参与话题
问答
  • hellochart

    千次下载 热门讨论 2016-05-26 11:23:10
    折线图demo eclipse版本
  • 先吐槽下csdn搜到的动态折线图,几乎全都是先一次性加载完数据,然后一点点绘制,比如有个动态心电图,有个动态气温...但是始终还有一个问题没解决,helloChart刷新过程中不支持手势放大缩小,所以我也试过用+-两个...

    先吐槽下csdn搜到的动态折线图,几乎全都是先一次性加载完数据,然后一点点绘制,比如有个动态心电图,有个动态气温,真的忍不住想问你们既然已经知道了所有数据,又何必动态加载呢?

    时隔半年,终于把代码贴上来了。后来我又发现了MPChart的动态图更好画,但是不如helloChart好看,并且性能更好。但是始终还有一个问题没解决,helloChart刷新过程中不支持手势放大缩小,所以我也试过用+-两个按钮改变试图范围,但是不如手势方便。

    正文:现在的代码只需要三个操作。

    1.调一个构造方法,传入参数

    2.不断的加入新数据

    3.销毁这个对象

    是不是非常简单好用,如果你有什么定制的需求,欢迎在我的代码上进行修改,如果有不清楚的评论就好了

    
    public class MyLineChart {
        private LineChartView lineChartView;
        private int lineNum = 1;
        private int MAX_VIEW_POINT_COUNT = 100;
        private int period = 20;
        private int[] colors = {Color.RED, Color.BLACK, Color.BLUE, Color.YELLOW, Color.GREEN};
    
        private OnChartValueSelectListener listener;
    
        private List<LinkedList<PointValue>> points;
        private List<Line> lines;
        private LineChartData lineChartData;
        private float[] dataSize;
    
        public MyLineChart(LineChartView lineChartView, int lineNum, int MAX_VIEW_POINT_COUNT, int period, OnChartValueSelectListener listener, int... colors){
            this.lineChartView = lineChartView;
            if (lineNum > 1){
                this.lineNum = lineNum;
            }
            if (MAX_VIEW_POINT_COUNT > 0){
                this.MAX_VIEW_POINT_COUNT = MAX_VIEW_POINT_COUNT;
            }
            if (period > 20){
                this.period = period;
            }
            if (listener != null){
                this.listener = listener;
            }
            if (colors.length > 0){
                this.colors = colors;
            }
    
            init();
            handler.postDelayed(runnable,period);
        }
    
        //每20ms刷新一次,相当于50Hz
        private Handler handler = new Handler();
        private Runnable runnable = new Runnable() {
            @Override
            public void run() {
                updateView();
                handler.postDelayed(runnable,period);
            }
        };
    
        private void init() {
            lineChartView.setInteractive(false);
            lineChartView.setBackgroundColor(Color.TRANSPARENT);
            lineChartView.setValueTouchEnabled(true);
            lineChartView.setViewportCalculationEnabled(false);//打开就会自动计算y轴范围,现在是人工计算
            lineChartView.setZoomType(ZoomType.HORIZONTAL);
            lineChartView.setOnValueTouchListener(new LineChartOnValueSelectListener() {
                @Override
                public void onValueSelected(int position, int value, PointValue pointValue) {
                    if (listener != null){
                        listener.onChartValueSelect(position, value, pointValue);
                    }
                }
    
                @Override
                public void onValueDeselected() {
    
                }
            });
    
            lineChartData = new LineChartData();
            lineChartData.setAxisYLeft(new Axis().setHasLines(true).setInside(true).setMaxLabelChars(6).setName("y"));
            lineChartData.setAxisXBottom(new Axis().setHasLines(false).setName("x"));
    
            lines = new ArrayList<Line>();
            points = new ArrayList<LinkedList<PointValue>>();
            dataSize = new float[lineNum];
            int j = 0;
            for (int i = 0; i < lineNum; i++) {
                if (j > colors.length){
                    j = 0;
                }
                Line line = new Line();
                line.setColor(colors[j]);
                line.setShape(ValueShape.CIRCLE);
                line.setPointRadius(1);
                line.setStrokeWidth(1);
                line.setCubic(false);
                line.setFilled(false);
                line.setHasLabels(false);
                line.setHasLines(true);
                line.setHasPoints(false);
                lines.add(line);
    
                LinkedList<PointValue> pointValues = new LinkedList<>();
                points.add(pointValues);
                j++;
            }
        }
    
        public void add(float[] value) {
            for (int i = 0; i < lineNum; i++){
                LinkedList<PointValue> pointValues = points.get(i);
                pointValues.add(new PointValue(dataSize[i], value[i]));
                if (dataSize[i] > MAX_VIEW_POINT_COUNT){
                    pointValues.removeFirst();
                }
                dataSize[i]++;
            }
        }
    
        private void updateView(){
            List<Float> tmp = new ArrayList<>();
            for (int i  = 0; i < lineNum; i++){
                tmp.add(dataSize[i]);
                LinkedList<PointValue> pointValues = points.get(i);
                Line line = lines.get(i);
                line.setValues(pointValues);
            }
            float size = Collections.max(tmp);
            if (size <= 0){
                return;
            }
            lineChartData.setLines(lines);
            lineChartView.setLineChartData(lineChartData);
    
            size += MAX_VIEW_POINT_COUNT >> 2;
            float[] y = setChartHeight();
            Viewport viewport;
            if (size > MAX_VIEW_POINT_COUNT){
                viewport = new Viewport(size - MAX_VIEW_POINT_COUNT, y[0], size, y[1]);
            }else {
                viewport = new Viewport(0, y[0], size, y[1]);
            }
            lineChartView.setCurrentViewport(viewport);
            lineChartView.setMaximumViewport(viewport);
        }
    
        /**
         * 使用两个全局变量来更新y轴上下限,避免数值跳动太大
         */
        private float top;
        private float bottom;
        private float[] setChartHeight(){
            List<Float> tmp = new ArrayList<>();
            for (int i = 0; i < lineNum; i++){
                List<PointValue> pointValues = points.get(i);
                for (PointValue value : pointValues){
                    tmp.add(value.getY());
                }
            }
            float max_top = Collections.max(tmp);
            float min_bottom = Collections.min(tmp);
    
            if (max_top == min_bottom){
                if (max_top == 0) {
                    return new float[]{1, -1};
                }else {
                    return new float[]{max_top + Math.abs(max_top) * 0.2f,min_bottom - Math.abs(min_bottom) * 0.2f};
                }
            }else {
                if (top == 0){
                    top = max_top;
                }else if (top < max_top - Math.abs(max_top) * 0.03f){
                    top = top + Math.abs(top - max_top) * 0.03f;
                }else if (top > max_top + Math.abs(max_top) * 0.03f){
                    top = top - Math.abs(top - max_top) * 0.03f;
                }else {
                    top = max_top;
                }
                if (bottom == 0){
                    bottom = min_bottom;
                }else if (bottom > min_bottom + Math.abs(min_bottom * 0.03f)){
                    bottom = bottom - Math.abs(bottom - min_bottom) * 0.03f;
                }else if (bottom < min_bottom - Math.abs(min_bottom * 0.03f)){
                    bottom = bottom + Math.abs(bottom - min_bottom) * 0.03f;
                }else {
                    bottom = min_bottom;
                }
            }
    
            float y = top - bottom;
            return new float[]{top + y * 0.2f, bottom - y * 0.2f};
        }
    
        public interface OnChartValueSelectListener {
            void onChartValueSelect(int position, int value, PointValue pointValue);
        }
    }
    

    如果你看到这里觉得有用的话,欢迎去我兄弟的博客寻宝,点下边的博客就可以了,感谢支持

    https://blog.csdn.net/ma598214297?utm_source=feed

    展开全文
  • HelloChart Eclipse

    2015-07-16 09:20:56
    使用Hellochart,需求中有断点数据;后面改了点源码;修改成功了;
  • HelloChart

    2016-06-30 15:53:00
    2019独角兽企业重金招聘Python工程师标准>>> ...

    HelloCharts

    转载于:https://my.oschina.net/u/2601891/blog/703438

    展开全文
  • hellochart的jar包

    2017-06-29 10:18:59
    这是我自己打包的最新的hellochart的jar包,因为我不使用Studio所以不能依赖,复制他的项目右比较麻烦,所以我自己花时间,把hellochart的工具类打包成一个jar包,算是目前最新的吧,以后就不一定了
  • HelloChart折线图

    2015-11-23 17:50:24
    Android helloChart 统计图表之折线图 blog地址http://blog.csdn.net/wwdlss
  • Android HelloChart饼图

    2015-11-23 17:54:30
    Android HelloChart饼图
  • HelloChart jar包

    2018-08-22 13:28:37
    HelloCharts最新jar下载 下载里面的hellocharts-library-XXX.jar 复制到你项目的libs文件夹即可
  • 接手的公司的一个项目,有一个需求是折线图可以通过上下滑动改变数值。原先的大佬自己从头写的,也能实现功能。大佬后来也把思路和代码分享出来了。大家有兴趣的可以看一下。手把手教你写一个可以上下滑动点改变值的...

    需求

    接手的公司的一个项目,有一个需求是折线图可以通过上下滑动改变数值。原先的大佬自己从头写的,也能实现功能。大佬后来也把思路和代码分享出来了。大家有兴趣的可以看一下。
    手把手教你写一个可以上下滑动点改变值的安卓折线图
    因为后来需求有些变动,原来的代码改动起来略显吃力,于是自己又以hellocharts为基础重新封装了一个自定义VIew,以此来实现功能。使用起来也更简洁一些。

    需求明确:

    • 多条折线图切换滑动
    • 手动上下滑动改变数值

    实现效果图

    滑动折线图

    分析

    现在最好用的图表库大概就是hellochart了。但是hellochart并没有提供滑动改变数值的接口以及相关内容。因此我们在hellochart的基础上进行功能扩展,通过自定义控件来实现如图效果。
    首先需要确定用户点击的是哪一条线,哪一个点。然后对对应的数据进行操作。重新进行赋值改变图表显示。

    实现

    尺寸计算

    首先计算控件宽高,因为要滑动,不宜进行1:1的计算,滑动难度高。因此乘以0.75,以方便响应用户手势操作。

    int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
    int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
    
    hLength = width * 0.75f;          // x轴长度
    vLength = height * 0.75f;         // y轴长度
    

    触摸 - 按下响应事件

    lineChartData = vccharts.this.getLineChartData();
    
                    for (int i = 0; i < lineChartData.getLines().size(); i++) {
                        Line line = lineChartData.getLines().get(i);
                        int pointRadius = ChartUtils.dp2px(density, line.getPointRadius());
                        for (int j = 0; j < line.getValues().size(); j++) {
                            PointValue pointValue = line.getValues().get(j);
                            final float rawValueX = vccharts.this.getChartComputator().computeRawX(pointValue.getX());
                            final float rawValueY = vccharts.this.getChartComputator().computeRawY(pointValue.getY());
                            if (isInArea(rawValueX, rawValueY, touchX, touchY, pointRadius + touchToleranceMargin)) {
                                LogUtils.e("     xuanzhong   xxx " + pointValue.getX() + "        yyy   " + pointValue.getY() + "           ");
                                vccharts.this.lineIndex = i;
                                vccharts.this.pointIndex = j;
                                isMoveChange = true;
                                for (Line line1 : lineChartData.getLines()) {
                                    line1.setStrokeWidth(2);
                                }
                                line.setStrokeWidth(4);
                                selectLineNumber = i;
                            }
                        }
                    }
    

    触摸 - 移动响应事件

    移动的时候需要判断位移以及差值,重新计算比例,确定新数值,进而更新数据,刷新View显示。

                        float y_max = 10;
                        float y_min = 0;
    //                    如果没有选中任何点
                        if (pointIndex == -1 || lineIndex == -1) {
                            return super.onTouchEvent(event);
                        }
                        lineChartData = vccharts.this.getLineChartData();
                        float y = lineChartData.getLines().get(lineIndex).getValues().get(pointIndex).getY();
                        float x = lineChartData.getLines().get(lineIndex).getValues().get(pointIndex).getX();
                        float y_new = y_max - coordinateConversionY(touchY);
    

    数值保护,防止超过数据边界

        if (y_new < y_pre) {
               y_new = y_pre;
         }
        if (y_new > y_after) {
               y_new = y_after;
        }
        if (y_new < y_min) {
                y_new = y_min;
        }
        if (y_new > y_max) {
               y_new = y_max;
        }
    

    重新设置数据,通知数据变化

    lineChartData.getLines().get(lineIndex).getValues().get(pointIndex).set(x, y_new);
    // 通知数据变化
     postInvalidate();
    

    触摸 - 抬起事件

    抬起的时候,取消选择的线条的状态,并且计算最终数据,进行显示

    if (vccharts.this.pointIndex == -1 || vccharts.this.lineIndex == -1) {
                            isMoveChange = false;
                            return true;
                        }
    
                        //                    通知数据变化
                        List<PointValue> values = lineChartData.getLines().get(lineIndex).getValues();
                        float[] changeData = new float[values.size()];
                        for (int i = 0; i < values.size(); i++) {
                            changeData[i] = values.get(i).getY();
                        }
    
                        try {
                            if (vcListener != null) {
                                vcListener.DataChange(lineIndex, new ZuniData(changeData));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
    
                        postInvalidate();
    
                        isMoveChange = false;
                        vccharts.this.pointIndex = -1;
                        vccharts.this.lineIndex = -1;
                        return true;
    

    判断是否点击对应点

        private boolean isInArea(float x, float y, float touchX, float touchY, float radius) {
            float diffX = touchX - x;
            float diffY = touchY - y;
            return Math.pow(diffX, 2) + Math.pow(diffY, 2) <= 2 * Math.pow(radius, 2);
        }
    

    完整代码:

    public class vccharts extends LineChartView {
        private static final int DEFAULT_TOUCH_TOLERANCE_MARGIN_DP = 4;
        private final ChartTouchHandler0 touchHandler0;
        boolean isMoveChange = false; // 是否需要根据移动改变
        boolean isBetterPreAfter = true; // 是否需要比较前后
        int pointIndex;
        int lineIndex;
        private LineChartData lineChartData;
        private float vLength;// 竖线长度
        private float hLength;// 横线长度
        private float density;
        private int touchToleranceMargin;
        private int moveNumber = 0;
        private int selectLineNumber = -1; // 现在选中的线条
        private vcDataChangeListener vcListener;
    
    
        public vccharts(Context context, AttributeSet attrs) {
            super(context, attrs);
    
            try {
                density = context.getResources().getDisplayMetrics().density;
                touchToleranceMargin = ChartUtils.dp2px(density, DEFAULT_TOUCH_TOLERANCE_MARGIN_DP);
                LogUtils.e("  touchToleranceMargin  " + touchToleranceMargin);
            } catch (Exception e) {
                e.printStackTrace();
            }
            touchHandler0 = new ChartTouchHandler0(context, this);
            vccharts.this.post(new Runnable() {
    
                @Override
                public void run() {
                    //在此处调用view.getMeasuredWidth()和getMeasuredHeight()可得到正确值
                    int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
                    int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
    
                    hLength = width * 0.75f;          // x轴长度
                    vLength = height * 0.75f;         // y轴长度
    
                    LogUtils.e("  vLength " + vLength + "  h " + hLength);
                }
            });
        }
    
        public void setVcListener(vcDataChangeListener vcListener) {
            this.vcListener = vcListener;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float touchY = event.getY();
            float touchX = event.getX();
    
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    LogUtils.e("                 按下了  ");
    
                    moveNumber = 0;
                    lineChartData = vccharts.this.getLineChartData();
    
                    for (int i = 0; i < lineChartData.getLines().size(); i++) {
                        Line line = lineChartData.getLines().get(i);
                        int pointRadius = ChartUtils.dp2px(density, line.getPointRadius());
                        for (int j = 0; j < line.getValues().size(); j++) {
                            PointValue pointValue = line.getValues().get(j);
                            final float rawValueX = vccharts.this.getChartComputator().computeRawX(pointValue.getX());
                            final float rawValueY = vccharts.this.getChartComputator().computeRawY(pointValue.getY());
                            if (isInArea(rawValueX, rawValueY, touchX, touchY, pointRadius + touchToleranceMargin)) {
                                LogUtils.e("     xuanzhong   xxx " + pointValue.getX() + "        yyy   " + pointValue.getY() + "           ");
                                vccharts.this.lineIndex = i;
                                vccharts.this.pointIndex = j;
                                isMoveChange = true;
                                for (Line line1 : lineChartData.getLines()) {
                                    line1.setStrokeWidth(2);
                                }
                                line.setStrokeWidth(4);
                                selectLineNumber = i;
                            }
                        }
                    }
                    return true;
                case MotionEvent.ACTION_MOVE:
                    if (isMoveChange) {
                        float y_max = 10;
                        float y_min = 0;
    //                    如果没有选中任何点
                        if (pointIndex == -1 || lineIndex == -1) {
                            return super.onTouchEvent(event);
                        }
                        lineChartData = vccharts.this.getLineChartData();
                        float y = lineChartData.getLines().get(lineIndex).getValues().get(pointIndex).getY();
                        float x = lineChartData.getLines().get(lineIndex).getValues().get(pointIndex).getX();
                        float y_new = y_max - coordinateConversionY(touchY);
                        if (isBetterPreAfter) {
                            float y_pre = y_min;
                            float y_after = y_max;
                            if (pointIndex < lineChartData.getLines().get(lineIndex).getValues().size() - 1) {
                                y_after = lineChartData.getLines().get(lineIndex).getValues().get(pointIndex + 1).getY();
                            }
                            if (pointIndex > 0) {
                                y_pre = lineChartData.getLines().get(lineIndex).getValues().get(pointIndex - 1).getY();
                            }
                            if (y_new < y_pre) {
                                y_new = y_pre;
                            }
                            if (y_new > y_after) {
                                y_new = y_after;
                            }
                        }
    
                        if (y_new < y_min) {
                            y_new = y_min;
                        }
                        if (y_new > y_max) {
                            y_new = y_max;
                        }
                        lineChartData.getLines().get(lineIndex).getValues().get(pointIndex).set(x, y_new);
    //                    通知数据变化
                        /*List<PointValue> values = lineChartData.getLines().get(lineIndex).getValues();
                        float[] changeData = new float[values.size()];
                        for (int i = 0; i < values.size(); i++) {
                            changeData[i] = values.get(i).getY();
                        }*/
                        postInvalidate();
                        /*if (vcListener != null) {
                            moveNumber++;
                            if (moveNumber % 3 == 0) {
                                vcListener.DataChange(lineIndex, new ZuniData(changeData));
                            }
                        }*/
                    }
                    return super.onTouchEvent(event);
                case MotionEvent.ACTION_UP:
                    LogUtils.e("  move " + moveNumber);
                    if (isMoveChange) {
                        if (vccharts.this.pointIndex == -1 || vccharts.this.lineIndex == -1) {
                            isMoveChange = false;
                            return true;
                        }
    
                        //                    通知数据变化
                        List<PointValue> values = lineChartData.getLines().get(lineIndex).getValues();
                        float[] changeData = new float[values.size()];
                        for (int i = 0; i < values.size(); i++) {
                            changeData[i] = values.get(i).getY();
                        }
    
                        try {
                            if (vcListener != null) {
                                vcListener.DataChange(lineIndex, new ZuniData(changeData));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
    
                        postInvalidate();
    
                        isMoveChange = false;
                        vccharts.this.pointIndex = -1;
                        vccharts.this.lineIndex = -1;
                        return true;
                    }
                    return true;
                case MotionEvent.ACTION_CANCEL:
                    if (isMoveChange) {
                        isMoveChange = false;
                        vccharts.this.pointIndex = -1;
                        vccharts.this.lineIndex = -1;
                        return true;
                    }
                    return true;
            }
            return super.onTouchEvent(event);
        }
    
        // 把坐标从 y 轴的位置转换成实际的值
        private float coordinateConversionY(float y) {
            LogUtils.i("坐标从 y 轴的位置转换成实际的值" + (10 - 0) / vLength * y);
            return (10 - 0) / vLength * y;
        }
    
        // 把坐标从 x 轴的位置转换成实际的值
        private float coordinateConversionX(float x) {
            LogUtils.i("坐标从 x 轴的位置转换成实际的值" + (11 - 0) / hLength * x);
            return (11 - 0) / hLength * x;
        }
    
        private boolean isInArea(float x, float y, float touchX, float touchY, float radius) {
            float diffX = touchX - x;
            float diffY = touchY - y;
            return Math.pow(diffX, 2) + Math.pow(diffY, 2) <= 2 * Math.pow(radius, 2);
        }
    
        public int getSelectLineNumber() {
            return selectLineNumber;
        }
    
        public void setSelectLineNumber(int selectLineNumber) {
            this.selectLineNumber = selectLineNumber;
        }
    
        public boolean isBetterPreAfter() {
            return isBetterPreAfter;
        }
    
        public void setBetterPreAfter(boolean betterPreAfter) {
            isBetterPreAfter = betterPreAfter;
        }
    
        public interface vcDataChangeListener {
            void DataChange(int line_index, ZuniData ZuniData);
        }
    }
    
    展开全文
  • 根据我的博文所写的一个关于实现hellochart开源框架的demo
  • helloChart 圆柱图

    千次阅读 2016-04-14 12:48:28
    注:本文是从github的例子上做的延伸以及添加了一些注释,并未在实际项目中使用,仅供参考 经测试,在pad上会出现适配问题,柱状图的位置和下标出现混乱,暂时无法解决 layout布局:  android:id="@+id/...
    注:本文是从github的例子上做的延伸以及添加了一些注释,并未在实际项目中使用,仅供参考
    经测试,在pad上会出现适配问题,柱状图的位置和下标出现混乱,暂时无法解决
    layout布局:
    <lecho.lib.hellocharts.view.ColumnChartView
            android:id="@+id/column"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </lecho.lib.hellocharts.view.ColumnChartView>

    java代码:

    import java.util.ArrayList;
    import java.util.List;
    import lecho.lib.hellocharts.listener.ColumnChartOnValueSelectListener;
    import lecho.lib.hellocharts.model.Axis;
    import lecho.lib.hellocharts.model.AxisValue;
    import lecho.lib.hellocharts.model.Column;
    import lecho.lib.hellocharts.model.ColumnChartData;
    import lecho.lib.hellocharts.model.SubcolumnValue;
    import lecho.lib.hellocharts.util.ChartUtils;
    import lecho.lib.hellocharts.view.Chart;
    import lecho.lib.hellocharts.view.ColumnChartView;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.Toast;
    /** 此开源控件在pad上面显示会出现坐标柱状图位置不对照的情况 */
    public class MainActivity extends Activity {
     private ColumnChartView mColumnChartView;
     private ColumnChartData mColumnChartData;
     private static final int DEFAULT_DATA = 0;
     private static final int SUBCOLUMNS_DATA = 1;
     private static final int STACKED_DATA = 2;
     private static final int NEGATIVE_SUBCOLUMNS_DATA = 3;
     private static final int NEGATIVE_STACKED_DATA = 4;
     private boolean hasAxes = true;
     private boolean hasAxesNames = true;
     private boolean hasLabels = true;
     private boolean hasLabelForSelected = false;
     private int dataType = DEFAULT_DATA;
     // private int dataType = SUBCOLUMNS_DATA;
     // private int dataType = STACKED_DATA;
     // private int dataType = NEGATIVE_SUBCOLUMNS_DATA;
     // private int dataType = NEGATIVE_STACKED_DATA;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      mColumnChartView = (ColumnChartView) findViewById(R.id.column);
      mColumnChartView.setOnValueTouchListener(new ValueTouchListener());
      generateData();
     }
     private void generateData() {
      switch (dataType) {
      case DEFAULT_DATA:
       generateDefaultData();
       break;
      case SUBCOLUMNS_DATA:
       generateSubcolumnsData();
       break;
      case STACKED_DATA:
       generateStackedData();
       break;
      case NEGATIVE_SUBCOLUMNS_DATA:
       generateNegativeSubcolumnsData();
       break;
      case NEGATIVE_STACKED_DATA:
       generateNegativeStackedData();
       break;
      default:
       generateDefaultData();
       break;
      }
     }
     private void reset() {
      hasAxes = true;
      hasAxesNames = true;
      hasLabels = false;
      hasLabelForSelected = false;
      dataType = DEFAULT_DATA;
      mColumnChartView.setValueSelectionEnabled(hasLabelForSelected);
     }
     private void generateDefaultData() {
      int numSubcolumns = 1;//设置每个柱状图显示的颜色数量(每个柱状图显示多少块)
      int numColumns = 8;//柱状图的数量
      // Column can have many subcolumns, here by default I use 1 subcolumn in each of 8 columns.
      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) {
        SubcolumnValue value = new SubcolumnValue((float) Math.random() * 50f + 5, ChartUtils.pickColor());//第一个值是数值(值>0 方向朝上,值<0,方向朝下),第二个值是颜色
        //    SubcolumnValue value = new SubcolumnValue((float) Math.random() * 50f + 5, Color.parseColor("#00000000"));//第一个值是数值,第二个值是颜色
        //    values.add(new SubcolumnValue((float) Math.random() * 50f + 5, ChartUtils.pickColor()));
        values.add(value);
       }
       Column column = new Column(values);//一个柱状图的实例
       column.setHasLabels(hasLabels);//设置是否显示每个柱状图的高度,
       column.setHasLabelsOnlyForSelected(hasLabelForSelected);//点击的时候是否显示柱状图的高度,和setHasLabels()和用的时候,setHasLabels()失效
       columns.add(column);
      }
      mColumnChartData = new ColumnChartData(columns);//表格的数据实例
      if (hasAxes) {
       Axis axisX = new Axis();
       //   axisX.setInside(true);//是否显示在里面,默认为false
       AxisValue value1 = new AxisValue(0f);//值是在哪显示 0 是指 第0个柱状图
       value1.setLabel("[0-15]");//设置显示的文本,默认为柱状图的位置
       AxisValue value2 = new AxisValue(1.0f);
       value2.setLabel("[15-30]");
       AxisValue value3 = new AxisValue(2.0f);
       value3.setLabel("[30-35]");
       AxisValue value4 = new AxisValue(3.0f);
       value4.setLabel("[35-45]");
       AxisValue value5 = new AxisValue(4.0f);
       value5.setLabel("[45-50]");
       AxisValue value6 = new AxisValue(5.0f);
       value6.setLabel("[50-55]");
       AxisValue value7 = new AxisValue(6.0f);
       value7.setLabel("[55-60]");
       AxisValue value8 = new AxisValue(7.0f);
       value8.setLabel("[65-70]");
       List<AxisValue> axisValues = new ArrayList<AxisValue>();
       axisValues.add(value1);
       axisValues.add(value2);
       axisValues.add(value3);
       axisValues.add(value4);
       axisValues.add(value5);
       axisValues.add(value6);
       axisValues.add(value7);
       axisValues.add(value8);
       axisX.setValues(axisValues);
       Axis axisY = new Axis().setHasLines(true);
       if (hasAxesNames) {
        axisX.setName("Axis X(涂涂的坐标轴)");//设置X轴的注释
        axisY.setName("Axis Y");//设置Y轴的注释
       }
       mColumnChartData.setAxisXBottom(axisX);//设置X轴显示的位置
       mColumnChartData.setAxisYLeft(axisY);//设置Y轴显示的位置
      } else {
       mColumnChartData.setAxisXBottom(null);
       mColumnChartData.setAxisYLeft(null);
      }
      mColumnChartView.setColumnChartData(mColumnChartData);//为View设置数据
     }
     /**
      * Generates columns with subcolumns, columns have larger separation than subcolumns.
      */
     private void generateSubcolumnsData() {
      int numSubcolumns = 4;
      int numColumns = 4;
      // Column can have many subcolumns, here I use 4 subcolumn in each of 8 columns.
      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()));
       }
       Column column = new Column(values);
       column.setHasLabels(hasLabels);
       column.setHasLabelsOnlyForSelected(hasLabelForSelected);
       columns.add(column);
      }
      mColumnChartData = new ColumnChartData(columns);
      if (hasAxes) {
       Axis axisX = new Axis();
       Axis axisY = new Axis().setHasLines(true);
       if (hasAxesNames) {
        axisX.setName("Axis X");
        axisY.setName("Axis Y");
       }
       mColumnChartData.setAxisXBottom(axisX);
       mColumnChartData.setAxisYLeft(axisY);
      } else {
       mColumnChartData.setAxisXBottom(null);
       mColumnChartData.setAxisYLeft(null);
      }
      mColumnChartView.setColumnChartData(mColumnChartData);
     }
     /**
      * Generates columns with stacked subcolumns.
      */
     private void generateStackedData() {
      int numSubcolumns = 4;
      int numColumns = 8;
      // Column can have many stacked subcolumns, here I use 4 stacke subcolumn in each of 4 columns.
      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() * 20f + 5, ChartUtils.pickColor()));
       }
       Column column = new Column(values);
       column.setHasLabels(hasLabels);
       column.setHasLabelsOnlyForSelected(hasLabelForSelected);
       columns.add(column);
      }
      mColumnChartData = new ColumnChartData(columns);
      // Set stacked flag.
      mColumnChartData.setStacked(true);
      if (hasAxes) {
       Axis axisX = new Axis();
       Axis axisY = new Axis().setHasLines(true);
       if (hasAxesNames) {
        axisX.setName("Axis X");//设置X轴的注释
        axisY.setName("Axis Y");//设置Y轴的注释
       }
       mColumnChartData.setAxisXBottom(axisX);//设置X轴显示的位置
       mColumnChartData.setAxisYLeft(axisY);//设置Y轴显示的位置
      } else {
       mColumnChartData.setAxisXBottom(null);
       mColumnChartData.setAxisYLeft(null);
      }
      mColumnChartView.setColumnChartData(mColumnChartData);
     }
     private void generateNegativeSubcolumnsData() {
      int numSubcolumns = 4;
      int numColumns = 4;
      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) {
        int sign = getSign();
        values.add(new SubcolumnValue((float) Math.random() * 50f * sign + 5 * sign, ChartUtils.pickColor
          ()));
       }
       Column column = new Column(values);
       column.setHasLabels(hasLabels);
       column.setHasLabelsOnlyForSelected(hasLabelForSelected);
       columns.add(column);
      }
      mColumnChartData = new ColumnChartData(columns);
      if (hasAxes) {
       Axis axisX = new Axis();
       Axis axisY = new Axis().setHasLines(true);
       if (hasAxesNames) {
        axisX.setName("Axis X");
        axisY.setName("Axis Y");
       }
       mColumnChartData.setAxisXBottom(axisX);
       mColumnChartData.setAxisYLeft(axisY);
      } else {
       mColumnChartData.setAxisXBottom(null);
       mColumnChartData.setAxisYLeft(null);
      }
      mColumnChartView.setColumnChartData(mColumnChartData);
     }
     private void generateNegativeStackedData() {
      int numSubcolumns = 4;
      int numColumns = 8;
      // Column can have many stacked subcolumns, here I use 4 stacke subcolumn in each of 4 columns.
      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) {
        int sign = getSign();
        values.add(new SubcolumnValue((float) Math.random() * 20f * sign + 5 * sign, ChartUtils.pickColor()));
       }
       Column column = new Column(values);
       column.setHasLabels(hasLabels);
       column.setHasLabelsOnlyForSelected(hasLabelForSelected);
       columns.add(column);
      }
      mColumnChartData = new ColumnChartData(columns);
      // Set stacked flag.
      mColumnChartData.setStacked(true);
      if (hasAxes) {
       Axis axisX = new Axis();
       Axis axisY = new Axis().setHasLines(true);
       if (hasAxesNames) {
        axisX.setName("Axis X");
        axisY.setName("Axis Y");
       }
       mColumnChartData.setAxisXBottom(axisX);
       mColumnChartData.setAxisYLeft(axisY);
      } else {
       mColumnChartData.setAxisXBottom(null);
       mColumnChartData.setAxisYLeft(null);
      }
      mColumnChartView.setColumnChartData(mColumnChartData);
     }
     private int getSign() {
      int[] sign = new int[]{-1, 1};
      return sign[Math.round((float) Math.random())];
     }
     private void toggleLabels() {
      hasLabels = !hasLabels;
      if (hasLabels) {
       hasLabelForSelected = false;
       mColumnChartView.setValueSelectionEnabled(hasLabelForSelected);
      }
      generateData();
     }
     private void toggleLabelForSelected() {
      hasLabelForSelected = !hasLabelForSelected;
      mColumnChartView.setValueSelectionEnabled(hasLabelForSelected);
      if (hasLabelForSelected) {
       hasLabels = false;
      }
      generateData();
     }
     private void toggleAxes() {
      hasAxes = !hasAxes;
      generateData();
     }
     private void toggleAxesNames() {
      hasAxesNames = !hasAxesNames;
      generateData();
     }
     /**
      * To animate values you have to change targets values and then call {@link Chart#startDataAnimation()}
      * method(don't confuse with View.animate()).
      */
     private void prepareDataAnimation() {
      for (Column column : mColumnChartData.getColumns()) {
       for (SubcolumnValue value : column.getValues()) {
        value.setTarget((float) Math.random() * 100);
       }
      }
     }
     private class ValueTouchListener implements ColumnChartOnValueSelectListener {
      @Override
      public void onValueSelected(int columnIndex, int subcolumnIndex, SubcolumnValue value) {
       Toast.makeText(MainActivity.this, "Selected: " + value, Toast.LENGTH_SHORT).show();
      }
      @Override
      public void onValueDeselected() {
      }
     }
    }
    展开全文
  • helloChart使用心得

    2018-03-16 15:19:47
    本人最近做一个需要用折线图来实时显示温度曲线的项目,做了很多尝试,比如Achartengine,MPAndroidChart等,Achartengine在华为7.0上运行报错,实在找不到MPAndroidChart jar包,只有helloChart解决了我的问题坑点1...
  • HelloChart Demo记录

    2016-07-21 14:12:40
    记录HelloChart 折线图的APIpackage com.example.mylinechart;import java.util.ArrayList; import java.util.List;import lecho.lib.hellocharts.formatter.SimpleAxisValueFormatter; import lecho.lib....
  • Android HelloChart Demo

    2015-10-18 17:09:00
    这几天,要做一个图标的统计,自己去网上查了下,现在用的比较多的有三种,AChartEngine是Google的一个开源图表库 这种我最开始就去导demo去了解他,不过里面是是英文,不好研究。我就放弃了,而且我看了demo后感觉...
  • java.lang.OutOfMemoryError at android.graphics.Bitmap.nativeCreate(Native Method) at android.graphics.Bitmap.createBitmap(Bitmap.java:689) at android.graphics.Bitmap.createBitmap(Bitmap.java:666) ...
  • hello chart——柱状图

    千次阅读 2017-03-29 01:05:55
    先看一个例子: test.xml: FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"... android:layout_width="match_parent" android:layout_height="match_parent"> lecho.lib.hellocharts.v
  • 图表开发工具,自测ok,能用0202020202020202020

空空如也

1 2 3 4 5 ... 20
收藏数 679
精华内容 271
关键字:

hellochart