如何在折线图中显示第一个值和最后一个值

时间:2018-08-29 13:31:25

标签: ios swift charts

我正在使用图表库,并且试图在折线图上添加第一个值和最后一个值。请让我知道如何实现。我尝试在linechartrenderer中自定义drawValue方法,但是没有用。

这是我的代码

var months = ["Dec 15", "Jun 16", "Dec 16", "Jun 17", "Dec 17", "Jun 18"]
var unitsSold = [50.0, 25.0, 50.0, 75.0, 100.0, 75.0]

查看加载:

    setChart(dataPoints: months, values: unitsSold)

方法:

  func setChart(dataPoints: [String], values: [Double]) {
    var dataEntries: [ChartDataEntry] = []

    for i in 0..<dataPoints.count {

        let dataEntry = ChartDataEntry(x: Double(i), y: values[i], data: dataPoints[i] as AnyObject)
        dataEntries.append(dataEntry)
    }

    let chartDataSet = LineChartDataSet(values: dataEntries, label: nil)
    chartDataSet.setColor(UIColor(red: 53/255, green: 85/255, blue: 123/255, alpha: 1))
    chartDataSet.circleRadius = 5
    chartDataSet.circleHoleRadius = 2
    chartDataSet.drawValuesEnabled = false
    chartDataSet.drawCirclesEnabled = false

    let chartData = LineChartData(dataSets: [chartDataSet])

    defaultChartView.data = chartData

    defaultChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: months)
    defaultChartView.xAxis.labelPosition = .bottom
    defaultChartView.xAxis.drawGridLinesEnabled = false
    //lineChartView.xAxis.avoidFirstLastClippingEnabled = true
    defaultChartView.xAxis.axisMinimum = 0
    defaultChartView.xAxis.granularity = 1

    defaultChartView.rightAxis.drawAxisLineEnabled = false
    defaultChartView.rightAxis.drawLabelsEnabled = false
    defaultChartView.rightAxis.enabled = false

    defaultChartView.leftAxis.drawAxisLineEnabled = false
    defaultChartView.leftAxis.axisMinimum = 0.0
    defaultChartView.leftAxis.drawLabelsEnabled = false

    //lineChartView.leftAxis.drawGridLinesEnabled = false
    //lineChartView.leftAxis.granularityEnabled = false


    defaultChartView.pinchZoomEnabled = true
    defaultChartView.doubleTapToZoomEnabled = true
    defaultChartView.legend.enabled = false
    defaultChartView.chartDescription?.text = " "



}

3 个答案:

答案 0 :(得分:0)

viewDidLoad:

guard let firstMonth = months.first, let lastMonth = months.last else {
return
}

let myMonths = [firstMonth, lastMonth]
guard let firstValue = unitsSold.first, let lastValue = unitsSold.last else {
return
}
let myValues = [firstValue, lastValue]

setChart(dataPoints: myMonths, values: myValues)

答案 1 :(得分:0)

解决方法是转到linechartrenderer文件并替换drawValues方法

<div class='col-xs-12'><img src='data:image/jpeg;base64,<?php
        $r = sqlsrv_query($database,'SELECT * FROM PDATicketSignature WHERE PDATicketSignature.PDATicketID = ?',array($_GET['ID']));
        if($r){$row = sqlsrv_fetch_array($r);
          if(is_array($row)){
            echo base64_encode($row['Signature']);
        }}?>' /></div>

答案 2 :(得分:0)

设置下面的X轴属性

barChartView.xAxis.setLabelCount(2, force: true)