Xuni for IOS のドキュメント
注釈
注釈は、重要なニュースまたはイベントをマークするために、FlexChart 上の特定のデータポイントにアタッチして使用されます。注釈は、画像、図形、テキストなどの任意の要素をチャート上に配置するためにも使用できます。Xuni FlexChart コントロールは、多角形、線、楕円、四角形、画像、テキストなどのさまざまな注釈が組み込まれています。

FlexChart 上の注釈の位置は、position プロパティを Bottom、Center、Left、Right、または Top に設定することで指定できます。FlexChart で注釈の添付方法を指定するには Attachment プロパティを使用し、その値を次のように設定します。

次の図は、注釈を適用した後の FlexChart コントロールを示しています。

注釈を FlexChart コントロールに適用するには、次の手順を実行します。

手順 1:データソースの作成

  1. Project Navigator で、プロジェクトを右クリックし、[新しいファイル]オプションを選択します。
  2. 新しいクラスのテンプレート(Cocoa Class)を選択し、[次へ]をクリックします。
  3. 新しいクラスに「ChartData」と名前を付け、ドロップダウンメニューからサブクラスとして NSObject を選択します。
  4. [終了]をクリックして ChartData.h および ChartData.m をアプリケーションに追加します。
  5. ChartData.swiftファイル、ChartData.h ファイルと ChartData.m ファイルの内容を次のコードに置き換えます。
    class func annotationData() -> NSMutableArray {
            return[
                ChartData(name: "US", sales: 1500, expenses: Double(arc4random()%5001), downloads: Double(arc4random()%20001)),
                ChartData(name: "Germany", sales: 4000, expenses: Double(arc4random()%5001), downloads: Double(arc4random()%20001)),
                ChartData(name: "UK", sales: 3000, expenses: Double(arc4random()%5001), downloads: Double(arc4random()%20001)),
                ChartData(name: "Japan", sales: 6000, expenses: Double(arc4random()%5001), downloads: Double(arc4random()%20001)),
                ChartData(name: "Italy", sales: 3500, expenses: Double(arc4random()%5001), downloads: Double(arc4random()%20001)),
                ChartData(name: "Greece", sales: 8500, expenses: Double(arc4random()%5001), downloads: Double(arc4random()%20001)),
                ChartData(name: "France", sales: 2300, expenses: Double(arc4random()%5001), downloads: Double(arc4random()%20001)),
                ChartData(name: "Spain", sales: 6500, expenses: Double(arc4random()%5001), downloads: Double(arc4random()%20001)),
                ChartData(name: "Ireland", sales: 4500, expenses: Double(arc4random()%5001), downloads: Double(arc4random()%20001)),
                ChartData(name: "Poland", sales: 9500, expenses: Double(arc4random()%5001), downloads: Double(arc4random()%20001))
            ]
        }
    
    
    @interface ChartData : NSObject
    
    @property NSString *name;
    @property NSNumber *sales;
    @property NSNumber *expenses;
    @property NSNumber *downloads;
    
    - (id)initWithName:(NSString *)name sales:(NSNumber *)sales expenses:(NSNumber *)expenses downloads:(NSNumber *)downloads;
    
    + (NSMutableArray *)demoData;
    
    + (NSMutableArray *)annotationData;
    
    @end
    
    @interface ChartPoint : NSObject
    
    @property NSNumber *x;
    @property NSNumber *y;
    
    - (id)initWithX:(NSNumber*)x y:(NSNumber*)y;
    
    + (NSMutableArray *)generateRandomPoints:(NSInteger)count;
    
    @end
    
    #import "ChartData.h"
    
    @implementation ChartData
    
    - (id)initWithName:(NSString *)name sales:(NSNumber *)sales expenses:(NSNumber *)expenses downloads:(NSNumber *)downloads {
        self = [super init];
        if(self){
            _name = name;
            _sales = sales;
            _expenses = expenses;
            _downloads = downloads;
        }
        return self;
    }
    + (NSNumber *)generateRandom:(NSInteger) max {
        return [NSNumber numberWithUnsignedInteger:(arc4random() % max)];
    }
    
    + (NSMutableArray *)demoData {
        ChartData *US = [[ChartData alloc] initWithName:@"US" sales:[ChartData generateRandom : 10001] expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *Germany = [[ChartData alloc] initWithName:@"Germany" sales:[ChartData generateRandom : 10001] expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *UK = [[ChartData alloc] initWithName:@"UK" sales:[ChartData generateRandom : 10001] expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *Japan = [[ChartData alloc] initWithName:@"Japan" sales:[ChartData generateRandom : 10001] expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *Italy = [[ChartData alloc] initWithName:@"Italy" sales:[ChartData generateRandom : 10001] expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *Greece = [[ChartData alloc] initWithName:@"Greece" sales:[ChartData generateRandom : 10001] expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        return [[NSMutableArray alloc] initWithObjects:US, Germany, UK, Japan, Italy, Greece, nil];
    }
    
    + (NSMutableArray *)annotationData {
        ChartData *US = [[ChartData alloc] initWithName:@"US" sales:@1500 expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *Germany = [[ChartData alloc] initWithName:@"Germany" sales:@4000 expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *UK = [[ChartData alloc] initWithName:@"UK" sales:@3000 expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *Japan = [[ChartData alloc] initWithName:@"Japan" sales:@6000 expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *Italy = [[ChartData alloc] initWithName:@"Italy" sales:@3500 expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *Greece = [[ChartData alloc] initWithName:@"Greece" sales:@8500 expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *France = [[ChartData alloc] initWithName:@"France" sales:@2300 expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *Spain = [[ChartData alloc] initWithName:@"Spain" sales:@6500 expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *Ireland = [[ChartData alloc] initWithName:@"Ireland" sales:@4500 expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        ChartData *Poland = [[ChartData alloc] initWithName:@"Poland" sales:@9500 expenses:[ChartData generateRandom : 5001] downloads:[ChartData generateRandom : 20001]];
        
        return [[NSMutableArray alloc] initWithObjects:US, Germany, UK, Japan, Italy, Greece, France, Spain, Ireland, Poland, nil];
    }
    
    @end
    
    @implementation ChartPoint
    
    - (id)initWithX:(NSNumber*)x y:(NSNumber*)y {
        self = [super init];
        if(self){
            _x = x;
            _y = y;
        }
        return self;
    }
    
    + (NSMutableArray *)generateRandomPoints:(NSInteger)count {
        NSMutableArray *points = [[NSMutableArray alloc] init];
        
        if (count % 2 == 1) {
            count++;
        }
        
        for (int i = 0; i < count / 2; i++) {
            do
            {
                double random = rand() / (double)RAND_MAX;
                double u = 2 * random - 1;
                double v = 2 * random - 1;
                
                double s = u * u + v * v;
                
                if (s < 1)
                {
                    s = sqrt(-2 * log(s) / s);
                    [points addObject:[[ChartPoint alloc] initWithX:[NSNumber numberWithInt:i]
                                                                  y:[NSNumber numberWithDouble:u * s]]];
                    [points addObject:[[ChartPoint alloc] initWithX:[NSNumber numberWithInt:i + 1]
                                                                  y:[NSNumber numberWithDouble:v * s]]];
                    break;
                }
            } while (true);
        }
        
        return points;
    }
    
    @end
    

手順 2:FlexChart コントロールの初期化と注釈の適用

ViewController.m ファイルの内容を次のコードに置き換えます。

import UIKit
import XuniFlexChartKit

class ViewController: UIViewController{

    var chart = FlexChart()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "Annotations"
        
        // ビューを読み込んだ後の追加セットアップを行います。
        let sales = XuniSeries(forChart: chart, binding: "sales, sales", name: "Sales")
        
        chart.series.addObject(sales)
        chart.itemsSource = ChartData.annotationData()
        chart.bindingX = "name"
        chart.chartType = XuniChartType.Line
        
        self.addAnnotations(chart)
        self.view.addSubview(chart)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        chart.frame = CGRectMake(0, 55, self.view.bounds.size.width, self.view.bounds.size.height - 55)
    }
    
    func addAnnotations(chart: FlexChart) {
        // 多角形の注釈。
        let polygon = XuniChartPolygonAnnotation(chart: chart)
        polygon.isVisible = true
        polygon.attachment = XuniChartAnnotationAttachment.Absolute
        polygon.color = UIColor.redColor()
        polygon.borderColor = UIColor(red: 20.0 / 255.0, green: 153.0 / 255.0, blue: 212.0 / 255.0, alpha: 1.0)
        polygon.borderWidth = 2
        polygon.textColor = UIColor.blackColor()
        polygon.text = "Absolute"
        polygon.tooltipText = "Polygon Annotation\nPaths:[{0,0},{40,40},{80,0},\n{60,-40},{20,-40}]\nAttachment:Absolute"
        
        let arr = NSMutableArray()
        arr.addObject(XuniPoint(x: 60, y: 40))
        arr.addObject(XuniPoint(x: 20, y: 80))
        arr.addObject(XuniPoint(x: 35, y: 120))
        arr.addObject(XuniPoint(x: 85, y: 120))
        arr.addObject(XuniPoint(x: 100, y: 80))
        polygon.points = arr
        
        // 楕円の注釈
        let ellipse = XuniChartEllipseAnnotation(chart: chart)
        ellipse.isVisible = true
        ellipse.position = XuniChartAnnotationPosition.Center
        ellipse.attachment = XuniChartAnnotationAttachment.Relative
        ellipse.width = 60
        ellipse.height = 40
        ellipse.point = XuniPoint(x: 0.8, y: 0.3)
        ellipse.color = UIColor(red: 241.0 / 255.0, green: 175.0 / 255.0, blue: 102.0 / 255.0, alpha: 1.0)
        ellipse.borderColor = UIColor(red: 188.0 / 255.0, green: 136.0 / 255.0, blue: 80.0 / 255.0, alpha: 1.0)
        ellipse.borderWidth = 2
        ellipse.text = "Relative"
        ellipse.tooltipText = "Ellipse Annotation\nPoint:{0.8,0.3}\nAttachment:Relative"
        
        // 行の注釈。
        let middleY = (chart.axisY.actualDataMax - chart.axisY.actualDataMin) / 2 + chart.axisY.actualDataMin
        let line = XuniChartLineAnnotation(chart: chart)
        line.isVisible = true
        line.attachment = XuniChartAnnotationAttachment.DataCoordinate
        line.start = XuniPoint(x: 0, y: middleY)
        line.end = XuniPoint(x: chart.axisX.actualDataMax, y: middleY)
        line.color = UIColor.blueColor()
        line.lineWidth = 3
        
        // テキストの注釈。
        let text = XuniChartTextAnnotation(chart: chart)
        text.isVisible = true
        text.position = XuniChartAnnotationPosition.Right
        text.attachment = XuniChartAnnotationAttachment.DataCoordinate
        text.width = 80
        text.height = 20
        text.point = XuniPoint(x: 0.3, y: middleY + 200)
        text.font = UIFont.boldSystemFontOfSize(12.0)
        text.text = "Data Coordinate"
        
        // イメージの注釈。
        let image = XuniChartImageAnnotation(chart: chart)
        image.isVisible = true
        image.position = XuniChartAnnotationPosition.Center
        image.attachment = XuniChartAnnotationAttachment.DataIndex
        image.seriesIndex = 0
        image.pointIndex = 2
        image.width = 50
        image.height = 50
        image.offset = XuniPoint(x: 0, y: 10)
        image.source = UIImage(named: "xuni_butterfly")
        image.tooltipText = "Image Annotation\nPointIndex:{2}\nAttachment:DataIndex"
        
        // 長方形の注釈
        let rect = XuniChartRectangleAnnotation(chart: chart)
        rect.isVisible = true
        rect.position = XuniChartAnnotationPosition.Center
        rect.attachment = XuniChartAnnotationAttachment.DataIndex
        rect.seriesIndex = 0
        rect.pointIndex = 6
        rect.width = 100
        rect.height = 50
        rect.color = UIColor(red: 169.0 / 255.0, green: 230.0 / 255.0, blue: 24.0 / 255.0, alpha: 1.0)
        rect.borderColor = UIColor(red: 34.0 / 255.0, green: 166.0 / 255.0, blue: 60.0 / 255.0, alpha: 1.0)
        rect.borderWidth = 2
        rect.text = "DataIndex"
        rect.tooltipText = "Rectangle Annotation\nPointIndex:{6}\nAttachment:DataIndex"
        
        //チャートに注釈を追加します。
        chart.annotations.addObject(polygon)
        chart.annotations.addObject(ellipse)
        chart.annotations.addObject(line)
        chart.annotations.addObject(text)
        chart.annotations.addObject(image)
        chart.annotations.addObject(rect)
    }    
}

#import "ViewController.h"
#import "ChartData.h"
#import "XuniFlexChartKit/XuniFlexChartKit.h"

@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
        // ビューを読み込んだ後の追加セットアップを行います。
        
    FlexChart *chart = [[FlexChart alloc] init];
    NSMutableArray *chartData = [ChartData annotationData];
    chart.bindingX = @"name";
    chart.chartType = XuniChartTypeLine;
    XuniSeries *sales = [[XuniSeries alloc] initForChart:chart binding:@"sales, sales" name:@"Sales"];
    [chart.series addObject:sales];
    chart.itemsSource = chartData;
    [self addAnnotations:chart];
    
    chart.tag = 1;
    [self.view addSubview:chart];
}

- (void)addAnnotations:(FlexChart *)chart {
    // 多角形の注釈。
    XuniChartPolygonAnnotation *polygon = [[XuniChartPolygonAnnotation alloc] initWithChart:chart];
    polygon.isVisible = YES;
    polygon.attachment = XuniChartAnnotationAttachmentAbsolute;
    polygon.color = [UIColor redColor];
    polygon.borderColor = [UIColor colorWithRed:20.0 / 255.0 green:153.0 / 255.0 blue:212.0 / 255.0 alpha:1.0];
    polygon.borderWidth = 2;
    polygon.textColor = [UIColor blackColor];
    polygon.text = @"Absolute";
    polygon.tooltipText = @"Polygon Annotation\nPaths:[{0,0},{40,40},{80,0},\n{60,-40},{20,-40}]\nAttachment:Absolute";
    
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    [arr addObject:[[XuniPoint alloc] initX:60 Y:40]];
    [arr addObject:[[XuniPoint alloc] initX:20 Y:80]];
    [arr addObject:[[XuniPoint alloc] initX:35 Y:120]];
    [arr addObject:[[XuniPoint alloc] initX:85 Y:120]];
    [arr addObject:[[XuniPoint alloc] initX:100 Y:80]];
    polygon.points = arr;
    
    // 楕円の注釈
    XuniChartEllipseAnnotation *ellipse = [[XuniChartEllipseAnnotation alloc] initWithChart:chart];
    ellipse.isVisible = YES;
    ellipse.position = XuniChartAnnotationPositionCenter;
    ellipse.attachment = XuniChartAnnotationAttachmentRelative;
    ellipse.width = 60;
    ellipse.height = 40;
    ellipse.point = [[XuniPoint alloc] initX:0.8 Y:0.3];
    ellipse.color = [UIColor colorWithRed:241.0 / 255.0 green:175.0 / 255.0 blue:102.0 / 255.0 alpha:1.0];
    ellipse.borderColor = [UIColor colorWithRed:188.0 / 255.0 green:136.0 / 255.0 blue:80.0 / 255.0 alpha:1.0];
    ellipse.borderWidth = 2;
    ellipse.text = @"Relative";
    ellipse.tooltipText = @"Ellipse Annotation\nPoint:{0.8,0.3}\nAttachment:Relative";
    
    // 行の注釈。
    double middleY = (chart.axisY.actualDataMax - chart.axisY.actualDataMin) / 2 + chart.axisY.actualDataMin;
    XuniChartLineAnnotation *line = [[XuniChartLineAnnotation alloc] initWithChart:chart];
    line.isVisible = YES;
    line.attachment = XuniChartAnnotationAttachmentDataCoordinate;
    line.start = [[XuniPoint alloc] initX:0 Y:middleY];
    line.end = [[XuniPoint alloc] initX:chart.axisX.actualDataMax Y:middleY];
    line.color = [UIColor blueColor];
    line.lineWidth = 3;
    
    // テキストの注釈。
    XuniChartTextAnnotation *text = [[XuniChartTextAnnotation alloc] initWithChart:chart];
    text.isVisible = YES;
    text.position = XuniChartAnnotationPositionRight;
    text.attachment = XuniChartAnnotationAttachmentDataCoordinate;
    text.width = 80;
    text.height = 20;
    text.point = [[XuniPoint alloc] initX:0.3 Y:middleY + 200];
    text.font = [UIFont boldSystemFontOfSize:12.0];
    text.text = @"Data Coordinate";
    
    // イメージの注釈。
    XuniChartImageAnnotation *image = [[XuniChartImageAnnotation alloc] initWithChart:chart];
    image.isVisible = YES;
    image.position = XuniChartAnnotationPositionCenter;
    image.attachment = XuniChartAnnotationAttachmentDataIndex;
    image.seriesIndex = 0;
    image.pointIndex = 2;
    image.width = 50;
    image.height = 50;
    image.offset = [[XuniPoint alloc] initX:0 Y:10];
    image.source = [UIImage imageNamed:@"xuni_butterfly"];
    image.tooltipText = @"Image Annotation\nPointIndex:{2}\nAttachment:DataIndex";
    
    // 長方形の注釈
    XuniChartRectangleAnnotation *rect = [[XuniChartRectangleAnnotation alloc] initWithChart:chart];
    rect.isVisible = YES;
    rect.position = XuniChartAnnotationPositionCenter;
    rect.attachment = XuniChartAnnotationAttachmentDataIndex;
    rect.seriesIndex = 0;
    rect.pointIndex = 6;
    rect.width = 100;
    rect.height = 50;
    rect.color = [UIColor colorWithRed:169.0 / 255.0 green:230.0 / 255.0 blue:24.0 / 255.0 alpha:1.0];
    rect.borderColor = [UIColor colorWithRed:34.0 / 255.0 green:166.0 / 255.0 blue:60.0 / 255.0 alpha:1.0];
    rect.borderWidth = 2;
    rect.text = @"DataIndex";
    rect.tooltipText = @"Rectangle Annotation\nPointIndex:{6}\nAttachment:DataIndex";
    
    // チャートに注釈を追加します。
    [chart.annotations addObject:polygon];
    [chart.annotations addObject:ellipse];
    [chart.annotations addObject:line];
    [chart.annotations addObject:text];
    [chart.annotations addObject:image];
    [chart.annotations addObject:rect];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    FlexChart *chart = (FlexChart*)[self.view viewWithTag:1];
    chart.frame = CGRectMake(0, 55, self.view.bounds.size.width, self.view.bounds.size.height - 55);
    [chart setNeedsDisplay];
}
@end

 

 


Copyright © GrapeCity inc. All rights reserved.