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.71k stars 751 forks source link

如何让最后一个数据的tooltip在图表渲染时就直接显示出来 #1492

Closed kenneyLee closed 11 months ago

kenneyLee commented 11 months ago

我们用的折线图,想默认打开页面看到图表时,就能直接看到最后一个数据的tooltip

AAChartModel commented 11 months ago

参考:

OC 版本的 demo 中也有此示例, 下载运行查看即可.

kenneyLee commented 11 months ago

请教下大佬,默认的 tooltip 显示出来了,但是位置不对,一直在最左边,请问如何调整它的位置。

注:

想让默认时的位置跟点击时的一样,都在点击的点的上方。

image image

代码如下:

- (AAOptions *)configureDashStyleTypeMixedChart {
    NSArray *linesColors;

    linesColors = @[@"#05C2CB",@"#ACABAE",@"#ACABAE",@"#ACABAE",@"#ACABAE",@"#ACABAE",@"#05C2CB"];

    NSString* subTitle = @"";
    NSString* tailString = @"";

    subTitle = @"千克(kg)";
    tailString = @"kg";

    __weak typeof(self) weakSelf = self;
    self.aaChartModel = AAChartModel.new
    .chartTypeSet(AAChartTypeLine)//图表类型
    .colorsThemeSet(linesColors)//设置主题颜色数组
    .backgroundColorSet(@"#FFFFFF")
    .tooltipValueSuffixSet(tailString)//设置浮动提示框单位后缀
    .yAxisLineWidthSet(@0)//Y轴轴线线宽为0即是隐藏Y轴轴线
    .markerRadiusSet(@1)

    .yAxisGridLineStyleSet([AALineStyle styleWithColor:@"#ACABAE" dashStyle:AAChartLineDashStyleTypeSolid width:@0.5])//y轴
    .xAxisGridLineStyleSet([AALineStyle styleWithColor:@"#ACABAE" dashStyle:AAChartLineDashStyleTypeSolid width:@0.5])
    .legendEnabledSet(false)
    .subtitleSet(subTitle)
    .subtitleStyleSet(AAStyleColorSizeWeight(@"#13ADB4", 12, AAChartFontWeightTypeThin))
    .subtitleAlignSet(@"left")
    .connectNullsSet(true)
    .categoriesSet(self.seriesXArray)
    .xAxisLabelsStyleSet(AAStyleColorSizeWeight(@"#00000044", 10, @"500"))
    .yAxisLabelsStyleSet(AAStyleColorSizeWeight(@"#00000044", 10, @"500"))
    .zoomTypeSet(AAChartZoomTypeX)  //重要属性,需要支持 X 轴横向滚动
    ;

        if (self.dataType == 1) {
            self.aaChartModel.yAxisTickPositionsSet(@[@2,@4,@6,@8,@10,@12,@14]);
            self.aaChartModel.yAxisMinSet(@(2));
        }
        else
        {
            self.aaChartModel.yAxisMaxSet(@(5));
            self.aaChartModel.yAxisMinSet(@(0));
        }

    AAOptions *aaOptions = self.aaChartModel.aa_toAAOptions;

    NSString *tooltipFormatter = [NSString stringWithFormat:(@AAJSFunc(function () {
        return '<div style=\"' + 'color:white; padding-left: 10px; padding-right: 10px; border-radius:11px; font-family:SourceHanSansCN-Medium; font-weight: 500; line-height: 18px; box-shadow: 0px 3px 5px 0px rgba(19,173,180,0.4);  font-size:12px\"' + '>'  + this.y.toFixed(1) + '%@'  + '</div>';
    })),tailString];

    aaOptions.tooltipSet(AATooltip.new
    .useHTMLSet(true)
    .sharedSet(false)
    .borderRadiusSet(@30)
    .followTouchMoveSet(false)
    .paddingSet(@2)  //外面宽度
    .formatterSet(tooltipFormatter)
    .backgroundColorSet(@"#13ADB4")
    .borderWidthSet(@0)

    .shadowSet(false)
    .valueDecimalsSet(@(1))//设置取值精确到小数点后几位
    );
    aaOptions.xAxis
    .tickmarkPlacementSet(@"on")
    ;
    aaOptions.plotOptions
    .splineSet(AASpline.new
               .statesSet(AAStates.new
                          .hoverSet(AAHover.new
                                    .lineWidthPlusSet(@2))));
    aaOptions.plotOptions.series.animation = (id)@(false);
    aaOptions.chart.resetZoomButtonSet(
                                       AAResetZoomButton.new
                                       .positionSet(AAPosition.new
                                                    .ySet(@900))
                                       );

    NSInteger defaultSelectedIndex = self.lineDatas.count - 1;
    NSString *javascriptCode = [NSString stringWithFormat:@"function() {\
        var points = [],\
            chart = this,\
            series = chart.series,\
            length = series.length;\
        \
        var pointElement = series[length - 1].data[%ld];\
        points.push(pointElement);\
        chart.tooltip.refresh(points);\
    }", (long)defaultSelectedIndex];

    AAChartEvents *chartEvents = [AAChartEvents new];
    chartEvents.loadSet(javascriptCode);

    aaOptions.chart.eventsSet(chartEvents);
    return aaOptions;
}
AAChartModel commented 11 months ago

试试配置一下 AATooltippositioner 属性看看效果, 参考: