我需要在 android 中绘制简单的条形图(类似于那个条形图)。我发现了很多库,但它们非常复杂并没有真正有用。也许有人已经使用了条形图并且知道一个好的?
问问题
3396 次
4 回答
2
我有一个非常简单易用的解决方案,用于Bar Chart
在 android 中使用MpAndroidChart Library
.
build.gradle
在文件中添加库:
compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
MainActivity.java:
BarChart barChart = (BarChart) findViewById(R.id.barchart);
ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(38f, 0));
entries.add(new BarEntry(52f, 1));
entries.add(new BarEntry(65f, 2));
entries.add(new BarEntry(30f, 3));
entries.add(new BarEntry(85f, 4));
entries.add(new BarEntry(19f, 5));
entries.add(new BarEntry(75f, 6));
BarDataSet bardataset = new BarDataSet(entries, " ");
ArrayList<String> labels = new ArrayList<String>();
labels.add("Mon");
labels.add("Tue");
labels.add("Wed");
labels.add("Thus");
labels.add("Fri");
labels.add("Sat");
labels.add("Sun");
BarData data = new BarData(labels, bardataset);
barChart.setData(data); // set the data and list of lables into chart
barChart.setDescription("Description"); // set the description
bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
barChart.animateY(5000);
活动主.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jatin.bargraph.MainActivity">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/barchart"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
于 2017-05-29T09:55:30.493 回答
1
我有一个非常简单的解决方案(你不需要什么特别的):
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class TestesActivity extends Activity {
private List<Double> s;
private LinearLayout linearChart;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearChart = (LinearLayout)findViewById(R.id.llGraph);
s = new ArrayList<Double>();
for (int i = 0; i < 12; i++) {
Random r = new Random();
s.add(r.nextDouble());
}
Thread t = new Thread() {
public void run() {
while (true) {
try {
Thread.sleep(5000);
} catch (Exception e) {
}
runOnUiThread(new Runnable() {
@Override
public void run() {
drawChart(s.size());
}
});
}
}
};
t.start();
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
drawChart(s.size());
}
public void drawChart(int count) {
linearChart.removeAllViews();
int h = linearChart.getMeasuredHeight();
int w = linearChart.getMeasuredWidth();
for (int k = 1; k <= count; k++) {
Random r = new Random();
int alt = r.nextInt(h);
View view = new View(this);
if (alt < h/4) {
view.setBackgroundColor(Color.RED);
} else if (alt < h /2) {
view.setBackgroundColor(Color.YELLOW);
} else {
view.setBackgroundColor(Color.GREEN);
}
view.setLayoutParams(new LinearLayout.LayoutParams((w / count) -3, alt));
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
params.setMargins(3, h - alt, 0, 0); // substitute parameters for left,top, right, bottom
view.setLayoutParams(params);
linearChart.addView(view);
}
}
}
布局.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/radialback">
<LinearLayout
android:id="@+id/llGraph" >
</LinearLayout>
</LinearLayout>
于 2013-07-21T14:47:46.110 回答
0
这真的很简单:
class Bar extend View{
int max; // Size of bar.
int current; //Used
Paint paint;
[...] constructors.
public void onDraw(Canvas c){
paint.setColor(0xFF00FF00);
c.drawRect(0, 0, getWidth(), getHeight()); //Draw whole View to green.
paint.setColor(0xFFFF0000);
c.drawRect(0, 0, getWidth()*current/max, getHeight()); //Draw used View to red.
}
}
进一步的美化取决于你。
于 2012-09-15T23:54:52.897 回答
0
我写了一个小部件来绘制波形或条形聊天。使用起来非常简单直接:https ://github.com/maxyou/SimpleWaveform
于 2016-01-20T15:02:37.387 回答