Skip to content

Commit 7d1a862

Browse files
committed
Completed implementation of grouped BarChart.
1 parent bf08124 commit 7d1a862

2 files changed

Lines changed: 19 additions & 9 deletions

File tree

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.graphics.Color;
55
import android.graphics.Typeface;
66
import android.os.Bundle;
7+
import android.util.Log;
78
import android.view.Menu;
89
import android.view.MenuItem;
910
import android.view.WindowManager;
@@ -15,6 +16,8 @@
1516
import com.github.mikephil.charting.data.BarData;
1617
import com.github.mikephil.charting.data.BarDataSet;
1718
import com.github.mikephil.charting.data.BarEntry;
19+
import com.github.mikephil.charting.data.Entry;
20+
import com.github.mikephil.charting.interfaces.OnChartValueSelectedListener;
1821
import com.github.mikephil.charting.utils.Legend;
1922
import com.github.mikephil.charting.utils.Legend.LegendPosition;
2023
import com.github.mikephil.charting.utils.XLabels;
@@ -23,7 +26,7 @@
2326

2427
import java.util.ArrayList;
2528

26-
public class BarChartActivityMultiDataset extends DemoBase implements OnSeekBarChangeListener {
29+
public class BarChartActivityMultiDataset extends DemoBase implements OnSeekBarChangeListener, OnChartValueSelectedListener {
2730

2831
private BarChart mChart;
2932
private SeekBar mSeekBarX, mSeekBarY;
@@ -46,6 +49,7 @@ protected void onCreate(Bundle savedInstanceState) {
4649
mSeekBarY.setOnSeekBarChangeListener(this);
4750

4851
mChart = (BarChart) findViewById(R.id.chart1);
52+
mChart.setOnChartValueSelectedListener(this);
4953
mChart.setDescription("");
5054

5155
// disable the drawing of values
@@ -235,4 +239,14 @@ public void onStopTrackingTouch(SeekBar seekBar) {
235239
// TODO Auto-generated method stub
236240

237241
}
242+
243+
@Override
244+
public void onValueSelected(Entry e, int dataSetIndex) {
245+
Log.i("Activity", "Selected: " + e.toString() + ", dataSet: " + dataSetIndex);
246+
}
247+
248+
@Override
249+
public void onNothingSelected() {
250+
Log.i("Activity", "Nothing selected.");
251+
}
238252
}

‎MPChartLib/src/com/github/mikephil/charting/charts/BarChart.java‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import com.github.mikephil.charting.data.DataSet;
1717
import com.github.mikephil.charting.data.Entry;
1818
import com.github.mikephil.charting.utils.Highlight;
19-
import com.github.mikephil.charting.utils.SelInfo;
2019
import com.github.mikephil.charting.utils.Utils;
2120

2221
import java.util.ArrayList;
@@ -748,22 +747,19 @@ public Highlight getHighlightByTouchPoint(float x, float y) {
748747
base = mDeltaX - 1;
749748

750749
int setCount = mOriginalData.getDataSetCount();
751-
int valCount = setCount * mOriginalData.getXValCount() + 1;
750+
int valCount = setCount * mOriginalData.getXValCount();
752751

753752
BarData bd = (BarData) mCurrentData;
754753

755-
// number of group-spaces
756-
int spaces = (int) (((float) valCount / (float) setCount) / (mDeltaX / base));
754+
// calculate the amount of bar-space between index 0 and touch position
755+
float space = (float) (((float) valCount / (float) setCount) / (mDeltaX / base));
757756

758-
float reduction = (float) spaces * bd.getGroupSpace() + bd.getGroupSpace() / 2f;
757+
float reduction = (float) space * bd.getGroupSpace();
759758

760759
int xIndex = (int) ((base - reduction) / setCount);
761760

762761
int dataSetIndex = ((int) (base - reduction)) % setCount;
763762

764-
Log.i(LOG_TAG, "XIndex: " + xIndex + ", dataSet: " + dataSetIndex + ", base: " + base
765-
+ ", spaces: " + spaces + ", reduction: " + reduction);
766-
767763
if (dataSetIndex == -1)
768764
return null;
769765

0 commit comments

Comments
 (0)