zoukankan      html  css  js  c++  java
  • <Android 应用 之路> MPAndroidChart~ScatterChart

    简介

    MPAndroidChart是PhilJay大神给Android开发者带来的福利。MPAndroidChart是一个功能强大并且使用灵活的图表开源库,支持Android和IOS两种,这里我们暂时只关注Android版本。

    Wiki

    https://github.com/PhilJay/MPAndroidChart/wiki

    Javadoc

    https://jitpack.io/com/github/PhilJay/MPAndroidChart/v3.0.0-beta1/javadoc/

    今日之图~ScatterChart

    这里写图片描述

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="mapdemo.example.com.mpandroidchartdemo.MainActivity">
    
        <com.github.mikephil.charting.charts.ScatterChart
            android:id="@+id/scatterchart"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Hello World!" />
    </RelativeLayout>

    代码

    public class MainActivity extends AppCompatActivity {
    
        @Bind(R.id.scatterchart)
        ScatterChart scatterChart;
    
        private Random random;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ButterKnife.bind(this);
            random = new Random();
            initScatterChart();
        }
    
        private void initScatterChart() {
    
            ArrayList<Entry> yVals = new ArrayList<>();
            ArrayList<String> xVals = new ArrayList<>();//初始化横纵坐标内容
            for (int i = 0; i < 5; i++) {
                xVals.add((i + 1) + "月");
                yVals.add(new Entry(random.nextInt(1000), i));//产生数据
            }
            ScatterDataSet scatterDataSet = new ScatterDataSet(yVals, "小明每月支出");
            scatterDataSet.setColors(ColorTemplate.COLORFUL_COLORS);//设置丰富多彩的颜色
            ScatterData scatterData = new ScatterData(xVals, scatterDataSet);//生成Scatterdata对象
    
            scatterChart.setData(scatterData);//设置对应数据
            scatterChart.getLegend().setPosition(Legend.LegendPosition.ABOVE_CHART_LEFT);
            scatterChart.getLegend().setForm(Legend.LegendForm.CIRCLE);
            scatterChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
                @Override
                public void onValueSelected(Entry entry, int i, Highlight highlight) {
                    Toast.makeText(MainActivity.this, "Something selected value = " + entry.getVal(), Toast.LENGTH_SHORT).show();
                }
                @Override
                public void onNothingSelected() {
                }
            });
    
            scatterChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴位置
            scatterChart.getAxisRight().setEnabled(false);//右侧Y轴关闭
            scatterChart.getAxisLeft().setAxisMinValue(0.0f);//设置最小Y值
            scatterChart.getXAxis().setGridColor(Color.RED);//设置纵向网格线条颜色
            scatterChart.getAxisLeft().setGridColor(Color.GREEN);//设置横向网格颜色
            scatterChart.setDescription("No Deal");//设置描述内容
            scatterChart.setDescriptionTextSize(20.f);//设置描述文字的字体颜色
            scatterChart.animateXY(1000, 1000);//动画效果
        }
    }

    备注

    ScatterChart和LineChart,BarChart很相似,使用方式也基本相同,基本用法就是这样,后面遇到问题在解决对应的问题,碰到新的需求再解决新的需求。任何问题肯定会有解决办法。

  • 相关阅读:
    #define用法详解
    memchr函数
    aiohttp模块1 client
    asyncio标准库7 Producer/consumer
    asyncio标准库6 Threads & Subprocess
    asyncio标准库5 TCP echo client and server
    asyncio标准库4 asyncio performance
    asyncio标准库3 HTTP client example
    asyncio标准库2 Hello Clock
    asyncio标准库1 Hello World
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6467173.html
Copyright © 2011-2022 走看看