How to implement graph view in android studio using GraphView Library - android

I want Show a graph exactly like this.
I am using GraphView Library version 4.x.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// we get graph view instance
GraphView graphview = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> bgseries= new LineGraphSeries<>(new DataPoint[]{
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6),
new DataPoint(5, 1),
new DataPoint(6, 5),
new DataPoint(7, 3),
new DataPoint(8, 2),
new DataPoint(9, 6)
});
graphview.addSeries(bgseries);
// LineGraphSeries<DataPoint> series2 = new LineGraphSeries<DataPoint>(new DataPoint[] {
// new DataPoint(0, 3),
// new DataPoint(1, 3),
// new DataPoint(2, 6),
// new DataPoint(3, 2),
// new DataPoint(4, 5)
// });
//graphview.addSeries(series2);
//graphview.setDrawBackground(true);
graphview.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright));
graphview.getViewport().setScalable(true);
graphview.getViewport().setScrollable(true);
graphview.setTitle("BloodSugar");
graphview.setTitleColor(getResources().getColor(android.R.color.white));
// legend
bgseries.setTitle("foo");
bgseries.setThickness(20);
bgseries.setDrawBackground(true);
bgseries.setBackgroundColor(R.color.red_bg);
// series2.setTitle("bar");
// series2.setColor(R.color.red_bg);
graphview.getLegendRenderer().setVisible(true);
graphview.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP); }
}
I tried alot to set a color on line.I can increase the thickness .also i want a curved one other than spikes.nothing is successfull.
Can you please help me?

For any one else landing on this page in seek of above kind of graph, following library's class CurveGraphWithRegionActivity.java might come useful.
http://devcodemarket.com/android-app-source-code-android-hz-grapher

Related

Change text color in Graphview in android

this is picture of GraphView.enter image description here
As you see Texts are black color. I want change this color to white. How can i change of this texts?
public class MainActivity extends AppCompatActivity {
GraphView graph;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
graph = (GraphView) findViewById(R.id.graph);
StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph);
staticLabelsFormatter.setHorizontalLabels(new String[] {"Yan", "Fev", "Mart", "Apr", "May", "Iyn",
"Iyl", "Avq", "Sent", "Okt", "Noy", "Dek"});
//staticLabelsFormatter.setVerticalLabels(new String[] {"low", "middle", "high", "koko"});
graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, 2),
new DataPoint(1,5),
new DataPoint(2, 4),
new DataPoint(3, 4),
new DataPoint(4, 8),
new DataPoint(5, 6),
new DataPoint(6, 8),
new DataPoint(7, 1),
new DataPoint(8, 5),
new DataPoint(9, 2),
new DataPoint(10, 7),
new DataPoint(11, 3),
});
series.setColor(Color.YELLOW);
series.setDrawBackground(true);
series.setDrawDataPoints(true);
graph.addSeries(series);
}
I also encountered this problem, unfortunately, due to lack of documentation of GraphView methods, I was on the verge of shifting to MPAndroidChart but I thought of giving one more try to change label color in GraphView. Thus, the following is the solution I found out:
graph.getGridLabelRenderer().setVerticalLabelsColor(Color.WHITE);
graph.getGridLabelRenderer().setHorizontalLabelsColor(Color.WHITE);
graph.getGridLabelRenderer().setVerticalLabelsColor(Color.WHITE);
graph.getGridLabelRenderer().setHorizontalLabelsColor(Color.WHITE);
graph.getGridLabelRenderer().reloadStyles();
Thus merging these lines with the provided code:
public class MainActivity extends AppCompatActivity {
GraphView graph;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
graph = (GraphView) findViewById(R.id.graph);
StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph);
staticLabelsFormatter.setHorizontalLabels(new String[] {"Yan", "Fev", "Mart", "Apr", "May", "Iyn",
"Iyl", "Avq", "Sent", "Okt", "Noy", "Dek"});
//staticLabelsFormatter.setVerticalLabels(new String[] {"low", "middle", "high", "koko"});
graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter);
//First two lines change the grid line color
graph.getGridLabelRenderer().setGridColor(Color.WHITE);
graph.getGridLabelRenderer().setHighlightZeroLines(false);
//Below two lines change the label color
graph.getGridLabelRenderer().setVerticalLabelsColor(Color.WHITE);
graph.getGridLabelRenderer().setHorizontalLabelsColor(Color.WHITE);
graph.getGridLabelRenderer().reloadStyles();
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, 2),
new DataPoint(1,5),
new DataPoint(2, 4),
new DataPoint(3, 4),
new DataPoint(4, 8),
new DataPoint(5, 6),
new DataPoint(6, 8),
new DataPoint(7, 1),
new DataPoint(8, 5),
new DataPoint(9, 2),
new DataPoint(10, 7),
new DataPoint(11, 3),
});
series.setColor(Color.YELLOW);
series.setDrawBackground(true);
series.setDrawDataPoints(true);
graph.addSeries(series);
}

background color only between line graph (GraphView)

Library: graphview
Version: 4.2.1
Is it possible to set background color between two curves? Like this:
setDrawBackground changes color between curve and coordinate line, it isn't what I need:
Code for exaple:
LineGraphSeries<DataPoint> seriesUp = new LineGraphSeries<>(new DataPoint[]{
new DataPoint(0, 3),
new DataPoint(1, 7),
new DataPoint(2, 5),
new DataPoint(3, 4),
new DataPoint(4, 8)
});
graph.addSeries(seriesUp);
seriesUp.setDrawBackground(false);
seriesUp.setColor(Color.MAGENTA);
seriesUp.setDrawBackground(true);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{
new DataPoint(0, 2),
new DataPoint(1, 6),
new DataPoint(2, 4),
new DataPoint(3, 3),
new DataPoint(4, 7)
});
graph.addSeries(series);
graph.getViewport().setScalableY(true);
series.setThickness(5);
series.setColor(Color.RED);
series.setAnimated(true);
series.setDrawDataPoints(true);
series.setDataPointsRadius(10);
series.setBackgroundColor(Color.CYAN);
series.setDrawBackground(false);

ArrayList in Graphiew

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));
....

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

Vertical barchart using Android Graphview

Is it possible to create a vertical bar chart using GraphView in android.?
GraphView graph = (GraphView) rootView.findViewById(R.id.graph);
BarGraphSeries<DataPoint> series = new BarGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, -2),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);
// legend
series.setTitle("foo");
graph.getLegendRenderer().setVisible(true);
graph.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP);

Categories

Resources