MPAndroid Chartの具体的な属性方法
8576 ワード
package com.ashzheng.mpandroidchart;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import java.util.ArrayList;
public class MainActivity extends Activity {
private LineChart mLineChart;
private XAxis xAxis; //X
private YAxis yAxis; //Y
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLineChart = (LineChart) findViewById(R.id.chart);
xAxis = mLineChart.getXAxis();
yAxis = mLineChart.getAxisLeft();
LineData mLineData = getLineData();
showChart(mLineChart, mLineData);
}
private void showChart(LineChart lineChart, LineData lineData) {
//General Chart Styling , 。
// https://github.com/PhilJay/MPAndroidChart/wiki/Specific-chart-settings
lineChart.setBackgroundColor(Color.argb(200, 173, 215, 210));// Color
lineChart.setDescription("setDescription "); // , String
lineChart.setDescriptionColor(Color.rgb(227, 135, 0)); // , Color
// lineChart.setDescriptionPosition(400f,600f); // , float , ,
// lineChart.setDescriptionTypeface(); // , Typeface
lineChart.setDescriptionTextSize(16); // ,float [6,16]
lineChart.setNoDataTextDescription(" (⊙o⊙)"); // , String
lineChart.setDrawGridBackground(false);// , false
lineChart.setGridBackgroundColor(Color.rgb(256, 0, 0));// , Color
lineChart.setDrawBorders(true); //
lineChart.setBorderColor(Color.rgb(236, 228, 126)); //
lineChart.setBorderWidth(20); // ,float ,dp
// lineChart.setMaxVisibleValueCount(); , setDrawValues() true
//Interaction with the Chart
//Enabling / disabling interaction
lineChart.setTouchEnabled(true); //
lineChart.setDragEnabled(true);//
lineChart.setScaleEnabled(true);// x y , true
lineChart.setScaleXEnabled(true); // x
lineChart.setScaleYEnabled(true); // y
lineChart.setPinchZoom(true); // x y 。
lineChart.setDoubleTapToZoomEnabled(true);// 。 true
lineChart.setHighlightEnabled(false); //If set to true, highlighting/selecting values via touch is possible for all underlying DataSets.
lineChart.setHighlightPerDragEnabled(true);// ( ), true
lineChart.setAutoScaleMinMaxEnabled(false);
// Chart fling / deceleration
lineChart.setDragDecelerationEnabled(true);// , , true(false ,true )
lineChart.setDragDecelerationFrictionCoef(0.99f);// , ,[0,1) 0 。
//Highlighting programmatically
// highlightValues(Highlight[] highs)
// Highlights the values at the given indices in the given DataSets. Provide null or an empty array to undo all highlighting.
// highlightValue(int xIndex, int dataSetIndex)
// Highlights the value at the given x-index in the given DataSet. Provide -1 as the x-index or dataSetIndex to undo all highlighting.
// getHighlighted()
// Returns an Highlight[] array that contains information about all highlighted entries, their x-index and dataset-index.
// https://github.com/PhilJay/MPAndroidChart/wiki/Interaction-with-the-Chart
// ,
// The Axis ,XY
xAxis.setEnabled(true); // X , true
xAxis.setDrawAxisLine(true); // , , true
xAxis.setDrawGridLines(true); // X , true
xAxis.setDrawLabels(true); // X , true
xAxis.setTextColor(Color.rgb(145, 13, 64)); //X
xAxis.setTextSize(5); //X dp
// xAxis.setTypeface(Typeface tf); //X
xAxis.setGridColor(Color.rgb(145, 13, 64)); //X
xAxis.setGridLineWidth(1); //X float
xAxis.enableGridDashedLine(40, 3, 0); // X (float lineLength, float spaceLength, float phase) ,1. ,2. ,3.
// , :
LimitLine ll = new LimitLine(10f, " ");
ll.setLineColor(Color.RED);
ll.setLineWidth(4f);
ll.setTextColor(Color.GRAY);
ll.setTextSize(12f);
// .. and more styling options
xAxis.addLimitLine(ll);
// X
xAxis.setLabelsToSkip(1); // , int
xAxis.resetLabelsToSkip(); //
xAxis.setAvoidFirstLastClipping(true);
xAxis.setSpaceBetweenLabels(4);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM_INSIDE);// :TOP, BOTTOM, BOTH_SIDED, TOP_INSIDE or BOTTOM_INSIDE.
// Y
yAxis.setStartAtZero(false); // Y 0
yAxis.setAxisMaxValue(50); // Y
yAxis.resetAxisMaxValue(); // Y ,
yAxis.setAxisMinValue(10); // Y
yAxis.resetAxisMinValue(); // Y ,
yAxis.setInverted(false); //Y , false,
yAxis.setSpaceTop(0); //Y ,
yAxis.setSpaceBottom(0); //Y ,
yAxis.setShowOnlyMinMax(false); // true Y
yAxis.setLabelCount(10, false); // Y , ,true
yAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART); // INSIDE_CHART(Y ) OUTSIDE_CHART( ( ))
// yAxis.setValueFormatter(YAxisValueFormatterf);
// Sets a custom ValueFormatter for this axis. This interface allows to format/modify
// the original label text and instead return a customized text.
// add data
lineChart.setData(lineData); //
// get the legend (only possible after setting data)
Legend mLegend = lineChart.getLegend(); // , y value
// modify the legend ...
// mLegend.setPosition(LegendPosition.LEFT_OF_CHART);
mLegend.setForm(Legend.LegendForm.CIRCLE);//
mLegend.setFormSize(2f);//
mLegend.setTextColor(Color.WHITE);//
// mLegend.setTypeface(mTf);//
lineChart.animateX(1000); // ,x
}
private LineData getLineData() {
ArrayList<Entry> valsComp1 = new ArrayList<Entry>(); //
ArrayList<Entry> valsComp2 = new ArrayList<Entry>();
Entry c1e1 = new Entry(100.000f, 1); // ,Entry(Y ,X );
valsComp1.add(c1e1);
Entry c1e2 = new Entry(50.000f, 2);
valsComp1.add(c1e2);
Entry c2e1 = new Entry(30.000f, 1); // ,Entry(Y ,X );
valsComp2.add(c2e1);
Entry c2e2 = new Entry(80.000f, 3);
valsComp2.add(c2e2);
LineDataSet setComp1 = new LineDataSet(valsComp1, "Company"); // ,LineDataSet( , );
LineDataSet setComp2 = new LineDataSet(valsComp2, "Company");
setComp1.setAxisDependency(YAxis.AxisDependency.LEFT); //
setComp2.setAxisDependency(YAxis.AxisDependency.LEFT);
ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>(); // 。
dataSets.add(setComp1);
dataSets.add(setComp2);
ArrayList<String> xVals = new ArrayList<String>(); //X
xVals.add("1.Q"); xVals.add("2.Q"); xVals.add("3.Q"); xVals.add("4.Q");
xVals.add("1.Q"); xVals.add("2.Q"); xVals.add("3.Q"); xVals.add("4.Q");
LineData data = new LineData(xVals, dataSets); //LineData(X , );
mLineChart.setData(data); //
mLineChart.invalidate(); //
return data;
}
}