我在条形图中显示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>
答案 0 :(得分:3)
我将所有系列更改为XYSeries
并将其简单地添加到dataset
,而无需调用额外的方法。
XYSeries series1 = new XYSeries("A");
dataset.addSeries(series1);
然后,添加像常规XYChart中的数据,如下所示:
series1.add(1, value);
series2.add(2, value);