use of com.db.chart.model.Bar in project WilliamChart by diogobernardino.
the class AxisRendererTest method findBorders_MaxMinEqual_MaxPlusStep.
@Test
public void findBorders_MaxMinEqual_MaxPlusStep() {
BarSet set = new BarSet();
for (int i = 0; i < 2; i++) set.addBar(new Bar(Integer.toString(i), (float) 0));
mData = new ArrayList<>();
mData.add(set);
assertArrayEquals(new int[] { 0, 9 }, mXRndr.findBorders(mData, 9));
}
use of com.db.chart.model.Bar in project WilliamChart by diogobernardino.
the class BarChartView method defineRegions.
/**
* (Optional) To be overridden in order for each chart to define its own clickable regions.
* This way, classes extending ChartView will only define their clickable regions.
* <p>
* Important: the returned vector must match the order of the data passed
* by the user. This ensures that onTouchEvent will return the correct index.
*
* @param data {@link java.util.ArrayList} of {@link com.db.chart.model.ChartSet}
* to use while defining each region of a {@link com.db.chart.view.BarChartView}
*
* @return {@link java.util.ArrayList} of {@link android.graphics.Region} with regions
* where click will be detected
*/
@Override
void defineRegions(ArrayList<ArrayList<Region>> regions, ArrayList<ChartSet> data) {
final int nSets = data.size();
final int nEntries = data.get(0).size();
float offset;
BarSet barSet;
Bar bar;
for (int i = 0; i < nEntries; i++) {
// Set first offset to draw a group of bars
offset = data.get(0).getEntry(i).getX() - drawingOffset;
for (int j = 0; j < nSets; j++) {
barSet = (BarSet) data.get(j);
bar = (Bar) barSet.getEntry(i);
if (bar.getValue() > 0)
regions.get(j).get(i).set((int) offset, (int) bar.getY(), (int) (offset += barWidth), (int) this.getZeroPosition());
else if (bar.getValue() < 0)
regions.get(j).get(i).set((int) offset, (int) this.getZeroPosition(), (int) (offset += barWidth), (int) bar.getY());
else
regions.get(j).get(i).set((int) offset, (int) this.getZeroPosition(), (int) (offset += barWidth), (int) this.getZeroPosition() + 1);
// If last bar of group no set spacing is necessary
if (j != nSets - 1)
offset += style.setSpacing;
}
}
}
use of com.db.chart.model.Bar in project WilliamChart by diogobernardino.
the class BarChartView method onDrawChart.
/**
* Method responsible to draw bars with the parsed screen points.
*
* @param canvas The canvas to draw on
* @param data {@link java.util.ArrayList} of {@link com.db.chart.model.ChartSet} to use
*/
@Override
public void onDrawChart(Canvas canvas, ArrayList<ChartSet> data) {
final int nSets = data.size();
final int nEntries = data.get(0).size();
float offset;
BarSet barSet;
Bar bar;
for (int i = 0; i < nEntries; i++) {
// Set first offset to draw a group of bars
offset = data.get(0).getEntry(i).getX() - drawingOffset;
for (int j = 0; j < nSets; j++) {
barSet = (BarSet) data.get(j);
bar = (Bar) barSet.getEntry(i);
// If entry value is 0 it won't be drawn
if (!barSet.isVisible())
continue;
// Style it!
if (!bar.hasGradientColor())
style.barPaint.setColor(bar.getColor());
else
style.barPaint.setShader(new LinearGradient(bar.getX(), this.getZeroPosition(), bar.getX(), bar.getY(), bar.getGradientColors(), bar.getGradientPositions(), Shader.TileMode.MIRROR));
applyShadow(style.barPaint, barSet.getAlpha(), bar.getShadowDx(), bar.getShadowDy(), bar.getShadowRadius(), bar.getShadowColor());
// Draw background
if (style.hasBarBackground) {
drawBarBackground(canvas, offset, this.getInnerChartTop(), offset + barWidth, this.getInnerChartBottom());
}
// Draw bar
if (// Positive
bar.getValue() >= 0)
drawBar(canvas, offset, bar.getY(), offset + barWidth, this.getZeroPosition());
else
// Negative
drawBar(canvas, offset, this.getZeroPosition(), offset + barWidth, bar.getY());
offset += barWidth;
// If last bar of group no set spacing is necessary
if (j != nSets - 1)
offset += style.setSpacing;
}
}
}
use of com.db.chart.model.Bar in project WilliamChart by diogobernardino.
the class StackBarChartView method defineRegions.
/**
* (Optional) To be overridden in order for each chart to define its own clickable regions.
* This way, classes extending ChartView will only define their clickable regions.
* <p>
* Important: the returned vector must match the order of the data passed
* by the user. This ensures that onTouchEvent will return the correct index.
*
* @param data {@link java.util.ArrayList} of {@link com.db.chart.model.ChartSet}
* to use while defining each region of a {@link com.db.chart.view.BarChartView}
*
* @return {@link java.util.ArrayList} of {@link android.graphics.Region} with regions
* where click will be detected
*/
@Override
void defineRegions(ArrayList<ArrayList<Region>> regions, ArrayList<ChartSet> data) {
int dataSize = data.size();
int setSize = data.get(0).size();
float verticalOffset;
float currBottomY;
float negVerticalOffset;
float negCurrBottomY;
float y1;
float barSize;
BarSet barSet;
Bar bar;
float zeroPosition = this.getZeroPosition();
for (int i = 0; i < setSize; i++) {
// Vertical offset to keep drawing bars on top of the others
verticalOffset = 0;
negVerticalOffset = 0;
// Bottom of the next bar to be drawn
currBottomY = zeroPosition;
negCurrBottomY = zeroPosition;
for (int j = 0; j < dataSize; j++) {
barSet = (BarSet) data.get(j);
bar = (Bar) barSet.getEntry(i);
barSize = Math.abs(zeroPosition - bar.getY());
// Then no need to have region
if (!barSet.isVisible())
continue;
if (bar.getValue() > 0) {
y1 = zeroPosition - (barSize + verticalOffset);
regions.get(j).get(i).set((int) (bar.getX() - barWidth / 2), (int) y1, (int) (bar.getX() + barWidth / 2), (int) currBottomY);
currBottomY = y1;
verticalOffset += barSize + 2;
} else if (bar.getValue() < 0) {
y1 = zeroPosition + (barSize - negVerticalOffset);
regions.get(j).get(i).set((int) (bar.getX() - barWidth / 2), (int) negCurrBottomY, (int) (bar.getX() + barWidth / 2), (int) y1);
negCurrBottomY = y1;
negVerticalOffset -= barSize;
} else {
// If bar.getValue() == 0, force region to 1 pixel
y1 = zeroPosition - (1 + verticalOffset);
regions.get(j).get(i).set((int) (bar.getX() - barWidth / 2), (int) y1, (int) (bar.getX() + barWidth / 2), (int) currBottomY);
}
}
}
}
use of com.db.chart.model.Bar in project WilliamChart by diogobernardino.
the class AxisRendererTest method setUp.
@Before
public void setUp() {
mXRndr = new XRenderer();
BarSet set = new BarSet();
for (int i = 0; i < 2; i++) set.addBar(new Bar(Integer.toString(i), (float) i));
mData = new ArrayList<>();
mData.add(set);
}
Aggregations