gpt4 book ai didi

android - 如何为图表引擎条形图中的每个条形分配空间?

转载 作者:太空狗 更新时间:2023-10-29 15:54:10 29 4
gpt4 key购买 nike

我正在开发安卓系统。我在谷歌中搜索了一些关于条形图的链接并尝试了它们。它运作良好。我对此有些怀疑。

enter image description here

我的代码:

package in.wptrafficanalyzer.achartenginebarchart;

import org.achartengine.ChartFactory;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.MultipleCategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

private String[] mMonth = new String[] { "Jan", "Feb", "Mar", "Apr", "May",
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Getting reference to the button btn_chart
Button btnChart = (Button) findViewById(R.id.btn_chart);

// Defining click event listener for the button btn_chart
OnClickListener clickListener = new OnClickListener() {

@Override
public void onClick(View v) {
// Draw the Income vs Expense Chart
openChart();
}
};

// Setting event click listener for the button btn_chart of the
// MainActivity layout
btnChart.setOnClickListener(clickListener);

}

private void openChart() {
int[] x = { 0, 1, 2, 3, 4, 5, 6, 7 };
int[] income = { 2000, 2500, 2700, 3000, 2800, 3500, 3700, 3800 };
// int[] expense = {2200, 2700, 2900, 2800, 2600, 3000, 3300, 3400 };

// Creating an XYSeries for Income
// CategorySeries incomeSeries = new CategorySeries("Income");
XYSeries incomeSeries = new XYSeries("Income");
// Creating an XYSeries for Income
// XYSeries expenseSeries = new XYSeries("Expense");
// Adding data to Income and Expense Series
for (int i = 0; i < x.length; i++) {
incomeSeries.add(i, income[i]);
// expenseSeries.add(i,expense[i]);
}

// Creating a dataset to hold each series
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
// Adding Income Series to the dataset
dataset.addSeries(incomeSeries);
// Adding Expense Series to dataset
// dataset.addSeries(expenseSeries);

// Creating XYSeriesRenderer to customize incomeSeries
XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();
incomeRenderer.setColor(Color.rgb(130, 130, 230));
incomeRenderer.setFillPoints(true);
incomeRenderer.setLineWidth(2);
incomeRenderer.setDisplayChartValues(true);

// Creating XYSeriesRenderer to customize expenseSeries
// XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
// expenseRenderer.setColor(Color.rgb(220, 80, 80));
// expenseRenderer.setFillPoints(true);
// expenseRenderer.setLineWidth(2);
// expenseRenderer.setDisplayChartValues(true);

// Creating a XYMultipleSeriesRenderer to customize the whole chart
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setXLabels(0);
multiRenderer.setChartTitle("Income vs Expense Chart");
multiRenderer.setXTitle("Year 2012");
multiRenderer.setYTitle("Amount in Dollars");
multiRenderer.setZoomButtonsVisible(true);
for (int i = 0; i < x.length; i++) {
multiRenderer.addXTextLabel(i, mMonth[i]);
}

// Adding incomeRenderer and expenseRenderer to multipleRenderer
// Note: The order of adding dataseries to dataset and renderers to
// multipleRenderer
// should be same
multiRenderer.addSeriesRenderer(incomeRenderer);
// multiRenderer.addSeriesRenderer(expenseRenderer);

// Creating an intent to plot bar chart using dataset and
// multipleRenderer
Intent intent = ChartFactory.getBarChartIntent(getBaseContext(),
dataset, multiRenderer, Type.DEFAULT);

// Start Activity
startActivity(intent);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

实际上我想让图表看起来像单独的条形图,但这里所有的条形图都组合在一起了。我怎样才能通过代码分离到单独的条形图。请帮我解决这个问题。将非常感谢。

最佳答案

按如下方式使用 barSpacing 选项

multiRenderer.setBarSpacing(1);

关于android - 如何为图表引擎条形图中的每个条形分配空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17165823/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com