MPAndroidChart v3.0.0-beta1 LineData constructor only takes 1 argument - android

I'm trying to follow this guide:
https://github.com/PhilJay/MPAndroidChart/wiki/Setting-Data
It has the line of code:
LineData data = new LineData(xVals, dataSets);
When I tried it in my AndroidStudio, the LineData() constructor only takes 1 argument instead of 2 arguments as shown in the guide.
Anyone know how to initialize with the new version of MPAndroidChart ?
I have the following in my gradle setting:
compile 'com.github.PhilJay:MPAndroidChart:v3.0.0-beta1'
Maybe I should drop to 2.2.5 ?
Update
Yes, I changed gradle to use 2.2.5 and now LineData() constructor has multiple parameters as expected.
Docs needs updating for version 3.0.

in this version
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
try it
public class MainActivity extends AppCompatActivity {
private LineChart lc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
public int ran() {
Random ran = new Random();
int i = ran.nextInt(199);
return i;
}
public int ran2() {
Random ran = new Random();
int i = ran.nextInt(49);
return i;
}
public void initData() {
lc.setExtraOffsets(12,50,24,0); //padding
setDescription("two lines example");
lc.animateXY(500, 0);
setLegend();
setYAxis();
setXAxis();
setChartData();
}
public void setLegend() {
Legend legend = lc.getLegend();
legend.setForm(Legend.LegendForm.LINE);
legend.setFormSize(20);
legend.setTextSize(20f);
legend.setFormLineWidth(1);
legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
legend.setTextColor(Color.BLACK);
}
public void setDescription(String descriptionStr) {
Description description = new Description();
description.setText(descriptionStr);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
Paint paint = new Paint();
paint.setTextSize(20);
float x = outMetrics.widthPixels - Utils.convertDpToPixel(12);
float y = Utils.calcTextHeight(paint, descriptionStr) + Utils.convertDpToPixel(12);
description.setPosition(x, y);
lc.setDescription(description);
}
public void setYAxis() {
final YAxis yAxisLeft = lc.getAxisLeft();
yAxisLeft.setAxisMaximum(200);
yAxisLeft.setAxisMinimum(0);
yAxisLeft.setGranularity(10);
yAxisLeft.setTextSize(12f);
yAxisLeft.setTextColor(Color.BLACK);
yAxisLeft.setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
return value == yAxisLeft.getAxisMinimum() ? (int) value + "" : (int) value +"";
}
});
lc.getAxisRight().setEnabled(false);
}
public void setXAxis() {
XAxis xAxis = lc.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setDrawGridLines(false);
xAxis.setLabelCount(20);
xAxis.setTextColor(Color.BLACK);
xAxis.setTextSize(12f);
xAxis.setGranularity(1);
xAxis.setAxisMinimum(0);
xAxis.setAxisMaximum(100);
xAxis.setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axis) {
return value == 0 ? "example" : (int) value + "";
}
});
}
public void setChartData() {
List<Entry> yVals1 = new ArrayList<>();
for (int i = 0; i < 100; i++) {
int j = ran();
yVals1.add(new Entry(1 + i,j));
}
List<Entry> yVals2 = new ArrayList<>();
for (int i = 0; i < 100; i++) {
int j = ran2();
yVals2.add(new Entry(1 + i,j));
}
LineDataSet lineDataSet1 = new LineDataSet(yVals1, "ex1");
lineDataSet1.setValueTextSize(20);
lineDataSet1.setDrawCircleHole(true);
lineDataSet1.setColor(Color.MAGENTA);
lineDataSet1.setMode(LineDataSet.Mode.LINEAR);
lineDataSet1.setDrawCircles(true);
lineDataSet1.setCubicIntensity(0.15f);
lineDataSet1.setCircleColor(Color.MAGENTA);
lineDataSet1.setLineWidth(1);
LineDataSet lineDataSet2 = new LineDataSet(yVals2, "ex2");
lineDataSet2.setValueTextSize(20);
lineDataSet2.setDrawCircleHole(true);
lineDataSet2.setColor(Color.BLUE);
lineDataSet2.setMode(LineDataSet.Mode.LINEAR);
lineDataSet2.setDrawCircles(true);
lineDataSet2.setCubicIntensity(0.15f);
lineDataSet2.setCircleColor(Color.BLUE);
lineDataSet2.setLineWidth(1);
.
.
.
ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
dataSets.add(lineDataSet1);
dataSets.add(lineDataSet2);
LineData lineData = new LineData(dataSets);
lc.setVisibleXRangeMaximum(5);
lc.setScaleXEnabled(true);
lc.setData(lineData);
}
and like this.

