use of com.github.mikephil.charting.data.BarDataSet in project MPAndroidChart by PhilJay.
the class HorizontalBarNegativeChartActivity method setData.
private void setData(int count, float range) {
float barWidth = 9f;
float spaceForBar = 10f;
ArrayList<BarEntry> values = new ArrayList<>();
for (int i = 0; i < count; i++) {
float val = (float) (Math.random() * range - range / 2);
values.add(new BarEntry(i * spaceForBar, val, getResources().getDrawable(R.drawable.star)));
}
BarDataSet set1;
if (chart.getData() != null && chart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
set1.setValues(values);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(values, "DataSet 1");
set1.setDrawIcons(false);
ArrayList<IBarDataSet> dataSets = new ArrayList<>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setValueTypeface(tfLight);
data.setBarWidth(barWidth);
chart.setData(data);
}
}
use of com.github.mikephil.charting.data.BarDataSet in project MPAndroidChart by PhilJay.
the class ListViewBarChartActivity method generateData.
/**
* generates a random ChartData object with just one DataSet
*
* @return Bar data
*/
private BarData generateData(int cnt) {
ArrayList<BarEntry> entries = new ArrayList<>();
for (int i = 0; i < 12; i++) {
entries.add(new BarEntry(i, (float) (Math.random() * 70) + 30));
}
BarDataSet d = new BarDataSet(entries, "New DataSet " + cnt);
d.setColors(ColorTemplate.VORDIPLOM_COLORS);
d.setBarShadowColor(Color.rgb(203, 203, 203));
ArrayList<IBarDataSet> sets = new ArrayList<>();
sets.add(d);
BarData cd = new BarData(sets);
cd.setBarWidth(0.9f);
return cd;
}
use of com.github.mikephil.charting.data.BarDataSet in project MPAndroidChart by PhilJay.
the class BarChartActivityMultiDataset method onProgressChanged.
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
float groupSpace = 0.08f;
// x4 DataSet
float barSpace = 0.03f;
// x4 DataSet
float barWidth = 0.2f;
// (0.2 + 0.03) * 4 + 0.08 = 1.00 -> interval per "group"
int groupCount = seekBarX.getProgress() + 1;
int startYear = 1980;
int endYear = startYear + groupCount;
tvX.setText(String.format(Locale.ENGLISH, "%d-%d", startYear, endYear));
tvY.setText(String.valueOf(seekBarY.getProgress()));
ArrayList<BarEntry> values1 = new ArrayList<>();
ArrayList<BarEntry> values2 = new ArrayList<>();
ArrayList<BarEntry> values3 = new ArrayList<>();
ArrayList<BarEntry> values4 = new ArrayList<>();
float randomMultiplier = seekBarY.getProgress() * 100000f;
for (int i = startYear; i < endYear; i++) {
values1.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
values2.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
values3.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
values4.add(new BarEntry(i, (float) (Math.random() * randomMultiplier)));
}
BarDataSet set1, set2, set3, set4;
if (chart.getData() != null && chart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) chart.getData().getDataSetByIndex(0);
set2 = (BarDataSet) chart.getData().getDataSetByIndex(1);
set3 = (BarDataSet) chart.getData().getDataSetByIndex(2);
set4 = (BarDataSet) chart.getData().getDataSetByIndex(3);
set1.setValues(values1);
set2.setValues(values2);
set3.setValues(values3);
set4.setValues(values4);
chart.getData().notifyDataChanged();
chart.notifyDataSetChanged();
} else {
// create 4 DataSets
set1 = new BarDataSet(values1, "Company A");
set1.setColor(Color.rgb(104, 241, 175));
set2 = new BarDataSet(values2, "Company B");
set2.setColor(Color.rgb(164, 228, 251));
set3 = new BarDataSet(values3, "Company C");
set3.setColor(Color.rgb(242, 247, 158));
set4 = new BarDataSet(values4, "Company D");
set4.setColor(Color.rgb(255, 102, 0));
BarData data = new BarData(set1, set2, set3, set4);
data.setValueFormatter(new LargeValueFormatter());
data.setValueTypeface(tfLight);
chart.setData(data);
}
// specify the width each bar should have
chart.getBarData().setBarWidth(barWidth);
// restrict the x-axis range
chart.getXAxis().setAxisMinimum(startYear);
// barData.getGroupWith(...) is a helper that calculates the width each group needs based on the provided parameters
chart.getXAxis().setAxisMaximum(startYear + chart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
chart.groupBars(startYear, groupSpace, barSpace);
chart.invalidate();
}
use of com.github.mikephil.charting.data.BarDataSet in project MPAndroidChart by PhilJay.
the class BarChartActivityMultiDataset method onOptionsItemSelected.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.viewGithub:
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartActivityMultiDataset.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues:
{
for (IBarDataSet set : chart.getData().getDataSets()) set.setDrawValues(!set.isDrawValuesEnabled());
chart.invalidate();
break;
}
case R.id.actionTogglePinch:
{
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
chart.setPinchZoom(true);
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax:
{
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionToggleBarBorders:
{
for (IBarDataSet set : chart.getData().getDataSets()) ((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
chart.invalidate();
break;
}
case R.id.actionToggleHighlight:
{
if (chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionSave:
{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
case R.id.animateX:
{
chart.animateX(2000);
break;
}
case R.id.animateY:
{
chart.animateY(2000);
break;
}
case R.id.animateXY:
{
chart.animateXY(2000, 2000);
break;
}
}
return true;
}
use of com.github.mikephil.charting.data.BarDataSet in project MPAndroidChart by PhilJay.
the class AnotherBarActivity method onOptionsItemSelected.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.viewGithub:
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/AnotherBarActivity.java"));
startActivity(i);
break;
}
case R.id.actionToggleValues:
{
for (IDataSet set : chart.getData().getDataSets()) set.setDrawValues(!set.isDrawValuesEnabled());
chart.invalidate();
break;
}
/*
case R.id.actionToggleIcons: { break; }
*/
case R.id.actionToggleHighlight:
{
if (chart.getData() != null) {
chart.getData().setHighlightEnabled(!chart.getData().isHighlightEnabled());
chart.invalidate();
}
break;
}
case R.id.actionTogglePinch:
{
if (chart.isPinchZoomEnabled())
chart.setPinchZoom(false);
else
chart.setPinchZoom(true);
chart.invalidate();
break;
}
case R.id.actionToggleAutoScaleMinMax:
{
chart.setAutoScaleMinMaxEnabled(!chart.isAutoScaleMinMaxEnabled());
chart.notifyDataSetChanged();
break;
}
case R.id.actionToggleBarBorders:
{
for (IBarDataSet set : chart.getData().getDataSets()) ((BarDataSet) set).setBarBorderWidth(set.getBarBorderWidth() == 1.f ? 0.f : 1.f);
chart.invalidate();
break;
}
case R.id.animateX:
{
chart.animateX(2000);
break;
}
case R.id.animateY:
{
chart.animateY(2000);
break;
}
case R.id.animateXY:
{
chart.animateXY(2000, 2000);
break;
}
case R.id.actionSave:
{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
saveToGallery();
} else {
requestStoragePermission(chart);
}
break;
}
}
return true;
}
Aggregations