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

大佬,能帮我看看这是为啥吗?更新数据的时候修改的图例不生效 #1479

Closed YuanJiaShuai closed 1 year ago

YuanJiaShuai commented 1 year ago
- (void)setData:(NSDictionary *)data{
    if(data){
        _data = data;

        NSString *today = [NSString stringEmpty:data[@"today"]];
        NSString *yesday = [NSString stringEmpty:data[@"yesday"]];

        NSArray *datas = data[@"datas"];

        NSMutableArray *array1 = [[NSMutableArray alloc]init];
        NSMutableArray *array2 = [[NSMutableArray alloc]init];
        for(int i = 0; i < datas.count; i++){
            NSArray *arr = datas[i];

            AADataLabels *todayDataLabels = AADataLabels.new
                .enabledSet(true)
                .verticalAlignSet(AAChartVerticalAlignTypeMiddle)
                .xSet(@0)
            .cropSet(false)
            .overflowSet(@"none")
            .allowOverlapSet(true)
            .alignSet(AAChartAlignTypeLeft)//设置图例位于水平方向上的右侧
                .ySet(@-10);

            AADataLabels *yesDayDataLabels = AADataLabels.new
                .enabledSet(true)
                .verticalAlignSet(AAChartVerticalAlignTypeMiddle)
                .xSet(@0)
            .cropSet(false)
            .overflowSet(@"none")
            .allowOverlapSet(true)
            .alignSet(AAChartAlignTypeRight)//设置图例位于水平方向上的右侧
                .ySet(@-10);

            CGFloat todayValue = [arr[0] floatValue]/100000000;
            {
                AAStyle *style = [[AAStyle alloc]init];
                style.fontSize = [NSString stringWithFormat:@"%0.2d", 11];
                style.fontFamily = @"DIN-Medium";
                style.fontWeight = @"thin";
                style.color = AARgbaColor(250, 59, 50, 1);

                AADataElement *positiveDataElement = AADataElement.new
                .ySet(@(todayValue))
                .colorSet(AARgbaColor(250, 59, 50, 1))
                .dataLabelsSet(todayDataLabels
                               .formatSet(@"{point.y:.2f} 亿")
                               .styleSet(style));
                [array1 addObject:positiveDataElement];
            }

            CGFloat yesValue = [arr[1] floatValue]/100000000;
            {
                AAStyle *style = [[AAStyle alloc]init];
                style.fontSize = [NSString stringWithFormat:@"%0.2d", 11];
                style.fontFamily = @"DIN-Medium";
                style.fontWeight = @"thin";
                style.color = AARgbaColor(22,125,245, 1);

                AADataElement *positiveDataElement = AADataElement.new
                .ySet(@(yesValue))
                .colorSet(AARgbaColor(22,125,245, 1))
                .dataLabelsSet(yesDayDataLabels
                               .formatSet(@"{point.y:.2f} 亿")
                               .styleSet(style));
                [array2 addObject:positiveDataElement];
            }
        }

        AASeriesElement *element1 = [[AASeriesElement alloc]init];
        element1.name = today;
        element1.data = array1;

        AASeriesElement *element2 = [[AASeriesElement alloc]init];
        element2.name = yesday;
        element2.data = array2;

        [self.upDownChart aa_onlyRefreshTheChartDataWithChartModelSeries:@[element2, element1] animation:YES];
//        [self.upDownChart aa_onlyRefreshTheChartDataWithChartModelSeries:@[element2, element1] animation:YES];
    }
}

这是我更新数据的方法,但是我使用最后一行代码更新图例,结果不生效

image

大佬,能帮我看看这是为啥吗?

AAChartModel commented 1 year ago
/// Function of only refresh the chart data
/// @param series Series array of AAOptions instance object
- (void)aa_onlyRefreshTheChartDataWithOptionsSeries:(NSArray<AASeriesElement *> *)series;

由注释可知, 此方法只会刷新 AASeriesElement 的 data 数组数据, 不会更新图例的名称.

AAChartModel commented 1 year ago

- (void)aa_onlyRefreshTheChartDataWithOptionsSeries:(NSArray<AASeriesElement *> *)series; 此方法实际上调用的是 JavaScript 的这个方法.:

        function onlyRefreshTheChartDataWithSeries(receivedSeries, animation) {
            var receivedSeriesArr = JSON.parse(receivedSeries);
            var seriesArrLength = receivedSeriesArr.length;
            for (var i = 0; i < seriesArrLength; i++) {
                var receivedSeriesElementData = receivedSeriesArr[i].data;
                var seriesElement = aaGlobalChart.series[i];
                seriesElement.setData(receivedSeriesElementData, false);
            }

            var animationBool = (animation == "1") ? true:false;
            aaGlobalChart.redraw(animationBool);
        }

由此可知实际上调用的是 Highchartsseries 更新数据的方法: setData(data [, redraw] [, animation] [, updatePoints])

Highcharts 在线 API 文档:

YuanJiaShuai commented 1 year ago

大佬,如果我想更新图例,应该怎么写啊?我试过用这个方法 [_upDownChart aa_drawChartWithOptions:aaOptions];,但是会用动画

AAChartModel commented 1 year ago

查看 AAChartView 中的这个方法:

/// A common chart update function
/// (you can update any chart element) to open, close, delete, add, resize, reformat, etc. elements in the chart.
/// Refer to https://api.highcharts.com/highcharts#Chart.update
///
/// It should be noted that when updating the array configuration,
/// for example, when updating configuration attributes including arrays such as xAxis, yAxis, series, etc., the updated data will find existing objects based on id and update them. If no id is configured or passed If the id does not find the corresponding object, the first element of the array is updated. Please refer to this example for details.
///
/// In a responsive configuration, the response of the different rules responsive.rules is actually calling chart.update, and the updated content is configured in chartOptions.
///
/// @param options A configuration object for the new chart options as defined in the options section of the API.
/// @param redraw Whether to redraw after updating the chart, the default is true
- (void)aa_updateChartWithOptions:(id)options redraw:(BOOL)redraw;

使用示例:

   AAOptions *testOptions = AAOptions.new
        .seriesSet(@[]);//此处添加你的由 AASeriesElement 元素组成的数组

    [self.aaChartView aa_updateChartWithOptions:testOptions redraw:true];
AAChartModel commented 1 year ago

这个是图表的高级更新功能, AAChartKit 的 demo 中有此方法的详细使用示例. 下载运行查看即可.

此方法的在线文档:

YuanJiaShuai commented 1 year ago

感谢大佬的指点,可以了