当前位置:   article > 正文

Android图表控件MPChart-柱状+折线组合图表基本使用_android 图表控件

android 图表控件
一、效果图

双模式效果:柱形图加折线图,双y轴,从右向左滑动…
日间模式
夜间模式

二、实现代码

1. 加入依赖
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.2'
  • 1
2. 布局中加入组合图标签
<com.github.mikephil.charting.charts.CombinedChart
                android:id="@+id/chart"
                android:layout_width="match_parent"
                android:layout_height="320dp"
                android:background="@color/content_dark_bg"/>
  • 1
  • 2
  • 3
  • 4
  • 5
3. 创建制表类CombinedChartManager

构方定义参数context、组合图控件对象,定义几个全局对象leftAxis、rightAxis、xAxis。

public class CombinedChartManager {
   
    private Context mContext;
    private CombinedChart mCombinedChart;
    private YAxis leftAxis,rightAxis;
    private XAxis xAxis;

    private int barColor,lineColor,circleColor,circleInColor,textColor,lineTextColor,barTextColor;
    private int[] klineTypes = {
   Constant.KlineType.ONE_HOUR,Constant.KlineType.SIX_HOUR,Constant.KlineType.ONE_DAY,Constant.KlineType.ONE_WEEK};

    @SuppressLint("ResourceType")
    public CombinedChartManager(Context context,CombinedChart combinedChart) {
   
        this.mContext=context;
        this.mCombinedChart = combinedChart;
        leftAxis = mCombinedChart.getAxisLeft();
        rightAxis = mCombinedChart.getAxisRight();
        xAxis = mCombinedChart.getXAxis();

        //颜色设置,可忽略
        lineColor=mContext.getResources().getColor(R.color.line_color);
        circleColor=mContext.getResources().getColor(R.color.circle_yellow_color);
        circleInColor=mContext.getResources().getColor(R.color.circle_yellow_in_color);
        textColor=mContext.getResources().getColor(R.color.table_x_text_color);
        lineTextColor=mContext.getResources().getColor(R.color.table_line_text_color);
        barTextColor=mContext.getResources().getColor(R.color.table_bar_text_color);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
4. 初始化图表

设置图标描述、背景色、边界、
是否允许缩放、是否拖动减速、是否可滑动、
图例说明、X轴、Y轴

private void initChart() {
   
        //不显示图标描述
        mCombinedChart.getDescription().setEnabled(false
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/254632
推荐阅读
相关标签
  

闽ICP备14008679号