use of lecho.lib.hellocharts.model.SliceValue in project hellocharts-android by lecho.
the class PieChartRenderer method drawSeparationLines.
private void drawSeparationLines(Canvas canvas) {
final PieChartData data = dataProvider.getPieChartData();
if (data.getValues().size() < 2) {
//No need for separation lines for 0 or 1 slices.
return;
}
final int sliceSpacing = ChartUtils.dp2px(density, data.getSlicesSpacing());
if (sliceSpacing < 1) {
//No need for separation lines
return;
}
final float sliceScale = 360f / maxSum;
float lastAngle = rotation;
final float circleRadius = originCircleOval.width() / 2f;
separationLinesPaint.setStrokeWidth(sliceSpacing);
for (SliceValue sliceValue : data.getValues()) {
final float angle = Math.abs(sliceValue.getValue()) * sliceScale;
sliceVector.set((float) (Math.cos(Math.toRadians(lastAngle))), (float) (Math.sin(Math.toRadians(lastAngle))));
normalizeVector(sliceVector);
float x1 = sliceVector.x * (circleRadius + touchAdditional) + originCircleOval.centerX();
float y1 = sliceVector.y * (circleRadius + touchAdditional) + originCircleOval.centerY();
canvas.drawLine(originCircleOval.centerX(), originCircleOval.centerY(), x1, y1, separationLinesPaint);
lastAngle += angle;
}
}
use of lecho.lib.hellocharts.model.SliceValue in project hellocharts-android by lecho.
the class PieChartRenderer method calculateMaxViewport.
/**
* Viewport is not really important for PieChart, this kind of chart doesn't relay on viewport but uses pixels
* coordinates instead. This method also calculates sum of all SliceValues.
*/
private void calculateMaxViewport() {
tempMaximumViewport.set(0, MAX_WIDTH_HEIGHT, MAX_WIDTH_HEIGHT, 0);
maxSum = 0.0f;
for (SliceValue sliceValue : dataProvider.getPieChartData().getValues()) {
maxSum += Math.abs(sliceValue.getValue());
}
}
use of lecho.lib.hellocharts.model.SliceValue in project hellocharts-android by lecho.
the class PieChartRenderer method drawLabels.
public void drawLabels(Canvas canvas) {
final PieChartData data = dataProvider.getPieChartData();
final float sliceScale = 360f / maxSum;
float lastAngle = rotation;
int sliceIndex = 0;
for (SliceValue sliceValue : data.getValues()) {
final float angle = Math.abs(sliceValue.getValue()) * sliceScale;
if (isTouched()) {
if (hasLabels) {
drawLabel(canvas, sliceValue, lastAngle, angle);
} else if (hasLabelsOnlyForSelected && selectedValue.getFirstIndex() == sliceIndex) {
drawLabel(canvas, sliceValue, lastAngle, angle);
}
} else {
if (hasLabels) {
drawLabel(canvas, sliceValue, lastAngle, angle);
}
}
lastAngle += angle;
++sliceIndex;
}
}
use of lecho.lib.hellocharts.model.SliceValue in project hellocharts-android by lecho.
the class PieChartRenderer method checkTouch.
@Override
public boolean checkTouch(float touchX, float touchY) {
selectedValue.clear();
final PieChartData data = dataProvider.getPieChartData();
final float centerX = originCircleOval.centerX();
final float centerY = originCircleOval.centerY();
final float circleRadius = originCircleOval.width() / 2f;
sliceVector.set(touchX - centerX, touchY - centerY);
// Check if touch is on circle area, if not return false;
if (sliceVector.length() > circleRadius + touchAdditional) {
return false;
}
// Check if touch is not in center circle, if yes return false;
if (data.hasCenterCircle() && sliceVector.length() < circleRadius * data.getCenterCircleScale()) {
return false;
}
// Get touchAngle and align touch 0 degrees with chart 0 degrees, that why I subtracting start angle,
// adding 360
// and modulo 360 translates i.e -20 degrees to 340 degrees.
final float touchAngle = (pointToAngle(touchX, touchY, centerX, centerY) - rotation + 360f) % 360f;
final float sliceScale = 360f / maxSum;
// No start angle here, see above
float lastAngle = 0f;
int sliceIndex = 0;
for (SliceValue sliceValue : data.getValues()) {
final float angle = Math.abs(sliceValue.getValue()) * sliceScale;
if (touchAngle >= lastAngle) {
selectedValue.set(sliceIndex, sliceIndex, SelectedValueType.NONE);
}
lastAngle += angle;
++sliceIndex;
}
return isTouched();
}
use of lecho.lib.hellocharts.model.SliceValue in project hellocharts-android by lecho.
the class PieChartRenderer method getValueForAngle.
/**
* Returns SliceValue that is under given angle, selectedValue (if not null) will be hold slice index.
*/
public SliceValue getValueForAngle(int angle, SelectedValue selectedValue) {
final PieChartData data = dataProvider.getPieChartData();
final float touchAngle = (angle - rotation + 360f) % 360f;
final float sliceScale = 360f / maxSum;
float lastAngle = 0f;
int sliceIndex = 0;
for (SliceValue sliceValue : data.getValues()) {
final float tempAngle = Math.abs(sliceValue.getValue()) * sliceScale;
if (touchAngle >= lastAngle) {
if (null != selectedValue) {
selectedValue.set(sliceIndex, sliceIndex, SelectedValueType.NONE);
}
return sliceValue;
}
lastAngle += tempAngle;
++sliceIndex;
}
return null;
}
Aggregations