Skip to content

Commit

Permalink
Multilines can now be filled, BarChart supports values.
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilJay committed May 27, 2014
1 parent 89da9a2 commit 5e16518
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 85 deletions.
40 changes: 24 additions & 16 deletions MPChartLib/src/com/github/mikephil/charting/BarChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class BarChart extends BarLineChartBase {
private boolean m3DEnabled = true;

/** flag that enables or disables the highlighting arrow */
private boolean mDrawHighlightArrow = true;
private boolean mDrawHighlightArrow = false;

public BarChart(Context context) {
super(context);
Expand Down Expand Up @@ -245,7 +245,7 @@ protected void drawData() {
ArrayList<Series> series = dataSet.getYVals();

// do the drawing
for (int j = 0; j < dataSet.getYValCount(); j++) {
for (int j = 0; j < dataSet.getSeriesCount(); j++) {

Paint paint = mDrawPaints[j % mDrawPaints.length];

Expand Down Expand Up @@ -284,26 +284,34 @@ protected void drawValues() {
// if values are drawn
if (mDrawYValues && mData.getYValCount() < mMaxVisibleCount * mScaleX) {

float[] valuePoints = new float[mData.getYValCount() * 2];
ArrayList<DataSet> dataSets = mData.getDataSets();

for (int i = 0; i < valuePoints.length; i += 2) {
valuePoints[i] = i / 2 + 0.5f; // add 0.5f too keep the values
// centered on top of the bars
valuePoints[i + 1] = getYValue(i / 2);
}
for (int i = 0; i < mData.getDataSetCount(); i++) {

DataSet dataSet = dataSets.get(i);
ArrayList<Series> series = dataSet.getYVals();

float[] valuePoints = generateTransformedValues(series, 0.5f);

transformPointArray(valuePoints);
for (int j = 0; j < valuePoints.length; j += 2) {

for (int i = 0; i < valuePoints.length; i += 2) {
if (isOffContentRight(valuePoints[j]))
break;

if (mDrawUnitInChart) {
if (isOffContentLeft(valuePoints[j]))
continue;

mDrawCanvas.drawText(mFormatValue.format(getYValue(i / 2)) + mUnit,
valuePoints[i], valuePoints[i + 1] - 12, mValuePaint);
} else {
float val = series.get(j / 2).getVal();

mDrawCanvas.drawText(mFormatValue.format(getYValue(i / 2)), valuePoints[i],
valuePoints[i + 1] - 12, mValuePaint);
if (mDrawUnitInChart) {

mDrawCanvas.drawText(mFormatValue.format(val) + mUnit,
valuePoints[j], valuePoints[j + 1] - 12, mValuePaint);
} else {

mDrawCanvas.drawText(mFormatValue.format(val), valuePoints[j],
valuePoints[j + 1] - 12, mValuePaint);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ protected void drawYLegend() {
* @param v
* @return
*/
protected boolean isOffCanvasRight(float p) {
protected boolean isOffContentRight(float p) {
if (p > mContentRect.right)
return true;
else
Expand All @@ -520,7 +520,7 @@ protected boolean isOffCanvasRight(float p) {
* @param v
* @return
*/
protected boolean isOffCanvasLeft(float p) {
protected boolean isOffContentLeft(float p) {
if (p < mContentRect.left)
return true;
else
Expand Down Expand Up @@ -927,7 +927,7 @@ public Highlight getIndexByTouchPoint(float x, float y) {
}

/**
* returns the index of the closest value
* returns the index DataSet that contains the closest value
*
* @param valsAtIndex
* @return
Expand Down
69 changes: 55 additions & 14 deletions MPChartLib/src/com/github/mikephil/charting/Chart.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ protected void init() {
}

/**
* sets the data for the chart
* Sets a new ChartData object for the chart.
*
* @param data
*/
Expand All @@ -228,6 +228,32 @@ public void setData(ChartData data) {
prepare();
}

/**
* Sets primitive data for the chart. Internally, this is converted into a
* ChartData object with one DataSet (type 0). If you have more specific
* requirements for your data, use the setData(ChartData data) method and
* create your own ChartData object with as many DataSets as you like.
*
* @param xVals
* @param yVals
*/
public void setData(ArrayList<String> xVals, ArrayList<Float> yVals) {

ArrayList<Series> series = new ArrayList<Series>();

for (int i = 0; i < yVals.size(); i++) {
series.add(new Series(yVals.get(i), i));
}

DataSet set = new DataSet(series, 0);
ArrayList<DataSet> dataSets = new ArrayList<DataSet>();
dataSets.add(set);

ChartData data = new ChartData(xVals, dataSets);

setData(data);
}

/**
* Prepares all the paint objects that are used for drawing the lines, bars
* or pie-slices. The number of paint objects used depends on the number of
Expand Down Expand Up @@ -322,6 +348,29 @@ protected void prepareContentRect() {
- mOffsetBottom);
}

/**
* transforms an arraylist of Series into a float array containing the x and
* y values transformed with all matrices
*
* @param series
* @param xoffset offset the chart values should have on the x-axis (0.5f)
* to center for barchart
* @return
*/
protected float[] generateTransformedValues(ArrayList<Series> series, float xOffset) {

float[] valuePoints = new float[series.size() * 2];

for (int j = 0; j < valuePoints.length; j += 2) {
valuePoints[j] = series.get(j / 2).getXIndex() + xOffset;
valuePoints[j + 1] = series.get(j / 2).getVal();
}

transformPointArray(valuePoints);

return valuePoints;
}

/**
* transform a path with all the given matrices VERY IMPORTANT: keep order
* to value-touch-offset
Expand Down Expand Up @@ -793,7 +842,7 @@ public float getAverage() {
*/
public float getAverage(int type) {
return mData.getDataSetByType(type).getYValueSum()
/ mData.getDataSetByType(type).getYValCount();
/ mData.getDataSetByType(type).getSeriesCount();
}

/**
Expand Down Expand Up @@ -963,12 +1012,6 @@ public float getMarkerPosY() {
return mMarkerPosY;
}

/** paint for the lines of the linechart */
public static final int PAINT_LINE = 1;

/** paint for the filled surface / area of the linechart */
public static final int PAINT_FILLED = 2;

/** paint for the grid lines (only line and barchart) */
public static final int PAINT_GRID = 3;

Expand All @@ -990,9 +1033,6 @@ public float getMarkerPosY() {
/** paint for the value text */
public static final int PAINT_VALUES = 8;

/** paint for the outer circle (linechart) */
public static final int PAINT_CIRCLES_OUTER = 9;

/** paint for the inner circle (linechart) */
public static final int PAINT_CIRCLES_INNER = 10;

Expand All @@ -1016,10 +1056,11 @@ public float getMarkerPosY() {

/**
* set a new paint object for the specified parameter in the chart e.g.
* Chart.PAINT_LINE
* Chart.PAINT_VALUES
*
* @param p --> Chart.PAINT_LINE, Chart.PAINT_GRID, Chart.PAINT_VALUES, ...
* @param which
* @param p the new paint object
* @param which Chart.PAINT_VALUES, Chart.PAINT_GRID, Chart.PAINT_VALUES,
* ...
*/
public void setPaint(Paint p, int which) {

Expand Down
2 changes: 1 addition & 1 deletion MPChartLib/src/com/github/mikephil/charting/ChartData.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public int getXValCount() {
public int getYValCount() {
int count = 0;
for (int i = 0; i < mDataSets.size(); i++) {
count += mDataSets.get(i).getYValCount();
count += mDataSets.get(i).getSeriesCount();
}

return count;
Expand Down
2 changes: 1 addition & 1 deletion MPChartLib/src/com/github/mikephil/charting/DataSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void calcYValueSum() {
*
* @return
*/
public int getYValCount() {
public int getSeriesCount() {
return mYVals.size();
}

Expand Down
88 changes: 38 additions & 50 deletions MPChartLib/src/com/github/mikephil/charting/LineChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas.VertexMode;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
Expand Down Expand Up @@ -94,7 +95,7 @@ protected void drawHighlights() {
DataSet set = getDataSetByIndex(mIndicesToHightlight[i].getDataSetIndex());

// check outofbounds
if (xIndex < set.getYValCount() && xIndex >= 0) {
if (xIndex < set.getSeriesCount() && xIndex >= 0) {

float[] pts = new float[] {
xIndex, mYChartMax, xIndex, mYChartMin, 0,
Expand Down Expand Up @@ -122,50 +123,54 @@ protected void drawData() {
DataSet dataSet = dataSets.get(i);
ArrayList<Series> series = dataSet.getYVals();

float[] valuePoints = new float[series.size() * 2];
float[] valuePoints = generateTransformedValues(series, 0f);

for (int j = 0; j < valuePoints.length; j += 2) {
valuePoints[j] = series.get(j / 2).getXIndex();
valuePoints[j + 1] = series.get(j / 2).getVal();
}

transformPointArray(valuePoints);
Paint paint = mDrawPaints[i % mDrawPaints.length];

for (int j = 0; j < valuePoints.length - 2; j += 2) {

if (isOffCanvasRight(valuePoints[j]))
if (isOffContentRight(valuePoints[j]))
break;

// make sure the lines don't do shitty things outside bounds
if (j != 0 && isOffCanvasLeft(valuePoints[j - 1]))
if (j != 0 && isOffContentLeft(valuePoints[j - 1]))
continue;

mDrawCanvas.drawLine(valuePoints[j], valuePoints[j + 1], valuePoints[j + 2],
valuePoints[j + 3],
mDrawPaints[i % mDrawPaints.length]);
valuePoints[j + 3], paint);
}
}

// if data is drawn filled
if (mDrawFilled) {

Path filled = new Path();
filled.moveTo(0, getYValue(0));
// if drawing filled is enabled
if (mDrawFilled) {
// mDrawCanvas.drawVertices(VertexMode.TRIANGLE_STRIP,
// valuePoints.length, valuePoints, 0,
// null, 0, null, 0, null, 0, 0, paint);

// filled is drawn with less alpha
paint.setAlpha(85);

// create a new path
for (int x = 1; x < mData.getYValCount(); x++) {
Path filled = new Path();
filled.moveTo(0, series.get(0).getVal());

filled.lineTo(x, getYValue(x));
}
// create a new path
for (int x = 1; x < series.size(); x++) {

// close up
filled.lineTo(mData.getXValCount() - 1, mYChartMin);
filled.lineTo(0f, mYChartMin);
filled.close();
filled.lineTo(x, series.get(x).getVal());
}

// close up
filled.lineTo(series.size() - 1, mYChartMin);
filled.lineTo(0f, mYChartMin);
filled.close();

transformPath(filled);
transformPath(filled);

mDrawCanvas.drawPath(filled, mFilledPaint);
mDrawCanvas.drawPath(filled, paint);

// restore alpha
paint.setAlpha(255);
}
}
}

Expand All @@ -188,21 +193,14 @@ protected void drawValues() {
DataSet dataSet = dataSets.get(i);
ArrayList<Series> series = dataSet.getYVals();

float[] positions = new float[dataSet.getYValCount() * 2];

for (int j = 0; j < positions.length; j += 2) {
positions[j] = series.get(j / 2).getXIndex();
positions[j + 1] = series.get(j / 2).getVal();
}

transformPointArray(positions);
float[] positions = generateTransformedValues(series, 0f);

for (int j = 0; j < positions.length; j += 2) {

if (isOffCanvasRight(positions[j]))
if (isOffContentRight(positions[j]))
break;

if (isOffCanvasLeft(positions[j]))
if (isOffContentLeft(positions[j]))
continue;

float val = series.get(j / 2).getVal();
Expand Down Expand Up @@ -236,23 +234,16 @@ protected void drawAdditional() {
DataSet dataSet = dataSets.get(i);
ArrayList<Series> series = dataSet.getYVals();

float[] positions = new float[dataSet.getYValCount() * 2];

for (int j = 0; j < positions.length; j += 2) {
positions[j] = series.get(j / 2).getXIndex();
positions[j + 1] = series.get(j / 2).getVal();
}

transformPointArray(positions);
float[] positions = generateTransformedValues(series, 0f);

for (int j = 0; j < positions.length; j += 2) {

if (isOffCanvasRight(positions[j]))
if (isOffContentRight(positions[j]))
break;

// make sure the circles don't do shitty things outside
// bounds
if (isOffCanvasLeft(positions[j]))
if (isOffContentLeft(positions[j]))
continue;

mDrawCanvas.drawCircle(positions[j], positions[j + 1], mCircleSize,
Expand Down Expand Up @@ -381,9 +372,6 @@ public void setPaint(Paint p, int which) {
super.setPaint(p, which);

switch (which) {
case PAINT_FILLED:
mFilledPaint = p;
break;
case PAINT_CIRCLES_INNER:
mCirclePaintInner = p;
break;
Expand Down
Binary file added performance.xlsx
Binary file not shown.

0 comments on commit 5e16518

Please sign in to comment.