我在条形图中显示 4 个值,其中每个条的颜色不同。当我显示图表时,每个条都相互重叠。我想setBarSpacing()
通过放置不同的值来在我尝试过的条之间留出空间,但它仍然不起作用。有没有其他想法。
package com.example.barchartview;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
@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,323};
CategorySeries series1 = new CategorySeries("A");
CategorySeries series2 = new CategorySeries("B");
CategorySeries series3 = new CategorySeries("C");
CategorySeries series4 = new CategorySeries("D");
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
series1.add(y[0]);
dataset.addSeries(series1.toXYSeries());
series2.add(y[1]);
dataset.addSeries(series2.toXYSeries());
series3.add(y[2]);
dataset.addSeries(series3.toXYSeries());
series4.add(y[3]);
dataset.addSeries(series4.toXYSeries());
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.RED);
XYSeriesRenderer renderer3 = new XYSeriesRenderer();
renderer3.setColor(Color.BLUE);
XYSeriesRenderer renderer4 = new XYSeriesRenderer();
renderer4.setColor(Color.CYAN);
mrenderer.addSeriesRenderer(renderer1);
mrenderer.addSeriesRenderer(renderer2);
mrenderer.addSeriesRenderer(renderer3);
mrenderer.addSeriesRenderer(renderer4);
return mrenderer;
}
}
这是我的代码。
这是我的 activity_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>