carekit-apple / CareKit

CareKit is an open source software framework for creating apps that help people better understand and manage their health.
https://www.researchandcare.org
Other
2.41k stars 443 forks source link

OCKDocumentElementChart not displaying Bar Chart Legend #107

Closed eburns1148 closed 5 years ago

eburns1148 commented 8 years ago

Using the following code using the branches master and stable:

let chart = OCKDocumentElementChart(chart: barChartItem)

The legend at the bottom of the bar chart is not displayed when rendered in the OCKDocument pdf. The legend shows fine for the Insights page. Is this something that must be done manually.

bar chart legend

eburns1148 commented 7 years ago

Is there any update on this issue?

umerkhan-apple commented 7 years ago

@eburns1148 The exporter should be including the bar chart legend automatically. No additional code is required. With iOS 10, there were several layout issues that were introduced in CareKit. Most of those have been resolved in the latest master.

Could you pull the latest master and test against it to make sure the issue still exists?

ninoguba commented 7 years ago

I just tested this in my app using the latest version of master and I can see that the legend doesnt show for all my charts. I have three charts and the legend only appears on the first one.

ninoguba commented 7 years ago

I narrowed the issue on the fixed size of the frame on OCKDocumentElementChart. Depending on how many data you're showing on your chart, you will have to increase the size of the frame to fit your chart + legend views.

However if you're showing less data you need to decrease the size of the frame to lessen the white space.

The fix to this should be to make the frame's height automatically adjust to its content.

ninoguba commented 7 years ago

Here's my workaround to dynamically size the frame of the chart based on the number of values in the data series.

@implementation OCKDocumentElementChart

- (instancetype)initWithChart:(OCKChart *)chart {
    self = [super init];
    if (self) {
        _chart = chart;
    }
    return self;
}

- (NSString *)HTMLContent {
    NSString *html = @"";
    if (_chart) {

        html = [html stringByAppendingString:@"<p>"];

        if (_chart.title) {
            html = [html stringByAppendingFormat:@"<b>%@</b><br/>\n", _chart.title];
        }

        UIView *view = [_chart chartView];

        float height = 320.0;
        if ([_chart isKindOfClass:[OCKBarChart class]]) {
            OCKBarChart *barChart = (OCKBarChart *)_chart;
            height = barChart.dataSeries[0].values.count * 64.5;
        }

        if (view) {
            // This triggers autolayout.
            UITableViewCell *cell = [[UITableViewCell alloc] init];
            [cell.contentView addSubview:view];
            cell.frame = CGRectMake(0, 0, 480, height);
        }

        view.frame = CGRectMake(0, 0, 480, height);
        view.backgroundColor = [UIColor whiteColor];

        html = [html stringByAppendingString:imageTagFromView(view)];

        if (_chart.text) {
            html = [html stringByAppendingFormat:@"<i>%@</i>\n", _chart.text];
        }

        html = [html stringByAppendingString:@"</p>"];

    }
    return html;
}

- (instancetype)copyWithZone:(NSZone *)zone {
    OCKDocumentElementChart *element = [[[self class] allocWithZone:zone] init];
    element->_chart = [_chart copy];
    return element;
}

@end
srinathtm-apple commented 5 years ago

Closing out this issue as this is no longer relevant due to the CareKit 2.0 beta release..