Reputation: 2482
I'm using Graph View to plot my bar graphs in the app I'm developing and everything is working fine in portrait mode. However,when changed to landscape, the graphs's labels get out of line with the data points as shown in the pictures below:
Portrait:
Landscape:
As you can see, the labels are miss aligned with respect to the bars, Here is my code for this:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_chart, container, false)
val graph = view?.findViewById(R.id.graph) as GraphView
val staticLabelsFormatter = StaticLabelsFormatter(graph)
staticLabelsFormatter.setHorizontalLabels(arrayOf("Basics", "Loops", "Functions"))
graph.getGridLabelRenderer().setLabelFormatter(staticLabelsFormatter)
graph.gridLabelRenderer.verticalLabelsColor = Color.WHITE
graph.gridLabelRenderer.horizontalLabelsColor = Color.WHITE
graph.gridLabelRenderer.gridStyle = GridLabelRenderer.GridStyle.NONE
graph.gridLabelRenderer.isVerticalLabelsVisible = false
graph.viewport.setMinX(0.0)
graph.viewport.setMinY(0.0)
graph.viewport.setMaxX(2.0)
graph.viewport.setMaxY(6.0)
graph.viewport.isXAxisBoundsManual = true
graph.viewport.isYAxisBoundsManual = true
val basics = arguments!!.getDouble("basics")
val loops = arguments!!.getDouble("loops")
val functions = arguments!!.getDouble("functions")
val series = BarGraphSeries(arrayOf(
DataPoint(0.37, basics)
))
val series2 = BarGraphSeries(arrayOf(
DataPoint(1.0, loops)
))
val series3 = BarGraphSeries(arrayOf(
DataPoint(1.6, functions)
))
series.setColor(Color.GREEN)
series2.setColor(Color.CYAN)
series3.setColor(Color.MAGENTA)
series.setAnimated(true)
series2.setAnimated(true)
series3.setAnimated(true)
series.setDrawValuesOnTop(true)
series2.setDrawValuesOnTop(true)
series3.setDrawValuesOnTop(true)
series.setValuesOnTopColor(Color.WHITE)
series2.setValuesOnTopColor(Color.WHITE)
series3.setValuesOnTopColor(Color.WHITE)
graph.addSeries(series)
graph.addSeries(series2)
graph.addSeries(series3)
return view
}
Is there a way to assign y values to labels instead of a x data point ? This issue is happening now because I'm forced to fix a y value to a x value. Idly, it would be to the label. Or is there a way to fix labels to a certain x value ?
Upvotes: 1
Views: 396
Reputation: 376
Try To use This code
private LineChart mChart;
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setPinchZoom(true);
LineDataSet set1 = new LineDataSet(x, "");
set1.setColor(Color.rgb(0, 82, 159));
set1.setLineWidth(1.5f);
set1.setCircleRadius(4f);
LineData data1 = new LineData(y, set1);
mChart.setData(data1);
mChart.invalidate();
Upvotes: 1