use of org.achartengine.renderer.SimpleSeriesRenderer in project sensorreadout by onyxbits.
the class AbstractChart method drawLegend.
/**
* Draws the chart legend.
*
* @param canvas the canvas to paint to
* @param renderer the series renderer
* @param titles the titles to go to the legend
* @param left the left X value of the area to draw to
* @param right the right X value of the area to draw to
* @param y the y value of the area to draw to
* @param width the width of the area to draw to
* @param height the height of the area to draw to
* @param legendSize the legend size
* @param paint the paint to be used for drawing
* @param calculate if only calculating the legend size
*
* @return the legend height
*/
protected int drawLegend(Canvas canvas, DefaultRenderer renderer, String[] titles, int left, int right, int y, int width, int height, int legendSize, Paint paint, boolean calculate) {
float size = 32;
if (renderer.isShowLegend()) {
float currentX = left;
float currentY = y + height - legendSize + size;
paint.setTextAlign(Align.LEFT);
paint.setTextSize(renderer.getLegendTextSize());
int sLength = Math.min(titles.length, renderer.getSeriesRendererCount());
for (int i = 0; i < sLength; i++) {
SimpleSeriesRenderer r = renderer.getSeriesRendererAt(i);
final float lineSize = getLegendShapeWidth(i);
if (r.isShowLegendItem()) {
String text = titles[i];
if (titles.length == renderer.getSeriesRendererCount()) {
paint.setColor(r.getColor());
} else {
paint.setColor(Color.LTGRAY);
}
float[] widths = new float[text.length()];
paint.getTextWidths(text, widths);
float sum = 0;
for (float value : widths) {
sum += value;
}
float extraSize = lineSize + 10 + sum;
float currentWidth = currentX + extraSize;
if (i > 0 && getExceed(currentWidth, renderer, right, width)) {
currentX = left;
currentY += renderer.getLegendTextSize();
size += renderer.getLegendTextSize();
currentWidth = currentX + extraSize;
}
if (getExceed(currentWidth, renderer, right, width)) {
float maxWidth = right - currentX - lineSize - 10;
if (isVertical(renderer)) {
maxWidth = width - currentX - lineSize - 10;
}
int nr = paint.breakText(text, true, maxWidth, widths);
text = text.substring(0, nr) + "...";
}
if (!calculate) {
drawLegendShape(canvas, r, currentX, currentY, i, paint);
drawString(canvas, text, currentX + lineSize + 5, currentY + 5, paint);
}
currentX += extraSize;
}
}
}
return Math.round(size + renderer.getLegendTextSize());
}
use of org.achartengine.renderer.SimpleSeriesRenderer in project AndrOBD by fr3ts0n.
the class ObdGaugeAdapter method getView.
/* (non-Javadoc)
* @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
EcuDataPv currPv = getItem(position);
int pid = currPv.getAsInt(EcuDataPv.FID_PID);
// if no recycled convertView delivered, then create a new one
if (convertView == null) {
convertView = mInflater.inflate(resourceId, parent, false);
holder = new ViewHolder();
// get all views into view holder
holder.gauge = (FrameLayout) convertView.findViewById(R.id.gauge);
holder.tvDescr = (TextView) convertView.findViewById(R.id.label);
// remember this view holder
convertView.setTag(holder);
} else {
// recall previous holder
holder = (ViewHolder) convertView.getTag();
}
convertView.getLayoutParams().width = minWidth;
convertView.getLayoutParams().height = minHeight;
// if no rendering component is registered with PV, then create and register new one
DialChart chartView = (DialChart) currPv.getRenderingComponent();
if (chartView == null) {
CategorySeries category = (CategorySeries) currPv.get(FID_GAUGE_SERIES);
Number minValue = (Number) currPv.get(EcuDataPv.FID_MIN);
Number maxValue = (Number) currPv.get(EcuDataPv.FID_MAX);
if (minValue == null)
minValue = 0f;
if (maxValue == null)
maxValue = 255f;
DialRenderer renderer = new DialRenderer();
renderer.setScale(1.25f);
// dial background
renderer.setPanEnabled(false);
renderer.setShowLegend(false);
renderer.setShowLabels(true);
renderer.setLabelsTextSize(mDisplayMetrics.densityDpi / 10);
renderer.setLabelsColor(Color.WHITE);
renderer.setShowLabels(true);
renderer.setVisualTypes(new DialRenderer.Type[] { DialRenderer.Type.NEEDLE });
renderer.setMinValue(minValue.doubleValue());
renderer.setMaxValue(maxValue.doubleValue());
renderer.setChartTitle(currPv.getUnits());
renderer.setChartTitleTextSize(mDisplayMetrics.densityDpi / 10);
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(ChartActivity.getItemColor(pid));
try {
r.setChartValuesFormat(labelFormat);
} catch (Exception e) {
// ignore
}
renderer.addSeriesRenderer(0, r);
// create chart view and register with PV
chartView = new DialChart(category, renderer);
currPv.setRenderingComponent(chartView);
}
convertView.setBackgroundColor(ChartActivity.getItemColor(pid) & 0x08FFFFFF);
// set new values for display
holder.tvDescr.setTextColor(ChartActivity.getItemColor(pid));
holder.tvDescr.setText(String.valueOf(currPv.get(EcuDataPv.FID_DESCRIPT)));
// replace DialChart if needed
holder.gauge.removeViewAt(0);
holder.gauge.addView(new GraphicalView(getContext(), chartView), 0);
return convertView;
}
use of org.achartengine.renderer.SimpleSeriesRenderer in project Anki-Android by Ramblurr.
the class XYChart method draw.
/**
* The graphical representation of the XY chart.
*
* @param canvas the canvas to paint to
* @param x the top left x value of the view to draw to
* @param y the top left y value of the view to draw to
* @param width the width of the view to draw to
* @param height the height of the view to draw to
* @param paint the paint
*/
public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint) {
paint.setAntiAlias(mRenderer.isAntialiasing());
int legendSize = getLegendSize(mRenderer, height / 5, mRenderer.getAxisTitleTextSize());
int[] margins = mRenderer.getMargins();
int left = x + margins[1];
int top = y + margins[0];
int right = x + width - margins[3];
int sLength = mDataset.getSeriesCount();
String[] titles = new String[sLength];
for (int i = 0; i < sLength; i++) {
titles[i] = mDataset.getSeriesAt(i).getTitle();
}
if (mRenderer.isFitLegend() && mRenderer.isShowLegend()) {
legendSize = drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, true);
}
int bottom = y + height - margins[2] - legendSize;
if (mScreenR == null) {
mScreenR = new Rect();
}
mScreenR.set(left, top, right, bottom);
drawBackground(mRenderer, canvas, x, y, width, height, paint, false, DefaultRenderer.NO_COLOR);
if (paint.getTypeface() == null || !paint.getTypeface().toString().equals(mRenderer.getTextTypefaceName()) || paint.getTypeface().getStyle() != mRenderer.getTextTypefaceStyle()) {
paint.setTypeface(Typeface.create(mRenderer.getTextTypefaceName(), mRenderer.getTextTypefaceStyle()));
}
Orientation or = mRenderer.getOrientation();
if (or == Orientation.VERTICAL) {
right -= legendSize;
bottom += legendSize - 20;
}
int angle = or.getAngle();
boolean rotate = angle == 90;
mScale = (float) (height) / width;
mTranslate = Math.abs(width - height) / 2;
if (mScale < 1) {
mTranslate *= -1;
}
mCenter = new Point((x + width) / 2, (y + height) / 2);
if (rotate) {
transform(canvas, angle, false);
}
int maxScaleNumber = -Integer.MAX_VALUE;
for (int i = 0; i < sLength; i++) {
maxScaleNumber = Math.max(maxScaleNumber, mDataset.getSeriesAt(i).getScaleNumber());
}
maxScaleNumber++;
if (maxScaleNumber < 0) {
return;
}
double[] minX = new double[maxScaleNumber];
double[] maxX = new double[maxScaleNumber];
double[] minY = new double[maxScaleNumber];
double[] maxY = new double[maxScaleNumber];
boolean[] isMinXSet = new boolean[maxScaleNumber];
boolean[] isMaxXSet = new boolean[maxScaleNumber];
boolean[] isMinYSet = new boolean[maxScaleNumber];
boolean[] isMaxYSet = new boolean[maxScaleNumber];
for (int i = 0; i < maxScaleNumber; i++) {
minX[i] = mRenderer.getXAxisMin(i);
maxX[i] = mRenderer.getXAxisMax(i);
minY[i] = mRenderer.getYAxisMin(i);
maxY[i] = mRenderer.getYAxisMax(i);
isMinXSet[i] = mRenderer.isMinXSet(i);
isMaxXSet[i] = mRenderer.isMaxXSet(i);
isMinYSet[i] = mRenderer.isMinYSet(i);
isMaxYSet[i] = mRenderer.isMaxYSet(i);
if (mCalcRange.get(i) == null) {
mCalcRange.put(i, new double[4]);
}
}
double[] xPixelsPerUnit = new double[maxScaleNumber];
double[] yPixelsPerUnit = new double[maxScaleNumber];
for (int i = 0; i < sLength; i++) {
XYSeries series = mDataset.getSeriesAt(i);
int scale = series.getScaleNumber();
if (series.getItemCount() == 0) {
continue;
}
if (!isMinXSet[scale]) {
double minimumX = series.getMinX();
minX[scale] = Math.min(minX[scale], minimumX);
mCalcRange.get(scale)[0] = minX[scale];
}
if (!isMaxXSet[scale]) {
double maximumX = series.getMaxX();
maxX[scale] = Math.max(maxX[scale], maximumX);
mCalcRange.get(scale)[1] = maxX[scale];
}
if (!isMinYSet[scale]) {
double minimumY = series.getMinY();
minY[scale] = Math.min(minY[scale], (float) minimumY);
mCalcRange.get(scale)[2] = minY[scale];
}
if (!isMaxYSet[scale]) {
double maximumY = series.getMaxY();
maxY[scale] = Math.max(maxY[scale], (float) maximumY);
mCalcRange.get(scale)[3] = maxY[scale];
}
}
for (int i = 0; i < maxScaleNumber; i++) {
if (maxX[i] - minX[i] != 0) {
xPixelsPerUnit[i] = (right - left) / (maxX[i] - minX[i]);
}
if (maxY[i] - minY[i] != 0) {
yPixelsPerUnit[i] = (float) ((bottom - top) / (maxY[i] - minY[i]));
}
}
boolean hasValues = false;
// use a linked list for these reasons:
// 1) Avoid a large contiguous memory allocation
// 2) We don't need random seeking, only sequential reading/writing, so
// linked list makes sense
clickableAreas = new HashMap<Integer, List<ClickableArea>>();
for (int i = 0; i < sLength; i++) {
XYSeries series = mDataset.getSeriesAt(i);
int scale = series.getScaleNumber();
if (series.getItemCount() == 0) {
continue;
}
hasValues = true;
SimpleSeriesRenderer seriesRenderer = mRenderer.getSeriesRendererAt(i);
// int originalValuesLength = series.getItemCount();
// int valuesLength = originalValuesLength;
// int length = valuesLength * 2;
List<Float> points = new ArrayList<Float>();
List<Double> values = new ArrayList<Double>();
float yAxisValue = Math.min(bottom, (float) (bottom + yPixelsPerUnit[scale] * minY[scale]));
LinkedList<ClickableArea> clickableArea = new LinkedList<ClickableArea>();
clickableAreas.put(i, clickableArea);
SortedMap<Double, Double> range = series.getRange(minX[scale], maxX[scale], 1);
int startIndex = -1;
int intRange = range.lastKey().intValue() - range.firstKey().intValue();
for (Entry<Double, Double> value : range.entrySet()) {
double xValue = value.getKey();
double yValue = value.getValue();
if (startIndex < 0) {
startIndex = series.getIndexForKey(xValue);
}
// points.add((float) (left + xPixelsPerUnit[scale]
// * (value.getKey().floatValue() - minX[scale])));
// points.add((float) (bottom - yPixelsPerUnit[scale]
// * (value.getValue().floatValue() - minY[scale])));
values.add(value.getKey());
values.add(value.getValue());
if (!isNullValue(yValue)) {
points.add((float) (left + xPixelsPerUnit[scale] * (xValue - minX[scale])));
points.add((float) (bottom - yPixelsPerUnit[scale] * (yValue - minY[scale])));
} else if (isRenderNullValues()) {
points.add((float) (left + xPixelsPerUnit[scale] * (xValue - minX[scale])));
points.add((float) (bottom - yPixelsPerUnit[scale] * (-minY[scale])));
} else {
if (points.size() > 0) {
drawSeries(series, canvas, paint, points, seriesRenderer, yAxisValue, i, or, startIndex, intRange);
ClickableArea[] clickableAreasForSubSeries = clickableAreasForPoints(MathHelper.getFloats(points), MathHelper.getDoubles(values), yAxisValue, i, startIndex, intRange);
clickableArea.addAll(Arrays.asList(clickableAreasForSubSeries));
points.clear();
values.clear();
}
clickableArea.add(null);
}
}
if (points.size() > 0) {
drawSeries(series, canvas, paint, points, seriesRenderer, yAxisValue, i, or, startIndex, intRange);
ClickableArea[] clickableAreasForSubSeries = clickableAreasForPoints(MathHelper.getFloats(points), MathHelper.getDoubles(values), yAxisValue, i, startIndex, intRange);
clickableArea.addAll(Arrays.asList(clickableAreasForSubSeries));
}
}
// draw stuff over the margins such as data doesn't render on these areas
drawBackground(mRenderer, canvas, x, bottom, width, height - bottom, paint, true, mRenderer.getMarginsColor());
drawBackground(mRenderer, canvas, x, y, width, margins[0], paint, true, mRenderer.getMarginsColor());
if (or == Orientation.HORIZONTAL) {
drawBackground(mRenderer, canvas, x, y, left - x, height - y, paint, true, mRenderer.getMarginsColor());
drawBackground(mRenderer, canvas, right, y, margins[3], height - y, paint, true, mRenderer.getMarginsColor());
} else if (or == Orientation.VERTICAL) {
drawBackground(mRenderer, canvas, right, y, width - right, height - y, paint, true, mRenderer.getMarginsColor());
drawBackground(mRenderer, canvas, x, y, left - x, height - y, paint, true, mRenderer.getMarginsColor());
}
boolean showLabels = mRenderer.isShowLabels() && hasValues;
boolean showGridX = mRenderer.isShowGridX();
boolean showCustomTextGrid = mRenderer.isShowCustomTextGrid();
if (showLabels || showGridX) {
List<Double> xLabels = getValidLabels(getXLabels(minX[0], maxX[0], mRenderer.getXLabels()));
Map<Integer, List<Double>> allYLabels = getYLabels(minY, maxY, maxScaleNumber);
int xLabelsLeft = left;
if (showLabels) {
paint.setColor(mRenderer.getXLabelsColor());
paint.setTextSize(mRenderer.getLabelsTextSize());
paint.setTextAlign(mRenderer.getXLabelsAlign());
if (mRenderer.getXLabelsAlign() == Align.LEFT) {
xLabelsLeft += mRenderer.getLabelsTextSize() / 4;
}
}
drawXLabels(xLabels, mRenderer.getXTextLabelLocations(), canvas, paint, xLabelsLeft, top, bottom, xPixelsPerUnit[0], minX[0], maxX[0]);
drawYLabels(allYLabels, canvas, paint, maxScaleNumber, left, right, bottom, yPixelsPerUnit, minY);
if (showLabels) {
paint.setColor(mRenderer.getLabelsColor());
for (int i = 0; i < maxScaleNumber; i++) {
Align axisAlign = mRenderer.getYAxisAlign(i);
Double[] yTextLabelLocations = mRenderer.getYTextLabelLocations(i);
for (Double location : yTextLabelLocations) {
if (minY[i] <= location && location <= maxY[i]) {
float yLabel = (float) (bottom - yPixelsPerUnit[i] * (location.doubleValue() - minY[i]));
String label = mRenderer.getYTextLabel(location, i);
paint.setColor(mRenderer.getYLabelsColor(i));
paint.setTextAlign(mRenderer.getYLabelsAlign(i));
if (or == Orientation.HORIZONTAL) {
if (axisAlign == Align.LEFT) {
canvas.drawLine(left + getLabelLinePos(axisAlign), yLabel, left, yLabel, paint);
drawText(canvas, label, left, yLabel - 2, paint, mRenderer.getYLabelsAngle());
} else {
canvas.drawLine(right, yLabel, right + getLabelLinePos(axisAlign), yLabel, paint);
drawText(canvas, label, right, yLabel - 2, paint, mRenderer.getYLabelsAngle());
}
if (showCustomTextGrid) {
paint.setColor(mRenderer.getGridColor());
canvas.drawLine(left, yLabel, right, yLabel, paint);
}
} else {
canvas.drawLine(right - getLabelLinePos(axisAlign), yLabel, right, yLabel, paint);
drawText(canvas, label, right + 10, yLabel - 2, paint, mRenderer.getYLabelsAngle());
if (showCustomTextGrid) {
paint.setColor(mRenderer.getGridColor());
canvas.drawLine(right, yLabel, left, yLabel, paint);
}
}
}
}
}
}
if (showLabels) {
paint.setColor(mRenderer.getLabelsColor());
float size = mRenderer.getAxisTitleTextSize();
paint.setTextSize(size);
paint.setTextAlign(Align.CENTER);
if (or == Orientation.HORIZONTAL) {
drawText(canvas, mRenderer.getXTitle(), x + width / 2, bottom + mRenderer.getLabelsTextSize() * 4 / 3 + size, paint, 0);
for (int i = 0; i < maxScaleNumber; i++) {
Align axisAlign = mRenderer.getYAxisAlign(i);
if (axisAlign == Align.LEFT) {
drawText(canvas, mRenderer.getYTitle(i), x + size, y + height / 2, paint, -90);
} else {
drawText(canvas, mRenderer.getYTitle(i), x + width, y + height / 2, paint, -90);
}
}
paint.setTextSize(mRenderer.getChartTitleTextSize());
drawText(canvas, mRenderer.getChartTitle(), x + width / 2, y + mRenderer.getChartTitleTextSize(), paint, 0);
} else if (or == Orientation.VERTICAL) {
drawText(canvas, mRenderer.getXTitle(), x + width / 2, y + height - size, paint, -90);
drawText(canvas, mRenderer.getYTitle(), right + 20, y + height / 2, paint, 0);
paint.setTextSize(mRenderer.getChartTitleTextSize());
drawText(canvas, mRenderer.getChartTitle(), x + size, top + height / 2, paint, 0);
}
}
}
if (or == Orientation.HORIZONTAL) {
drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, false);
} else if (or == Orientation.VERTICAL) {
transform(canvas, angle, true);
drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, false);
transform(canvas, angle, false);
}
if (mRenderer.isShowAxes()) {
paint.setColor(mRenderer.getAxesColor());
canvas.drawLine(left, bottom, right, bottom, paint);
boolean rightAxis = false;
for (int i = 0; i < maxScaleNumber && !rightAxis; i++) {
rightAxis = mRenderer.getYAxisAlign(i) == Align.RIGHT;
}
if (or == Orientation.HORIZONTAL) {
canvas.drawLine(left, top, left, bottom, paint);
if (rightAxis) {
canvas.drawLine(right, top, right, bottom, paint);
}
} else if (or == Orientation.VERTICAL) {
canvas.drawLine(right, top, right, bottom, paint);
}
}
if (rotate) {
transform(canvas, angle, true);
}
}
use of org.achartengine.renderer.SimpleSeriesRenderer in project sensorreadout by onyxbits.
the class PieChart method draw.
/**
* The graphical representation of the pie chart.
*
* @param canvas the canvas to paint to
* @param x the top left x value of the view to draw to
* @param y the top left y value of the view to draw to
* @param width the width of the view to draw to
* @param height the height of the view to draw to
* @param paint the paint
*/
@Override
public void draw(Canvas canvas, int x, int y, int width, int height, Paint paint) {
paint.setAntiAlias(mRenderer.isAntialiasing());
paint.setStyle(Style.FILL);
paint.setTextSize(mRenderer.getLabelsTextSize());
int legendSize = getLegendSize(mRenderer, height / 5, 0);
int left = x;
int top = y;
int right = x + width;
int sLength = mDataset.getItemCount();
double total = 0;
String[] titles = new String[sLength];
for (int i = 0; i < sLength; i++) {
total += mDataset.getValue(i);
titles[i] = mDataset.getCategory(i);
}
if (mRenderer.isFitLegend()) {
legendSize = drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, true);
}
int bottom = y + height - legendSize;
drawBackground(mRenderer, canvas, x, y, width, height, paint, false, DefaultRenderer.NO_COLOR);
float currentAngle = mRenderer.getStartAngle();
int mRadius = Math.min(Math.abs(right - left), Math.abs(bottom - top));
int radius = (int) (mRadius * 0.35 * mRenderer.getScale());
if (mCenterX == NO_VALUE) {
mCenterX = (left + right) / 2;
}
if (mCenterY == NO_VALUE) {
mCenterY = (bottom + top) / 2;
}
// Hook in clip detection after center has been calculated
mPieMapper.setDimensions(radius, mCenterX, mCenterY);
boolean loadPieCfg = !mPieMapper.areAllSegmentPresent(sLength);
if (loadPieCfg) {
mPieMapper.clearPieSegments();
}
float shortRadius = radius * 0.9f;
float longRadius = radius * 1.1f;
RectF oval = new RectF(mCenterX - radius, mCenterY - radius, mCenterX + radius, mCenterY + radius);
List<RectF> prevLabelsBounds = new ArrayList<RectF>();
for (int i = 0; i < sLength; i++) {
SimpleSeriesRenderer seriesRenderer = mRenderer.getSeriesRendererAt(i);
if (seriesRenderer.isGradientEnabled()) {
RadialGradient grad = new RadialGradient(mCenterX, mCenterY, longRadius, seriesRenderer.getGradientStartColor(), seriesRenderer.getGradientStopColor(), TileMode.MIRROR);
paint.setShader(grad);
} else {
paint.setColor(seriesRenderer.getColor());
}
float value = (float) mDataset.getValue(i);
float angle = (float) (value / total * 360);
if (seriesRenderer.isHighlighted()) {
double rAngle = Math.toRadians(90 - (currentAngle + angle / 2));
float translateX = (float) (radius * 0.1 * Math.sin(rAngle));
float translateY = (float) (radius * 0.1 * Math.cos(rAngle));
oval.offset(translateX, translateY);
canvas.drawArc(oval, currentAngle, angle, true, paint);
oval.offset(-translateX, -translateY);
} else {
canvas.drawArc(oval, currentAngle, angle, true, paint);
}
paint.setColor(seriesRenderer.getColor());
paint.setShader(null);
drawLabel(canvas, mDataset.getCategory(i), mRenderer, prevLabelsBounds, mCenterX, mCenterY, shortRadius, longRadius, currentAngle, angle, left, right, mRenderer.getLabelsColor(), paint, true, false);
if (mRenderer.isDisplayValues()) {
drawLabel(canvas, getLabel(mRenderer.getSeriesRendererAt(i).getChartValuesFormat(), mDataset.getValue(i)), mRenderer, prevLabelsBounds, mCenterX, mCenterY, shortRadius / 2, longRadius / 2, currentAngle, angle, left, right, mRenderer.getLabelsColor(), paint, false, true);
}
// Save details for getSeries functionality
if (loadPieCfg) {
mPieMapper.addPieSegment(i, value, currentAngle, angle);
}
currentAngle += angle;
}
prevLabelsBounds.clear();
drawLegend(canvas, mRenderer, titles, left, right, y, width, height, legendSize, paint, false);
drawTitle(canvas, x, y, width, paint);
}
Aggregations