ArrayList in Graphiew - android

I have two double ArrayList (ArrayList) how can I pass these arrayLists to GraphView to draw my linear chart?
for limited points we can use these codes :
GraphView graph = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
but my ArrayLists have thousands points!

You can do like,
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(X[0], Y[0]),
new DataPoint(X[1], Y[1]),
new DataPoint(X[2], Y[2]),
new DataPoint(X[3], Y[3]),
new DataPoint(X[4], Y[4])
});
OR
If u have bulk of data means use `for loop`
this may helps you.

If you want to populate the LineGraphSeries with 1000 point then you should consider to define a list and pass this when you construct the object of the
LineGraphSeries class
Example
List<DataPoint> myPointList = ....
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(Arrays.asList(myPointList));
....

Related

How can i draw lines of ranges in a line graph using grahpview on android?

I'm new using the GraphView Library but i need to show a graph like this below
i'd been tried modifying some demos from GraphView but idk how to do it
https://drive.google.com/file/d/1qi4HAPqBeewZ9bHfsRjuc0lmWhW9SbxU/view?usp=sharing
I code a workarround solution using the code below.
i'm just hidding the default grid lines and drawing straight lines changing his colors
#Override
public void initGraph(GraphView graph) {
DataPoint[] minPoints = new DataPoint[30];
DataPoint[] maxPoints = new DataPoint[30];
DataPoint[] teoricPoints = new DataPoint[30];
DataPoint[] basePoint = new DataPoint[1];
for (int i = 0; i < 30; i++) {
//points[i] = new DataPoint(i, Math.sin(i*0.5) * 20*(Math.random()*10+1));
minPoints[i] = new DataPoint(i, 20);
maxPoints[i] = new DataPoint(i, 45);
teoricPoints[i] = new DataPoint(i, 30);
}
basePoint[0] = new DataPoint(0, 0);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(minPoints);
LineGraphSeries<DataPoint> series2 = new LineGraphSeries<>(maxPoints);
LineGraphSeries<DataPoint> series3 = new LineGraphSeries<>(teoricPoints);
LineGraphSeries<DataPoint> series4 = new LineGraphSeries<>(basePoint);
graph.getGridLabelRenderer().setGridColor(Color.RED);
graph.getGridLabelRenderer().setHighlightZeroLines(false);
graph.getGridLabelRenderer().setHorizontalLabelsColor(Color.GREEN);
graph.getGridLabelRenderer().setVerticalLabelsColor(Color.RED);
graph.getGridLabelRenderer().setVerticalLabelsVAlign(GridLabelRenderer.VerticalLabelsVAlign.ABOVE);
graph.getGridLabelRenderer().setGridStyle(GridLabelRenderer.GridStyle.NONE);
graph.getGridLabelRenderer().reloadStyles();
// styling viewport
graph.getViewport().setBackgroundColor(Color.argb(255, 222, 222, 222));
graph.getViewport().setDrawBorder(true);
graph.getViewport().setBorderColor(Color.BLUE);
// styling series
series.setTitle("Random Curve 1");
series.setColor(Color.GREEN);
series.setThickness(8);
series2.setTitle("Random Curve 2");
series2.setColor(Color.GREEN);
series2.setThickness(8);
series3.setTitle("Random Curve 3");
series3.setColor(Color.GREEN);
series3.setThickness(8);
series4.setTitle("Random Curve 4");
series4.setColor(Color.GREEN);
series4.setThickness(8);
graph.getLegendRenderer().setFixedPosition(150, 0);
graph.addSeries(series);
graph.addSeries(series2);
graph.addSeries(series3);
graph.addSeries(series4);
}

JJOE64 Android graphview set LineGraphSeries Dynamically

Hi I am trying to draw a chart with this library but its entries static like:
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);
How can i fix this that accepts ex. a list view elements that created on runtime ?
Basicly I want something like this:
for (int i = 0; i < list.size(); i++) {
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(i, list.getElement()),
});
}
Try something like this:
DataPoint[] dataPoints = new DataPoint[list.size()]; // declare an array of DataPoint objects with the same size as your list
for (int i = 0; i < list.size(); i++) {
// add new DataPoint object to the array for each of your list entries
dataPoints[i] = new DataPoint(i, list.getElement()); // not sure but I think the second argument should be of type double
}
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(dataPoints); // This one should be obvious right? :)

How to set up a legend with Graphview in Android Studio

I have a screen on my app that display the following graph:
Is there a way to change my legend so that it can say what each color represents? Currently I can get the blue square to show up but it doesn't represent any of the numbers. Here is the code I'm using when creating the graph:
GraphView graph = (GraphView) findViewById(R.id.graph);
BarGraphSeries<DataPoint> series = new BarGraphSeries<DataPoint>(new DataPoint[]{
new DataPoint(1, personal),
new DataPoint(2, fun),
new DataPoint(3, work),
new DataPoint(4, food),
new DataPoint(5, commute),
new DataPoint(6,bills)
});
graph.setTitle("Expenses");
graph.addSeries(series);
graph.getLegendRenderer().setVisible(true);
graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);
series.setValueDependentColor(new ValueDependentColor<DataPoint>() {
#Override
public int get(DataPoint data) {
return Color.rgb((int) data.getX() * 255 / 4, (int) Math.abs(data.getY() * 255 / 6), 100);
}
});
series.setSpacing(50);
series.setDrawValuesOnTop(true);
series.setValuesOnTopColor(Color.RED);
backToMainMenu();
}
I don't think you need to customize the legend. The LegendRenderer class uses the title of each series of data to display what the bars on the chart represent.
series.setTitle("This will display in the legend.");
However, you only have one series of data in this example. If you must have labels in the legend for each bar, I suggest adding multiple series with different titles to your chart. Each series will have its own title and color.
// Each series represents one bar.
BarGraphSeries<DataPoint> series1 = new BarGraphSeries<DataPoint>(new DataPoint[]{ new DataPoint(1, personal)});
BarGraphSeries<DataPoint> series2 = new BarGraphSeries<DataPoint>(new DataPoint[]{ new DataPoint(2, fun)});
BarGraphSeries<DataPoint> series3 = new BarGraphSeries<DataPoint>(new DataPoint[]{ new DataPoint(3, work)});
BarGraphSeries<DataPoint> series4 = new BarGraphSeries<DataPoint>(new DataPoint[]{ new DataPoint(4, food)});
BarGraphSeries<DataPoint> series5 = new BarGraphSeries<DataPoint>(new DataPoint[]{ new DataPoint(5, commute)});
BarGraphSeries<DataPoint> series6 = new BarGraphSeries<DataPoint>(new DataPoint[]{ new DataPoint(6, bills)});
// Add titles to be displayed in the legend.
series1.setTitle("personal");
series2.setTitle("fun");
series3.setTitle("work");
series4.setTitle("food");
series5.setTitle("commute");
series6.setTitle("bills");
// Add color to your bars.
series1.setColor(Color.rbg(1,2,3));
series2.setColor(Color.rbg(4,5,6));
series3.setColor(Color.rbg(7,8,9));
series4.setColor(Color.rbg(0,1,2));
series5.setColor(Color.rbg(3,4,5));
series6.setColor(Color.rbg(6,7,8));
// Add each series to your graph.
graph.addSeries(series1);
graph.addSeries(series2);
graph.addSeries(series3);
graph.addSeries(series4);
graph.addSeries(series5);
graph.addSeries(series6);
// Display the legend.
graph.getLegendRenderer().setVisible(true);
you could use a custom legend renderer and extend the default implementation.
https://github.com/jjoe64/GraphView/blob/master/src/main/java/com/jjoe64/graphview/LegendRenderer.java
Or you disable the legend in graphview, use a FrameLayout and overlay the graph with your very own views for your legend

Android drawing graph using graph view

I have a graph that is not a real time graph, i want to add the x-axis and y-axis from the data that i have.
GraphView line_graph = (GraphView) findViewById(R.id.graph3);
LineGraphSeries<DataPoint> line_series =
new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, 0),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
line_graph.addSeries(line_series);
i was just testing the graph with the above points. And now i have an array received from database using JSON, i want to use them as the x-axis data and y-axis data. Just like array[oddNumber] as x-axis data, array[evenNumber] as y-axis data. Is there any way to do that?
Get your JSON data and convert it in Integer array like xAxis and yAxis.
Now do like this,
Integer[] xAxis = new Integer[]{0, 1, 2, 3, 4};
Integer[] yAxis = new Integer[]{0, 5, 3, 2, 6};
GraphView line_graph = (GraphView) findViewById(R.id.graph3);
DataPoint[] dataPoints = new DataPoint[xAxis.length];
for (int i = 0; i < xAxis.length; i++)
{
dataPoints[i] = new DataPoint(xAxis[i],yAxis[i]);
}
LineGraphSeries<DataPoint> line_series =
new LineGraphSeries<DataPoint>(dataPoints);
line_graph.addSeries(line_series);
Note: Length of xAxis and yAxis array must be same.

How to replace the numbers on the bottom of Bar GraphView with strings?

I'm using jjoe64 GraphView library for creating a bar GraphView in my app.
When I'm creating a new DataPoint, it requires 2 values: X int, and a Y int, like this:
GraphView graph = (GraphView) findViewById(R.id.graph);
BarGraphSeries<DataPoint> series = new BarGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, -1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);
What can I do to replace the X int on the bottom with Strings?
EDIT #1
What you call "bottom" is "X-axis". To label it use
StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph);
staticLabelsFormatter.setHorizontalLabels(new String[] {"old", "middle", "new"});
graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);
Further reading: Documentation
Or if you use GraphView 3.x:
graphView.setHorizontalLabels(new String[] {"0.5", "1", "1.5", "2.0"});

Categories

Resources