一个简单小巧的Android图表:Charter
一个简单小巧的图表,包括曲线图和柱状图,支持动画效果。
build.gradle
dependencies { compile 'com.hrules:charter:1.1.1' }
MainActivity.java
package com.hrules.charter.demo; import android.content.res.Resources; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import com.hrules.charter.CharterBar; import com.hrules.charter.CharterLine; import com.hrules.charter.CharterXLabels; import com.hrules.charter.CharterYLabels; import java.util.Random; public class MainActivity extends AppCompatActivity { private static final int DEFAULT_ITEMS_COUNT = 15; private static final int DEFAULT_RANDOM_VALUE_MIN = 10; private static final int DEFAULT_RANDOM_VALUE_MAX = 100; private float[] values; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); values = fillRandomValues(DEFAULT_ITEMS_COUNT, DEFAULT_RANDOM_VALUE_MAX, DEFAULT_RANDOM_VALUE_MIN); Resources res = getResources(); int[] barColors = new int[] { res.getColor(R.color.lightblue500), res.getColor(R.color.lightblue400), res.getColor(R.color.lightblue300) }; // charter_line final CharterLine charterLine = (CharterLine) findViewById(R.id.charter_line); charterLine.setValues(values); charterLine.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { values = fillRandomValues(DEFAULT_ITEMS_COUNT, DEFAULT_RANDOM_VALUE_MAX, DEFAULT_RANDOM_VALUE_MIN); charterLine.setValues(values); charterLine.show(); } }); // charter_bar final CharterBar charterBar = (CharterBar) findViewById(R.id.charter_bar); charterBar.setValues(values); charterBar.setColors(barColors); charterBar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { values = fillRandomValues(DEFAULT_ITEMS_COUNT, DEFAULT_RANDOM_VALUE_MAX, DEFAULT_RANDOM_VALUE_MIN); charterBar.setValues(values); charterBar.show(); } }); // charter_bar_no_margin final CharterBar charterBarNoMargin = (CharterBar) findViewById(R.id.charter_bar_no_margin); charterBarNoMargin.setValues(values); charterBarNoMargin.setBarMargin(0); charterBarNoMargin.setColors(barColors); charterBarNoMargin.setColorsBackground(new int[] { android.R.color.white }); charterBarNoMargin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { values = fillRandomValues(DEFAULT_ITEMS_COUNT, DEFAULT_RANDOM_VALUE_MAX, DEFAULT_RANDOM_VALUE_MIN); charterBarNoMargin.setValues(values); charterBarNoMargin.show(); } }); // charter_line_XLabel final CharterXLabels charterLineLabelX = (CharterXLabels) findViewById(R.id.charter_line_XLabel); charterLineLabelX.setStickyEdges(true); charterLineLabelX.setValues(values); final CharterLine charterLineWithLabel = (CharterLine) findViewById(R.id.charter_line_with_XLabel); charterLineWithLabel.setValues(values); charterLineWithLabel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { values = fillRandomValues(DEFAULT_ITEMS_COUNT, DEFAULT_RANDOM_VALUE_MAX, DEFAULT_RANDOM_VALUE_MIN); charterLineWithLabel.setValues(values); charterLineWithLabel.show(); charterLineLabelX.setValues(values); } }); // charter_bar_XLabel final CharterXLabels charterBarLabelX = (CharterXLabels) findViewById(R.id.charter_bar_XLabel); charterBarLabelX.setStickyEdges(false); charterBarLabelX.setVisibilityPattern(new boolean[] { true, false }); charterBarLabelX.setValues(values); final CharterBar charterBarWithLabel = (CharterBar) findViewById(R.id.charter_bar_with_XLabel); charterBarWithLabel.setValues(values); charterBarWithLabel.setColors(barColors); charterBarWithLabel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { values = fillRandomValues(DEFAULT_ITEMS_COUNT, DEFAULT_RANDOM_VALUE_MAX, DEFAULT_RANDOM_VALUE_MIN); charterBarWithLabel.setValues(values); charterBarWithLabel.show(); charterBarLabelX.setValues(values); } }); // charter_line_YLabel final CharterYLabels charterLineYLabel = (CharterYLabels) findViewById(R.id.charter_line_YLabel); charterLineYLabel.setValues(values, true); final CharterLine charterLineWithYLabel = (CharterLine) findViewById(R.id.charter_line_with_YLabel); charterLineWithYLabel.setValues(values); charterLineWithYLabel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { values = fillRandomValues(DEFAULT_ITEMS_COUNT, DEFAULT_RANDOM_VALUE_MAX, DEFAULT_RANDOM_VALUE_MIN); charterLineWithYLabel.setValues(values); charterLineWithYLabel.show(); charterLineYLabel.setValues(values, true); } }); // charter_bar_YLabel final CharterYLabels charterBarYLabel = (CharterYLabels) findViewById(R.id.charter_bar_YLabel); charterBarYLabel.setVisibilityPattern(new boolean[] { true, false }); charterBarYLabel.setValues(values, true); final CharterBar charterBarWithYLabel = (CharterBar) findViewById(R.id.charter_bar_with_YLabel); charterBarWithYLabel.setValues(values); charterBarWithYLabel.setColors(barColors); charterBarWithYLabel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { values = fillRandomValues(DEFAULT_ITEMS_COUNT, DEFAULT_RANDOM_VALUE_MAX, DEFAULT_RANDOM_VALUE_MIN); charterBarWithYLabel.setValues(values); charterBarWithYLabel.show(); charterBarYLabel.setValues(values, true); } }); } private float[] fillRandomValues(int length, int max, int min) { Random random = new Random(); float[] newRandomValues = new float[length]; for (int i = 0; i < newRandomValues.length; i++) { newRandomValues[i] = random.nextInt(max - min + 1) - min; } return newRandomValues; } }
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!