Added additional functionality to set units for chart and legend values.
This commit is contained in:
parent
2c08ca9929
commit
9c945acb2c
17 changed files with 519 additions and 108 deletions
|
@ -27,6 +27,7 @@
|
|||
<activity android:screenOrientation="landscape" android:name="MultipleChartsActivity"></activity>
|
||||
<activity android:name="BarChartActivity"></activity>
|
||||
<activity android:name="PieChartActivity"></activity>
|
||||
<activity android:name="MultiLineChartActivity"></activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
|
|
@ -3,13 +3,19 @@
|
|||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent" >
|
||||
|
||||
<LinearLayout
|
||||
<ScrollView
|
||||
android:id="@+id/scrollView1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:padding="10dp"
|
||||
android:orientation="vertical" >
|
||||
android:layout_height="match_parent"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentTop="true" >
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:orientation="vertical"
|
||||
android:padding="10dp" >
|
||||
|
||||
<Button
|
||||
android:id="@+id/button1"
|
||||
|
@ -36,15 +42,23 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="4dp"
|
||||
android:text="MultipleCharts" />
|
||||
android:text="MultilineChart" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button5"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="4dp"
|
||||
android:text="MultipleCharts" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/button6"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="4dp"
|
||||
android:text="View on GitHub" />
|
||||
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
||||
</RelativeLayout>
|
|
@ -10,7 +10,9 @@ import android.widget.SeekBar.OnSeekBarChangeListener;
|
|||
import android.widget.TextView;
|
||||
|
||||
import com.github.mikephil.charting.BarChart;
|
||||
import com.github.mikephil.charting.BarSeries;
|
||||
import com.github.mikephil.charting.ColorTemplate;
|
||||
import com.github.mikephil.charting.Series;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
@ -139,12 +141,12 @@ public class BarChartActivity extends Activity implements OnSeekBarChangeListene
|
|||
xVals.add((i)+"");
|
||||
}
|
||||
|
||||
ArrayList<Float> yVals = new ArrayList<Float>();
|
||||
ArrayList<Series> yVals = new ArrayList<Series>();
|
||||
|
||||
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
|
||||
float mult = (mSeekBarY.getProgress()+1);
|
||||
float val = (float) (Math.random() * mult * 0.1) + 3;// + (float) ((mult * 0.1) / 10);
|
||||
yVals.add(val);
|
||||
yVals.add(new BarSeries(val));
|
||||
}
|
||||
|
||||
tvX.setText(""+ (mSeekBarX.getProgress() + 1));
|
||||
|
|
|
@ -12,6 +12,8 @@ import android.widget.SeekBar.OnSeekBarChangeListener;
|
|||
import android.widget.TextView;
|
||||
|
||||
import com.github.mikephil.charting.LineChart;
|
||||
import com.github.mikephil.charting.LineSeries;
|
||||
import com.github.mikephil.charting.Series;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
@ -152,12 +154,12 @@ public class LineChartActivity extends Activity implements OnSeekBarChangeListen
|
|||
xVals.add((i)+"");
|
||||
}
|
||||
|
||||
ArrayList<Float> yVals = new ArrayList<Float>();
|
||||
ArrayList<Series> yVals = new ArrayList<Series>();
|
||||
|
||||
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
|
||||
float mult = (mSeekBarY.getProgress()+1);
|
||||
float val = (float) (Math.random() * mult * 0.1) + 3;// + (float) ((mult * 0.1) / 10);
|
||||
yVals.add(val);
|
||||
yVals.add(new LineSeries(val, 0, i));
|
||||
}
|
||||
|
||||
tvX.setText(""+ (mSeekBarX.getProgress() + 1));
|
||||
|
|
|
@ -24,11 +24,13 @@ public class MainActivity extends Activity implements OnClickListener {
|
|||
Button btn3 = (Button) findViewById(R.id.button3);
|
||||
Button btn4 = (Button) findViewById(R.id.button4);
|
||||
Button btn5 = (Button) findViewById(R.id.button5);
|
||||
Button btn6 = (Button) findViewById(R.id.button6);
|
||||
btn1.setOnClickListener(this);
|
||||
btn2.setOnClickListener(this);
|
||||
btn3.setOnClickListener(this);
|
||||
btn4.setOnClickListener(this);
|
||||
btn5.setOnClickListener(this);
|
||||
btn6.setOnClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -50,10 +52,14 @@ public class MainActivity extends Activity implements OnClickListener {
|
|||
startActivity(i);
|
||||
break;
|
||||
case R.id.button4:
|
||||
i = new Intent(this, MultipleChartsActivity.class);
|
||||
i = new Intent(this, MultiLineChartActivity.class);
|
||||
startActivity(i);
|
||||
break;
|
||||
case R.id.button5:
|
||||
i = new Intent(this, MultipleChartsActivity.class);
|
||||
startActivity(i);
|
||||
break;
|
||||
case R.id.button6:
|
||||
i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/PhilJay/MPAndroidChart"));
|
||||
startActivity(i);
|
||||
break;
|
||||
|
|
|
@ -0,0 +1,183 @@
|
|||
package com.example.mpchartexample;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.SeekBar;
|
||||
import android.widget.SeekBar.OnSeekBarChangeListener;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.github.mikephil.charting.LineChart;
|
||||
import com.github.mikephil.charting.LineSeries;
|
||||
import com.github.mikephil.charting.Series;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MultiLineChartActivity extends Activity implements OnSeekBarChangeListener {
|
||||
|
||||
private LineChart mChart;
|
||||
private SeekBar mSeekBarX, mSeekBarY;
|
||||
private TextView tvX, tvY;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
||||
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
setContentView(R.layout.activity_linechart);
|
||||
|
||||
tvX = (TextView) findViewById(R.id.tvXMax);
|
||||
tvY = (TextView) findViewById(R.id.tvYMax);
|
||||
|
||||
mSeekBarX = (SeekBar) findViewById(R.id.seekBar1);
|
||||
mSeekBarX.setOnSeekBarChangeListener(this);
|
||||
|
||||
mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);
|
||||
mSeekBarY.setOnSeekBarChangeListener(this);
|
||||
|
||||
mChart = (LineChart) findViewById(R.id.chart1);
|
||||
// mChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.LIBERTY_COLORS)));
|
||||
|
||||
// mChart.setDrawFilled(true);
|
||||
// mChart.setRoundedYLegend(false);
|
||||
// mChart.setStartAtZero(true);
|
||||
mChart.setDrawYValues(false);
|
||||
mChart.setLineWidth(4f);
|
||||
mChart.setCircleSize(4f);
|
||||
// mChart.setSpacePercent(20, 10);
|
||||
mChart.setYLegendCount(6);
|
||||
mChart.setTouchEnabled(true);
|
||||
mChart.setHighlightEnabled(true);
|
||||
// mChart.highlightValues(new int[] {2, 6});
|
||||
mChart.setDragEnabled(true);
|
||||
mChart.setTouchEnabled(true);
|
||||
|
||||
TextView textView = new TextView(this);
|
||||
textView.setVisibility(View.VISIBLE);
|
||||
textView.setBackgroundColor(Color.WHITE);
|
||||
textView.setPadding(15, 15, 15, 15);
|
||||
textView.setText("Marker View");
|
||||
|
||||
mChart.setDrawMarkerView(true);
|
||||
mChart.setMarkerView(textView);
|
||||
|
||||
mSeekBarX.setProgress(45);
|
||||
mSeekBarY.setProgress(100);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.line, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
|
||||
switch (item.getItemId()) {
|
||||
case R.id.actionToggleRound: {
|
||||
if (mChart.isYLegendRounded())
|
||||
mChart.setRoundedYLegend(false);
|
||||
else
|
||||
mChart.setRoundedYLegend(true);
|
||||
mChart.invalidate();
|
||||
break;
|
||||
}
|
||||
case R.id.actionToggleValues: {
|
||||
if (mChart.isDrawYValuesEnabled())
|
||||
mChart.setDrawYValues(false);
|
||||
else
|
||||
mChart.setDrawYValues(true);
|
||||
mChart.invalidate();
|
||||
break;
|
||||
}
|
||||
case R.id.actionToggleHighlight: {
|
||||
if (mChart.isHighlightEnabled())
|
||||
mChart.setHighlightEnabled(false);
|
||||
else
|
||||
mChart.setHighlightEnabled(true);
|
||||
mChart.invalidate();
|
||||
break;
|
||||
}
|
||||
case R.id.actionToggleFilled: {
|
||||
if (mChart.isDrawFilledEnabled())
|
||||
mChart.setDrawFilled(false);
|
||||
else
|
||||
mChart.setDrawFilled(true);
|
||||
mChart.invalidate();
|
||||
break;
|
||||
}
|
||||
case R.id.actionToggleCircles: {
|
||||
if (mChart.isDrawCirclesEnabled())
|
||||
mChart.setDrawCircles(false);
|
||||
else
|
||||
mChart.setDrawCircles(true);
|
||||
mChart.invalidate();
|
||||
break;
|
||||
}
|
||||
case R.id.actionToggleStartzero: {
|
||||
if (mChart.isStartAtZeroEnabled())
|
||||
mChart.setStartAtZero(false);
|
||||
else
|
||||
mChart.setStartAtZero(true);
|
||||
|
||||
mChart.invalidate();
|
||||
break;
|
||||
}
|
||||
case R.id.actionToggleAdjustXLegend: {
|
||||
if (mChart.isAdjustXLegendEnabled())
|
||||
mChart.setAdjustXLegend(false);
|
||||
else
|
||||
mChart.setAdjustXLegend(true);
|
||||
|
||||
mChart.invalidate();
|
||||
break;
|
||||
}
|
||||
case R.id.actionSave: {
|
||||
// mChart.saveToGallery("title"+System.currentTimeMillis());
|
||||
mChart.saveToPath("title"+System.currentTimeMillis(), "");
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
|
||||
ArrayList<String> xVals = new ArrayList<String>();
|
||||
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
|
||||
xVals.add((i)+"");
|
||||
}
|
||||
|
||||
ArrayList<Series> yVals = new ArrayList<Series>();
|
||||
|
||||
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
|
||||
float mult = (mSeekBarY.getProgress()+1);
|
||||
float val = (float) (Math.random() * mult * 0.1) + 3;// + (float) ((mult * 0.1) / 10);
|
||||
yVals.add(new LineSeries(val, 0, i));
|
||||
}
|
||||
|
||||
tvX.setText(""+ (mSeekBarX.getProgress() + 1));
|
||||
tvY.setText(""+ (mSeekBarY.getProgress() / 10));
|
||||
|
||||
mChart.setData(xVals, yVals);
|
||||
mChart.invalidate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStartTrackingTouch(SeekBar seekBar) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStopTrackingTouch(SeekBar seekBar) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
|
@ -25,55 +25,55 @@ public class MultipleChartsActivity extends Activity {
|
|||
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
||||
setContentView(R.layout.activity_multiplecharts);
|
||||
|
||||
mLineChart = (LineChart) findViewById(R.id.barChart1);
|
||||
mBarChart = (BarChart) findViewById(R.id.barChart2);
|
||||
mPieChart = (PieChart) findViewById(R.id.pieChart1);
|
||||
mBarChart3D = (BarChart) findViewById(R.id.barChart4);
|
||||
|
||||
ArrayList<String> xvalsSmall = new ArrayList<String>();
|
||||
ArrayList<String> xvalsLarge = new ArrayList<String>();
|
||||
|
||||
ArrayList<Float> small = new ArrayList<Float>();
|
||||
small.add(3f);
|
||||
small.add(7f);
|
||||
small.add(17f);
|
||||
small.add(4f);
|
||||
small.add(6f);
|
||||
|
||||
for(int i = 0; i < small.size(); i++) {
|
||||
xvalsSmall.add("Val"+i);
|
||||
}
|
||||
|
||||
ArrayList<Float> large = new ArrayList<Float>();
|
||||
|
||||
for(int i = 0; i < 1000; i++) {
|
||||
large.add((float) (Math.random() * 50));
|
||||
xvalsLarge.add("Val"+i);
|
||||
}
|
||||
|
||||
mLineChart.setData(xvalsLarge, large);
|
||||
mLineChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.FRESH_COLORS)));
|
||||
mLineChart.setDrawFilled(false);
|
||||
mLineChart.setStartAtZero(false);
|
||||
// mLineChart.setRoundedYLegend(true);
|
||||
mLineChart.setYRange(-10f, 60f);
|
||||
|
||||
mBarChart.setData(xvalsLarge, large);
|
||||
mBarChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.FRESH_COLORS)));
|
||||
mBarChart.set3DEnabled(false);
|
||||
mBarChart.setMaxVisibleValueCount(10);
|
||||
mBarChart.setRoundedYLegend(true);
|
||||
mBarChart.setDescription("");
|
||||
|
||||
mPieChart.setData(xvalsSmall, small);
|
||||
mPieChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.FRESH_COLORS)));
|
||||
// mChart3.highlightValues(new int[] {0, 1, 2, 3, 4} );
|
||||
mPieChart.setDrawYValues(true);
|
||||
mPieChart.setDrawXValues(false);
|
||||
|
||||
mBarChart3D.setData(xvalsLarge, large);
|
||||
mBarChart3D.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.LIBERTY_COLORS)));
|
||||
mBarChart3D.setRoundedYLegend(true);
|
||||
mBarChart3D.setDescription("Description.");
|
||||
// mLineChart = (LineChart) findViewById(R.id.barChart1);
|
||||
// mBarChart = (BarChart) findViewById(R.id.barChart2);
|
||||
// mPieChart = (PieChart) findViewById(R.id.pieChart1);
|
||||
// mBarChart3D = (BarChart) findViewById(R.id.barChart4);
|
||||
//
|
||||
// ArrayList<String> xvalsSmall = new ArrayList<String>();
|
||||
// ArrayList<String> xvalsLarge = new ArrayList<String>();
|
||||
//
|
||||
// ArrayList<Float> small = new ArrayList<Float>();
|
||||
// small.add(3f);
|
||||
// small.add(7f);
|
||||
// small.add(17f);
|
||||
// small.add(4f);
|
||||
// small.add(6f);
|
||||
//
|
||||
// for(int i = 0; i < small.size(); i++) {
|
||||
// xvalsSmall.add("Val"+i);
|
||||
// }
|
||||
//
|
||||
// ArrayList<Float> large = new ArrayList<Float>();
|
||||
//
|
||||
// for(int i = 0; i < 1000; i++) {
|
||||
// large.add((float) (Math.random() * 50));
|
||||
// xvalsLarge.add("Val"+i);
|
||||
// }
|
||||
//
|
||||
// mLineChart.setData(xvalsLarge, large);
|
||||
// mLineChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.FRESH_COLORS)));
|
||||
// mLineChart.setDrawFilled(false);
|
||||
// mLineChart.setStartAtZero(false);
|
||||
//// mLineChart.setRoundedYLegend(true);
|
||||
// mLineChart.setYRange(-10f, 60f);
|
||||
//
|
||||
// mBarChart.setData(xvalsLarge, large);
|
||||
// mBarChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.FRESH_COLORS)));
|
||||
// mBarChart.set3DEnabled(false);
|
||||
// mBarChart.setMaxVisibleValueCount(10);
|
||||
// mBarChart.setRoundedYLegend(true);
|
||||
// mBarChart.setDescription("");
|
||||
//
|
||||
// mPieChart.setData(xvalsSmall, small);
|
||||
// mPieChart.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.FRESH_COLORS)));
|
||||
//// mChart3.highlightValues(new int[] {0, 1, 2, 3, 4} );
|
||||
// mPieChart.setDrawYValues(true);
|
||||
// mPieChart.setDrawXValues(false);
|
||||
//
|
||||
// mBarChart3D.setData(xvalsLarge, large);
|
||||
// mBarChart3D.setColorTemplate(new ColorTemplate(ColorTemplate.getColors(this, ColorTemplate.LIBERTY_COLORS)));
|
||||
// mBarChart3D.setRoundedYLegend(true);
|
||||
// mBarChart3D.setDescription("Description.");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@ import android.widget.TextView;
|
|||
import com.github.mikephil.charting.ColorTemplate;
|
||||
import com.github.mikephil.charting.OnChartValueSelectedListener;
|
||||
import com.github.mikephil.charting.PieChart;
|
||||
import com.github.mikephil.charting.PieSeries;
|
||||
import com.github.mikephil.charting.Series;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
@ -122,12 +124,12 @@ public class PieChartActivity extends Activity implements OnSeekBarChangeListene
|
|||
@Override
|
||||
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
||||
|
||||
ArrayList<Float> yVals = new ArrayList<Float>();
|
||||
ArrayList<Series> yVals = new ArrayList<Series>();
|
||||
|
||||
for (int i = 0; i < mSeekBarX.getProgress(); i++) {
|
||||
float mult = (mSeekBarY.getProgress());
|
||||
float val = (float) (Math.random() * mult) + mult / 5;// + (float) ((mult * 0.1) / 10);
|
||||
yVals.add(val);
|
||||
yVals.add(new PieSeries(val));
|
||||
}
|
||||
|
||||
tvX.setText(""+ (mSeekBarX.getProgress()));
|
||||
|
|
|
@ -135,7 +135,7 @@ public class BarChart extends BarLineChartBase {
|
|||
|
||||
mHighlightPaint.setAlpha(120);
|
||||
|
||||
float y = mYVals.get(index);
|
||||
float y = mYVals.get(index).getVal();
|
||||
float left = index + mBarSpace / 2f;
|
||||
float right = index + 1f - mBarSpace / 2f;
|
||||
float top = y >= 0 ? y : 0;
|
||||
|
@ -210,7 +210,7 @@ public class BarChart extends BarLineChartBase {
|
|||
|
||||
for (int i = 0; i < mYVals.size(); i++) {
|
||||
|
||||
float y = mYVals.get(i);
|
||||
float y = mYVals.get(i).getVal();
|
||||
float left = i + mBarSpace / 2f;
|
||||
float right = i + 1f - mBarSpace / 2f;
|
||||
float top = y >= 0 ? y : 0;
|
||||
|
@ -242,7 +242,7 @@ public class BarChart extends BarLineChartBase {
|
|||
|
||||
Paint paint = mDrawPaints[i % mDrawPaints.length];
|
||||
|
||||
float y = mYVals.get(i);
|
||||
float y = mYVals.get(i).getVal();
|
||||
float left = i + mBarSpace / 2f;
|
||||
float right = i + 1f - mBarSpace / 2f;
|
||||
float top = y >= 0 ? y : 0;
|
||||
|
@ -280,15 +280,25 @@ public class BarChart extends BarLineChartBase {
|
|||
for (int i = 0; i < valuePoints.length; i += 2) {
|
||||
valuePoints[i] = i / 2 + 0.5f; // add 0.5f too keep the values
|
||||
// centered on top of the bars
|
||||
valuePoints[i + 1] = mYVals.get(i / 2);
|
||||
valuePoints[i + 1] = mYVals.get(i / 2).getVal();
|
||||
}
|
||||
|
||||
transformPointArray(valuePoints);
|
||||
|
||||
for (int i = 0; i < valuePoints.length; i += 2) {
|
||||
mDrawCanvas.drawText(
|
||||
mFormatValue.format(mYVals.get(i / 2)),
|
||||
valuePoints[i], valuePoints[i + 1] - 12, mValuePaint);
|
||||
|
||||
if(mDrawUnitInChart) {
|
||||
|
||||
mDrawCanvas.drawText(
|
||||
mFormatValue.format(mYVals.get(i / 2).getVal()) + mUnit,
|
||||
valuePoints[i], valuePoints[i + 1] - 12, mValuePaint);
|
||||
} else {
|
||||
|
||||
|
||||
mDrawCanvas.drawText(
|
||||
mFormatValue.format(mYVals.get(i / 2).getVal()),
|
||||
valuePoints[i], valuePoints[i + 1] - 12, mValuePaint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,12 @@ import java.util.ArrayList;
|
|||
*/
|
||||
public abstract class BarLineChartBase extends Chart {
|
||||
|
||||
/**
|
||||
* string that is drawn next to the values in the chart, indicating their
|
||||
* unit
|
||||
*/
|
||||
protected String mUnit = "";
|
||||
|
||||
/** the maximum number of entried to which values will be drawn */
|
||||
protected int mMaxVisibleCount = 100;
|
||||
|
||||
|
@ -55,6 +61,17 @@ public abstract class BarLineChartBase extends Chart {
|
|||
/** array that contains all values of the y-axis legend */
|
||||
protected Float[] mYLegend = new Float[mYLegendCount];
|
||||
|
||||
/** if true, units are drawn next to the values in the chart */
|
||||
protected boolean mDrawUnitInChart = false;
|
||||
|
||||
/**
|
||||
* if true, units are drawn next to the values in the legend
|
||||
*/
|
||||
protected boolean mDrawUnitInLegend = true;
|
||||
|
||||
/** if true, x-legend text is centered */
|
||||
protected boolean mCenterXLegendText = false;
|
||||
|
||||
/**
|
||||
* if set to true, the x-legend entries will adjust themselves when scaling
|
||||
* the graph
|
||||
|
@ -416,6 +433,10 @@ public abstract class BarLineChartBase extends Chart {
|
|||
|
||||
position[0] = i;
|
||||
|
||||
// center the text
|
||||
if (mCenterXLegendText)
|
||||
position[0] += 0.5f;
|
||||
|
||||
transformPointArray(position);
|
||||
|
||||
if (position[0] >= mOffsetLeft && position[0] <= getWidth() - mOffsetRight + 10) {
|
||||
|
@ -461,13 +482,36 @@ public abstract class BarLineChartBase extends Chart {
|
|||
if (i >= positions.length - 2)
|
||||
yPos = positions[i + 1] + 14;
|
||||
|
||||
mDrawCanvas.drawText(mFormatYLegend.format(mYLegend[i / 2]),
|
||||
positions[i] - 10,
|
||||
yPos, mYLegendPaint);
|
||||
if (!mDrawTopYLegendEntry && i >= positions.length - 2)
|
||||
return;
|
||||
|
||||
if(mDrawUnitInLegend) {
|
||||
mDrawCanvas.drawText(mFormatYLegend.format(mYLegend[i / 2]) + mUnit,
|
||||
positions[i] - 10,
|
||||
yPos, mYLegendPaint);
|
||||
} else {
|
||||
mDrawCanvas.drawText(mFormatYLegend.format(mYLegend[i / 2]),
|
||||
positions[i] - 10,
|
||||
yPos, mYLegendPaint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** indicates if the top y-legend entry is drawn or not */
|
||||
private boolean mDrawTopYLegendEntry = true;
|
||||
|
||||
/**
|
||||
* set this to true to enable drawing the top y-legend entry. Disabling this
|
||||
* can be helpful when the y-legend and x-legend interfear with each other.
|
||||
* default: true
|
||||
*
|
||||
* @param enabled
|
||||
*/
|
||||
public void setDrawTopYLegendEntry(boolean enabled) {
|
||||
mDrawTopYLegendEntry = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* draws a line that surrounds the chart
|
||||
*/
|
||||
|
@ -697,6 +741,15 @@ public abstract class BarLineChartBase extends Chart {
|
|||
prepare();
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the unit that is drawn next to the values in the chart, e.g. %
|
||||
*
|
||||
* @param unit
|
||||
*/
|
||||
public void setUnit(String unit) {
|
||||
mUnit = unit;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns true if the chart is set to start at zero, false otherwise
|
||||
*
|
||||
|
@ -706,6 +759,25 @@ public abstract class BarLineChartBase extends Chart {
|
|||
return mStartAtZero;
|
||||
}
|
||||
|
||||
/**
|
||||
* if set to true, units are drawn next to values in the chart, default:
|
||||
* false
|
||||
*
|
||||
* @param enabled
|
||||
*/
|
||||
public void setDrawUnitsInChart(boolean enabled) {
|
||||
mDrawUnitInChart = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* if set to true, units are drawn next to y-legend values, default: true
|
||||
*
|
||||
* @param enabled
|
||||
*/
|
||||
public void setDrawUnitsInLegend(boolean enabled) {
|
||||
mDrawUnitInLegend = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns true if the y-legend is set to be rounded, false if not
|
||||
*
|
||||
|
@ -715,6 +787,24 @@ public abstract class BarLineChartBase extends Chart {
|
|||
return mRoundedYLegend;
|
||||
}
|
||||
|
||||
/**
|
||||
* set this to true to center the x-legend text, default: false
|
||||
*
|
||||
* @param enabled
|
||||
*/
|
||||
public void setCenterXLegend(boolean enabled) {
|
||||
mCenterXLegendText = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns true if the x-legend text is centered
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public boolean isXLegendCentered() {
|
||||
return mCenterXLegendText;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the width of the grid lines (min 0.1f, max = 3f)
|
||||
*
|
||||
|
@ -796,7 +886,7 @@ public abstract class BarLineChartBase extends Chart {
|
|||
* @param y
|
||||
* @return
|
||||
*/
|
||||
public float getValueByTouchPoint(float x, float y) {
|
||||
public Series getSeriesByTouchPoint(float x, float y) {
|
||||
return mYVals.get(getIndexByTouchPoint(x, y));
|
||||
}
|
||||
|
||||
|
|
10
MPChartLib/src/com/github/mikephil/charting/BarSeries.java
Normal file
10
MPChartLib/src/com/github/mikephil/charting/BarSeries.java
Normal file
|
@ -0,0 +1,10 @@
|
|||
package com.github.mikephil.charting;
|
||||
|
||||
public class BarSeries extends Series {
|
||||
|
||||
public BarSeries(float val) {
|
||||
super(val);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -69,7 +69,7 @@ public abstract class Chart extends View {
|
|||
protected ArrayList<String> mXVals;
|
||||
|
||||
/** list that holds all values of the y-axis */
|
||||
protected ArrayList<Float> mYVals;
|
||||
protected ArrayList<Series> mYVals;
|
||||
|
||||
/** final bitmap that contains all information and is drawn to the screen */
|
||||
protected Bitmap mDrawBitmap;
|
||||
|
@ -200,7 +200,7 @@ public abstract class Chart extends View {
|
|||
mOffsetTop = (int) Utils.convertDpToPixel(mOffsetTop);
|
||||
|
||||
mXVals = new ArrayList<String>();
|
||||
mYVals = new ArrayList<Float>();
|
||||
mYVals = new ArrayList<Series>();
|
||||
|
||||
mDrawPaint = new Paint();
|
||||
|
||||
|
@ -227,7 +227,7 @@ public abstract class Chart extends View {
|
|||
* @param xVals
|
||||
* @param yVals
|
||||
*/
|
||||
public void setData(ArrayList<String> xVals, ArrayList<Float> yVals) {
|
||||
public void setData(ArrayList<String> xVals, ArrayList<Series> yVals) {
|
||||
|
||||
if (xVals == null || xVals.size() <= 1 || yVals == null || yVals.size() <= 1) {
|
||||
Log.e(LOG_TAG,
|
||||
|
@ -265,15 +265,15 @@ public abstract class Chart extends View {
|
|||
*/
|
||||
protected void calcMinMax() {
|
||||
|
||||
mYMin = mYVals.get(0);
|
||||
mYMax = mYVals.get(0);
|
||||
mYMin = mYVals.get(0).getVal();
|
||||
mYMax = mYVals.get(0).getVal();
|
||||
|
||||
for (int i = 0; i < mYVals.size(); i++) {
|
||||
if (mYVals.get(i) < mYMin)
|
||||
mYMin = mYVals.get(i);
|
||||
if (mYVals.get(i).getVal() < mYMin)
|
||||
mYMin = mYVals.get(i).getVal();
|
||||
|
||||
if (mYVals.get(i) > mYMax)
|
||||
mYMax = mYVals.get(i);
|
||||
if (mYVals.get(i).getVal() > mYMax)
|
||||
mYMax = mYVals.get(i).getVal();
|
||||
}
|
||||
|
||||
mYChartMin = mYMin;
|
||||
|
@ -295,7 +295,7 @@ public abstract class Chart extends View {
|
|||
mYValueSum = 0;
|
||||
|
||||
for (int i = 0; i < mYVals.size(); i++) {
|
||||
mYValueSum += Math.abs(mYVals.get(i));
|
||||
mYValueSum += Math.abs(mYVals.get(i).getVal());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -648,7 +648,7 @@ public abstract class Chart extends View {
|
|||
float[] values = new float[indices.length];
|
||||
|
||||
for (int i = 0; i < values.length; i++)
|
||||
values[i] = getYValue(indices[i]);
|
||||
values[i] = getSeries(indices[i]).getVal();
|
||||
|
||||
// notify the listener
|
||||
mSelectionListener.onValuesSelected(values, indices);
|
||||
|
@ -684,7 +684,7 @@ public abstract class Chart extends View {
|
|||
return;
|
||||
|
||||
int index = mIndicesToHightlight[0];
|
||||
float value = mYVals.get(index);
|
||||
float value = mYVals.get(index).getVal();
|
||||
|
||||
// position of the marker depends on selected value index and value
|
||||
float[] pts = new float[] {
|
||||
|
@ -1063,6 +1063,15 @@ public abstract class Chart extends View {
|
|||
mDrawMarkerView = enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the draw color for the value paint object
|
||||
*
|
||||
* @param color
|
||||
*/
|
||||
public void setValuePaintColor(int color) {
|
||||
mValuePaint.setColor(color);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns true if y-value drawing is enabled, false if not
|
||||
*
|
||||
|
@ -1078,7 +1087,7 @@ public abstract class Chart extends View {
|
|||
* @param index
|
||||
* @return
|
||||
*/
|
||||
public float getYValue(int index) {
|
||||
public Series getSeries(int index) {
|
||||
return mYVals.get(index);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,9 +6,10 @@ import android.graphics.Color;
|
|||
import android.graphics.Paint;
|
||||
import android.graphics.Paint.Style;
|
||||
import android.graphics.Path;
|
||||
import android.graphics.RectF;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class LineChart extends BarLineChartBase {
|
||||
|
||||
/** the radius of the circle-shaped value indicators */
|
||||
|
@ -106,11 +107,13 @@ public class LineChart extends BarLineChartBase {
|
|||
|
||||
for (int i = 0; i < mIndicesToHightlight.length; i++) {
|
||||
|
||||
// RectF highlight = new RectF(mIndicesToHightlight[i] - mHighlightWidth / 2,
|
||||
// mYChartMax, mIndicesToHightlight[i] + mHighlightWidth / 2, mYChartMin);
|
||||
// transformRect(highlight);
|
||||
// mDrawCanvas.drawRect(highlight, mHighlightPaint);
|
||||
|
||||
// RectF highlight = new RectF(mIndicesToHightlight[i] -
|
||||
// mHighlightWidth / 2,
|
||||
// mYChartMax, mIndicesToHightlight[i] + mHighlightWidth / 2,
|
||||
// mYChartMin);
|
||||
// transformRect(highlight);
|
||||
// mDrawCanvas.drawRect(highlight, mHighlightPaint);
|
||||
|
||||
int index = mIndicesToHightlight[i];
|
||||
|
||||
// check outofbounds
|
||||
|
@ -118,9 +121,9 @@ public class LineChart extends BarLineChartBase {
|
|||
|
||||
float[] pts = new float[] {
|
||||
index, mYChartMax, index, mYChartMin,
|
||||
0, mYVals.get(index), mDeltaX, mYVals.get(index)
|
||||
0, mYVals.get(index).getVal(), mDeltaX, mYVals.get(index).getVal()
|
||||
};
|
||||
|
||||
|
||||
transformPointArray(pts);
|
||||
// draw the highlight lines
|
||||
mDrawCanvas.drawLines(pts, mHighlightPaint);
|
||||
|
@ -136,11 +139,11 @@ public class LineChart extends BarLineChartBase {
|
|||
protected void drawData() {
|
||||
|
||||
Path p = new Path();
|
||||
p.moveTo(0, mYVals.get(0));
|
||||
p.moveTo(0, mYVals.get(0).getVal());
|
||||
|
||||
for (int x = 1; x < mYVals.size(); x++) {
|
||||
|
||||
p.lineTo(x, mYVals.get(x));
|
||||
p.lineTo(x, mYVals.get(x).getVal());
|
||||
}
|
||||
|
||||
transformPath(p);
|
||||
|
@ -151,12 +154,12 @@ public class LineChart extends BarLineChartBase {
|
|||
if (mDrawFilled) {
|
||||
|
||||
Path filled = new Path();
|
||||
filled.moveTo(0, mYVals.get(0));
|
||||
filled.moveTo(0, mYVals.get(0).getVal());
|
||||
|
||||
// create a new path
|
||||
for (int x = 1; x < mYVals.size(); x++) {
|
||||
|
||||
filled.lineTo(x, mYVals.get(x));
|
||||
filled.lineTo(x, mYVals.get(x).getVal());
|
||||
}
|
||||
|
||||
// close up
|
||||
|
@ -180,15 +183,24 @@ public class LineChart extends BarLineChartBase {
|
|||
|
||||
for (int i = 0; i < valuePoints.length; i += 2) {
|
||||
valuePoints[i] = i / 2;
|
||||
valuePoints[i + 1] = mYVals.get(i / 2);
|
||||
valuePoints[i + 1] = mYVals.get(i / 2).getVal();
|
||||
}
|
||||
|
||||
transformPointArray(valuePoints);
|
||||
|
||||
for (int i = 0; i < valuePoints.length; i += 2) {
|
||||
mDrawCanvas.drawText(
|
||||
mFormatValue.format(mYVals.get(i / 2)),
|
||||
valuePoints[i], valuePoints[i + 1] - 12, mValuePaint);
|
||||
|
||||
if (mDrawUnitInChart) {
|
||||
|
||||
mDrawCanvas.drawText(
|
||||
mFormatValue.format(mYVals.get(i / 2).getVal()) + mUnit,
|
||||
valuePoints[i], valuePoints[i + 1] - 12, mValuePaint);
|
||||
} else {
|
||||
|
||||
mDrawCanvas.drawText(
|
||||
mFormatValue.format(mYVals.get(i / 2).getVal()),
|
||||
valuePoints[i], valuePoints[i + 1] - 12, mValuePaint);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -206,7 +218,7 @@ public class LineChart extends BarLineChartBase {
|
|||
|
||||
for (int i = 0; i < positions.length; i += 2) {
|
||||
positions[i] = i / 2;
|
||||
positions[i + 1] = mYVals.get(i / 2);
|
||||
positions[i + 1] = mYVals.get(i / 2).getVal();
|
||||
}
|
||||
|
||||
transformPointArray(positions);
|
||||
|
@ -303,6 +315,32 @@ public class LineChart extends BarLineChartBase {
|
|||
return mLineWidth;
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the color for the line paint
|
||||
*
|
||||
* @param color
|
||||
*/
|
||||
public void setLineColor(int color) {
|
||||
mLinePaint.setColor(color);
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the color for the outer circle paint
|
||||
* @param color
|
||||
*/
|
||||
public void setCircleColor(int color) {
|
||||
mCirclePaintOuter.setColor(color);
|
||||
}
|
||||
|
||||
/**
|
||||
* sets the color for the fill-paint
|
||||
*
|
||||
* @param color
|
||||
*/
|
||||
public void setFillColor(int color) {
|
||||
mFilledPaint.setColor(color);
|
||||
}
|
||||
|
||||
/**
|
||||
* set the width of the highlightning lines, default 3f
|
||||
*
|
||||
|
|
21
MPChartLib/src/com/github/mikephil/charting/LineSeries.java
Normal file
21
MPChartLib/src/com/github/mikephil/charting/LineSeries.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
package com.github.mikephil.charting;
|
||||
|
||||
public class LineSeries extends Series {
|
||||
|
||||
private int mLineIndex = 0;
|
||||
private int mXIndex = 0;
|
||||
|
||||
public LineSeries(float val, int lineIndex, int xIndex) {
|
||||
super(val);
|
||||
mLineIndex = lineIndex;
|
||||
mXIndex = xIndex;
|
||||
}
|
||||
|
||||
public int getLineIndex() {
|
||||
return mLineIndex;
|
||||
}
|
||||
|
||||
public int getXIndex() {
|
||||
return mXIndex;
|
||||
}
|
||||
}
|
|
@ -277,7 +277,7 @@ public class PieChart extends Chart {
|
|||
mAbsoluteAngles = new float[mYVals.size()];
|
||||
|
||||
for (int i = 0; i < mYVals.size(); i++) {
|
||||
mDrawAngles[i] = calcAngle(mYVals.get(i));
|
||||
mDrawAngles[i] = calcAngle(mYVals.get(i).getVal());
|
||||
|
||||
if (i > 0)
|
||||
mAbsoluteAngles[i] = mAbsoluteAngles[i - 1] + mDrawAngles[i];
|
||||
|
@ -418,9 +418,9 @@ public class PieChart extends Chart {
|
|||
String val = "";
|
||||
|
||||
if (mUsePercentValues)
|
||||
val = mFormatValue.format(getPercentOfTotal(mYVals.get(i))) + " %";
|
||||
val = mFormatValue.format(getPercentOfTotal(mYVals.get(i).getVal())) + " %";
|
||||
else
|
||||
val = mFormatValue.format(mYVals.get(i));
|
||||
val = mFormatValue.format(mYVals.get(i).getVal());
|
||||
|
||||
// draw everything, depending on settings
|
||||
if (mDrawXVals && mDrawYValues) {
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package com.github.mikephil.charting;
|
||||
|
||||
public class PieSeries extends Series {
|
||||
|
||||
public PieSeries(float val) {
|
||||
super(val);
|
||||
|
||||
}
|
||||
}
|
14
MPChartLib/src/com/github/mikephil/charting/Series.java
Normal file
14
MPChartLib/src/com/github/mikephil/charting/Series.java
Normal file
|
@ -0,0 +1,14 @@
|
|||
package com.github.mikephil.charting;
|
||||
|
||||
public abstract class Series {
|
||||
|
||||
private float mVal = 0f;
|
||||
|
||||
public Series(float val) {
|
||||
mVal = val;
|
||||
}
|
||||
|
||||
public float getVal() {
|
||||
return mVal;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue