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
Related
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));
....
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.
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"});
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
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);