我搜索并搜索了当用户使用Swift的图表(是iOS图表)点击条形图中的条形图时如何显示MarkerView。
文档说明库能够突出显示值(带有可自定义的弹出视图)"使用MarkerViews,但我不知道如何展示。
当用户点击条形图中的条形图时,我想要一个小工具提示,如下图所示。
我已准备好chartValueSelected函数,当选择一个条时会触发该函数。
答案 0 :(得分:10)
所以你正在使用Charts吗?
你检查过BallonMarker.swift吗?
/ChartsDemo/Classes/Components/BallonMarker.swift
<强>夫特强>
let marker:BalloonMarker = BalloonMarker(color: UIColor.redColor(), font: UIFont(name: "Helvetica", size: 12)!, insets: UIEdgeInsets(top: 7.0, left: 7.0, bottom: 7.0, right: 7.0))
marker.minimumSize = CGSizeMake(75.0, 35.0)
chartView.marker = marker
Swift 3更新
let marker:BalloonMarker = BalloonMarker(color: UIColor.black, font: UIFont(name: "Helvetica", size: 12)!, textColor: UIColor.white, insets: UIEdgeInsets(top: 7.0, left: 7.0, bottom: 7.0, right: 7.0))
marker.minimumSize = CGSize(width: 75.0, height: 35.0)
chartView.marker = marker
<强>目标C 强>
BalloonMarker *marker = [[BalloonMarker alloc] initWithColor:[UIColor colorWithRed:14.0/255.0 alpha:1.0] font:[UIFont systemFontOfSize:12.0] insets: UIEdgeInsetsMake(7.0, 7.0, 7.0, 7.0)];
marker.minimumSize = CGSizeMake(75.f, 35.f);
_chartView.marker = marker;
答案 1 :(得分:1)
使用Objective C代码的Swift图表库将是这样的。我在这里只显示一些代码片段。
ChartViewController.m
-(void)initializeChart {
//chart specifications will go here
...
...
//set balloon marker to the chart
BalloonMarker *marker = [[BalloonMarker alloc]
initWithColor: kColorBrown1
font: [UIFont systemFontOfSize:12.0]
textColor: UIColor.whiteColor
insets: UIEdgeInsetsMake(8.0, 8.0, 20.0, 8.0)];
marker.chartView = chartView;
marker.minimumSize = CGSizeMake(80.f, 40.f);
chartView.marker = marker;
}
BalloonMarker.swift
import Charts
...
...
//This method will be called when ever user clicks on a chart to refresh the marker text. You need to specify the value here.
open override func refreshContent(entry: ChartDataEntry, highlight: Highlight)
{
setLabel(entry.y!)
}
答案 2 :(得分:1)
如果未显示标记,即使将BalloonMarker添加到图表中,也要确保在图表视图中启用了标记
chartView.drawMarkers = true
在图表数据集上启用突出显示。这应该在您分配图表数据集后完成。
chartView.data?.highlightEnabled = true