I’m trying to make bar char in androidplot. In my domain axis I placed timestamp values. When I place on graph values of which timestamp differ for couple days I have huge gap between bars. You can see it on image below.
I want my bars were next to each other like on the screen below.
Is this possible in androidplot? Here is my code:
public class ChartActivity extends Activity
{
private ArrayList<Measure> measures;
private XYPlot plot;
private MyBarFormatter formatter1;
private Number minXSeriesValue;
private Number maxXSeriesValue;
private Number minYSeriesValue;
private Number maxYSeriesValue;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chart);
measures = getIntent().getParcelableArrayListExtra(getString(R.string.constant_measures_intent_tag));
plot = (XYPlot) findViewById(R.id.mySimpleXYPlot);
plot.setRangeStep(XYStepMode.INCREMENT_BY_VAL, 1);
plot.setRangeLowerBoundary(0, BoundaryMode.FIXED);
plot.getGraphWidget().setRangeLabelOrientation(-45);
plot.getGraphWidget().setGridPadding(30, 10, 30, 0);
plot.setTicksPerDomainLabel(2);
plot.setDomainValueFormat(new MyDateFormat());
plot.getGraphWidget().setDomainLabelOrientation(-45);
plot.getGraphWidget().setDomainLabelVerticalOffset(5);
plot.setRangeValueFormat(new Format() {
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
Number num = (Number) obj;
switch(num.intValue()) {
case 1:
toAppendTo.append("1");
break;
case 2:
toAppendTo.append("2");
break;
case 3:
toAppendTo.append("3");
break;
case 4:
toAppendTo.append("4");
break;
case 5:
toAppendTo.append("5");
break;
case 6:
toAppendTo.append("6");
break;
case 7:
toAppendTo.append("7");
break;
case 8:
toAppendTo.append("8");
break;
case 9:
toAppendTo.append("9");
break;
case 10:
toAppendTo.append("10");
break;
default:
toAppendTo.append("");
break;
}
return toAppendTo;
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
});
List<Number> values = new ArrayList<Number>();
List<Number> domain = new ArrayList<Number>();
for(Measure measure : measures) {
values.add(measure.getThreshold().getValue());
domain.add(measure.getDate().getTime());
}
minXSeriesValue = domain.get(0);
maxXSeriesValue = domain.get(0);
for(int i=0 ;i<domain.size(); i++) {
if(minXSeriesValue == null || minXSeriesValue.doubleValue() > domain.get(i).doubleValue())
minXSeriesValue = domain.get(i);
if(maxXSeriesValue == null || maxXSeriesValue.doubleValue() < domain.get(i).doubleValue())
maxXSeriesValue = domain.get(i);
}
XYSeries series = new SimpleXYSeries(domain,values,"");
plot.setRangeTopMin(11);
plot.getLegendWidget().setVisible(false);
formatter1 = new MyBarFormatter(Color.argb(200, 100, 150, 100), Color.LTGRAY);
plot.addSeries(series, formatter1);
MyBarRenderer renderer = ((MyBarRenderer)plot.getRenderer(MyBarRenderer.class));
renderer.setBarRenderStyle(BarRenderer.BarRenderStyle.SIDE_BY_SIDE);
renderer.setBarWidthStyle(BarRenderer.BarWidthStyle.FIXED_WIDTH);
renderer.setBarWidth(10);
renderer.setBarGap(5);
plot.setDomainBoundaries(minXSeriesValue.longValue()-1,maxXSeriesValue.longValue()+1, BoundaryMode.FIXED);
plot.getGraphWidget().getDomainSubGridLinePaint().setColor(Color.TRANSPARENT);
plot.redraw();
}
class MyBarFormatter extends BarFormatter {
public MyBarFormatter(int fillColor, int borderColor) {
super(fillColor, borderColor);
}
@Override
public Class<? extends SeriesRenderer> getRendererClass() {
return MyBarRenderer.class;
}
@Override
public SeriesRenderer getRendererInstance(XYPlot plot) {
return new MyBarRenderer(plot);
}
}
class MyBarRenderer extends BarRenderer<MyBarFormatter> {
public MyBarRenderer(XYPlot plot) {
super(plot);
}
//@Override
// TODO: figure out why using @Override screws up the Maven builds
protected MyBarFormatter getFormatter(int index, XYSeries series) {
return getFormatter(series);
}
}
private class MyDateFormat extends Format {
private static final long serialVersionUID = 1L;
private SimpleDateFormat dateFormat = new SimpleDateFormat("MM.dd");// HH.mm");
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
long timestamp = ((Number) obj).longValue();
Date date = new Date(timestamp);
return dateFormat.format(date, toAppendTo, pos);
}
@Override
public Object parseObject(String source, ParsePosition pos) {
return null;
}
}
}
Androidplot can do a bunch of stuff, although it takes some time getting used to.
In androidplot, when you want to make a bar graph, you can only choose to set the gap width of the bar width, but not both. Being able to edit both the gap width and bar width could set the actual width of the bars in the graph to two different values.
According to the documentation on the Androidplot website, http://androidplot.com/javadoc/0.6.0/com/androidplot/xy/BarRenderer.html you can only use the
setBarWidth
method if the BarWidthStyle is set toFIXED_WIDTH
,and you can only use the
setBarGap
if the BarWidthStyle is set toVARIABLE_WIDTH
Hope this helped!