Basically i want to draw a graph where x-axis gives you the date and y axis gives u the count
I am getting data from server and parsing it in my two array's one is for count and other is for date
i am getting values in two array like this
SoapObject o=(SoapObject)p2.getProperty(xx);
UserCallStatusBean ld=new UserCallStatusBean();
if(o.getProperty("Month").toString().equals("anyType{}")){
ld.setDuration("");
}
else{
ld.setDuration(o.getProperty("Month").toString());
}
ld.setCount(Integer.parseInt(o.getProperty("Count").toString()));
CallStatus.add(ld);
GraphViewData o1=new GraphViewData(_iLoopCounter,Double.parseDouble(o.getProperty("Count").toString()));
String o2=new String(o.getProperty("Month").toString());
//String o2=new String(String.valueOf(_iLoopCounter));
arrData[xx]=o1;
arrXaxis[xx]=o2;
_iLoopCounter++;
After Getting the Value in two array i use Graph View Demo to create a grapg like
setContentView(R.layout.graphs);
// init example series data
exampleSeries = new GraphViewSeries(arrData);
// graph with dynamically genereated horizontal and vertical labels
GraphView graphView;
graphView = new BarGraphView(
this,"");
graphView.addSeries(exampleSeries); // data
graphView.setScalable(true);
graphView.setScrollable(true);
graphView.setViewPort(2,6);
graphView.setScrollable(true);
graphView.setHorizontalLabels(arrXaxis);
graphView.setGravity(Gravity.CENTER);
graphView.setBaselineAlignedChildIndex(4);
//graphView.setHorizontalLabels(labels);
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(graphView);
Now My issue of concern is i am getting value of count on y-axis but i am also getting count of date on x-axis instead i want to show the date on x-axis
I Have read various articles to get string value on x-axis ,finded one to use Cutom lavel formatter , But didnt knw how to use so what i can do to get x-axis ? (Date Value)
I hope u understands ,Expecting Answer Pleasue will b aLl Mine
Thanks in advance
Related
I need to draw graph like the image i have uploaded , i am using MP chart library and develop a graph but want to customize it according to my requirement but not able to find solution for my requirements my basic requirement is for x axis i want to show custom values at axis like 5-11 12-18 but i am passing value to x axis like this
private ArrayList<String> setXAxisValues() {
ArrayList<String> xVals = new ArrayList<String>();
xVals.add("10");
xVals.add("20");
xVals.add("30");
xVals.add("30.5");
xVals.add("40");
return xVals;
}
So it is showing x values like this 10 20 30 so on so i want my graph to be built upon using these x value which is happening right now but want to show custom value at bottom like 5-11 etc and this value is dynamic coming from Api response so please help me about this , Waiting for positive and early response Thanks in Advance
To format the x-axis values, you should use the setValueFormatter method which takes a callback interface were you have to implement the getFormattedValue method which is called before drawing the x-axis value.
xAxis.setValueFormatter((value, axis) -> {
String res = "";
try {
// get current position of x-axis
int currentPosition = (int) value;
// check if position between array bounds
if (currentPosition > -1 && currentPosition < maxSize) {
// get value from formatted values array
res = xVals.get(currentPosition);
}
} catch (Exception e) {
// handle exception
}
return res;
});
I have a LineSeries
RadCartesianChartView chart = new RadCartesianChartView(context);
CartesianChartGrid cartesianChartGrid = new CartesianChartGrid();
cartesianChartGrid.setMajorYLinesRenderMode(GridLineRenderMode.INNER_AND_LAST);
cartesianChartGrid.setMajorXLinesRenderMode(GridLineRenderMode.INNER_AND_LAST);
cartesianChartGrid.setLineThickness(1);
cartesianChartGrid.setLineColor(Color.CYAN);
chart.setGrid(cartesianChartGrid);
LineSeries series = new LineSeries();
series.setStrokeColor(Color.GRAY);
But line is shown continuous without any point denoting the data point.
How can I show circles as data points on line.
To show the data points you have to add some methods in series.
series.setShowLabels(true);
And in case you need you can format labels in any java format you need
series.setLabelFormat("%.0f");
Hope it helps
I am using jjoe64 Graph View in my android application.
and I am trying to show dynamic values in graph but first time it contains no values but graph is not appearing.
give me solution.
http://www.android-graphview.org/
https://github.com/jjoe64/GraphView
Can you show us your code?
Have you tried something like
graph = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>();
graph.addSeries(series);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setYAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(0);
graph.getViewport().setMinY(0);
graph.getViewport().setMaxY(4);
graph.onDataChanged(true, true);
There's a bug report logged for this one, and it appears the issue will be fixed in the next release (4.0.1).Anyway I simply coded around the issue, by displaying some text, indicating there's no data at that point eg.
// If there is no data, let the user know, else blank the textBox.
TextView fragmentText = (TextView) v.findViewById(R.id.history_text);
if ( xLabels.size() < 2 ){
fragmentText.setTextSize(24);
fragmentText.setText(getString(R.string.history_graph_no_data));
} else {
// Remove the "No Data" text
fragmentText.setText("");
LinearLayout layout = (LinearLayout) v.findViewById(R.id.history_graph);
addGraphToLayout(layout);
}
in my app I'm using GraphView library to create a LineGraphView. It shows data from specified dates from example from 18.01 to 30.01. When I change the boundaries for example from 25.01 to 29.01 GraphView updates the chart (cuts not needed lines) but still shows wrong x axis data (18.01 to 30.01), however when user starts to interact with chart (zooming, scrolling) it updates it's x axis and everything is fine. Is there a method to force GraphView to update it's x axis? I have only encountered this problem if user has been interacting with chart (zooming, scrolling) before changing dates if there was no interaction graphview updates properly. I'm creating charts this way:
Inside onCreate:
mGraphView = new LineGraphView(this, "Temperature Measurements");
And a function to create a chart when I fetch data from database:
public void createChart(Cursor cursor) {
GraphViewData[] values = new GraphViewData[cursor.getCount()];
while (cursor.moveToNext()) {
String time = cursor.getString(cursor
.getColumnIndex(MeasurementEntry.DATE_IN_MINUTES));
String temp = cursor.getString(cursor
.getColumnIndex(MeasurementEntry.TEMPERATURE));
values[cursor.getPosition()] = new GraphViewData(
Double.parseDouble(time), Double.parseDouble(temp));
}
mGraphView.removeAllSeries();
mGraphView.addSeries(new GraphViewSeries(values));
mGraphView.setScalable(true);
mGraphView.getGraphViewStyle().setTextSize(15);
final SimpleDateFormat formatter = new SimpleDateFormat(
"dd.MM.yyyy HH:mm");
mGraphView.setCustomLabelFormatter(new CustomLabelFormatter() {
#Override
public String formatLabel(double value, boolean isValueX) {
if (isValueX) {
return formatter.format(new Date((long) value));
}
return null;
}
});
}
And here is the screenshot with the problem:
OK, so I made it however I think it can be done better, what I done is removing graphView from parent layout, creating new graphview and adding new graphview - I do this sequence everytime user is changing the boundaries. Maybe it will help somebody (short snippet of createChart method, container is my parent layout):
container.removeView(mGraphView);
mGraphView = new LineGraphView(this, "Temperature Measurements");
container.addView(mGraphView);
mGraphView.removeAllSeries();
mGraphView.addSeries(new GraphViewSeries(values));
mGraphView.setScalable(true);
mGraphView.getGraphViewStyle().setTextSize(15);
I have 4 textview such as t11,t12,t13,t14 and I have also 4 value in array val[4].
I want to store these values randomly in textviews. but I am getting little problem.
I have done following code:
TextView t11,t12,t13,t14;
Random r = new Random();
for (int i = 0; i < val.length; i++) {
int val[4]=r.nextInt(10);
Log.d("horror", "Randm Array of VAL:" +val[i]);
}
In the Log,there are 4 values displayed but how to display them in textviews.
I have coded but it does not work properly.
t1[i+1].setText("" +val[i]);
and
In this case,values are properly displayed, but i want to do code optimization.
t11.setText("" +val[0]);
t12.setText("" +val[1]);
t13.setText("" +val[2]);
t14.setText("" +val[3]);
Thanks in advance.
Every time you loop in for, you create another integer array. Take the definition of val out of the for loop.
you can store their references inside an array , it won't create new objects. so this should do the job
TextView [] textviews = {t11,t12,t13,t14};
for(int i =0;i<textviews.length;++i){
textviews[i].setText(val[i]);
}
For your TextView use something like,
TextView [] tv = {t11,t12,t13,t14};
and use tv for other going stuff... So now, you can getting it work by,
tv[i+1].setText("" +val[i]);