You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为HighCharts的yAxis stackLabels设置动态文本?Objective-C实现求助

How to Implement Dynamic Stack Labels with Grade Text in Highcharts (Objective-C)

Hey there! Let's figure out how to get those dynamic stack labels showing grades like Good, Excellent, Poor, etc. working in Objective-C for Highcharts iOS. The JS example uses a custom qTotals property, but since Highcharts' iOS API doesn't have that built into stackLabels, we'll replicate the logic using the formatter callback and stack totals instead.

Step 1: Set Up Your Grade Mapping

First, define how stack totals map to your desired grade labels. Adjust the numeric thresholds to match your exact requirements:

// Define your grade rules – tweak these values based on your needs
NSDictionary *gradeThresholds = @{
    @"Excellent": @(90),  // Total ≥ 90 → Excellent
    @"Good": @(70),       // 70 ≤ Total < 90 → Good
    @"Average": @(50),    // 50 ≤ Total < 70 → Average
    @"Below Average": @(30), // 30 ≤ Total < 50 → Below Average
    @"Poor": @(0)         // Total < 30 → Poor
};

Step 2: Configure Y-Axis Stack Labels

Next, set up the y-axis stack labels with a formatter block. This block will calculate the stack total and map it to the correct grade:

// Create and configure the y-axis
HCYAxis *yAxis = [[HCYAxis alloc] init];
yAxis.stackLabels = [[HCStackLabels alloc] init];
yAxis.stackLabels.enabled = @YES;

// Use the formatter callback to generate dynamic grade text
yAxis.stackLabels.formatter = ^NSString *(HCFormatterContext *context) {
    // Get the total value of the current stack
    NSNumber *stackTotal = context.total;
    if (!stackTotal) return @"";
    
    // Match the total to the corresponding grade
    NSString *grade = @"Poor";
    CGFloat totalValue = [stackTotal floatValue];
    
    if (totalValue >= [gradeThresholds[@"Excellent"] floatValue]) {
        grade = @"Excellent";
    } else if (totalValue >= [gradeThresholds[@"Good"] floatValue]) {
        grade = @"Good";
    } else if (totalValue >= [gradeThresholds[@"Average"] floatValue]) {
        grade = @"Average";
    } else if (totalValue >= [gradeThresholds[@"Below Average"] floatValue]) {
        grade = @"Below Average";
    }
    
    return grade;
};

Step 3: Configure Stacked Series

Make sure your chart series are set to stack together (use the same stack identifier) so Highcharts calculates the correct totals:

// Example stacked column series
HCColumnSeries *series1 = [[HCColumnSeries alloc] init];
series1.name = @"First Series";
series1.data = @[@45, @75, @95];
series1.stack = @"gradeStack"; // Same stack ID for all stacked series

HCColumnSeries *series2 = [[HCColumnSeries alloc] init];
series2.name = @"Second Series";
series2.data = @[@30, @15, @5];
series2.stack = @"gradeStack";

// Add series and y-axis to the chart
HCHighcharts *chart = [[HCHighcharts alloc] initWithOptions:@{}];
chart.yAxis = @[yAxis];
chart.series = @[series1, series2];

// Render the chart in a HCChartView
HCChartView *chartView = [[HCChartView alloc] initWithFrame:self.view.bounds];
chartView.options = chart.options;
[self.view addSubview:chartView];

Key Notes:

  • The HCFormatterContext's total property gives you the sum of all stacked values for that category – this replaces the custom qTotals logic from the JS example.
  • Double-check that all stacked series share the same stack value; without this, Highcharts won't calculate combined totals for the labels.
  • If you need more complex logic (like custom calculations beyond simple totals), you can store additional data in your series' userOptions property and access it via the formatter context.

内容的提问来源于stack exchange,提问作者Kuldeep

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:36:07