Version 3.0 is initialized like so:
LineChart lineChart = new LineChart(context);
lineChart.setMinimumHeight(ToolBox.dpToPixels(context, 300));
lineChart.setMinimumWidth(ToolBox.getScreenWidth());
ArrayList<Entry> yVals = new ArrayList<>();
for(int i = 0; i < frigbot.getEquipment().getTemperatures().size(); i++)
{
Temperature temperature = frigbot.getEquipment().getTemperatures().get(i);
yVals.add(new Entry(
i, temperature.getValue().floatValue()
));
}
LineDataSet dataSet = new LineDataSet(yVals, "graph name");
dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
dataSet.setCubicIntensity(0.2f);
LineData data = new LineData(dataSet);
lineChart.setData(data);
It appears we can't specify custom horizontal labels, LineChart itself will automatically generate the horizontal and vertical axis labelling.

Related

How to make the CombinedChart-BarChart average distributed on the x-axis?

I use MpAndroidChart-CombinedChart in my project,but I met a problem,please look the follow screenshot.
As you saw,the X axis right part didn't filled.In other words,I want set the bar space,but I can't.
I try barData.setBarWidth(0.4f),it didn't work,it will make the bar width Larger,but it will not make bar space lager.
public static void initCombinedChart(List<FieldFragment.CombinedBean> list, CombinedChart combinedChart) {
if (list == null || list.size() == 0) return;
combinedChart.setPinchZoom(false);
combinedChart.setDragEnabled(false);
combinedChart.setTouchEnabled(false);
combinedChart.getLegend().setEnabled(false);
combinedChart.getDescription().setEnabled(false);
XAxis xAxis = combinedChart.getXAxis();
xAxis.setDrawGridLines(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setYOffset(5);
xAxis.setAvoidFirstLastClipping(true);
YAxis leftYAxis = combinedChart.getAxisLeft();
leftYAxis.setLabelCount(6, true);
leftYAxis.setDrawAxisLine(false);
leftYAxis.setDrawGridLines(true);
leftYAxis.setGridColor(Color.parseColor("#DDDDDD"));
leftYAxis.setXOffset(10f);
leftYAxis.setAxisMinimum(0);
leftYAxis.setSpaceBottom(0);
leftYAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
YAxis rightYAxis = combinedChart.getAxisRight();
rightYAxis.setSpaceBottom(0);
rightYAxis.setDrawAxisLine(false);
rightYAxis.setDrawGridLines(true);
rightYAxis.setAxisMinimum(0);
rightYAxis.setXOffset(10);
rightYAxis.setLabelCount(6, true);
rightYAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
List<Entry> lineEntries = new ArrayList<>();
List<BarEntry> barEntries = new ArrayList<>();
List<String> xValues = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
lineEntries.add(new Entry(i, list.get(i).getRightYValue()));
barEntries.add(new BarEntry(i, list.get(i).getLeftYValue()));
xValues.add(list.get(i).getxValue());
}
xAxis.setValueFormatter(new IAxisValueFormatter() {
#Override
public String getFormattedValue(float value, AxisBase axisBase) {
try {
int position = (int) value;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse(xValues.get(position));
SimpleDateFormat format = new SimpleDateFormat("MM.dd");
return format.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
});
// 折线数据
LineDataSet lineDataSet = new LineDataSet(lineEntries, "");
lineDataSet.setDrawValues(false);
lineDataSet.setDrawCircles(false);
lineDataSet.setLineWidth(3f);
lineDataSet.setColor(Color.parseColor("#00c2bd"));
lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
lineDataSet.setDrawFilled(true); //这个不要忘了
lineDataSet.setFillColor(Color.parseColor("#00c2bd"));
lineDataSet.setFillAlpha(50);
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
List<ILineDataSet> lineDataSets = new ArrayList<>();
lineDataSets.add(lineDataSet);
LineData lineData = new LineData(lineDataSets);
// 柱子数据
BarDataSet barDataSet = new BarDataSet(barEntries, "");
barDataSet.setDrawValues(false);
barDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
barDataSet.setColor(Color.parseColor("#1490ee"));
BarData barData = new BarData(barDataSet);
barData.setBarWidth(0.4f); //值越大柱子越宽
// 解决bar显示不全问题
float barWidth = barData.getBarWidth() / 2;
xAxis.setAxisMinimum(-barWidth);
xAxis.setAxisMaximum(xValues.size() - barWidth);
CombinedData combinedData = new CombinedData();
combinedData.setData(lineData);
combinedData.setData(barData);
combinedChart.setData(combinedData);
combinedChart.invalidate();
}

Is it possible to get a combined line and bar graph using mpandroidchartlibrary-2-2-4

Please help me with the code for generating a combined Line and bar graph with mpandroidchartlibrary-2-2-4.
Please find the Code
main.java
for (int win = 0; win < sprintArray.length; win++) {
labels.add(win, window_List.get(win));
System.out.println("WINDOW LIST VALUE IN CHART" + window_List.get(win).toString());
}
for (int window = 0; window < sprintArray.length; window++) {
float wc_entry = Float.parseFloat(workcompleted_list.get(window).toString());
float wr_entry = Float.parseFloat(workremaining_list.get(window).toString());
float wa_entry = Float.parseFloat(workadded_List.get(window).toString());
System.out.println("WC VALUE"+workcompleted_list);
System.out.println("WR VALUE"+workremaining_list);
System.out.println("WA VALUE"+workadded_List);
wc_group.add(new BarEntry(window+3,wc_entry));
wr_group.add(new BarEntry(window+3,wr_entry));
wa_group.add(new BarEntry(window+3,wa_entry));
}
BarDataSet barDataSet1 = new BarDataSet(wc_group, "Work Completed");
barDataSet1.setColor(Color.rgb(217, 227, 184));
barDataSet1.setDrawValues(false);
BarDataSet barDataSet2 = new BarDataSet(wr_group, "Work Remaining");
barDataSet2.setColor(Color.rgb(107, 161, 202));
barDataSet2.setDrawValues(false);
BarDataSet barDataSet3 = new BarDataSet(wa_group, "Work Added");
barDataSet3.setColor(Color.rgb(77, 124, 159));
barDataSet3.setDrawValues(false);
final BarData data = new BarData(barDataSet1);
data.addDataSet(barDataSet2);
data.addDataSet(barDataSet3);
data.setBarWidth(0.45f);
barChart.setData(data);
barChart.setNoDataText("No Data Found!!!");
barChart.setNoDataTextColor(Color.YELLOW);
barChart.setFitBars(true);
barChart.animateY(2000);
barChart.setDrawGridBackground(false);
barChart.setDrawBorders(true);
barChart.getAxisRight().setEnabled(false);
barChart.setDescription(null);
barChart.getAxisLeft().setStartAtZero(true);
barChart.setVisibleXRange(2,10);
barChart.getAxisRight().setStartAtZero(true);
barChart.groupBars(2,0.72f,0.04f);
XAxis xAxis = barChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextSize(8);
barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(labels)); //labels list have values "original estimate,1,2,3,4,5"
xAxis.setLabelCount(labels.size());
xAxis.setDrawGridLines(false);
xAxis.setAxisLineColor(Color.BLACK);
xAxis.setAxisLineColor(Color.rgb(255, 51, 153));
barChart.setDrawValueAboveBar(false);
YAxis yAxis = barChart.getAxisLeft();
yAxis.setDrawAxisLine(false);
yAxis.setDrawGridLines(false);
yAxis.setLabelCount(5);
}
IndexAxisValueFormatter.java
this is uded to set the labels
public class IndexAxisValueFormatter implements IAxisValueFormatter {
private String[] mValues = new String[] {};
private int mValueCount = 0;
#Override
public String getFormattedValue(float value, AxisBase axis) {
int index = Math.round(value);
if (index < 0 || index >= mValueCount || index != (int)value)
return "";
return mValues[index];
}
public IndexAxisValueFormatter() {
}
public IndexAxisValueFormatter(String[] values) {
if (values != null)
setValues(values);
}
public IndexAxisValueFormatter(Collection<String> values) {
if (values != null)
setValues(values.toArray(new String[values.size()]));
}
public String[] getValues()
{
return mValues;
}
public void setValues(String[] values)
{
if (values == null)
values = new String[] {};
this.mValues = values;
this.mValueCount = values.length;
}
}
When i run this code only "4 and 5 " labels are displayed on the chart in reverse order. cannot see remaining labels
Why You don't use v3.0.2?
Visit this link, there is example how to do combined chart.
https://github.com/PhilJay/MPAndroidChart/blob/master/MPChartExample/src/com/xxmassdeveloper/mpchartexample/CombinedChartActivity.java

