MPAndroidChart使用之HalfPieChart

一、引入MPAndroidChart库:

项目下build.gradle:

allprojects {
	repositories {
		maven { url "https://jitpack.io" }
	}
}
your app下build.gradle:

dependencies {
	compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'
}
Sync Now即可。

二、
效果:


布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

</RelativeLayout>

HalfPieChartActivity.java

package com.james.mpandroidchart.ui.activity;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.Display;
import android.widget.RelativeLayout;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.james.mpandroidchart.R;

import java.util.ArrayList;

/**
 * Created by 1 on 2017/3/28.
 */

public class HalfPieChartActivity extends AppCompatActivity {
    private PieChart mChart;
    private Typeface mTfLight;
    private Typeface mTfRegular;
    private String[] mParties = new String[] {
            "Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G", "Party H",
            "Party I", "Party J", "Party K", "Party L", "Party M", "Party N", "Party O", "Party P",
            "Party Q", "Party R", "Party S", "Party T", "Party U", "Party V", "Party W", "Party X",
            "Party Y", "Party Z"
    };
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTfLight = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");
        mTfRegular = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
        setContentView(R.layout.activity_piechart_half);
        mChart = (PieChart) findViewById(R.id.chart1);
        moveOffScreen();
        mChart.setUsePercentValues(true);
        mChart.getDescription().setEnabled(false);

        mChart.setCenterTextTypeface(mTfLight);//设置字体
        mChart.setCenterText(generateCenterSpannableText());//中间显示文字

        mChart.setDrawHoleEnabled(true);//true设置绘图孔启用
        mChart.setHoleColor(Color.WHITE);//设置绘图孔颜色

        mChart.setTransparentCircleColor(Color.WHITE);//
        mChart.setTransparentCircleAlpha(110);//透明度

        mChart.setHoleRadius(58f);//中间圆的半径
        mChart.setTransparentCircleRadius(61f);//

        mChart.setDrawCenterText(true);//中心是否允许画文字

        mChart.setRotationEnabled(false);//整个视图是否旋转
        mChart.setHighlightPerTapEnabled(true);//true点击各个板块会向上突起一点

        mChart.setMaxAngle(180f); // 显示一半
        mChart.setRotationAngle(180f);
        mChart.setCenterTextOffset(0, -20);//中间文字向上偏移20
        setData(4, 100);//数据

        mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);//设置进来动画。1400是动画执行的时间

        Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//竖直方向
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//水平方向设置
        l.setOrientation(Legend.LegendOrientation.VERTICAL);//PartyA,PartyB,PartyC,PartyD竖直排放
        l.setDrawInside(false);
        l.setXEntrySpace(7f);//PartyA,PartyB,PartyC,PartyD的x轴间距
        l.setYEntrySpace(0f);//PartyA,PartyB,PartyC,PartyD的y轴间距
        l.setYOffset(0f);//y轴偏移量

        // entry label styling
        mChart.setEntryLabelColor(Color.WHITE);
        mChart.setEntryLabelTypeface(mTfRegular);
        mChart.setEntryLabelTextSize(12f);
    }

    private void setData(int count, float range) {

        ArrayList<PieEntry> values = new ArrayList<PieEntry>();

        for (int i = 0; i < count; i++) {
            values.add(new PieEntry((float) ((Math.random() * range) + range / 5), mParties[i % mParties.length]));
        }

        PieDataSet dataSet = new PieDataSet(values, "Election Results");
        dataSet.setSliceSpace(3f);
        dataSet.setSelectionShift(5f);

        dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
        //dataSet.setSelectionShift(0f);

        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.WHITE);
        data.setValueTypeface(mTfLight);//设置字体
        mChart.setData(data);

        mChart.invalidate();
    }
    private void moveOffScreen() {


        Display display = getWindowManager().getDefaultDisplay();
        int height = display.getHeight();  // deprecated

        int offset = (int)(height * 0.65); /* percent to move */

        RelativeLayout.LayoutParams rlParams =
                (RelativeLayout.LayoutParams)mChart.getLayoutParams();
        rlParams.setMargins(0, 0, 0, -offset);
        mChart.setLayoutParams(rlParams);
    }
    private SpannableString generateCenterSpannableText() {

        SpannableString s = new SpannableString("MPAndroidChart\ndeveloped by Philipp Jahoda");
        s.setSpan(new RelativeSizeSpan(1.7f), 0, 14, 0);
        s.setSpan(new StyleSpan(Typeface.NORMAL), 14, s.length() - 15, 0);
        s.setSpan(new ForegroundColorSpan(Color.GRAY), 14, s.length() - 15, 0);
        s.setSpan(new RelativeSizeSpan(.8f), 14, s.length() - 15, 0);
        s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 14, s.length(), 0);
        s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 14, s.length(), 0);
        return s;
    }
}



 mChart.setTransparentCircleRadius(61f);//
指的是:

 mChart.setEntryLabelColor(Color.RED);

设置的是

 Legend l = mChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        l.setXEntrySpace(7f);
        l.setYEntrySpace(0f);
        l.setYOffset(0f);

设置的是:





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MPAndroidChart是一款功能强大的Android图表库,它支持多种类型的图表,如折线图、柱状图、饼图、散点图等,并且可以进行多种自定义设置和交互操作。以下是MPAndroidChart使用详解: 1. 导入库 在项目的build.gradle文件中添加以下依赖: ``` dependencies { implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0' } ``` 2. 添加布局 在布局文件中添加一个空的布局,用于显示图表: ``` <LinearLayout android:id="@+id/chart_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" /> ``` 3. 初始化图表 在代码中获取布局并初始化图表: ``` // 获取布局 LinearLayout chartLayout = findViewById(R.id.chart_layout); // 创建图表 LineChart chart = new LineChart(this); // 添加到布局中 chartLayout.addView(chart); ``` 4. 设置数据 设置图表的数据源: ``` List<Entry> entries = new ArrayList<>(); entries.add(new Entry(0, 2)); entries.add(new Entry(1, 4)); entries.add(new Entry(2, 6)); entries.add(new Entry(3, 8)); entries.add(new Entry(4, 10)); LineDataSet dataSet = new LineDataSet(entries, "Label"); LineData lineData = new LineData(dataSet); chart.setData(lineData); ``` 5. 自定义样式 可以对图表进行多种自定义设置,如图例、坐标轴、标签等: ``` // 设置图例 Legend legend = chart.getLegend(); legend.setEnabled(false); // 设置X轴 XAxis xAxis = chart.getXAxis(); xAxis.setPosition(XAxis.XAxisPosition.BOTTOM); // 设置Y轴 YAxis yAxis = chart.getAxisLeft(); yAxis.setAxisMinimum(0f); yAxis.setAxisMaximum(12f); yAxis.setGranularity(1f); yAxis.setValueFormatter(new ValueFormatter() { @Override public String getFormattedValue(float value) { return String.valueOf((int) value); } }); // 设置标签 chart.getDescription().setEnabled(false); ``` 6. 添加交互操作 可以为图表添加多种交互操作,如缩放、拖动等: ``` // 缩放 chart.setScaleEnabled(true); chart.setPinchZoom(true); // 拖动 chart.setDragEnabled(true); ``` 以上就是MPAndroidChart使用详解,希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值