MPAndroidChart set center vertical line

前端 未结 3 588
我在风中等你
我在风中等你 2021-01-17 14:31

I want to set a vertical line in center of LineChart like this:

When scrolling to each point, it can notify to change the date below (

相关标签:
3条回答
  • 2021-01-17 14:43

    Have you tried using getEntryByTouchPoint on your chart supplying the x and y coordinates of the center of the chart?

    public Entry getEntryByTouchPoint(float x, float y)

    returns the Entry object displayed at the touched position of the chart

    0 讨论(0)
  • 2021-01-17 14:49

    Maybe it's too late but here is my answer. It's encoded in Swift using Charts (MPAndroidCharts port for iOS) but API is 99% the same ;)

    let verticalPointEntry = ChartDataEntry(x: xValue, y: yValue)
    
    let dataSet = LineChartDataSet(values: [verticalPointEntry], label: "")
    dataSet.drawCirclesEnabled = false
    dataSet.drawValuesEnabled = false
    dataSet.setDrawHighlightIndicators(true)
    dataSet.drawHorizontalHighlightIndicatorEnabled = false
    dataSet.highlightColor = UIColor.white
    dataSet.highlightLineWidth = 1   
    
    let highlightPoint = Highlight(x: xValue, y: yValue, dataSetIndex: datasetIndex)
    self.highlightValues([highlightPoint])
    
    // "yourNormalDataSet" is your regular dataSet in which you want to display vertical line over it
    let chartData = LineChartData(dataSets: [yourNormalDataSet, dataSet])
    self.data = chartData
    self.data?.notifiyDataChanged()
    self.notifyDataSetChanged
    

    This will display a vercital line over the point defined by your xValue variable.

    Hope it helps!

    0 讨论(0)
  • 2021-01-17 14:56

    Take a look at the method

    protected void drawGridBackground(Canvas c) {
    

    in the BarLineChartBase class (parent for a LineChart). In that method you have all data to draw your line right in the middle. Something like this

    RectF rectF = mViewPortHandler.getContentRect();
    float xMiddle = (rectF.right - rectF.left)/2;
    Paint p = new Paint();
    p.setColor(Color.BLACK);
    c.drawLine(xMiddle, rectF.bottom, xMiddle, rectF.top, p);
    
    0 讨论(0)
提交回复
热议问题