xcode 当用户点击图表时创建一个 MarkerView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38212750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:00:46  来源:igfitidea点击:

Create a MarkerView when user clicks on Chart

iosxcodeswiftbar-chartios-charts

提问by Tophat Gordon

I have searched and searched for how to display the MarkerView when the user clicks on a bar in a bar chart using Charts (was iOS-charts) for Swift.

我已经搜索并搜索了如何在用户单击条形图中的条形图时使用 Swift 的 Charts(是 iOS 图表)显示 MarkerView。

The documentation states the library is capable of "Highlighting values (with customizable popup-views)" using MarkerViews, but I don't know how to show one.

该文档指出该库能够使用 MarkerViews 来“突出显示值(具有可自定义的弹出视图)”,但我不知道如何显示。

I want a little tool tip to display, like the image below, when the user clicks on a bar in the bar chart.

当用户单击条形图中的条形时,我希望显示一个小工具提示,如下图所示。

Tool tip on bar graph: enter image description here

条形图上的工具提示: 在此处输入图片说明

I have the chartValueSelected function ready which fires when a bar is selected.

我已准备好 chartValueSelected 函数,该函数会在选择条形图时触发。

回答by Edison

So you are using Chartsright?

所以你在使用图表吗?

Did you check BallonMarker.swift?

你检查 BallonMarker.swift 了吗?

/ChartsDemo/Classes/Components/BallonMarker.swift

/ChartsDemo/Classes/Components/BallonMarker.swift

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 Update

斯威夫特 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

Objective-C

目标-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;

回答by Krutika Sonawala

Swift chart library with Objective C code will be something like this.. I am here just showing some code snippets.

带有 Objective C 代码的 Swift 图表库将是这样的……我在这里只是展示一些代码片段。

  1. 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;
    }
    
  2. 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!)        
    }
    
  1. 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;
    }
    
  2. 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!)        
    }
    

回答by Shashidhar Yamsani

Even after adding the BalloonMarker to the chart if the markers are not displayed make sure that the markers are enabled on the chart view

即使在将 BalloonMarker 添加到图表后,如果未显示标记,请确保在图表视图中启用了标记

chartView.drawMarkers = true

highlighting is enabled on the chart data set. This is should be done after you assign the chart data set.

在图表数据集上启用了突出显示。这应该在您分配图表数据集之后完成。

chartView.data?.highlightEnabled = true