How to show labels on right and values to left side in HorizontalBarChart?

Drawing a horizontal bar chart using MPAndroidChart 3.0.2. the values are shown on the right of the bars. I could use setValueFormatter and use IAxisValueFormatter interface to display the labels on the right. But the values are not displayed now.
{
HorizontalBarChart barChart = (HorizontalBarChart) itemView.findViewById(R.id.barChart);
BarData data = new BarData();
ArrayList<BarEntry> valueSet1 = new ArrayList<>();
ArrayList<String> labels = new ArrayList<>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
ArrayList<String> ylabels = new ArrayList<>();
int dataCount=0;
for (int i=0;i<6;++i) {
BarEntry entry = new BarEntry(dataCount,(i+1)*2);
valueSet1.add(entry);
ylabels.add(" "+i);
dataCount++;
}
List<IBarDataSet> dataSets = new ArrayList<>();
BarDataSet bds = new BarDataSet(valueSet1, " ");
bds.setColors(ColorTemplate.MATERIAL_COLORS);
String[] xAxisLabels = labels.toArray(new String[0]);
String[] yAxisLabels = ylabels.toArray(new String[0]);
bds.setStackLabels(xAxisLabels);
dataSets.add(bds);
data.addDataSet(bds);
data.setDrawValues(true);
data.setBarWidth(0.4f);
XAxis xaxis = barChart.getXAxis();
xaxis.setDrawGridLines(false);
xaxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xaxis.setGranularityEnabled(true);
xaxis.setGranularity(1);
xaxis.setDrawLabels(true);
xaxis.setLabelCount(dataCount+1);
xaxis.setXOffset(10);
xaxis.setDrawAxisLine(false);
CategoryBarChartXaxisFormatter xaxisFormatter = new CategoryBarChartXaxisFormatter(xAxisLabels);
xaxis.setValueFormatter(xaxisFormatter);
YAxis yAxisLeft = barChart.getAxisLeft();
yAxisLeft.setEnabled(false);
YAxis yAxisRight = barChart.getAxisRight();
yAxisRight.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
yAxisRight.setDrawGridLines(false);
yAxisRight.setDrawAxisLine(false);
Legend legend = barChart.getLegend();
legend.setEnabled(false);
barChart.setFitBars(true);
barChart.setData(data);
barChart.setDescription(null);
}
public class CategoryBarChartXaxisFormatter implements IAxisValueFormatter {
private String[] mValues;
public CategoryBarChartXaxisFormatter(String[] values) {
this.mValues = values;
}
#Override
public String getFormattedValue(float value, AxisBase axis) {
int val = (int)value;
String label="";
if(val>=0 && val<mValues.length) {
label= mValues[val];
}
else {
label= "";
}
return label;
}
}
requirement is to display label strings on the right and a number on the left corresponding to each bar.
I did check some stack overflow and google it but didn't find anything that works so far.
i get this
but this is an example of my requirement
Do appreciate some help.
Thanks for your time
I implemented something similar just a few days back. Here's the code...
public class MainActivity extends AppCompatActivity {
protected HorizontalBarChart mChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> labels = new ArrayList<>();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
mChart = (HorizontalBarChart) findViewById(R.id.barChart);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
mChart.getDescription().setEnabled(false);
mChart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
XAxis xl = mChart.getXAxis();
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
xl.setDrawAxisLine(true);
xl.setDrawGridLines(false);
CategoryBarChartXaxisFormatter xaxisFormatter = new CategoryBarChartXaxisFormatter(labels);
xl.setValueFormatter(xaxisFormatter);
xl.setGranularity(1);
YAxis yl = mChart.getAxisLeft();
yl.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
yl.setDrawGridLines(false);
yl.setEnabled(false);
yl.setAxisMinimum(0f);
YAxis yr = mChart.getAxisRight();
yr.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
yr.setDrawGridLines(false);
yr.setAxisMinimum(0f);
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < 6; i++) {
yVals1.add(new BarEntry(i, (i+1)*10));
}
BarDataSet set1;
set1 = new BarDataSet(yVals1, "DataSet 1");
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setBarWidth(.9f);
mChart.setData(data);
mChart.getLegend().setEnabled(false);
}
public class CategoryBarChartXaxisFormatter implements IAxisValueFormatter {
ArrayList<String> mValues;
public CategoryBarChartXaxisFormatter(ArrayList<String> values) {
this.mValues = values;
}
#Override
public String getFormattedValue(float value, AxisBase axis) {
int val = (int) value;
String label = "";
if (val >= 0 && val < mValues.size()) {
label = mValues.get(val);
} else {
label = "";
}
return label;
}
}
}
RESULT
Hope this helps.

Same BarData shows different in BarChart and CombinedChart

I am using MPAndroidChart2.2.5 to draw charts in my project,which contains 2 group of BarDataand 1 group of LineData.When I use the BarData in Barchart,the bars shows well.But when I use the same BarData in CombinedChart,the position of the bars differs.Here's my activity.
Here's a screenshot:
.
public class CombinedChartActivity extends BaseActivity {
private CombinedChart combinedChart;
private BarChart barChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_combined_chart);
combinedChart = (CombinedChart) findViewById(R.id.chart);
barChart = (BarChart) findViewById(R.id.barChart);
initChart(combinedChart);
initChart(barChart);
barChart.setData(getBarData());
CombinedData combinedData = new CombinedData();
combinedData.setXVals(getXVals());
combinedData.setData(getLineData());
combinedData.setData(getBarData());
combinedChart.setData(combinedData);
}
private void initChart(BarLineChartBase barLineChartBase) {
barLineChartBase.setDescription("");
barLineChartBase.getAxisLeft().setEnabled(false);
barLineChartBase.getAxisRight().setEnabled(false);
barLineChartBase.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
barLineChartBase.getXAxis().setGridColor(R.color.white);
barLineChartBase.getXAxis().setAvoidFirstLastClipping(true);
barLineChartBase.setBackgroundColor(Color.WHITE);
}
private BarData getBarData() {
List<IBarDataSet> iBarDataSets = new ArrayList<>();
ArrayList<BarEntry> barEntries = new ArrayList<>();
for (int i = 0; i < 7; i++) {
BarEntry barEntry = new BarEntry(i + 1, i);
barEntries.add(barEntry);
}
BarDataSet barDataSet = new BarDataSet(barEntries, "Bar1");
barDataSet.setColor(Color.rgb(60, 220, 78));
barDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
ArrayList<BarEntry> barEntries2 = new ArrayList<>();
for (int i = 0; i < 7; i++) {
BarEntry barEntry = new BarEntry((float) (i + Math.random() * 10), i);
barEntries2.add(barEntry);
}
BarDataSet barDataSet1 = new BarDataSet(barEntries2, "Bar2");
barDataSet1.setColor(R.color.level_3);
barDataSet1.setAxisDependency(YAxis.AxisDependency.RIGHT);
iBarDataSets.add(barDataSet);
iBarDataSets.add(barDataSet1);
BarData barData = new BarData(getXVals(), iBarDataSets);[![enter image description here][1]][1]
return barData;
}
private LineData getLineData() {
ArrayList<Entry> entries = new ArrayList<>();
for (int i = 0; i < 7; i++) {
Entry entry = new Entry((float) (Math.random() * 10), i);
entries.add(entry);
}
List<ILineDataSet> iLineDataSets = new ArrayList<>();
LineDataSet lds1 = new LineDataSet(entries, "Line");
lds1.setLineWidth(0.2f);
lds1.setColor(R.color.level_3);
lds1.setDrawFilled(true);
lds1.setFillAlpha(150);
lds1.setFillColor(R.color.level_2);
lds1.setDrawValues(false);
lds1.setHighlightEnabled(true);
lds1.setDrawHorizontalHighlightIndicator(false);
lds1.setHighLightColor(R.color.white);
lds1.setDrawCubic(true);
lds1.setDrawCircles(true);
lds1.setCircleColor(R.color.white);
lds1.setCircleColorHole(R.color.theme_orange);
lds1.setAxisDependency(YAxis.AxisDependency.LEFT);
iLineDataSets.add(lds1);
LineData lineData = new LineData(getXVals(), iLineDataSets);
return lineData;
}
private List<String> getXVals() {
ArrayList<String> xVals = new ArrayList<>();
xVals.add("Monday");
xVals.add("Tuesday");
xVals.add("Wednesday");
xVals.add("Thursday");
xVals.add("Friday");
xVals.add("Saturday");
xVals.add("Sunday");
return xVals;
}
}
I use same BarData in both charts.What causes the difference.How can I make the bars in Combinedchart show as in the Barchart?

