【问题标题】:How to cut highlight in iOS Charts only up to the data point?如何将iOS图表中的突出显示仅剪切到数据点?
【发布时间】:2018-12-03 21:36:55
【问题描述】:

如何自定义折线图中的荧光笔?我想将突出显示仅绘制到数据点。我还想自定义 x 轴上突出显示的标签。下面是我正在努力实现的参考图片。

这是我到目前为止所做的。 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)

【问题讨论】:

  • 问题是广泛的。为什么你没有发布你已经拥有的代码?你试过什么?
  • 哦,我想我的问题是对的。我将添加一个关于我已经完成的操作和一些代码的参考屏幕截图。但基本上,如果它是突出显示点的一部分,我只需将 x 轴标签设置为粗体并使用不同的颜色,并且我必须将突出显示线仅剪切到数据点。
  • @Bjørn 更新了我的问题
  • 在您使用 LineScatterCandleRadarRenderer.swift 的图表中的 drawHighlightLines 函数中),将行 context.move(to: CGPoint(x: point.x, y: viewPortHandler.contentTop)) 更改为 context.move(to: CGPoint(x: point.x, y: viewPortHandler.contentTop + point.y - offsetOfMarkerToHighlightLine))
  • @taki,你得到解决方案了吗?如果可能,请分享

标签: ios swift charts ios-charts


【解决方案1】:

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

所有属性都可以通过 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];

您可以启用高亮指示器,选择的 dat 点将用垂直线突出显示:

[set setDrawHighlightIndicators:YES];

您可以根据需要设置圆半径:

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

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

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

编辑:

要更改字体颜色和类型,您需要更改 xAxis 颜色和字体,如下所述

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

对于数据点值的线限制,我认为我们不能在当前版本中限制这一点。

希望这将帮助您实现您的要求。

【讨论】:

  • 抱歉,更新了我的问题。我只需要自定义高亮线并更改 x 轴标签的字体样式(如果它的 x 值被突出显示)
  • 好的,所以对于 xAxis 颜色,您可以将其设为粗体并自定义字体和颜色,但对于限制数据点线,我认为这是不可能的。
  • 高亮线怎么样?
  • 我认为这在当前库中是不可能的,但您仍然可以搜索或尝试自己,希望您在这一点上取得成功。
猜你喜欢
  • 1970-01-01
  • 2017-02-06
  • 2018-01-22
  • 2018-03-05
  • 2017-09-12
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多