See picture below, trying to figure out why they it is skipping the '1', '3', and so forth.
Where i set the series and graph:
DataPoint[] dataPoints = new DataPoint[rankList.size()]; // declare an array of DataPoint objects with the same size as your list
for (int i = 0; i < rankList.size(); i++) {
dataPoints[i] = new DataPoint(i, Double.parseDouble(rankList.get(i))); // not sure but I think the second argument should be of type double
}
BarGraphSeries<DataPoint> series2 = new BarGraphSeries<DataPoint>(dataPoints); // This one should be obvious right? :)
series2.setAnimated(true);
series2.setTitle("Random Curve 1");
series2.setColor(Color.GREEN);
series2.setSpacing(30);
series2.setDataWidth(1);
graph2.getViewport().setMinX(-1);
graph2.getViewport().setMaxX(12);
graph2.addSeries(series2);
The right info is being plotted, but i've tried a bunch of stuff from the docs and get get it to work.
Sorry I misunderstood your question...
This will help:
yourGraph.getGridLabelRenderer().setNumHorizontalLabels(numberOfBars);
It actually shows all bars. You've set series2.setSpacing(30);.
Instead do series2.setSpacing(0);.
Related
I am facing some weird problems with my (Line) GraphView!
To explain it a bit better I'll add a pic that shows what happens when I start scrolling horizontal (vertical scrolling is not needed).
The thing is:
The DataPoints are displayed correctly, but not my labels! I wrote the correct label in red color into the pictures above!
Whenever I start scrolling the labels start to "jump" around weirdly.
The DataPoints are received from a SharedPreference file with String/Integer pairs [Strings are always MMMyyyy (f.e. "Mar2020")]
I get the pairs out of my SP-file, put the Strings as static labels into my graph use the DataPoints in this way "logical" way (not the actual code!)
DataPoint[0] = (0, valueOfKey0)
DataPoint[1] = (1, valueOfKey1)
Here is the code how the graph is called for / drawn in the Activity
private void setupGraph(final GraphView graph, String subName){
GraphDataHelper graphDataHelper = new GraphDataHelper(mContext);
LineGraphSeries<DataPoint> series;
/*Have to call .removeAllSeries to draw each graph individually
* Otherwise they will be drawn over another!*/
graph.removeAllSeries();
/*Gets Values from each Sub
* --> Gets values form each key/value pair of specific SharedPreference-file
* --> sets values in order (0, 1, 2,... n)*/
series = new LineGraphSeries<DataPoint>(graphDataHelper.generateGraphData(subName));
series.setDrawDataPoints(true);
series.setColor(getResources().getColor(R.color.casualWhite));
series.setAnimated(true);
graph.addSeries(series);
/*Fill Labels for X-Axis:
* Get the specific Monthly-Keys for the choosen Sub
* In special cases extra rules are declared in .getSpecificSubMonthKeys() method
* Put all entries of ArrayList in Array (StaticLabelsFormatter only works with "normal" Array)*/
ArrayList<String> array_paths = graphDataHelper.getSpecificSubMonthKeys(subName);
int size = array_paths.size();
String[] paths = new String[size];
for (int i=0; i<size; i++){
paths[i] = array_paths.get(i);
}
StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph);
staticLabelsFormatter.setHorizontalLabels(paths);
graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);
graph.getViewport().setXAxisBoundsManual(true);
if(size == 5 || size > 5){
graph.getViewport().setMinX(0.0);
graph.getViewport().setMaxX(4.0);
graph.getGridLabelRenderer().setNumHorizontalLabels(5);
} else if(size == 4){
graph.getViewport().setMinX(0.0);
graph.getViewport().setMaxX(3.0);
graph.getGridLabelRenderer().setNumHorizontalLabels(4);
} else if(size == 3){
graph.getViewport().setMinX(0.0);
graph.getViewport().setMaxX(2.0);
graph.getGridLabelRenderer().setNumHorizontalLabels(3);
} else if(size == 2){
graph.getViewport().setMinX(0.0);
graph.getViewport().setMaxX(1.0);
graph.getGridLabelRenderer().setNumHorizontalLabels(2);
}
graph.getViewport().setScrollable(true);
graph.getGridLabelRenderer().setHorizontalLabelsColor(getResources().getColor(R.color.midBlue));
graph.getGridLabelRenderer().setVerticalLabelsColor(getResources().getColor(R.color.midBlue));
graph.getGridLabelRenderer().setGridColor(getResources().getColor(R.color.darkBlue));
graph.getGridLabelRenderer().setPadding(50);
graph.getGridLabelRenderer().setLabelsSpace(30);
I call for .removeAllSeries because the User is able to call for another graph via a dropdown-Spinner.
Also I set different MinX & MaxX Points because there can be the case that an entry doesn't have 5 DataPoints yet and so I change the Viewport in regard of that (didn't find a better solution for that so I am also open for some better code here, but it does work this way). My SharedPreference-file always has a minimun of 2 key/value pairs
Also little extra question: Is there a way to give ONLY the horizontal labels some extra labelspace. If I use setLabelSpace it will also apply on the vertical labels and I do not want that. But the horizontal labels are just so close to the axis.
Also sorry for ugly code. It was my first time using GraphView!
Thanks ahead!
The following code is what I have pieced together from articles on here but im really stumped what next to try
ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
for (int i = 1; i < 4; i++){
bitmapArray.add(BitmapFactory.decodeFile("/storage/emulated/0/Pictures/" + Global.svar7));
canvas.drawBitmap(bitmapArray[i], 0, 0, null);
}
Basically I am trying to draw 3 bitmaps to the canvas one below the other within a loop
This is the nearest I have got with just one error on the Canvas.drawBitmap line saying that bitmapArray[i] has the error
The type of the expression must be an array type but it resolved to ArrayList
I have search this error on here and can only find examples that include strings and setting controls
Any ideas? can you please point me in the right direction?
Your help is Greatly appreciated
Mark
To access items in an ArrayList, use ArrayList.get():
canvas.drawBitmap(bitmapArray.get(i-1), 0, 0, null);
You'll need to subtract 1 from the index because you are starting your loop from 1 not 0.
You try to use ArrayList as Array. replace bitmapArray[i] with bitmapArray.get(i).
Also you should consider that array and List indexes start from 0, not from 1, so you need also to replace for (int i = 1; i < 4; i++){ with for (int i = 0; i < 3; i++){
I am relatively new to Android and i want to create an array that increases in size and i declared an array like this:
private Sprite something[]; //sprite is my custom class that does sprites
And i want to add elements like this, but it gives me error...
something[i++] = new Sprite(spriteSheet,numRows,numColumns,true,x,y);
//i is number of elements and Sprite(...) is the constructor
Can someone tell me what i am doing wrong here, and why i get the zygote error and a null pointer exception?
Thanks!
Java doesn't really work like Javascript does. Arrays have fixed size, as per something = new Sprite[10]; where the element indices of something are 0 to 9.
The functionality you want can however be achieved using List<Sprite> something = new ArrayList<Sprite>();, and then something.add(new Sprite(spriteSheet, numRows, numColumns, true, x, y));.
EDIT:
Iteration of an ArrayList works like this:
List<Sprite> sprites = new ArrayList<Sprite>();
for(Sprite sprite : sprites)
{
sprite.print();
}
However, the following statement:
List<Sprite> sprites = new ArrayList<Sprite>();
for(Sprite sprite : sprites)
{
sprites.remove(sprite);
}
Will throw a ConcurrentModificationException.
Therefore, you can either use an iterator manually:
List<Sprite> sprites = new ArrayList<Sprite>();
Iterator<Sprite> iterator = sprites.iterator();
while(iterator.hasNext())
{
Sprite sprite = iterator.next();
...
iterator.remove();
}
Or, personally when I need to modify the elements on the list during iteration, I use the following:
List<Sprite> sprites = new ArrayList<Sprite>();
for(int i = 0; i < sprites.size(); i++)
{
Sprite sprite = sprites.get(i);
...
sprites.remove(i--);
}
Although if I remember correctly, the iterator is the recommended approach.
I have a problem with the visualization of a line graph, as you can see in the chart the x poins don't match exactly with the y point, they are slighty righter.
On Apr 12, 2012 the x value must be exactly 2
On Apr 13, 2012 the x value must be exactly 3
On Apr 16, 2012 the x value must be exactly 6
Has anyone else met my same proplem?
Can someone help me?
My code is very simple, I have a list of object, each element of which contains both data value and int value and I put these value in the two different arrays that will be used to draw the chart. I'm using achartengine-07 library.
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
XYMultipleSeriesRenderer aRenderer = new XYMultipleSeriesRenderer();
Date[] x = new Date[list.size()];
for(int j=0;j<list.size();j++){
x[j] = list.get(j).getData();
}
int[] y = new int[list.size()];
for(int j=0;j<list.size();j++){
y[j] =Integer.parseInt(list.get(j).getRank());
}
TimeSeries series = new TimeSeries(h[i]);
for(int k = 0; k <x.length; k++)
series.add(x[k], y[k]);
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setPointStyle(PointStyle.SQUARE);
renderer.setFillPoints(true);
dataset.addSeries(series);
aRenderer.setXLabels(x.length);
aRenderer.setYLabels(y.length);
aRenderer.addSeriesRenderer(renderer);
The time chart in AChartEngine tries to compute the best "rounded" labels like everyday midnight or so.
If you don't like this approach, you could build a regular line chart with custom labels:
renderer.addXTextLabel();
I builded a regular line chart, like suggested by Dan, with custom labels and to remove the previous label I put setXLabels(0):
aRenderer.addXTextLabel();
// To remove the previous label put
aRenderer.setXLabels(0);
I have a 2D game that uses two integer arrays to track x and y coordinates as shown below:
private int gridX[] = { 0,0,0,0,0 }
private int gridY[] = { 0,0,0,0,0 }
The problem is I can have a LOT of objects on the screen that needs to be tracked. Is there a way to add integers / create new blocks as needed? IE in a loop, do something like
gridX[].add(); or something like that. I'm relatively new to java and droid development and I'm having trouble finding a good tutorial or example that shows how to do this without having to initialize the gridX and gridY to sizes of 100 or so.
This is important, as I am about 90% sure that all those unused 0's are causing androids garbage cleanup to lag my application.
Why dont you use an array list instead of an Integer array?
that way you can dynamically add items to the list
ArrayList<Integer> myList = new ArrayList<Integer>();
myList.add(1);
myList.add(2);
Why not also use the Point class?
List<Point> points = new ArrayList<Point>();
points.add(new Point(0, 0));
points.add(new Point(50, 70));
Point point = points.get(1);
Log.d("MyApp", String.format("The point is: (%d, %d)", point.x, point.y);
This way you are keeping track of your x and y coordinates together and there is less opportunity for error.