Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to just draw one circle value for LineChartView #2391

Closed
williamhqs opened this issue Apr 25, 2017 · 13 comments
Closed

How to just draw one circle value for LineChartView #2391

williamhqs opened this issue Apr 25, 2017 · 13 comments

Comments

@williamhqs
Copy link

As the title described, i want to draw only one circle one a specific value, but right now it draw all the values.
I want
bgrzd
I got
geqpn

code

func configureLines() {
        var set:LineChartDataSet?
        if let count = data?.dataSetCount, count > 0 {
            set = data?.dataSets[0] as? LineChartDataSet;
            set?.values = chartDataValues
            data?.notifyDataChanged()
            notifyDataSetChanged()
        } else {
            set = LineChartDataSet(values: chartDataValues, label: "DataSet 1")
            set?.axisDependency = .left
            set?.drawValuesEnabled = false
            set?.valueTextColor = UIColor.green
            set?.circleRadius = 4
            set?.drawCirclesEnabled = true
            set?.drawCircleHoleEnabled = false
//            set?.highlightColor = UIColor.red
            set?.highlightEnabled = false
            set?.setCircleColor(UIColor.yellowFontColor)
            set?.fillColor = UIColor.chartFillColor
            set?.fillAlpha = 1.0
            set?.drawFilledEnabled = true
            set?.setColor(UIColor.yellowFontColor)
            set?.lineWidth = 1
            print(set!)
            var dataSets = [LineChartDataSet]()
            dataSets.append(set!)
            let data = LineChartData(dataSets: dataSets)
            data.setValueTextColor(UIColor.red)
            self.data = data
        }
    }

How should i make it just show one circle ?

@thierryH91200
Copy link
Contributor

combine scatter chart with one point and line chart

@liuxuan30
Copy link
Member

set set?.drawCirclesEnabled = true to false, and override drawHighlighted() or drawCircles() to just draw highlight circle for specific value.

@williamhqs
Copy link
Author

HI @liuxuan30

Follow your idea, i am trying to subclass LineChartRenderer then reassign the render variable to the subclass render. Then use it on my subclass LineChartView. I am not sure if i understood correct. I can't override drawCircles() drawExtras can be override, and it call drawCircles inside.

But my problem is i can't find the parameter used by the render init. I think i am wrong. Here's my code. Thank you for any help!

class BSLineChartRenderer: LineChartRenderer {
    override func drawExtras(context: CGContext) {        
    }
}

class BSMyWealthLineChartView: LineChartView {
    
    var chartDataValues = [ChartDataEntry]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
      // init(dataProvider: LineChartDataProvider?, animator: Animator?, viewPortHandler: ViewPortHandler?) 
        renderer = DBSLineChartRenderer() //Without dataProvider etc.
    }
    

@liuxuan30
Copy link
Member

just like how line chart view does:

    internal override func initialize()
    {
        super.initialize()
        
        renderer = LineChartRenderer(dataProvider: self, animator: _animator, viewPortHandler: _viewPortHandler)
    }

@williamhqs
Copy link
Author

HI @liuxuan30 There are errors like

"Static member 'initialize' cannot be used on instance of type 'LineChartView'

As attached:
screen shot 2017-04-28 at 4 01 44 pm

@liuxuan30
Copy link
Member

are you defining outside of the framework, so restricted by interval?

@UweMeier
Copy link

@liuxuan30 You tell to override drawHighlighted(), but how should one subclass LineChartRenderer and override drawHighlighted() when several necessary properties and functions are internal?

@tundsD53
Copy link

tundsD53 commented Oct 1, 2018

@liuxuan30 Seems as though these properties that we would need in order to write our own custom renderer are internal, so we wouldn't be able too. Is it possible to create a protocol function that allows us to decide whether a circle is drawn at a specific point. So that you can keep these properties internal.

@MobileVet
Copy link

Just in case anyone else is looking to do the same... the easiest solution is to draw a second data set that consists of only a single value. Turn off the circles on the first set that produces the line and then turn on the circle for the second set. You can use

LineChartView.lineData?.addDataSet(set)

@MihaelIsaev
Copy link

@MobileVet You're the best man! Thank you very much!!!! 🚀

@longsirhero
Copy link

import Foundation
import CoreGraphics
import Charts

