Skip to content

Commit

Permalink
Added some conveniance methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed Sep 23, 2014
1 parent 8c02244 commit 031848b
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@

package com.xxmassdeveloper.mpchartexample;

import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
Expand All @@ -15,8 +21,10 @@
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.filter.Approximator;
import com.github.mikephil.charting.data.filter.Approximator.ApproximatorType;
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.Legend;
import com.github.mikephil.charting.utils.Legend.LegendPosition;
import com.github.mikephil.charting.utils.XLabels;
Expand All @@ -27,7 +35,7 @@

import java.util.ArrayList;

public class BarChartActivity extends DemoBase implements OnSeekBarChangeListener {
public class BarChartActivity extends DemoBase implements OnSeekBarChangeListener, OnChartValueSelectedListener {

private BarChart mChart;
private SeekBar mSeekBarX, mSeekBarY;
Expand All @@ -47,6 +55,7 @@ protected void onCreate(Bundle savedInstanceState) {
mSeekBarY = (SeekBar) findViewById(R.id.seekBar2);

mChart = (BarChart) findViewById(R.id.chart1);
mChart.setOnChartValueSelectedListener(this);

// enable the drawing of values
mChart.setDrawYValues(true);
Expand Down Expand Up @@ -274,4 +283,17 @@ private void setData(int count, float range) {

mChart.setData(data);
}

@SuppressLint("NewApi")
@Override
public void onValueSelected(Entry e, int dataSetIndex) {

RectF bounds = mChart.getBarBounds((BarEntry) e);
PointF position = mChart.getPosition(e);

Log.i("bounds", bounds.toString());
Log.i("position", position.toString());
}

public void onNothingSelected() {};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

package com.xxmassdeveloper.mpchartexample;

import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
Expand All @@ -16,6 +17,8 @@
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
import com.github.mikephil.charting.interfaces.OnDrawListener;
import com.github.mikephil.charting.utils.XLabels;
import com.github.mikephil.charting.utils.YLabels;
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
import com.xxmassdeveloper.mpchartexample.notimportant.DemoBase;

import java.util.ArrayList;
Expand Down Expand Up @@ -58,12 +61,24 @@ protected void onCreate(Bundle savedInstanceState) {

// if disabled, drawn datasets with the finger will not be automatically
// finished
mChart.setAutoFinish(false);
mChart.setAutoFinish(true);
mChart.setDrawGridBackground(false);

mChart.setDrawLegend(false);

// add dummy-data to the chart
initWithDummyData();

Typeface tf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");

XLabels xl = mChart.getXLabels();
xl.setTypeface(tf);
xl.setAvoidFirstLastClipping(true);

YLabels yl = mChart.getYLabels();
yl.setTypeface(tf);

mChart.setValueTypeface(tf);

mChart.setYRange(-40f, 40f, true);
// call this to reset the changed y-range
Expand All @@ -73,13 +88,15 @@ protected void onCreate(Bundle savedInstanceState) {
private void initWithDummyData() {
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < 24; i++) {
xVals.add((i) + "h");
xVals.add((i) + ":00");
}

ArrayList<Entry> yVals = new ArrayList<Entry>();

// create a dataset and give it a type (0)
LineDataSet set1 = new LineDataSet(yVals, "DataSet");
set1.setLineWidth(3f);
set1.setCircleSize(5f);

ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
dataSets.add(set1); // add the datasets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package com.xxmassdeveloper.mpchartexample.notimportant;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
Expand All @@ -25,6 +24,7 @@
import com.xxmassdeveloper.mpchartexample.BarChartActivityMultiDataset;
import com.xxmassdeveloper.mpchartexample.CandleStickChartActivity;
import com.xxmassdeveloper.mpchartexample.CubicLineChartActivity;
import com.xxmassdeveloper.mpchartexample.DrawChartActivity;
import com.xxmassdeveloper.mpchartexample.DynamicalAddingActivity;
import com.xxmassdeveloper.mpchartexample.InvertedLineChartActivity;
import com.xxmassdeveloper.mpchartexample.LineChartActivity;
Expand Down Expand Up @@ -146,14 +146,14 @@ public void onItemClick(AdapterView<?> av, View v, int pos, long arg3) {
startActivity(i);
break;
case 8:
// i = new Intent(this, DrawChartActivity.class);
// startActivity(i);

AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("Feature not available");
b.setMessage("Due to recent changes to the data model of the library, this feature is temporarily not available.");
b.setPositiveButton("OK", null);
b.create().show();
i = new Intent(this, DrawChartActivity.class);
startActivity(i);

// AlertDialog.Builder b = new AlertDialog.Builder(this);
// b.setTitle("Feature not available");
// b.setMessage("Due to recent changes to the data model of the library, this feature is temporarily not available.");
// b.setPositiveButton("OK", null);
// b.create().show();
break;
case 9:
i = new Intent(this, SimpleChartDemo.class);
Expand Down
55 changes: 45 additions & 10 deletions MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ protected void calcMinMax(boolean fixedValues) {

@Override
protected void drawHighlights() {

int setCount = mOriginalData.getDataSetCount();

for (int i = 0; i < mIndicesToHightlight.length; i++) {
Expand All @@ -202,11 +202,13 @@ protected void drawHighlights() {
&& index < (mDeltaX * mPhaseX) / mOriginalData.getDataSetCount()) {

Entry e = getEntryByDataSetIndex(index, dataSetIndex);

if(e == null) continue;

if (e == null)
continue;

// calculate the correct x-position
float x = index * setCount + dataSetIndex + mOriginalData.getGroupSpace() / 2f + mOriginalData.getGroupSpace() * index;
float x = index * setCount + dataSetIndex + mOriginalData.getGroupSpace() / 2f
+ mOriginalData.getGroupSpace() * index;
float y = e.getVal();

prepareBar(x, y, ds.getBarSpace());
Expand Down Expand Up @@ -234,10 +236,10 @@ protected void drawHighlights() {

@Override
protected void drawData() {

ArrayList<BarDataSet> dataSets = mOriginalData.getDataSets();
int setCount = mOriginalData.getDataSetCount();

// the space between bar-groups
float space = mOriginalData.getGroupSpace();

Expand Down Expand Up @@ -368,12 +370,13 @@ protected void drawXLabels(float yPos) {
float[] position = new float[] {
0f, 0f
};

int step = mCurrentData.getDataSetCount();

for (int i = 0; i < mCurrentData.getXValCount(); i += mXLabels.mXAxisLabelModulus) {

position[0] = i * step + i * mOriginalData.getGroupSpace() + mOriginalData.getGroupSpace() / 2f;
position[0] = i * step + i * mOriginalData.getGroupSpace()
+ mOriginalData.getGroupSpace() / 2f;

// center the text
if (mXLabels.isCenterXLabelsEnabled())
Expand Down Expand Up @@ -418,7 +421,7 @@ protected void drawVerticalGrid() {
float[] position = new float[] {
0f, 0f
};

// take into consideration that multiple DataSets increase mDeltaX
int step = mCurrentData.getDataSetCount();

Expand Down Expand Up @@ -733,7 +736,7 @@ public Highlight getHighlightByTouchPoint(float x, float y) {

int setCount = mOriginalData.getDataSetCount();
int valCount = setCount * mOriginalData.getXValCount();

// calculate the amount of bar-space between index 0 and touch position
float space = (float) (((float) valCount / (float) setCount) / (mDeltaX / base));

Expand All @@ -749,6 +752,38 @@ public Highlight getHighlightByTouchPoint(float x, float y) {
return new Highlight(xIndex, dataSetIndex);
}

/**
* Returns the bounding box of the specified Entry in the specified DataSet.
* Returns null if the Entry could not be found in the charts data.
*
* @param e
* @param dataSetIndex
* @return
*/
public RectF getBarBounds(BarEntry e) {

BarDataSet set = mOriginalData.getDataSetForEntry(e);

if (set == null)
return null;

float barspace = set.getBarSpace();
float y = e.getVal();
float x = e.getXIndex();

float spaceHalf = barspace / 2f;
float left = x + spaceHalf;
float right = x + 1f - spaceHalf;
float top = y >= 0 ? y : 0;
float bottom = y <= 0 ? y : 0;

RectF bounds = new RectF(left, top, right, bottom);

transformRect(bounds);

return bounds;
}

/**
* sets the skew (default 0.3f), the skew indicates how much the 3D effect
* of the chart is turned to the right
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewParent;

import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarLineScatterCandleData;
import com.github.mikephil.charting.data.BarLineScatterCandleRadarDataSet;
import com.github.mikephil.charting.data.Entry;
Expand Down Expand Up @@ -1377,6 +1379,34 @@ public boolean hasFixedYValues() {
return mFixedYValues;
}

/**
* Returns the position (in pixels) the provided Entry has inside the chart
* view or null, if the provided Entry is null.
*
* @param e
* @return
*/
public PointF getPosition(Entry e) {

if (e == null)
return null;

float[] vals = new float[] {
e.getXIndex(), e.getVal()
};

if (this instanceof BarChart) {

BarDataSet set = (BarDataSet) mOriginalData.getDataSetForEntry(e);
if (set != null)
vals[0] += set.getBarSpace() / 2f;
}

transformPointArray(vals);

return new PointF(vals[0], vals[1]);
}

/**
* sets the color for the grid lines
*
Expand Down
19 changes: 14 additions & 5 deletions MPChartLib/src/com/github/mikephil/charting/charts/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -445,14 +445,14 @@ protected void calcFormats() {
if (!mUseCustomFormatter) {

float reference = 0f;
if(mOriginalData.getXValCount() < 2) {
reference = Math.max(Math.abs(mYChartMin), Math.abs(mYChartMax));

if (mOriginalData.getXValCount() < 2) {

reference = Math.max(Math.abs(mYChartMin), Math.abs(mYChartMax));
} else {
reference = mDeltaY;
}

int digits = Utils.getFormatDigits(reference);

StringBuffer b = new StringBuffer();
Expand Down Expand Up @@ -1339,6 +1339,15 @@ public void setPhaseX(float phase) {
*/
/** BELOW THIS ONLY GETTERS AND SETTERS */

/**
* Returns the canvas object the chart uses for drawing.
*
* @return
*/
public Canvas getCanvas() {
return mDrawCanvas;
}

/**
* set a selection listener for the chart
*
Expand Down
25 changes: 25 additions & 0 deletions MPChartLib/src/com/github/mikephil/charting/data/ChartData.java
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,31 @@ public boolean removeEntry(int xIndex, int dataSetIndex) {
return removeEntry(e, dataSetIndex);
}

/**
* Returns the DataSet that contains the provided Entry, or null, if no
* DataSet contains this Entry.
*
* @param e
* @return
*/
public T getDataSetForEntry(Entry e) {

if (e == null)
return null;

for (int i = 0; i < mDataSets.size(); i++) {

T set = mDataSets.get(i);

for (int j = 0; j < set.getEntryCount(); j++) {
if (e.equalTo(set.getEntryForXIndex(e.getXIndex())))
return set;
}
}

return null;
}

/**
* Returns all colors used across all DataSet objects this object
* represents.
Expand Down
Loading

0 comments on commit 031848b

Please sign in to comment.