如何在iOS图表中剪切高亮显示直到数据点?

时间:2017-07-05 07:34:43

标签: ios swift charts ios-charts

如何在折线图中自定义荧光笔?我想剪切高亮显示只绘制到数据点。我还想在x轴上自定义突出显示的标签。下面是我想要实现的参考图片。

enter image description here

这是我到目前为止所做的。 What I've achieved so far

以下是我的折线图视图设置的代码

let lineChartView: LineChartView = {
    let chart = LineChartView(frame: .zero)
    chart.chartDescription = nil
    chart.dragEnabled = true
    chart.scaleYEnabled = false
    chart.scaleXEnabled = true
    chart.pinchZoomEnabled = false
    chart.drawGridBackgroundEnabled = true
    chart.gridBackgroundColor = .clear
    chart.legend.enabled = false
    chart.zoom(scaleX: 2, scaleY: 1, x: 0, y: 0)
    chart.setViewPortOffsets(left: 30, top: 30, right: 30, bottom: 30)

    chart.rightAxis.enabled = false

    let yAxis = chart.leftAxis
    yAxis.enabled = true
    yAxis.drawAxisLineEnabled = false
    yAxis.drawLabelsEnabled = false
    yAxis.gridColor = UIColor(rgba: "#414042")
    yAxis.gridLineWidth = 1
    yAxis.axisMaximum = 6000

    chart.xAxis.enabled = true
    chart.xAxis.drawGridLinesEnabled = false
    chart.xAxis.drawLabelsEnabled = true
    chart.xAxis.drawAxisLineEnabled = true
    chart.xAxis.axisLineColor = .darkGray
    chart.xAxis.axisLineWidth = 1
    chart.xAxis.valueFormatter = IndexAxisValueFormatter(values: ["Jan", "Feb", "Mar", "April", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"])
    chart.xAxis.labelPosition = .bottom
    chart.xAxis.labelTextColor = .darkGray
    chart.xAxis.labelFont = UIFont.sourceSansProLight(withSize: 15.5)
    chart.xAxis.granularityEnabled = true
    chart.xAxis.granularity = 1

    chart.highlightPerTapEnabled = false

    chart.backgroundColor = .clear

    return chart
}()

这是在图表和突出显示中添加随机数据点的代码。

var values: [ChartDataEntry] = []
var highlight: Highlight? = nil

for index in 0..<12 {
    let randomValue = Double(arc4random_uniform(5000))
    values.append(ChartDataEntry(x: Double(index), y: randomValue))

    if index == 4 {
        highlight = Highlight(x: Double(index), y: randomValue, dataSetIndex: 0)
    }
}

let dataSet = LineChartDataSet(values: values, label: "")
dataSet.highlightEnabled = true
dataSet.drawHorizontalHighlightIndicatorEnabled = false
dataSet.highlightColor = UIColor(rgba: "#1899FF")
dataSet.highlightLineWidth = 1
dataSet.mode = .horizontalBezier
dataSet.circleColors = Array(repeating: UIColor(rgba: "#1899FF"), count: 12)
dataSet.fillColor = UIColor(rgba: "#1899FF")
dataSet.drawCircleHoleEnabled = false
dataSet.drawFilledEnabled = true
dataSet.lineWidth = 0.0
dataSet.drawValuesEnabled = true
dataSet.axisDependency = .left
dataSet.valueFormatter = self

let data = LineChartData(dataSet: dataSet)
data.setDrawValues(true)
data.setValueFont(UIFont.sourceSansProLight(withSize: 15.5))
data.setValueTextColor(UIColor(rgba: "#1899FF"))

lineChartView.data = data
lineChartView.highlightValue(highlight)

1 个答案:

答案 0 :(得分:0)

对于数据点的突出显示区域,您可以使用以下属性并实现此

所有属性都可以通过dataSet使用,因此您可以像下面一样创建LineChartDataSet,并根据您的要求使用不同的属性。

//DataSet
LineChartDataSet *set = [[LineChartDataSet alloc] initWithValues:values label:@"closed"];
set.lineWidth = 2;

使用填充颜色设置ChartFill属性:

 set.fill = [ChartFill fillWithColor:<Your Legend Color>];
 [set setColor:<Your Legend Color>];
 [set setCircleColor:<Your Legend Color>];
 [set setCircleHoleColor:<Your Legend Color>];

您需要启用drawfill:

set.drawFilledEnabled = YES;

您可以使用此属性启用圈子:

[set setDrawCirclesEnabled:YES];

您可以启用高亮显示指示器选择哪个数据点将使用垂直线突出显示:

[set setDrawHighlightIndicators:YES];

您可以根据自己的要求设置圆形半径:

[set setCircleRadius:5.0f];
[set setCircleHoleRadius:4.0f];

您也可以使用突出显示的数据点指定行:

[set setHighlightLineWidth:1.0];
[set setHighlightColor:kAppLegend3GraphColorForCAP];

修改

更改字体颜色&amp;您需要更改xAxis颜色和字体,如下所述

lineChartView.xAxis.labelFont = UIFont.systemFont(ofSize: 20.0, weight: UIFontWeightRegular)
lineChartView.xAxis.labelTextColor = .orange

对于数据点值的行限制,我认为我们无法在当前版本中限制此值。

希望这有助于您达到您的要求。