/// Chart that draws lines, surfaces, circles, ...
open class StrategyLineChartView: LineChartView {

public override init(frame: CGRect)
{
    super.init(frame: frame)
    renderer = StrategyLineChartRenderer(dataProvider: self, animator: chartAnimator, viewPortHandler: viewPortHandler)
    
}

public required init?(coder aDecoder: NSCoder)
{
    super.init(coder: aDecoder)
    renderer = StrategyLineChartRenderer(dataProvider: self, animator: chartAnimator, viewPortHandler: viewPortHandler)
}

}

@ghost
Copy link

ghost commented Jul 29, 2021

Just in case anyone else is looking to do the same... the easiest solution is to draw a second data set that consists of only a single value. Turn off the circles on the first set that produces the line and then turn on the circle for the second set. You can use

LineChartView.lineData?.addDataSet(set)

这个方法经过我的亲自实践,确实是可行的,就是在原有的点上加一个
`LineChartDataSet *chartSet = [[LineChartDataSet alloc] initWithEntries:tempDataEntryArray];

    chartSet.highlightEnabled = YES;
    chartSet.highlightLineWidth = 1;
    chartSet.lineWidth = 2;
    chartSet.drawCirclesEnabled = true;
    
    chartSet.cubicIntensity = 0.2;
    chartSet.circleRadius = 4.0f;
    [chartSet setCircleColor:[UIColor colorWithHex:tempdeptModel.color]]; // 这行代码很重要设置这个点的颜色的,如果不设置点的颜色会有问题
    //    选中的十字线去除
    chartSet.drawHorizontalHighlightIndicatorEnabled = NO;
    chartSet.drawVerticalHighlightIndicatorEnabled = YES;
    [chartSet setMode:LineChartModeHorizontalBezier];`

想到这个方法真的很鸡贼

@Hiphone-Du
Copy link

Just in case anyone else is looking to do the same... the easiest solution is to draw a second data set that consists of only a single value. Turn off the circles on the first set that produces the line and then turn on the circle for the second set. You can use
LineChartView.lineData?.addDataSet(set)

这个方法经过我的亲自实践,确实是可行的,就是在原有的点上加一个 `LineChartDataSet *chartSet = [[LineChartDataSet alloc] initWithEntries:tempDataEntryArray];

    chartSet.highlightEnabled = YES;
    chartSet.highlightLineWidth = 1;
    chartSet.lineWidth = 2;
    chartSet.drawCirclesEnabled = true;
    
    chartSet.cubicIntensity = 0.2;
    chartSet.circleRadius = 4.0f;
    [chartSet setCircleColor:[UIColor colorWithHex:tempdeptModel.color]]; // 这行代码很重要设置这个点的颜色的,如果不设置点的颜色会有问题
    //    选中的十字线去除
    chartSet.drawHorizontalHighlightIndicatorEnabled = NO;
    chartSet.drawVerticalHighlightIndicatorEnabled = YES;
    [chartSet setMode:LineChartModeHorizontalBezier];`

想到这个方法真的很鸡贼

我的需求是:点击到哪里,只显示选中点,其他的不显示。方法如下,仅供学习参考

1.首先设置图标:
[self optionTapped:@"toggleIcons"];//保留图标

2.定义一个临时选中点
@Property (nonatomic, strong) ChartDataEntry *tempDataEntry;//临时选中点

3.通过点击代理,将选中的Entry传过来,并刷新updateChartData

  • (void)chartValueSelected:(ChartViewBase * __nonnull)chartView entry:(ChartDataEntry * __nonnull)entry highlight:(ChartHighlight * __nonnull)highlight
    {
    NSLog(@"chartValueSelected");

    //获取当前ChartDataEntry,找一下下标
    self.tempDataEntry = entry;
    [self updateChartData];
    }

  1. updateChartData的时候,设置当前选中的图标,其他点图标置空
    if (_chartView.data.dataSetCount > 0)
    {
    set1 = (CustomLineChartDataSet *)_chartView.data.dataSets[0];
    //自己加的,点击的地方有圆点,其他的没有圆点
    for (ChartDataEntry *nowDataEntry in values) {

         if (nowDataEntry.x == self.tempDataEntry.x && nowDataEntry.y == self.tempDataEntry.y) {//获取当前点击的点!!!
             [nowDataEntry setIcon:[UIImage imageNamed:@"icon"]];
         }else{
             [nowDataEntry setIcon:[UIImage imageNamed:@""]];
         }
     }
     //自己加的
     [set1 replaceEntries: values];
     [_chartView.data notifyDataChanged];
     [_chartView notifyDataSetChanged];
    

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants