sjw601748054
头皮阴都都
2017-10-20 02:31

hellochart之combolinecolumnchar左右Y轴不同绑定

20

使用了combolinecolumncharview,但是折线图和柱状图只能绑定左侧Y轴。我的需求是完成左侧Y轴对应折线图,右侧Y轴对应柱状图。目前只能做到右侧Y轴实际值是左侧的参数,标签是右侧单独的
求问怎么实现柱状图绑定右侧Y轴

    public class fragment3 extends Fragment {

private ComboLineColumnChartView comboChart;
private List<AxisValue> values ;
private final static float[] yzhou = new float[]{0,33,66,99,132};
private final static float[] yValues = new float[]{0, 175, 350, 525, 750};
private final static String[] yValuesString = new String[]{"0", "175", "350", "525", "750"};

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab03,container,false);

    comboChart = (ComboLineColumnChartView) view.findViewById(R.id.combo_chart);
    comboChart.setZoomEnabled(true);//设置是否支持缩放
    //为图表设置值得触摸事件
    //设置值触摸侦听器,将触发图表顶部的变化。
    comboChart.setOnValueTouchListener(new ComboLineColumnChartOnValueSelectListener() {
        @Override
        public void onColumnValueSelected(int i, int i1, SubcolumnValue subcolumnValue) {

        }
        @Override
        public void onPointValueSelected(int i, int i1, PointValue pointValue) {

        }
        @Override
        public void onValueDeselected() {

        }
    });


    //定义组合数据对象
    ComboLineColumnChartData comboLineColumnChartData = new ComboLineColumnChartData();
    //为图表设置数据,数据类型为ComboLineColumnChartData
    comboChart.setComboLineColumnChartData(comboLineColumnChartData);

    //为组合图设置折现图数据
    List<Line> dataLine = initDataLine();//数据源
    LineChartData lineCharData = initLineCharData(dataLine);//添加数据
    lineCharData.setLines(dataLine);


    comboLineColumnChartData.setValueLabelsTextColor(Color.WHITE);// 设置数据文字颜色
    comboLineColumnChartData.setValueLabelTextSize(25);// 设置数据文字大小
    comboLineColumnChartData.setValueLabelTypeface(Typeface.MONOSPACE);// 设置数据文字样式



    Axis axisX = new Axis();
    Axis axisY = new Axis().setHasLines(true);
    axisY.setMaxLabelChars(5);//max label length, for example 60
    values = new ArrayList<>();
    for(int i = 0; i < yzhou.length; i++){
        AxisValue value = new AxisValue(yzhou[i]);
        values.add(value);
    }
    axisY.setValues(values);
    axisX.setHasLines(false);
    lineCharData.setAxisYLeft(axisY);
    //右边Y轴刻度
    values = new ArrayList<>();
    for (int i=0;i<yValues.length;++i) {
        AxisValue value = new AxisValue(yValues[i]);
        value.setLabel(yValuesString[i]);
        values.add(value);
    }
    Axis axisRY = new Axis();
    axisRY.setValues(values);
    Log.e(TAG, "onCreateView: "+values );
    axisX.setValues(axisValues);
    axisY.setTextColor(Color.WHITE);
    axisX.setTextColor(Color.WHITE);
    axisRY.setTextColor(Color.WHITE);
    lineCharData.setAxisYLeft(axisY);
    lineCharData.setAxisXBottom(axisX);
    //为组合图设置柱形图数据
    List<Column> dataColumn = initColumnLine();
    ColumnChartData columnChartData = new ColumnChartData(dataColumn);
    columnChartData.setAxisYRight(axisRY);
    columnChartData.setAxisXBottom(axisX);
    comboLineColumnChartData.setColumnChartData(columnChartData);
    comboLineColumnChartData.setLineChartData(lineCharData);
    comboLineColumnChartData.setAxisYRight(axisRY);//设置右边显示的轴
    comboLineColumnChartData.setAxisYLeft(axisY);
    comboLineColumnChartData.setAxisXBottom(axisX);
    comboChart.setComboLineColumnChartData(comboLineColumnChartData);//为组合图添加数据

    Viewport viewport = initViewPort();
    comboChart.setMaximumViewport(viewport);
    comboChart.setCurrentViewport(viewport);

    return view;
}

private Viewport initViewPort() {
    Viewport viewport = new Viewport();
    viewport.top = 132;
    viewport.bottom = 0;
    viewport.left = -1;
    viewport.right = 12;

    return viewport;
}

//设置折线图,添加设置好的数据
public static LineChartData initLineCharData(List dataLine) {
LineChartData lineCharData = new LineChartData(dataLine);
//初始化轴
Axis axisX = new Axis();
Axis axisY = new Axis();
lineCharData.setAxisYLeft(axisY);
lineCharData.setAxisXBottom(axisX);
return lineCharData;
}

//定义方法设置折线图中数据
public static List<Line> initDataLine() {
    List<Line> lineList = new ArrayList<>();
    List<PointValue> pointValueList = new ArrayList<>();

    for (int i = 0; i < months.length; ++i) {
        pointValueList.add(new PointValue(i, (float) Math.random() * 130f ));
        axisValues.add(new AxisValue(i).setLabel(months[i]));
    }
    Line line = new Line(pointValueList);
    line.setColor(ChartUtils.COLOR_GREEN);
    line.setShape(ValueShape.CIRCLE);
    //设置标签选中时可见
    line.setHasLabelsOnlyForSelected(true);
    lineList.add(line);

    return lineList;
}

//设置柱状图,添加设置好的数据
public static ColumnChartData initColumnCharData(List<Column> dataLine) {
    ColumnChartData columnData = new ColumnChartData(dataLine);


    columnData.setAxisXBottom(new Axis(axisValues).setHasLines(true)
            .setTextColor(Color.BLACK));
    columnData.setAxisYRight(new Axis().setHasLines(true)
            .setTextColor(Color.BLACK).setMaxLabelChars(2));
    // 设置选中模式以保证选中月份突出显示

// comboChart.setValueSelectionEnabled(true);
// comboChart.setZoomType(ZoomType.HORIZONTAL);

    return columnData;
}

//定义方法设置柱状图中数据
public static List<Column> initColumnLine() {
    List<Column> list = new ArrayList<>();
    List<SubcolumnValue> subcolumnValueList;
    axisValues = new ArrayList<AxisValue>();
    int numSubcolumns = 1;
    for (int i = 0; i < months.length; ++i) {
        subcolumnValueList = new ArrayList<SubcolumnValue>();
        for (int j = 0; j < numSubcolumns; ++j) {
            subcolumnValueList.add(new SubcolumnValue((float) Math.random() * 130f  ,
                    ChartUtils.COLOR_BLUE));
        }
        axisValues.add(new AxisValue(i).setLabel(months[i]));

        // 点击柱状图就展示数据量
        list.add(new Column(subcolumnValueList).setHasLabelsOnlyForSelected(true));
    }
    return list;
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

相关推荐