AAChartModel / AAChartKit

📈📊🚀🚀🚀An elegant modern declarative data visualization chart framework for iOS, iPadOS and macOS. Extremely powerful, supports line, spline, area, areaspline, column, bar, pie, scatter, angular gauges, arearange, areasplinerange, columnrange, bubble, box plot, error bars, funnel, waterfall and polar chart types. 极其精美而又强大的现代化声明式数据可视化图表框架,支持柱状图、条形图、折线图、曲线图、折线填充图、曲线填充图、气泡图、扇形图、环形图、散点图、雷达图、混合图等各种类型的多达几十种的信息图图表,完全满足工作所需.
https://cocoapods.org/pods/AAChartKit
MIT License
4.7k stars 751 forks source link

如何默认选中折线图某个点 #1532

Closed yl3066882156 closed 5 months ago

yl3066882156 commented 5 months ago

WeChat95ef8ea5a7ae1e4d26770ae196ff4c16 WeChatcd1bf61b274dc4e5f9043c199347bf9d 第一张图是设计图,第二张时我添加的marker,以下是代码片段

//折线数组,多条线就增加元素
    NSMutableArray *series = [[NSMutableArray alloc]init];
    [self.temperatureDataSourceArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        NSArray *array = obj;

        NSMutableArray *dataSource = [[NSMutableArray alloc]init];
        dataSource = array.mutableCopy;
        [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            CGFloat num = [[NSString stringWithFormat:@"%@", obj] floatValue];
            if (idx == array.count - 1) {

                AADataElement *singleSpecialData = AADataElement.new
                .markerSet(AAMarker.new
                           .lineColorSet(idx == 0 ? @"#0DBAFE" : @"#7DDAFF")//外边框颜色
                           .lineWidthSet(@5)//边框宽度
                           .fillColorSet(idx == 0 ? @"#0DBAFE" : @"#7DDAFF")//中心填充色
                           .radiusSet(@5)/*半径*/
                           )
                .ySet([NSNumber numberWithFloat:num]);

                [dataSource  replaceObjectAtIndex:idx withObject:singleSpecialData];
            }
        }];

        //折线数据1
        AASeriesElement *element1 = AASeriesElement.new
            .lineWidthSet(@2)
            .statesSet(AAStates.new
                       .hoverSet(AAHover.new
                                 .enabledSet(true)
                                 //手指盘旋或选中图表时,禁止线条变粗
                                 .lineWidthPlusSet(@0)))
            .markerSet(AAMarker.new
                       .radiusSet(@0)
                       .symbolSet(AAChartSymbolTypeCircle)
                       .statesSet(AAMarkerStates.new
                                  .hoverSet(AAMarkerHover.new
//                                            .enabledSet(false)
                                            .lineColorSet(idx == 0 ?@"#0DBAFE" : @"#7DDAFF")//外边框颜色
                                            .lineWidthSet(@5)//边框宽度
                                            .fillColorSet(idx == 0 ?@"#0DBAFE" : @"#7DDAFF")//中心填充色
                                            .radiusSet(@5)/*半径*/)))
            .dataSet(dataSource);

        //折线数组,多条线就增加元素
        [series addObject:element1];

    }];

但是我滑动时,添加的marker是不会消失的,还是半径5,怎么在滑动时,让他消失,也就是默认选中某个点,而不是我这样手动加上这个点,最终呈现这样的 WechatIMG19

AAChartModel commented 5 months ago

参考 demo 中的如下配置示例, 运行查看交互效果:

//https://api.highcharts.com/highcharts/plotOptions.series.marker.states.hover
//https://api.highcharts.com/highcharts/plotOptions.series.events.mouseOver
//https://api.highcharts.com/class-reference/Highcharts.Point#select
//https://github.com/AAChartModel/AAChartKit/issues/1532
+ (AAOptions *)defaultSelectedAPointForLineChart {
    AADataElement *defaultPointData = AADataElement.new
        .ySet(@29.9)
        .selectedSet(@true);

    NSArray *dataArr = @[@29.9, @71.5, @106.4, @129.2, @144.0, @176.0, @135.6, @148.5, @216.4, @194.1, @95.6, defaultPointData];
    NSInteger defaultPointIndex = dataArr.count - 1;

    AAOptions *options = AAOptions.new
        .xAxisSet(AAXAxis.new
            .categoriesSet(@[@"Jan", @"Feb", @"Mar", @"Apr", @"May", @"Jun", @"Jul", @"Aug", @"Sep", @"Oct", @"Nov", @"Dec"]))
        .plotOptionsSet(AAPlotOptions.new
            .seriesSet(AASeries.new
                .eventsSet(AASeriesEvents.new
                    .mouseOverSet([NSString stringWithFormat:@AAJSFunc(function () {
                        if(this.hasRun) return;

                        var series = this;
                        var defaultPointIndex = %ld;
                        if (series.data.length > defaultPointIndex) {
                            defaultPoint = series.data[defaultPointIndex];
                            defaultPoint.select(false);
                        }

                        this.hasRun = true;
                    }), (long)defaultPointIndex]))
                .allowPointSelectSet(@true)
                .markerSet(AAMarker.new
                    .statesSet(AAMarkerStates.new
                        .selectSet(AAMarkerSelect.new
                            .fillColorSet(@"red")
                            .lineWidthSet(@10)
                            .lineColorSet(@"yellow")
                            .radiusSet(@20))))))
        .seriesSet(@[
            AASeriesElement.new
                .dataSet(dataArr)
        ]);

    return options;
}
AAChartModel commented 5 months ago
  1. 想要查看以上的配置内容, 需要下载最新 demo 代码.
  2. 想要在你的项目中使用以上配置的话, 需要更新 AAChartKitpod 库. 参考: