Display hours per hour in GraphView? - android

Using the GraphView library i found this part of code:
/*
* use Date as x axis label
*/
long now = new Date().getTime();
data = new GraphViewData[size];
for (int i=0; i<size; i++) {
data[i] = new GraphViewData(now+(i*60*60*24*1000), rand.nextInt(20)); // next day
}
exampleSeries = new GraphViewSeries(data);
if (getIntent().getStringExtra("type").equals("bar")) {
graphView = new BarGraphView(
this
, "GraphViewDemo"
);
} else {
graphView = new LineGraphView(
this
, "GraphViewDemo"
);
((LineGraphView) graphView).setDrawBackground(true);
}
graphView.addSeries(exampleSeries); // data
That displays the date like "Sept 16, Sept 20" Ect etc... Is there possible change this code and display hour per hour? Like "06.00, 07.00, 08.00" etc etc?

You shoud use SimpleDateFormat for date formatting:
SimpleDateFormat hoursDateFormat = new SimpleDateFormat("h.mm");
for (int i=0; i<size; i++) {
data[i] = new GraphViewData(hoursDateFormat.format(now+(i*60*60*24*1000)), rand.nextInt(20)); // next day
}

Related

I am getting single bar chart on stacked Barchart which I am adding values dynamically.I need to get all bar charts

I need to get all bars but I am getting single bar (last array).Even the logic is right.I don't know please help me.Thanks in advance.I search in google but I didn't got answer.
Below is main part of full code
Initialize
String empName[];
int converted[], notconverted[], notcontacted[];
Here I made Json parsing the data coming from the server
for (int i = 0; i < employees_array.length(); i++) {
empName = new String[employees_array.length()];
converted = new int[employees_array.length()];
notconverted = new int[employees_array.length()];
notcontacted = new int[employees_array.length()];
JSONObject empData = employees_array.getJSONObject(i);
empName[i] = empData.getString("EmployeeName");
empName[i].replace("-?\\d+.\\d+", " ");
if (empData.has("ConvertedActionCount")) {
converted[i] = employees_array.getJSONObject(i).getInt("ConvertedActionCount");
}
if (empData.has("NotConvertedActionCount")) {
notconverted[i] = employees_array.getJSONObject(i).getInt("NotConvertedActionCount");
}
if (empData.has("NotContactedActionCount")) {
notcontacted[i] = employees_array.getJSONObject(i).getInt("NotContactedActionCount");
}
Log.d("arraysize", converted[i] + "");
function_EMPChart(empArraysize, empName, converted,
notconverted, notcontacted);
I am passing values to function Emp chart method
private void function_EMPChart(int empArraysize, String[] s, int[]
converted, int[] notconverted, int[] notcontacted) {
XAxis xl = msc_managerDashboard_empStats.getXAxis();
xl.setPosition(XAxis.XAxisPosition.BOTTOM);
xl.setDrawAxisLine(true);
xl.setDrawGridLines(false);
xl.setGranularity(10f);
YAxis yl = msc_managerDashboard_empStats.getAxisLeft();
yl.setDrawAxisLine(true);
yl.setDrawGridLines(true);
yl.setAxisMinimum(0f); // this replaces setStartAtZero(true)
YAxis yr = msc_managerDashboard_empStats.getAxisRight();
yr.setDrawAxisLine(true);
yr.setDrawGridLines(false);
yr.setAxisMinimum(0f);
ArrayList<BarEntry> yVals = new ArrayList<BarEntry>();
ArrayList<String> xVals = new ArrayList<>();
xVals = new ArrayList<String>(Arrays.asList(s));
msc_managerDashboard_empStats.setFitBars(true);
for (int j = 0; j < s.length; j++) {
Log.d("checklength", j + "");
int value1 = converted[j];
int value2 = notconverted[j];
int value3 = notcontacted[j];
Log.d("getvalue", value1 + " " + value2 + " " + value3);
yVals.add(new BarEntry(j, new float[]{value1, value2, value3}));
yVals.add(new BarEntry(4, new float[]{10.0f, 20.0f, 30.0f}));
yVals.add(new BarEntry(5, new float[]{5.0f, 15.0f, 20.0f}));
Log.d("checky", yVals.add(new BarEntry(j, new float[]{converted[j], notconverted[j], notcontacted[j]})) + "");
}
BarDataSet set1 = new BarDataSet(yVals, "");
set1.setColors(getColors());
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
BarData data = new BarData(dataSets);
data.notifyDataChanged();
msc_managerDashboard_empStats.setData(data);
msc_managerDashboard_empStats.notifyDataSetChanged();
msc_managerDashboard_empStats.invalidate();
Log.d("yvalues", yVals + "");
msc_managerDashboard_empStats.setFitBars(true);
}
I guess you are using this library.
I don't really understand your code in what you try to accomplish (I see multiple bars per employee I guess).
At least what is wrong, you call setData within this for loop for (int i = 0; i < employees_array.length(); i++), which means you override the data every time.
You should create a list of BarDataSets in that for loop and set the data in the end.
Your main issue here is that you create a new set of data every time. If you keep the same set your code might also work. Define dataSets once in the beginning and keep adding your sets. I mean this line:
ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
dataSets.add(set1);
I guess you want to have a grouped bar chart which you can find here: https://github.com/PhilJay/MPAndroidChart/wiki/Setting-Data#grouped-barchart
Example:
YourData[] group1 = ...;
YourData[] group2 = ...;
List<BarEntry> entriesGroup1 = new ArrayList<>();
List<BarEntry> entriesGroup2 = new ArrayList<>();
// fill the lists
for(int i = 0; i < group1.length; i++) {
entriesGroup1.add(new BarEntry(i, group1.getValue()));
entriesGroup2.add(new BarEntry(i, group2.getValue()));
}
BarDataSet set1 = new BarDataSet(entriesGroup1, "Group 1");
BarDataSet set2 = new BarDataSet(entriesGroup2, "Group 2");
BarData data = new BarData(set1, set2);
data.setBarWidth(barWidth); // set the width of each bar
barChart.setData(data);
barChart.groupBars(1980f, groupSpace, barSpace); // perform the "explicit" grouping
barChart.invalidate(); // refresh

How to customize value for numbers in NumberPicker in android?

I have a number picker for setting a data limit in MB. right now, I have numberPicker, contains numeric values in sequence like [1,2,3,....., 2000 MB].
But I want a numberPicker that should contain numeric values like [100,200,300,...., 2000MB].
How can I do this?
Displayed array values for your picker
int NUMBER_OF_VALUES = 20; //num of values in the picker
int PICKER_RANGE = 100;
...
String[] displayedValues = new String[NUMBER_OF_VALUES];
//Populate the array
for(int i=0; i<NUMBER_OF_VALUES; i++)
displayedValues[i] = String.valueOf(PICKER_RANGE * (i+1));
/* OR: if the array is easy to be hard-coded, then just hard-code it:
String[] displayedValues = {"100", "200", "300", .....}; */
Set arr in your picker :
numPicker.setMinValue(0);
numPicker.setMaxValue(displayedValues.size()-1);
numPicker.setDisplayedValues(displayedValues);
get/set the value of the picker :
//To get the current value in the picker
choosenValue = displayedValues[numPicker.getValue()];
//To set a new value (let's say 150)
for( int i=0; i<displayedValues.length ; i++ )
if( displayedValues[i].equals("300") )
numPicker.setValue(i);
Try the code below
int start = 100;
String[] numbers = new String[20];
for(int i =0 ; i < 20 ; i++) {
numbers[i] = start + "";
start = start + 100;
}
NumberPicker numberPicker = (NumberPicker) findViewById(R.id.id_number_picker);
numberPicker.setMaxValue(20);
numberPicker.setMinValue(1);
numberPicker.setDisplayedValues(numbers);
final String[] nums = new String[21];
for(int i=0; i<nums.length; i++) {
nums[i] = Integer.toString((i+1)*100);
}
numberPicker.setMinValue(0);
numberPicker.setMinValue(19);
numberPicker.setDisplayedValues(nums);

How to move Date under X axis?

I am using PhilJay/MPAndroidChart library in my app and I'd like to know if 2 things are possible:
to show the last 14 days and then scroll for previous days?
Can the dates (currently on the top of the graph) be moved to the bottom, along the x axis?
Here is the current pic from the app and where i want to move date
my method:
private void populateGraph(Cursor data) {
ArrayList<Entry> cravingsPoints = new ArrayList<Entry>();
ArrayList<Entry> severityPoints = new ArrayList<Entry>();
ArrayList<String> dates = new ArrayList<String>();
int index = 0;
for (int i = 0; i < data.getCount(); i++) {
if (i == 0) {
// if orientation changed we need to start from the first one again
data.moveToFirst();
} else {
data.moveToNext();
}
try {
String date = data.getString(data.getColumnIndex(SmokeFreeContentProvider.DIARY_DATE));
int cravings = data.getInt(data.getColumnIndex(SmokeFreeContentProvider.DIARY_CRAVINGS_COUNT));
int severity = data.getInt(data.getColumnIndex(SmokeFreeContentProvider.DIARY_CRAVINGS_SEVERITY));
DateTime diaryEntry = DateTimeFormat.forPattern("yyyyMMdd").parseDateTime(date);
String entryLabel = diaryEntry.toString("dd MMM");
cravingsPoints.add(new Entry(cravings, index));
severityPoints.add(new Entry(severity, index));
dates.add(entryLabel);
index++;
} catch (Exception e) {
Log.e("SmokeFreeCravingsGraph", e.getMessage(), e);
}
}
LineDataSet cravingsLineData = new LineDataSet(cravingsPoints, getString(R.string.cravings));
LineDataSet severityLineData = new LineDataSet(severityPoints, getString(R.string.severity));
cravingsLineData.setCircleSize(4f);
cravingsLineData.setLineWidth(6f);
cravingsLineData.setColor(getResources().getColor(R.color.green));
severityLineData.setCircleSize(4f);
severityLineData.setLineWidth(6f);
severityLineData.setColor(getResources().getColor(android.R.color.holo_blue_light));
ArrayList<LineDataSet> dataSets = new ArrayList<LineDataSet>();
dataSets.add(cravingsLineData);
dataSets.add(severityLineData);
Paint infoPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
infoPaint.setTextAlign(Paint.Align.CENTER);
infoPaint.setTextSize(com.github.mikephil.charting.utils.Utils.convertDpToPixel(14f));
infoPaint.setColor(getResources().getColor(R.color.dark_grey));
mChart.setDrawGridBackground(false);
mChart.setDrawYValues(false);
mChart.setDescription("");
mChart.setStartAtZero(true);
mChart.setPaint(infoPaint, Chart.PAINT_INFO);
mChart.setNoDataText(getString(R.string.no_cravings_info));
mChart.setNoDataTextDescription(getString(R.string.no_cravings_full_info));
mChart.setData(new LineData(dates, dataSets));
}
Yes, check the documentation: Modifying the Viewport
Take a look at the setVisibleXRange(float xRange) method.
Yes, check the documentation: XAxis
Take a look at the xAxis.setPosition(...) method.

how add date to TimeSeries in LineGraph achartengine

I use AchartEngine to create a LineGraph.
I have the data from the database in the format:
dd-mm-yyyy count
for example:
01-05-2013 3
01-08-2013 7
01-11-2013 4
01-12-2013 15
...
code fragment
...
int values = new Date[myCursor.getCount()];
Date[] dat = new Date[myCursor.getCount()];
SimpleDateFormat dfIn = new SimpleDateFormat("yyyy-MM-dd");
do{
try {
dat[kk]=dfIn.parse(myCursor.getString(0));
values[kk]=myCursor.getInt(1);
}
catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
kk++;
}
while(myCursor.moveToNext());
...
TimeSeries series = new TimeSeries("Line1");
for( int i = 0; i < dat.length; i++)
{
series.add(datki[i], values[i]);
}
I have a questions:
Dates and values ​​I hold in separate tables.
How can I add to TimeSeries this data ???
Is it possible to add the date in the format mm-yyyy ???
Please help
If using chartfactory :
intent = ChartFactory.getTimeChartIntent(this, getDateDemoDataset(), getDemoRenderer(), "MM-yyyy");
If using timechart:
TimeChart chart = new TimeChart(dataset, renderer);
chart.setDateFormat("MM-yyyy");

Set String values on x-axis on graph?

I have a lineGraph with float values on both x-axis and y-axis? My question is that how can i set String values on x-axis like date or name of months.I am using GraphView-3.0.jar library.my code is
values_list.add(1.0);
values_list.add(5.0);
values_list.add(9.0);
values_list.add(12.0);
values_list.add(17.0);
values_list.add(1.0);
values_list.add(13.0);
values_list.add(23.0);
graphData = new GraphViewData[(values_list.size())];
double price_copy = 0;
for (int i = 0; i < values_list.size(); i++) {
price_copy = values_list.get(i);
graphData[i] = new GraphViewData(i, price_copy);
}
GraphView graphView;
graphView = new LineGraphView(this // context
, "" );// graphView.addSeries(exampleSeries);// data
graphView.addSeries(new GraphViewSeries("values",
new GraphViewStyle(Color.rgb(00, 00, 128), 3),
graphData));
graphView.setShowLegend(true);
// set view port, start=2, size=40
graphView.setViewPort(0, 10);
graphView.setScalable(true);
graphView.setScrollable(true);
LinearLayout layout = (LinearLayout) findViewById(R.id.LNL_CHART);
layout.addView(graphView);
Also i have no idea about AchartEngine,thanks in advance.
Use achart engine library. you can search for that on google. it helped me.Thanks
Just use the label formatter. You could store your x-labels as string in an array.
For example:
final String[] xLabels = new String[] {
"foo", "bar", "third", "bla", "more"
};
GraphView graphView = new LineGraphView(this, "example") {
   #Override
   protected String formatLabel(double value, boolean isValueX) {
      if (isValueX) {
         return xLabels[(int) value];
      } else {
         // return y label as number
         return super.formatLabel(value, isValueX); // let the y-value be normal-formatted
      }
   }
};

Categories

Resources