I have a barchart with 3 bars shown. Three bars displayed with different widths. How to set equal width to all bars?
This is the snapshot of my barchart
Please any one suggest!!
Any Help will be Appreciated!!
I have a barchart with 3 bars shown. Three bars displayed with different widths. How to set equal width to all bars?
This is the snapshot of my barchart
Please any one suggest!!
Any Help will be Appreciated!!
It looks like you have 2 series of data. One series has 2 items in it and the green one has only one item. When you have only one item in the series, it may make sense to set the bar width using:
renderer.setBarWidth(width);
Another solution is to add at least two items to your series.
@SuppressLint("ParserError")
public class MainActivity extends Activity {
private GraphicalView BarChartView;
@SuppressLint("ParserError")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (BarChartView==null )
{
XYMultipleSeriesRenderer renderer = buildBarRenderer();
setChartSettings(renderer);
BarChartView = ChartFactory.getBarChartView(this, getBarDataset(), renderer, Type.DEFAULT);
LinearLayout playout = (LinearLayout) findViewById(R.id.graph2);
playout.addView(BarChartView);
;
}
else
{
BarChartView.repaint();
BarChartView.setVisibility(View.GONE);
}
}
private void setChartSettings(XYMultipleSeriesRenderer renderer)
{
renderer.setYTitle("Hits");
renderer.setXAxisMin(0);
renderer.setXAxisMax(7);
renderer.setYAxisMin(0);
renderer.setYAxisMax(900);
}
private XYMultipleSeriesDataset getBarDataset()
{
int[] y = {624,430,712};
XYSeries series1 = new XYSeries("A");
XYSeries series2 = new XYSeries("B");
XYSeries series3 = new XYSeries("C");
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
series1.add(1,y[0]);
dataset.addSeries(series1);
series2.add(2,y[1]);
dataset.addSeries(series2);
series3.add(3,y[2]);
dataset.addSeries(series3);
return dataset;
}
protected XYMultipleSeriesRenderer buildBarRenderer() {
XYMultipleSeriesRenderer mrenderer = new XYMultipleSeriesRenderer();
double[] range = {0,5,0,5};
mrenderer.setAxisTitleTextSize(16);
mrenderer.setChartTitleTextSize(20);
mrenderer.setLabelsTextSize(15);
mrenderer.setLegendTextSize(15);
mrenderer.setBarSpacing(0);
mrenderer.setXLabels(1);
mrenderer.addXTextLabel(2.5, "");
mrenderer.setMargins(new int[] {20, 30, 15, 0});
mrenderer.setAxesColor(Color.WHITE);
mrenderer.setChartTitle("");
mrenderer.setXTitle("");
mrenderer.setInitialRange(range, 1);
XYSeriesRenderer renderer1 = new XYSeriesRenderer();
renderer1.setColor(Color.GREEN);
XYSeriesRenderer renderer2 = new XYSeriesRenderer();
renderer2.setColor(Color.GREEN);
XYSeriesRenderer renderer3 = new XYSeriesRenderer();
renderer3.setColor(Color.BLUE);
mrenderer.addSeriesRenderer(renderer1);
mrenderer.addSeriesRenderer(renderer2);
mrenderer.addSeriesRenderer(renderer3);
return mrenderer;
}
}
here is the xml file
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainActivity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout android:id="@+id/graph2"
android:layout_width="160dip"
android:layout_height="200dip"
android:scaleType="fitEnd"
android:layout_marginTop="40dip"
android:layout_marginLeft="790dip"/>
</RelativeLayout>
Try it !! Enjoyyy