Android MPChart—气泡图

本文介绍了如何在Android项目中使用MPChart库创建气泡图,详细讲解了布局文件的设置以及BubbleChartActivity的实现,展示了根据不同Y轴数据改变气泡颜色的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第三方资源库MPChart如何添加到项目中就不说了,不知道的网上搜一下,很多。

也可以参考:https://blog.youkuaiyun.com/lvxiaobo1994/article/details/82790187

本篇主要说明MPChart中BubbleChart(气泡图)是如何调用实现的。

一、布局文件

<com.github.mikephil.charting.charts.BubbleChart
    android:id="@+id/bubbleChart"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

二、BubbleChartActivity

我这里实现的是三种不同层次的气泡图,根据Y轴数据判断气泡的颜色。

public class BubbleChartActivity extends AppCompatActivity {
    private BubbleChart bubbleChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bubble_chart);
        setView();
        setData();
    }

    private void setView() {
        bubbleChart = findViewById(R.id.bubbleChart);
        bubbleChart.getDescription().setEnabled(false);
        bubbleChart.setOnChartValueSelectedListener(this);
        bubbleChart.setDrawGridBackground(false);
        bubbleChart.setTouchEnabled(true);
        bubbleChart.setDragEnabled(true);
        bubbleChart.setScaleEnabled(true);
        bubbleChart.setMaxVisibleValueCount(200);
        bubbleChart.setPinchZoom(true);
        Legend l = bubbleChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        YAxis yl = bubbleChart.getAxisLeft();
        yl.setSpaceTop(30f);
        yl.setSpaceBottom(30f);
        yl.setDrawZeroLine(false);
        bubbleChart.getAxisRight().setEnabled(false);
        XAxis xl = bubbleChart.getXAxis();
        xl.setPosition(XAxis.XAxisPosition.BOTTOM);
        bubbleChart.animateY(2000);
    }


    private void setData() {
        int count = 10;
        int range = 100;

        ArrayList<BubbleEntry> yVals = new ArrayList<BubbleEntry>();

        ArrayList<BubbleEntry> yVals_blue = new ArrayList<BubbleEntry>();
        ArrayList<BubbleEntry> yVals_yellow = new ArrayList<BubbleEntry>();
        ArrayList<BubbleEntry> yVals_red = new ArrayList<BubbleEntry>();

        for (int i = 0; i < count; i++) {
            float val = (float) (Math.random() * range);
            yVals.add(new BubbleEntry(i + 1, val, val));
        }

        for (int i = 0; i < yVals.size(); i++) {
            BubbleEntry bubbleEntry = yVals.get(i);
            if (bubbleEntry.getY() <= 100 / 3) {
                yVals_red.add(bubbleEntry);
            } else if (bubbleEntry.getY() > 100 / 3 && bubbleEntry.getY() <= 200 / 3) {
                yVals_yellow.add(bubbleEntry);
            } else if (bubbleEntry.getY() > 200 / 3) {
                yVals_blue.add(bubbleEntry);
            }
        }


        BubbleDataSet set1 = new BubbleDataSet(yVals_blue, "优秀");
        set1.setDrawIcons(false);
        set1.setColor(Color.BLUE, 100);
        set1.setDrawValues(true);

        BubbleDataSet set2 = new BubbleDataSet(yVals_yellow, "良好");
        set2.setDrawIcons(false);
        set2.setColor(Color.YELLOW, 100);
        set2.setDrawValues(true);

        BubbleDataSet set3 = new BubbleDataSet(yVals_red, "很差");
        set3.setDrawIcons(false);
        set3.setColor(Color.RED, 100);
        set3.setDrawValues(true);

        ArrayList<IBubbleDataSet> dataSets = new ArrayList<IBubbleDataSet>();
        dataSets.add(set1);
        dataSets.add(set2);
        dataSets.add(set3);
        BubbleData data = new BubbleData(dataSets);
        data.setDrawValues(true);
        data.setValueTextSize(14f);
        data.setValueTextColor(Color.BLACK);
        data.setHighlightCircleWidth(1.5f);
        bubbleChart.setData(data);
        bubbleChart.invalidate();
    }
}

至于里面BubbleChart的属性方法是什么意思,大家可以自行百度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值