No data appears on BarChart for X entries when you use y-values as floats

I have tried to add a BarChart in my Code, nothing appear when I launch my app.
The purpose of my code is that: when the user enter the start date (StartDate), end date (EndDate) and the product name, the graph should show the show price evolution during that period.
I have tried the code below, but nothing appear on the graph.
Please help to find the issue, or propose to me another code.
public class ResultProductStatisticsActivity extends AppCompatActivity {
private String mCategory;
private String mName;
private long mStartDate;
private long mEndDate;
private int mNumber = 0;
private ProgressBar mProgressBar;
private ArrayList<ShopDetail> mDetail = new ArrayList<ShopDetail>();
private ArrayList<Float> mPriceList = new ArrayList<Float>();
protected Typeface mTfLight;
private Typeface mTypeFaceLight;
private TextView mMontextViewtest;
private BarChart mChart;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.result_product_statistics_activity);
mChart = (BarChart) findViewById(R.id.m_barchart);
// mChart.setOnChartValueSelectedListener(this);
mChart.setDrawBarShadow(false);
mChart.setDrawValueAboveBar(true);
mChart.setDescription("");
// if more than 60 entries are displayed in the chart, no values will be
// drawn
mChart.setMaxVisibleValueCount(60);
// scaling can now only be done on x- and y-axis separately
mChart.setPinchZoom(false);
mChart.setDrawGridBackground(false);
// mChart.setDrawYLabels(false);
AxisValueFormatter xAxisFormatter = new DayAxisValueFormatter(mChart);
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTypeface(mTfLight);
xAxis.setDrawGridLines(false);
xAxis.setGranularity(1f); // only intervals of 1 day
xAxis.setLabelCount(7);
xAxis.setValueFormatter(xAxisFormatter);
AxisValueFormatter custom = new MyAxisValueFormatter();
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTypeface(mTfLight);
leftAxis.setLabelCount(8, false);
leftAxis.setValueFormatter(custom);
leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
leftAxis.setSpaceTop(15f);
leftAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setDrawGridLines(false);
rightAxis.setTypeface(mTfLight);
rightAxis.setLabelCount(8, false);
rightAxis.setValueFormatter(custom);
rightAxis.setSpaceTop(15f);
rightAxis.setAxisMinValue(0f); // this replaces setStartAtZero(true)
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.BELOW_CHART_LEFT);
l.setForm(Legend.LegendForm.SQUARE);
l.setFormSize(9f);
l.setTextSize(11f);
l.setXEntrySpace(4f);
// l.setExtra(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
// "def", "ghj", "ikl", "mno" });
// l.setCustom(ColorTemplate.VORDIPLOM_COLORS, new String[] { "abc",
// "def", "ghj", "ikl", "mno" });
init();
mChart.setMarkerView(new XYMarkerView(this, xAxisFormatter));
}
private void init(){
mCategory = Myrecord.getSearchShopCategory(this);
mName = Myrecord.getSearchShopName(this);
mStartDate = Myrecord.getSearchStartTime(this);
mEndDate = Myrecord.getSearchEndTime(this);
if(mStartDate == -1){
mStartDate = Long.MIN_VALUE;
}
if(mEndDate == -1){
mEndDate = Long.MAX_VALUE;
}
Thread thread = new Thread(){
#Override
public void run() {
MyDB db = new MyDB(ResultProductStatisticsActivity.this);
ArrayList<MyDetail> detailList = db.getMyDetailByAll(mCategory, mName, -1, 0, -1);
for(int i = 0; i < detailList.size(); i ++){
MyDetail detail = detailList.get(i);
long date = detail.date;
if(date >= mStartDate && date <= mEndDate){
mPriceList.add(db.getPriceGoodsByOwnName("ALEVONS")+0f);
}
}
ResultProductStatisticsActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
setData(mPriceList);
}
});
}
};
thread.start();
}
private void setData(ArrayList<Float> mPriceList) {
mChart.getXAxis().setAxisMinValue(mStartDate);
mChart.getXAxis().setAxisMaxValue(mEndDate);
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < mPriceList.size(); i++) {
yVals1.add(new BarEntry(i , mPriceList.get(i)));
}
BarDataSet set1;
if (mChart.getData() != null &&
mChart.getData().getDataSetCount() > 0) {
set1 = (BarDataSet) mChart.getData().getDataSetByIndex(0);
set1.setValues(yVals1);
mChart.getData().notifyDataChanged();
mChart.notifyDataSetChanged();
} else {
set1 = new BarDataSet(yVals1, "Evolution du prix produit");
set1.setColors(ColorTemplate.MATERIAL_COLORS);
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.setValueTextSize(10f);
data.setValueTypeface(mTfLight);
data.setBarWidth(0.9f);
mChart.setData(data);
}
}
}
A wild guess, without any more details provided: your're setting the x-axis min and max values to the start and end date. I'm guessing this is encoded in unix time, so you have something like 1470614400 and 1471219200.
Now looking at your data, it will start with 0 and go up to the number of prices you have in your price list (mPriceList.size()). So with 10 prices we'd have values at 0 through 9. However, since your x-axis starts at 1470614400, no data will be shown (although the axis, gridlines etc. should be visible).
So in order to fix your code (depending on how your price list is structured, this example assumes one price every hour), you'd need to change your code in setData to this:
for (int i = 0; i < mPriceList.size(); i++) {
yVals1.add(new BarEntry(mStartDate + i * 3600, mPriceList.get(i)));
}
If you have a list of dates associated with the price values, you can also simply do this:
for (int i = 0; i < mPriceList.size(); i++) {
yVals1.add(new BarEntry(mDateList.get(i), mPriceList.get(i)));
}
We the reply of(#TR4Android),
I have update my code such below and I can get the graph:
float start = 0f;
mChart.getXAxis().setAxisMinValue(start);
mChart.getXAxis().setAxisMaxValue(mPriceList.size());
for (int i = 0; i < mPriceList.size(); i++) {
yVals1.add(new BarEntry(i , mPriceList.get(i)));
}
But I have a bit issue with start date end date where each item has been bought. I have put each date in array list but I don't see how I can put it on xAxis. The Array list is :
private ArrayList<Long> mDateList = new ArrayList<Long>();
One example of mDateList value is 2016/08/13

Categories

Resources