GraphView大小不正确

时间:2018-11-05 04:45:47

标签: android android-studio android-layout kotlin android-graphview

我正在使用Graph View在正在开发的应用中绘制我的条形图,并且在纵向模式下一切正常。但是,当更改为横向时,图形的标签与数据点不一致,如下图所示:

肖像:

enter image description here

风景:

enter image description here

如您所见,标签相对于条未对齐,这是我的代码:

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
}

是否可以将y值分配给标签而不是x数据点?因为我被迫将y值固定为x值,所以现在发生了此问题。奇怪的是,这将是标签。还是有办法将标签固定为某个x值?

1 个答案:

答案 0 :(得分:1)

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