Skip to content

Commit

Permalink
Merge pull request #36 from dima7a14/master
Browse files Browse the repository at this point in the history
thanks
  • Loading branch information
hongyin163 authored Sep 1, 2016
2 parents bf75993 + a17c2e3 commit e5057c0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
1 change: 1 addition & 0 deletions PieChart.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ PieChart.propTypes = {
chartPadding:PropTypes.string,
legend:PropTypes.object,
holeRadius: PropTypes.number,
transparentCircleRadius: PropTypes.number,
drawSliceText: PropTypes.bool,
usePercentValues: PropTypes.bool,
centerText: PropTypes.string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public void setData(LineChart chart,ReadableMap rm){
if(config.hasKey("lineWidth")) dataSet.setLineWidth((float) config.getDouble("lineWidth"));
if(config.hasKey("drawValues")) dataSet.setDrawValues(config.getBoolean("drawValues"));
if(config.hasKey("valueTextColor")) dataSet.setValueTextColor(Color.parseColor(config.getString("valueTextColor")));
if (config.hasKey("drawCircleHole")) dataSet.setDrawCircleHole(config.getBoolean("drawCircleHole"));
if(config.hasKey("drawValues")) dataSet.setDrawValues(config.getBoolean("drawValues"));
if(config.hasKey("colors")){
ReadableArray colorsArray = config.getArray("colors");
ArrayList<Integer> colors = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,32 @@ public String getName() {

@Override
protected PieChart createViewInstance(ThemedReactContext reactContext) {
PieChart chart= new PieChart(reactContext);
return chart;
PieChart chart=new PieChart(reactContext);

// initialise event listener to bind to chart
new MPPieChartSelectionEventListener(chart);

return chart;
}

// @Override
// protected PieChart createViewInstance(ThemedReactContext reactContext) {
// PieChart chart= new PieChart(reactContext);
// return chart;
// }

@ReactProp(name = "holeRadius", defaultFloat = 50f)
public void setHoleRadius(PieChart chart, float holeRadius){
chart.setHoleRadius(holeRadius);
chart.invalidate();
}

@ReactProp(name = "transparentCircleRadius", defaultFloat = 55f)
public void setTransparentCircleRadius(PieChart chart, float transparentCircleRadius){
chart.setTransparentCircleRadius(transparentCircleRadius);
chart.invalidate();
}

@ReactProp(name="backgroundColor", defaultInt = Color.WHITE)
public void setBackgroundColor(PieChart chart, int backgroundColor){
chart.setBackgroundColor(backgroundColor);
Expand Down Expand Up @@ -102,6 +118,9 @@ public void setData(PieChart chart,ReadableMap rm){
int[] colors=new int[]{Color.parseColor(config.getString("color"))};
dataSet.setColors(colors);
}
if(config.hasKey("drawValues")) dataSet.setDrawValues(config.getBoolean("drawValues"));
if(config.hasKey("valueTextColor")) dataSet.setValueTextColor(Color.parseColor(config.getString("valueTextColor")));

pieData.addDataSet(dataSet);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cn.mandata.react_native_mpchart;

import android.view.MotionEvent;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;

import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;


/*
* implement OnChartValueSelectedListener interface to bridge selection callback
* events support
* use eventName 'topSelect' mapping to 'onSelect' callback prop in JS
*/
public class MPPieChartSelectionEventListener implements OnChartValueSelectedListener {
private PieChart chart=null;
public MPPieChartSelectionEventListener(){

}
public MPPieChartSelectionEventListener(PieChart chart){
this.chart=chart;

// bind selection callback listener to chart
this.chart.setOnChartValueSelectedListener(this);
}

@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
WritableMap event = Arguments.createMap();
event.putInt("xIndex", e.getXIndex());
event.putDouble("yValue", e.getVal());

ThemedReactContext reactContext = (ThemedReactContext)this.chart.getContext();

reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
this.chart.getId(),
"topSelect",
event
);
}

@Override
public void onNothingSelected() {
WritableMap event = Arguments.createMap();

// pass xIndex as -1 for representing no selection
event.putInt("xIndex", -1);

ThemedReactContext reactContext = (ThemedReactContext)this.chart.getContext();

reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
this.chart.getId(),
"topSelect",
event
);
}
}

0 comments on commit e5057c0

Please sign in to comment.