03 August 2015

Data


  • ChartDataEntry 条目 (一个x下标,一个y值)
  • ChartDataSet 数据集合 (一系列的条目)
  • ChartData 数据 (多个数据集)

ChartDataEntry.swift

//
//  ChartDataEntry.swift
//  Charts
//
//  Created by Daniel Cohen Gindi on 23/2/15.

// // Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda // A port of MPAndroidChart for iOS // Licensed under Apache License 2.0 // // https://github.com/danielgindi/ios-charts //

import Foundation

// Swift标准库定义了一个Equatable协议,要求任何遵循的类型实现等式 == 和不等式 != 对两个该类型进行比较。所有的Swift标准类型自动支持Equatable协议 public class ChartDataEntry: NSObject, Equatable { /// the actual value (y axis) /// y值 public var value = Double(0.0)

<span class="c1">/// the index on the x-axis</span>
<span class="c1">/// x坐标值</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">xIndex</span> <span class="o">=</span> <span class="kt">Int</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

<span class="c1">/// optional spot for additional data this Entry represents</span>
<span class="c1">/// 条目 额外可选数据</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">data</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="p">?</span>

<span class="kd">public</span> <span class="k">override</span> <span class="nf">init</span><span class="p">()</span>
<span class="p">{</span>
    <span class="k">super</span><span class="o">.</span><span class="nf">init</span><span class="p">()</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="nf">init</span><span class="p">(</span><span class="nv">value</span><span class="p">:</span> <span class="kt">Double</span><span class="p">,</span> <span class="nv">xIndex</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">super</span><span class="o">.</span><span class="nf">init</span><span class="p">()</span>

    <span class="k">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
    <span class="k">self</span><span class="o">.</span><span class="n">xIndex</span> <span class="o">=</span> <span class="n">xIndex</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="nf">init</span><span class="p">(</span><span class="nv">value</span><span class="p">:</span> <span class="kt">Double</span><span class="p">,</span> <span class="nv">xIndex</span><span class="p">:</span> <span class="kt">Int</span><span class="p">,</span> <span class="nv">data</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="p">?)</span>
<span class="p">{</span>
    <span class="k">super</span><span class="o">.</span><span class="nf">init</span><span class="p">()</span>

    <span class="k">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
    <span class="k">self</span><span class="o">.</span><span class="n">xIndex</span> <span class="o">=</span> <span class="n">xIndex</span>
    <span class="k">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">data</span>
<span class="p">}</span>

<span class="c1">// MARK: NSObject</span>
<span class="c1">// objective-c : #pragram mark - NSObject</span>

<span class="kd">public</span> <span class="k">override</span> <span class="kd">func</span> <span class="nf">isEqual</span><span class="p">(</span><span class="nv">object</span><span class="p">:</span> <span class="kt">AnyObject</span><span class="p">?)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">object</span> <span class="o">===</span> <span class="kc">nil</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">false</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">object</span><span class="o">!.</span><span class="nf">isKindOfClass</span><span class="p">(</span><span class="k">self</span><span class="o">.</span><span class="k">dynamicType</span><span class="p">))</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">false</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">object</span><span class="o">!.</span><span class="n">data</span> <span class="o">!==</span> <span class="n">data</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">object</span><span class="o">!.</span><span class="n">data</span><span class="o">.</span><span class="nf">isEqual</span><span class="p">(</span><span class="k">self</span><span class="o">.</span><span class="n">data</span><span class="p">))</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">false</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">object</span><span class="o">!.</span><span class="n">xIndex</span> <span class="o">!=</span> <span class="n">xIndex</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">false</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="nf">fabs</span><span class="p">(</span><span class="n">object</span><span class="o">!.</span><span class="n">value</span> <span class="o">-</span> <span class="n">value</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mf">0.00001</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">false</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="kc">true</span>
<span class="p">}</span>

<span class="c1">// MARK: NSObject</span>
<span class="c1">// toString</span>
<span class="kd">public</span> <span class="k">override</span> <span class="k">var</span> <span class="nv">description</span><span class="p">:</span> <span class="kt">String</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="s">"ChartDataEntry, xIndex: </span><span class="se">\(</span><span class="n">xIndex</span><span class="se">)</span><span class="s">, value </span><span class="se">\(</span><span class="n">value</span><span class="se">)</span><span class="s">"</span>
<span class="p">}</span>

<span class="c1">// MARK: NSCopying</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">copyWithZone</span><span class="p">(</span><span class="nv">zone</span><span class="p">:</span> <span class="kt">NSZone</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">AnyObject</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">copy</span> <span class="o">=</span> <span class="k">self</span><span class="o">.</span><span class="k">dynamicType</span><span class="o">.</span><span class="nf">allocWithZone</span><span class="p">(</span><span class="n">zone</span><span class="p">)</span> <span class="k">as</span> <span class="kt">ChartDataEntry</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">xIndex</span> <span class="o">=</span> <span class="n">xIndex</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">data</span>
    <span class="k">return</span> <span class="n">copy</span>
<span class="p">}</span>

}

public func ==(lhs: ChartDataEntry, rhs: ChartDataEntry) -> Bool { if (lhs === rhs) { return true }

<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">lhs</span><span class="o">.</span><span class="nf">isKindOfClass</span><span class="p">(</span><span class="n">rhs</span><span class="o">.</span><span class="k">dynamicType</span><span class="p">))</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="kc">false</span>
<span class="p">}</span>

<span class="k">if</span> <span class="p">(</span><span class="n">lhs</span><span class="o">.</span><span class="n">data</span> <span class="o">!==</span> <span class="n">rhs</span><span class="o">.</span><span class="n">data</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">lhs</span><span class="o">.</span><span class="n">data</span><span class="o">!.</span><span class="nf">isEqual</span><span class="p">(</span><span class="n">rhs</span><span class="o">.</span><span class="n">data</span><span class="p">))</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="kc">false</span>
<span class="p">}</span>

<span class="k">if</span> <span class="p">(</span><span class="n">lhs</span><span class="o">.</span><span class="n">xIndex</span> <span class="o">!=</span> <span class="n">rhs</span><span class="o">.</span><span class="n">xIndex</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="kc">false</span>
<span class="p">}</span>

<span class="k">if</span> <span class="p">(</span><span class="nf">fabs</span><span class="p">(</span><span class="n">lhs</span><span class="o">.</span><span class="n">value</span> <span class="o">-</span> <span class="n">rhs</span><span class="o">.</span><span class="n">value</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mf">0.00001</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="kc">false</span>
<span class="p">}</span>

<span class="k">return</span> <span class="kc">true</span>

}

ChartDataSet

Swift 提供了三种不同的访问级别。这些访问级别相对于源文件中定义的实体,同时也相对于这些源文件所属的模块。

  1. Public:可以访问自己模块或应用中源文件里的任何实体,别人也可以访问引入该模块中源文件里的所有实体。通常情况下,某个接口或 Framework 是可以被任何人使用时,你可以将其设置为 public 级别。
  2. Internal:可以访问自己模块或应用中源文件里的任何实体,但是别人不能访问该模块中源文件里的实体。通常情况下,某个接口或 Framework 作为内部结构使用时,你可以将其设置为 internal 级别。
  3. Private:只能在当前源文件中使用的实体,称为私有实体。使用 private 级别,可以用作隐藏某些功能的实现细节。

Public 为最高级访问级别,Private 为最低级访问级别。

//
//  ChartDataSet.swift
//  Charts
//
//  Created by Daniel Cohen Gindi on 23/2/15.

// // Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda // A port of MPAndroidChart for iOS // Licensed under Apache License 2.0 // // https://github.com/danielgindi/ios-charts //

import Foundation import UIKit

public class ChartDataSet: NSObject { // 颜色,循环使用 public var colors = UIColor // y值集合 internal var yVals: [ChartDataEntry]! // y值最大值 internal var yMax = Double(0.0) // y值最小值 internal var yMin = Double(0.0) // y值总和 internal var yValueSum = Double(0.0)

<span class="c1">/// the last start value used for calcMinMax</span>
<span class="kd">internal</span> <span class="k">var</span> <span class="nv">_lastStart</span><span class="p">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="mi">0</span>

<span class="c1">/// the last end value used for calcMinMax</span>
<span class="kd">internal</span> <span class="k">var</span> <span class="nv">_lastEnd</span><span class="p">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="mi">0</span>

<span class="c1">// 标签</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">label</span><span class="p">:</span> <span class="kt">String</span><span class="p">?</span> <span class="o">=</span> <span class="s">"DataSet"</span>
<span class="c1">// 可用</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">visible</span> <span class="o">=</span> <span class="kc">true</span>
<span class="c1">// 是否绘画y值</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">drawValuesEnabled</span> <span class="o">=</span> <span class="kc">true</span>

<span class="c1">/// the color used for the value-text</span>
<span class="c1">/// y值文字的种颜色</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">valueTextColor</span><span class="p">:</span> <span class="kt">UIColor</span> <span class="o">=</span> <span class="kt">UIColor</span><span class="o">.</span><span class="nf">blackColor</span><span class="p">()</span>

<span class="c1">/// the font for the value-text labels</span>
<span class="n">y值字体</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">valueFont</span><span class="p">:</span> <span class="kt">UIFont</span> <span class="o">=</span> <span class="kt">UIFont</span><span class="o">.</span><span class="nf">systemFontOfSize</span><span class="p">(</span><span class="mf">7.0</span><span class="p">)</span>

<span class="c1">/// the formatter used to customly format the values</span>
<span class="c1">// 自定义格式化</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">valueFormatter</span><span class="p">:</span> <span class="kt">NSNumberFormatter</span><span class="p">?</span>

<span class="c1">/// the axis this DataSet should be plotted against.</span>
<span class="c1">// 根据哪个y坐标, 左?右?</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">axisDependency</span> <span class="o">=</span> <span class="kt">ChartYAxis</span><span class="o">.</span><span class="kt">AxisDependency</span><span class="o">.</span><span class="kt">Left</span>

<span class="c1">// 外部可访问属性</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">yVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">ChartDataEntry</span><span class="p">]</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_yVals</span> <span class="p">}</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">yValueSum</span><span class="p">:</span> <span class="kt">Double</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_yValueSum</span> <span class="p">}</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">yMin</span><span class="p">:</span> <span class="kt">Double</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_yMin</span> <span class="p">}</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">yMax</span><span class="p">:</span> <span class="kt">Double</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_yMax</span> <span class="p">}</span>

<span class="c1">/// if true, value highlighting is enabled</span>
<span class="c1">// 能否高亮</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">highlightEnabled</span> <span class="o">=</span> <span class="kc">true</span>

<span class="c1">/// :returns: true if value highlighting is enabled for this dataset</span>
<span class="c1">// 是否高亮状态</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">isHighlightEnabled</span><span class="p">:</span> <span class="kt">Bool</span> <span class="p">{</span> <span class="k">return</span> <span class="n">highlightEnabled</span> <span class="p">}</span>

<span class="kd">public</span> <span class="k">override</span> <span class="nf">init</span><span class="p">()</span>
<span class="p">{</span>
    <span class="k">super</span><span class="o">.</span><span class="nf">init</span><span class="p">()</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="nf">init</span><span class="p">(</span><span class="nv">yVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">ChartDataEntry</span><span class="p">]?,</span> <span class="nv">label</span><span class="p">:</span> <span class="kt">String</span><span class="p">?)</span>
<span class="p">{</span>
    <span class="k">super</span><span class="o">.</span><span class="nf">init</span><span class="p">()</span>

    <span class="k">self</span><span class="o">.</span><span class="n">label</span> <span class="o">=</span> <span class="n">label</span>
    <span class="n">_yVals</span> <span class="o">=</span> <span class="n">yVals</span> <span class="o">==</span> <span class="kc">nil</span> <span class="p">?</span> <span class="p">[</span><span class="kt">ChartDataEntry</span><span class="p">]()</span> <span class="p">:</span> <span class="n">yVals</span>

    <span class="c1">// default color</span>
    <span class="n">colors</span><span class="o">.</span><span class="nf">append</span><span class="p">(</span><span class="kt">UIColor</span><span class="p">(</span><span class="nv">red</span><span class="p">:</span> <span class="mf">140.0</span><span class="o">/</span><span class="mf">255.0</span><span class="p">,</span> <span class="nv">green</span><span class="p">:</span> <span class="mf">234.0</span><span class="o">/</span><span class="mf">255.0</span><span class="p">,</span> <span class="nv">blue</span><span class="p">:</span> <span class="mf">255.0</span><span class="o">/</span><span class="mf">255.0</span><span class="p">,</span> <span class="nv">alpha</span><span class="p">:</span> <span class="mf">1.0</span><span class="p">))</span>

    <span class="k">self</span><span class="o">.</span><span class="nf">calcMinMax</span><span class="p">(</span><span class="nv">start</span><span class="p">:</span> <span class="n">_lastStart</span><span class="p">,</span> <span class="nv">end</span><span class="p">:</span> <span class="n">_lastEnd</span><span class="p">)</span>
    <span class="k">self</span><span class="o">.</span><span class="nf">calcYValueSum</span><span class="p">()</span>
<span class="p">}</span>

<span class="c1">// 便利构造方法</span>
<span class="kd">public</span> <span class="n">convenience</span> <span class="nf">init</span><span class="p">(</span><span class="nv">yVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">ChartDataEntry</span><span class="p">]?)</span>
<span class="p">{</span>
    <span class="k">self</span><span class="o">.</span><span class="nf">init</span><span class="p">(</span><span class="nv">yVals</span><span class="p">:</span> <span class="n">yVals</span><span class="p">,</span> <span class="nv">label</span><span class="p">:</span> <span class="s">"DataSet"</span><span class="p">)</span>
<span class="p">}</span>

<span class="c1">/// Use this method to tell the data set that the underlying data has changed</span>
<span class="c1">// 当数据改变时调用</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">notifyDataSetChanged</span><span class="p">()</span>
<span class="p">{</span>
    <span class="nf">calcMinMax</span><span class="p">(</span><span class="nv">start</span><span class="p">:</span> <span class="n">_lastStart</span><span class="p">,</span> <span class="nv">end</span><span class="p">:</span> <span class="n">_lastEnd</span><span class="p">)</span>
    <span class="nf">calcYValueSum</span><span class="p">()</span>
<span class="p">}</span>

<span class="kd">internal</span> <span class="kd">func</span> <span class="nf">calcMinMax</span><span class="p">(</span><span class="nv">#start</span> <span class="p">:</span> <span class="kt">Int</span><span class="p">,</span> <span class="nv">end</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">let</span> <span class="nv">yValCount</span> <span class="o">=</span> <span class="n">_yVals</span><span class="o">.</span><span class="n">count</span>

    <span class="k">if</span> <span class="n">yValCount</span> <span class="o">==</span> <span class="mi">0</span>
    <span class="p">{</span>
        <span class="k">return</span>
    <span class="p">}</span>

    <span class="k">var</span> <span class="nv">endValue</span> <span class="p">:</span> <span class="kt">Int</span>

    <span class="k">if</span> <span class="n">end</span> <span class="o">==</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">end</span> <span class="o">&gt;=</span> <span class="n">yValCount</span>
    <span class="p">{</span>
        <span class="n">endValue</span> <span class="o">=</span> <span class="n">yValCount</span> <span class="o">-</span> <span class="mi">1</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="n">endValue</span> <span class="o">=</span> <span class="n">end</span>
    <span class="p">}</span>

    <span class="n">_lastStart</span> <span class="o">=</span> <span class="n">start</span>
    <span class="n">_lastEnd</span> <span class="o">=</span> <span class="n">endValue</span>

    <span class="n">_yMin</span> <span class="o">=</span> <span class="kt">DBL_MAX</span>
    <span class="n">_yMax</span> <span class="o">=</span> <span class="o">-</span><span class="kt">DBL_MAX</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="n">start</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">endValue</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">let</span> <span class="nv">e</span> <span class="o">=</span> <span class="n">_yVals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">e</span><span class="o">.</span><span class="n">value</span><span class="o">.</span><span class="n">isNaN</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">value</span> <span class="o">&lt;</span> <span class="n">_yMin</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">_yMin</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">value</span> <span class="o">&gt;</span> <span class="n">_yMax</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">_yMax</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">_yMin</span> <span class="o">==</span> <span class="kt">DBL_MAX</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_yMin</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">_yMax</span> <span class="o">=</span> <span class="mf">0.0</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kd">private</span> <span class="kd">func</span> <span class="nf">calcYValueSum</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">_yValueSum</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">for</span> <span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_yVals</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span>
    <span class="p">{</span>
        <span class="n">_yValueSum</span> <span class="o">+=</span> <span class="nf">fabs</span><span class="p">(</span><span class="n">_yVals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="k">var</span> <span class="nv">entryCount</span><span class="p">:</span> <span class="kt">Int</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_yVals</span><span class="o">!.</span><span class="n">count</span><span class="p">;</span> <span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">yValForXIndex</span><span class="p">(</span><span class="nv">x</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Double</span>
<span class="p">{</span>
    <span class="k">let</span> <span class="nv">e</span> <span class="o">=</span> <span class="k">self</span><span class="o">.</span><span class="nf">entryForXIndex</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">e</span> <span class="o">!==</span> <span class="kc">nil</span> <span class="o">&amp;&amp;</span> <span class="n">e</span><span class="o">!.</span><span class="n">xIndex</span> <span class="o">==</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span> <span class="k">return</span> <span class="n">e</span><span class="o">!.</span><span class="n">value</span> <span class="p">}</span>
    <span class="k">else</span> <span class="p">{</span> <span class="k">return</span> <span class="kt">Double</span><span class="o">.</span><span class="kt">NaN</span> <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// Returns the first Entry object found at the given xIndex with binary search. </span>
<span class="c1">/// If the no Entry at the specifed x-index is found, this method returns the Entry at the closest x-index. </span>
<span class="c1">/// Returns nil if no Entry object at that index.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">entryForXIndex</span><span class="p">(</span><span class="nv">x</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">ChartDataEntry</span><span class="p">?</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">index</span> <span class="o">=</span> <span class="k">self</span><span class="o">.</span><span class="nf">entryIndex</span><span class="p">(</span><span class="nv">xIndex</span><span class="p">:</span> <span class="n">x</span><span class="p">)</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">index</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="n">_yVals</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="kc">nil</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">entriesForXIndex</span><span class="p">(</span><span class="nv">x</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">[</span><span class="kt">ChartDataEntry</span><span class="p">]</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">entries</span> <span class="o">=</span> <span class="p">[</span><span class="kt">ChartDataEntry</span><span class="p">]()</span>

    <span class="k">var</span> <span class="nv">low</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">var</span> <span class="nv">high</span> <span class="o">=</span> <span class="n">_yVals</span><span class="o">.</span><span class="n">count</span> <span class="o">-</span> <span class="mi">1</span>

    <span class="k">while</span> <span class="p">(</span><span class="n">low</span> <span class="o">&lt;=</span> <span class="n">high</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">var</span> <span class="nv">m</span> <span class="o">=</span> <span class="kt">Int</span><span class="p">((</span><span class="n">high</span> <span class="o">+</span> <span class="n">low</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
        <span class="k">var</span> <span class="nv">entry</span> <span class="o">=</span> <span class="n">_yVals</span><span class="p">[</span><span class="n">m</span><span class="p">]</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">==</span> <span class="n">entry</span><span class="o">.</span><span class="n">xIndex</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">while</span> <span class="p">(</span><span class="n">m</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">_yVals</span><span class="p">[</span><span class="n">m</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">xIndex</span> <span class="o">==</span> <span class="n">x</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">m</span><span class="o">--</span>
            <span class="p">}</span>

            <span class="n">high</span> <span class="o">=</span> <span class="n">_yVals</span><span class="o">.</span><span class="n">count</span>
            <span class="k">for</span> <span class="p">(;</span> <span class="n">m</span> <span class="o">&lt;</span> <span class="n">high</span><span class="p">;</span> <span class="n">m</span><span class="o">++</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">entry</span> <span class="o">=</span> <span class="n">_yVals</span><span class="p">[</span><span class="n">m</span><span class="p">]</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="n">xIndex</span> <span class="o">==</span> <span class="n">x</span><span class="p">)</span>
                <span class="p">{</span>
                    <span class="n">entries</span><span class="o">.</span><span class="nf">append</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
                <span class="p">}</span>
                <span class="k">else</span>
                <span class="p">{</span>
                    <span class="k">break</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="n">_yVals</span><span class="p">[</span><span class="n">m</span><span class="p">]</span><span class="o">.</span><span class="n">xIndex</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">low</span> <span class="o">=</span> <span class="n">m</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="p">}</span>
        <span class="k">else</span>
        <span class="p">{</span>
            <span class="n">high</span> <span class="o">=</span> <span class="n">m</span> <span class="o">-</span> <span class="mi">1</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="n">entries</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">entryIndex</span><span class="p">(</span><span class="n">xIndex</span> <span class="nv">x</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">low</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="k">var</span> <span class="nv">high</span> <span class="o">=</span> <span class="n">_yVals</span><span class="o">.</span><span class="n">count</span> <span class="o">-</span> <span class="mi">1</span>
    <span class="k">var</span> <span class="nv">closest</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>

    <span class="k">while</span> <span class="p">(</span><span class="n">low</span> <span class="o">&lt;=</span> <span class="n">high</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">var</span> <span class="nv">m</span> <span class="o">=</span> <span class="p">(</span><span class="n">high</span> <span class="o">+</span> <span class="n">low</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span>
        <span class="k">var</span> <span class="nv">entry</span> <span class="o">=</span> <span class="n">_yVals</span><span class="p">[</span><span class="n">m</span><span class="p">]</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">==</span> <span class="n">entry</span><span class="o">.</span><span class="n">xIndex</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">while</span> <span class="p">(</span><span class="n">m</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="n">_yVals</span><span class="p">[</span><span class="n">m</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">xIndex</span> <span class="o">==</span> <span class="n">x</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">m</span><span class="o">--</span>
            <span class="p">}</span>

            <span class="k">return</span> <span class="n">m</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="n">entry</span><span class="o">.</span><span class="n">xIndex</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">low</span> <span class="o">=</span> <span class="n">m</span> <span class="o">+</span> <span class="mi">1</span>
        <span class="p">}</span>
        <span class="k">else</span>
        <span class="p">{</span>
            <span class="n">high</span> <span class="o">=</span> <span class="n">m</span> <span class="o">-</span> <span class="mi">1</span>
        <span class="p">}</span>

        <span class="n">closest</span> <span class="o">=</span> <span class="n">m</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="n">closest</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">entryIndex</span><span class="p">(</span><span class="n">entry</span> <span class="nv">e</span><span class="p">:</span> <span class="kt">ChartDataEntry</span><span class="p">,</span> <span class="nv">isEqual</span><span class="p">:</span> <span class="kt">Bool</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">isEqual</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_yVals</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">_yVals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="nf">isEqual</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
            <span class="p">{</span>
                <span class="k">return</span> <span class="n">i</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_yVals</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">_yVals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">===</span> <span class="n">e</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="k">return</span> <span class="n">i</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
<span class="p">}</span>

<span class="c1">/// Returns the number of entries this DataSet holds.</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">valueCount</span><span class="p">:</span> <span class="kt">Int</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_yVals</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="p">}</span>

<span class="c1">/// Adds an Entry to the DataSet dynamically.</span>
<span class="c1">/// Entries are added to the end of the list.</span>
<span class="c1">/// This will also recalculate the current minimum and maximum values of the DataSet and the value-sum.</span>
<span class="c1">/// :param: e the entry to add</span>
<span class="c1">/// 动态添加 条目 到 数据集,添加到末尾,自动计算最大值,最小值和总和。</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">addEntry</span><span class="p">(</span><span class="nv">e</span><span class="p">:</span> <span class="kt">ChartDataEntry</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">val</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">_yVals</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_yVals</span> <span class="o">=</span> <span class="p">[</span><span class="kt">ChartDataEntry</span><span class="p">]()</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">_yVals</span><span class="o">.</span><span class="n">count</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_yMax</span> <span class="o">=</span> <span class="n">val</span>
        <span class="n">_yMin</span> <span class="o">=</span> <span class="n">val</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">_yMax</span> <span class="o">&lt;</span> <span class="n">val</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_yMax</span> <span class="o">=</span> <span class="n">val</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">_yMin</span> <span class="o">&gt;</span> <span class="n">val</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_yMin</span> <span class="o">=</span> <span class="n">val</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="n">_yValueSum</span> <span class="o">+=</span> <span class="n">val</span>

    <span class="n">_yVals</span><span class="o">.</span><span class="nf">append</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="p">}</span>

<span class="c1">/// Adds an Entry to the DataSet dynamically.</span>
<span class="c1">/// Entries are added to their appropriate index respective to it's x-index.</span>
<span class="c1">/// This will also recalculate the current minimum and maximum values of the DataSet and the value-sum.</span>
<span class="c1">/// :param: e the entry to add</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">addEntryOrdered</span><span class="p">(</span><span class="nv">e</span><span class="p">:</span> <span class="kt">ChartDataEntry</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">val</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">_yVals</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_yVals</span> <span class="o">=</span> <span class="p">[</span><span class="kt">ChartDataEntry</span><span class="p">]()</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">_yVals</span><span class="o">.</span><span class="n">count</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_yMax</span> <span class="o">=</span> <span class="n">val</span>
        <span class="n">_yMin</span> <span class="o">=</span> <span class="n">val</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">_yMax</span> <span class="o">&lt;</span> <span class="n">val</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_yMax</span> <span class="o">=</span> <span class="n">val</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">_yMin</span> <span class="o">&gt;</span> <span class="n">val</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_yMin</span> <span class="o">=</span> <span class="n">val</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="n">_yValueSum</span> <span class="o">+=</span> <span class="n">val</span>

    <span class="k">if</span> <span class="n">_yVals</span><span class="o">.</span><span class="n">last</span><span class="p">?</span><span class="o">.</span><span class="n">xIndex</span> <span class="o">&gt;</span> <span class="n">e</span><span class="o">.</span><span class="n">xIndex</span>
    <span class="p">{</span>
        <span class="k">var</span> <span class="nv">closestIndex</span> <span class="o">=</span> <span class="nf">entryIndex</span><span class="p">(</span><span class="nv">xIndex</span><span class="p">:</span> <span class="n">e</span><span class="o">.</span><span class="n">xIndex</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">_yVals</span><span class="p">[</span><span class="n">closestIndex</span><span class="p">]</span><span class="o">.</span><span class="n">xIndex</span> <span class="o">&lt;</span> <span class="n">e</span><span class="o">.</span><span class="n">xIndex</span>
        <span class="p">{</span>
            <span class="n">closestIndex</span><span class="o">++</span>
        <span class="p">}</span>
        <span class="n">_yVals</span><span class="o">.</span><span class="nf">insert</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="nv">atIndex</span><span class="p">:</span> <span class="n">closestIndex</span><span class="p">)</span>
        <span class="k">return</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="n">_yVals</span><span class="o">.</span><span class="nf">append</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">removeEntry</span><span class="p">(</span><span class="nv">entry</span><span class="p">:</span> <span class="kt">ChartDataEntry</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">removed</span> <span class="o">=</span> <span class="kc">false</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_yVals</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">_yVals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">===</span> <span class="n">entry</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_yVals</span><span class="o">.</span><span class="nf">removeAtIndex</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
            <span class="n">removed</span> <span class="o">=</span> <span class="kc">true</span>
            <span class="k">break</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">removed</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_yValueSum</span> <span class="o">-=</span> <span class="n">entry</span><span class="o">.</span><span class="n">value</span>
        <span class="nf">calcMinMax</span><span class="p">(</span><span class="nv">start</span><span class="p">:</span> <span class="n">_lastStart</span><span class="p">,</span> <span class="nv">end</span><span class="p">:</span> <span class="n">_lastEnd</span><span class="p">)</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="n">removed</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">removeEntry</span><span class="p">(</span><span class="nv">#xIndex</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">index</span> <span class="o">=</span> <span class="k">self</span><span class="o">.</span><span class="nf">entryIndex</span><span class="p">(</span><span class="nv">xIndex</span><span class="p">:</span> <span class="n">xIndex</span><span class="p">)</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">index</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">var</span> <span class="nv">e</span> <span class="o">=</span> <span class="n">_yVals</span><span class="o">.</span><span class="nf">removeAtIndex</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>

        <span class="n">_yValueSum</span> <span class="o">-=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>
        <span class="nf">calcMinMax</span><span class="p">(</span><span class="nv">start</span><span class="p">:</span> <span class="n">_lastStart</span><span class="p">,</span> <span class="nv">end</span><span class="p">:</span> <span class="n">_lastEnd</span><span class="p">)</span>

        <span class="k">return</span> <span class="kc">true</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="kc">false</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">resetColors</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">colors</span><span class="o">.</span><span class="nf">removeAll</span><span class="p">(</span><span class="nv">keepCapacity</span><span class="p">:</span> <span class="kc">false</span><span class="p">)</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">addColor</span><span class="p">(</span><span class="nv">color</span><span class="p">:</span> <span class="kt">UIColor</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">colors</span><span class="o">.</span><span class="nf">append</span><span class="p">(</span><span class="n">color</span><span class="p">)</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">setColor</span><span class="p">(</span><span class="nv">color</span><span class="p">:</span> <span class="kt">UIColor</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">colors</span><span class="o">.</span><span class="nf">removeAll</span><span class="p">(</span><span class="nv">keepCapacity</span><span class="p">:</span> <span class="kc">false</span><span class="p">)</span>
    <span class="n">colors</span><span class="o">.</span><span class="nf">append</span><span class="p">(</span><span class="n">color</span><span class="p">)</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">colorAt</span><span class="p">(</span><span class="k">var</span> <span class="nv">index</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">UIColor</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">index</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">index</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="n">colors</span><span class="p">[</span><span class="n">index</span> <span class="o">%</span> <span class="n">colors</span><span class="o">.</span><span class="n">count</span><span class="p">]</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="k">var</span> <span class="nv">isVisible</span><span class="p">:</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">visible</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="k">var</span> <span class="nv">isDrawValuesEnabled</span><span class="p">:</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">drawValuesEnabled</span>
<span class="p">}</span>

<span class="c1">/// Checks if this DataSet contains the specified Entry.</span>
<span class="c1">/// :returns: true if contains the entry, false if not.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">contains</span><span class="p">(</span><span class="nv">e</span><span class="p">:</span> <span class="kt">ChartDataEntry</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="k">for</span> <span class="n">entry</span> <span class="k">in</span> <span class="n">_yVals</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">entry</span><span class="o">.</span><span class="nf">isEqual</span><span class="p">(</span><span class="n">e</span><span class="p">))</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="kc">true</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="kc">false</span>
<span class="p">}</span>

<span class="c1">/// Removes all values from this DataSet and recalculates min and max value.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">clear</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">_yVals</span><span class="o">.</span><span class="nf">removeAll</span><span class="p">(</span><span class="nv">keepCapacity</span><span class="p">:</span> <span class="kc">true</span><span class="p">)</span>
    <span class="n">_lastStart</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">_lastEnd</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="nf">notifyDataSetChanged</span><span class="p">()</span>
<span class="p">}</span>

<span class="c1">// MARK: NSObject</span>

<span class="kd">public</span> <span class="k">override</span> <span class="k">var</span> <span class="nv">description</span><span class="p">:</span> <span class="kt">String</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="kt">String</span><span class="p">(</span><span class="nv">format</span><span class="p">:</span> <span class="s">"ChartDataSet, label: %@, %i entries"</span><span class="p">,</span> <span class="nv">arguments</span><span class="p">:</span> <span class="p">[</span><span class="k">self</span><span class="o">.</span><span class="n">label</span> <span class="p">??</span> <span class="s">""</span><span class="p">,</span> <span class="n">_yVals</span><span class="o">.</span><span class="n">count</span><span class="p">])</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="k">override</span> <span class="k">var</span> <span class="nv">debugDescription</span><span class="p">:</span> <span class="kt">String</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">desc</span> <span class="o">=</span> <span class="n">description</span> <span class="o">+</span> <span class="s">":"</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_yVals</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">desc</span> <span class="o">+=</span> <span class="s">"</span><span class="se">\n</span><span class="s">"</span> <span class="o">+</span> <span class="n">_yVals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">description</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="n">desc</span>
<span class="p">}</span>

<span class="c1">// MARK: NSCopying</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">copyWithZone</span><span class="p">(</span><span class="nv">zone</span><span class="p">:</span> <span class="kt">NSZone</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">AnyObject</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">copy</span> <span class="o">=</span> <span class="k">self</span><span class="o">.</span><span class="k">dynamicType</span><span class="o">.</span><span class="nf">allocWithZone</span><span class="p">(</span><span class="n">zone</span><span class="p">)</span> <span class="k">as</span> <span class="kt">ChartDataSet</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">colors</span> <span class="o">=</span> <span class="n">colors</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">_yVals</span> <span class="o">=</span> <span class="n">_yVals</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">_yMax</span> <span class="o">=</span> <span class="n">_yMax</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">_yMin</span> <span class="o">=</span> <span class="n">_yMin</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">_yValueSum</span> <span class="o">=</span> <span class="n">_yValueSum</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">label</span> <span class="o">=</span> <span class="k">self</span><span class="o">.</span><span class="n">label</span>
    <span class="k">return</span> <span class="n">copy</span>
<span class="p">}</span>

}

ChartData

//
//  ChartData.swift
//  Charts
//
//  Created by Daniel Cohen Gindi on 23/2/15.

// // Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda // A port of MPAndroidChart for iOS // Licensed under Apache License 2.0 // // https://github.com/danielgindi/ios-charts //

import Foundation import UIKit

public class ChartData: NSObject { // y最大值 internal var yMax = Double(0.0) // y最小值 internal var yMin = Double(0.0) // 左y轴最大值 internal var leftAxisMax = Double(0.0) // 左y轴最小值 internal var leftAxisMin = Double(0.0) // 右y轴最大值 internal var rightAxisMax = Double(0.0) // 右y轴最小值 internal var rightAxisMin = Double(0.0) // y值总和 private var yValueSum = Double(0.0) // y值个数 private var yValCount = Int(0)

<span class="c1">/// the last start value used for calcMinMax</span>
<span class="kd">internal</span> <span class="k">var</span> <span class="nv">_lastStart</span><span class="p">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="mi">0</span>

<span class="c1">/// the last end value used for calcMinMax</span>
<span class="kd">internal</span> <span class="k">var</span> <span class="nv">_lastEnd</span><span class="p">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="mi">0</span>

<span class="c1">/// the average length (in characters) across all x-value strings</span>
<span class="kd">private</span> <span class="k">var</span> <span class="nv">_xValAverageLength</span> <span class="o">=</span> <span class="kt">Double</span><span class="p">(</span><span class="mf">0.0</span><span class="p">)</span>

<span class="kd">internal</span> <span class="k">var</span> <span class="nv">_xVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">String</span><span class="p">?]</span><span class="o">!</span>
<span class="kd">internal</span> <span class="k">var</span> <span class="nv">_dataSets</span><span class="p">:</span> <span class="p">[</span><span class="kt">ChartDataSet</span><span class="p">]</span><span class="o">!</span>

<span class="kd">public</span> <span class="k">override</span> <span class="nf">init</span><span class="p">()</span>
<span class="p">{</span>
    <span class="k">super</span><span class="o">.</span><span class="nf">init</span><span class="p">()</span>

    <span class="n">_xVals</span> <span class="o">=</span> <span class="p">[</span><span class="kt">String</span><span class="p">?]()</span>
    <span class="n">_dataSets</span> <span class="o">=</span> <span class="p">[</span><span class="kt">ChartDataSet</span><span class="p">]()</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="nf">init</span><span class="p">(</span><span class="nv">xVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">String</span><span class="p">?]?,</span> <span class="nv">dataSets</span><span class="p">:</span> <span class="p">[</span><span class="kt">ChartDataSet</span><span class="p">]?)</span>
<span class="p">{</span>
    <span class="k">super</span><span class="o">.</span><span class="nf">init</span><span class="p">()</span>

    <span class="n">_xVals</span> <span class="o">=</span> <span class="n">xVals</span> <span class="o">==</span> <span class="kc">nil</span> <span class="p">?</span> <span class="p">[</span><span class="kt">String</span><span class="p">?]()</span> <span class="p">:</span> <span class="n">xVals</span>
    <span class="n">_dataSets</span> <span class="o">=</span> <span class="n">dataSets</span> <span class="o">==</span> <span class="kc">nil</span> <span class="p">?</span> <span class="p">[</span><span class="kt">ChartDataSet</span><span class="p">]()</span> <span class="p">:</span> <span class="n">dataSets</span>

    <span class="k">self</span><span class="o">.</span><span class="nf">initialize</span><span class="p">(</span><span class="n">_dataSets</span><span class="p">)</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="nf">init</span><span class="p">(</span><span class="nv">xVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">NSObject</span><span class="p">]?,</span> <span class="nv">dataSets</span><span class="p">:</span> <span class="p">[</span><span class="kt">ChartDataSet</span><span class="p">]?)</span>
<span class="p">{</span>
    <span class="k">super</span><span class="o">.</span><span class="nf">init</span><span class="p">()</span>

    <span class="n">_xVals</span> <span class="o">=</span> <span class="n">xVals</span> <span class="o">==</span> <span class="kc">nil</span> <span class="p">?</span> <span class="p">[</span><span class="kt">String</span><span class="p">?]()</span> <span class="p">:</span> <span class="kt">ChartUtils</span><span class="o">.</span><span class="nf">bridgedObjCGetStringArray</span><span class="p">(</span><span class="nv">objc</span><span class="p">:</span> <span class="n">xVals</span><span class="o">!</span><span class="p">)</span>
    <span class="n">_dataSets</span> <span class="o">=</span> <span class="n">dataSets</span> <span class="o">==</span> <span class="kc">nil</span> <span class="p">?</span> <span class="p">[</span><span class="kt">ChartDataSet</span><span class="p">]()</span> <span class="p">:</span> <span class="n">dataSets</span>

    <span class="k">self</span><span class="o">.</span><span class="nf">initialize</span><span class="p">(</span><span class="n">_dataSets</span><span class="p">)</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="n">convenience</span> <span class="nf">init</span><span class="p">(</span><span class="nv">xVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">String</span><span class="p">?]?)</span>
<span class="p">{</span>
    <span class="k">self</span><span class="o">.</span><span class="nf">init</span><span class="p">(</span><span class="nv">xVals</span><span class="p">:</span> <span class="n">xVals</span><span class="p">,</span> <span class="nv">dataSets</span><span class="p">:</span> <span class="p">[</span><span class="kt">ChartDataSet</span><span class="p">]())</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="n">convenience</span> <span class="nf">init</span><span class="p">(</span><span class="nv">xVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">NSObject</span><span class="p">]?)</span>
<span class="p">{</span>
    <span class="k">self</span><span class="o">.</span><span class="nf">init</span><span class="p">(</span><span class="nv">xVals</span><span class="p">:</span> <span class="n">xVals</span><span class="p">,</span> <span class="nv">dataSets</span><span class="p">:</span> <span class="p">[</span><span class="kt">ChartDataSet</span><span class="p">]())</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="n">convenience</span> <span class="nf">init</span><span class="p">(</span><span class="nv">xVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">String</span><span class="p">?]?,</span> <span class="nv">dataSet</span><span class="p">:</span> <span class="kt">ChartDataSet</span><span class="p">?)</span>
<span class="p">{</span>
    <span class="k">self</span><span class="o">.</span><span class="nf">init</span><span class="p">(</span><span class="nv">xVals</span><span class="p">:</span> <span class="n">xVals</span><span class="p">,</span> <span class="nv">dataSets</span><span class="p">:</span> <span class="n">dataSet</span> <span class="o">===</span> <span class="kc">nil</span> <span class="p">?</span> <span class="nv">nil</span> <span class="p">:</span> <span class="p">[</span><span class="n">dataSet</span><span class="o">!</span><span class="p">])</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="n">convenience</span> <span class="nf">init</span><span class="p">(</span><span class="nv">xVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">NSObject</span><span class="p">]?,</span> <span class="nv">dataSet</span><span class="p">:</span> <span class="kt">ChartDataSet</span><span class="p">?)</span>
<span class="p">{</span>
    <span class="k">self</span><span class="o">.</span><span class="nf">init</span><span class="p">(</span><span class="nv">xVals</span><span class="p">:</span> <span class="n">xVals</span><span class="p">,</span> <span class="nv">dataSets</span><span class="p">:</span> <span class="n">dataSet</span> <span class="o">===</span> <span class="kc">nil</span> <span class="p">?</span> <span class="nv">nil</span> <span class="p">:</span> <span class="p">[</span><span class="n">dataSet</span><span class="o">!</span><span class="p">])</span>
<span class="p">}</span>

<span class="kd">internal</span> <span class="kd">func</span> <span class="nf">initialize</span><span class="p">(</span><span class="nv">dataSets</span><span class="p">:</span> <span class="p">[</span><span class="kt">ChartDataSet</span><span class="p">])</span>
<span class="p">{</span>
    <span class="nf">checkIsLegal</span><span class="p">(</span><span class="n">dataSets</span><span class="p">)</span>

    <span class="nf">calcMinMax</span><span class="p">(</span><span class="nv">start</span><span class="p">:</span> <span class="n">_lastStart</span><span class="p">,</span> <span class="nv">end</span><span class="p">:</span> <span class="n">_lastEnd</span><span class="p">)</span>
    <span class="nf">calcYValueSum</span><span class="p">()</span>
    <span class="nf">calcYValueCount</span><span class="p">()</span>

    <span class="nf">calcXValAverageLength</span><span class="p">()</span>
<span class="p">}</span>

<span class="c1">// calculates the average length (in characters) across all x-value strings</span>
<span class="kd">internal</span> <span class="kd">func</span> <span class="nf">calcXValAverageLength</span><span class="p">()</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">_xVals</span><span class="o">.</span><span class="n">count</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_xValAverageLength</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">return</span>
    <span class="p">}</span>

    <span class="k">var</span> <span class="nv">sum</span> <span class="o">=</span> <span class="mi">1</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_xVals</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">sum</span> <span class="o">+=</span> <span class="n">_xVals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="kc">nil</span> <span class="p">?</span> <span class="mi">0</span> <span class="p">:</span> <span class="nf">count</span><span class="p">(</span><span class="n">_xVals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">!</span><span class="p">)</span>
    <span class="p">}</span>

    <span class="n">_xValAverageLength</span> <span class="o">=</span> <span class="kt">Double</span><span class="p">(</span><span class="n">sum</span><span class="p">)</span> <span class="o">/</span> <span class="kt">Double</span><span class="p">(</span><span class="n">_xVals</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>
<span class="p">}</span>

<span class="c1">// Checks if the combination of x-values array and DataSet array is legal or not.</span>
<span class="c1">// :param: dataSets</span>
<span class="kd">internal</span> <span class="kd">func</span> <span class="nf">checkIsLegal</span><span class="p">(</span><span class="nv">dataSets</span><span class="p">:</span> <span class="p">[</span><span class="kt">ChartDataSet</span><span class="p">]</span><span class="o">!</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">dataSets</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span>
    <span class="p">}</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">yVals</span><span class="o">.</span><span class="n">count</span> <span class="o">&gt;</span> <span class="n">_xVals</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="nf">println</span><span class="p">(</span><span class="s">"One or more of the DataSet Entry arrays are longer than the x-values array of this Data object."</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">notifyDataChanged</span><span class="p">()</span>
<span class="p">{</span>
    <span class="nf">initialize</span><span class="p">(</span><span class="n">_dataSets</span><span class="p">)</span>
<span class="p">}</span>

<span class="c1">/// calc minimum and maximum y value over all datasets</span>
<span class="kd">internal</span> <span class="kd">func</span> <span class="nf">calcMinMax</span><span class="p">(</span><span class="nv">#start</span><span class="p">:</span> <span class="kt">Int</span><span class="p">,</span> <span class="nv">end</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span>
<span class="p">{</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span> <span class="o">==</span> <span class="kc">nil</span> <span class="o">||</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_yMax</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">_yMin</span> <span class="o">=</span> <span class="mf">0.0</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="n">_lastStart</span> <span class="o">=</span> <span class="n">start</span>
        <span class="n">_lastEnd</span> <span class="o">=</span> <span class="n">end</span>

        <span class="n">_yMin</span> <span class="o">=</span> <span class="kt">DBL_MAX</span>
        <span class="n">_yMax</span> <span class="o">=</span> <span class="o">-</span><span class="kt">DBL_MAX</span>

        <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="nf">calcMinMax</span><span class="p">(</span><span class="nv">start</span><span class="p">:</span> <span class="n">start</span><span class="p">,</span> <span class="nv">end</span><span class="p">:</span> <span class="n">end</span><span class="p">)</span>

            <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">yMin</span> <span class="o">&lt;</span> <span class="n">_yMin</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">_yMin</span> <span class="o">=</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">yMin</span>
            <span class="p">}</span>

            <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">yMax</span> <span class="o">&gt;</span> <span class="n">_yMax</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">_yMax</span> <span class="o">=</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">yMax</span>
            <span class="p">}</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">_yMin</span> <span class="o">==</span> <span class="kt">DBL_MAX</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_yMin</span> <span class="o">=</span> <span class="mf">0.0</span>
            <span class="n">_yMax</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="p">}</span>

        <span class="c1">// left axis</span>
        <span class="k">var</span> <span class="nv">firstLeft</span> <span class="o">=</span> <span class="nf">getFirstLeft</span><span class="p">()</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">firstLeft</span> <span class="o">!==</span> <span class="kc">nil</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_leftAxisMax</span> <span class="o">=</span> <span class="n">firstLeft</span><span class="o">!.</span><span class="n">yMax</span>
            <span class="n">_leftAxisMin</span> <span class="o">=</span> <span class="n">firstLeft</span><span class="o">!.</span><span class="n">yMin</span>

            <span class="k">for</span> <span class="n">dataSet</span> <span class="k">in</span> <span class="n">_dataSets</span>
            <span class="p">{</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">dataSet</span><span class="o">.</span><span class="n">axisDependency</span> <span class="o">==</span> <span class="o">.</span><span class="kt">Left</span><span class="p">)</span>
                <span class="p">{</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">dataSet</span><span class="o">.</span><span class="n">yMin</span> <span class="o">&lt;</span> <span class="n">_leftAxisMin</span><span class="p">)</span>
                    <span class="p">{</span>
                        <span class="n">_leftAxisMin</span> <span class="o">=</span> <span class="n">dataSet</span><span class="o">.</span><span class="n">yMin</span>
                    <span class="p">}</span>

                    <span class="k">if</span> <span class="p">(</span><span class="n">dataSet</span><span class="o">.</span><span class="n">yMax</span> <span class="o">&gt;</span> <span class="n">_leftAxisMax</span><span class="p">)</span>
                    <span class="p">{</span>
                        <span class="n">_leftAxisMax</span> <span class="o">=</span> <span class="n">dataSet</span><span class="o">.</span><span class="n">yMax</span>
                    <span class="p">}</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="p">}</span>

        <span class="c1">// right axis</span>
        <span class="k">var</span> <span class="nv">firstRight</span> <span class="o">=</span> <span class="nf">getFirstRight</span><span class="p">()</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">firstRight</span> <span class="o">!==</span> <span class="kc">nil</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_rightAxisMax</span> <span class="o">=</span> <span class="n">firstRight</span><span class="o">!.</span><span class="n">yMax</span>
            <span class="n">_rightAxisMin</span> <span class="o">=</span> <span class="n">firstRight</span><span class="o">!.</span><span class="n">yMin</span>

            <span class="k">for</span> <span class="n">dataSet</span> <span class="k">in</span> <span class="n">_dataSets</span>
            <span class="p">{</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">dataSet</span><span class="o">.</span><span class="n">axisDependency</span> <span class="o">==</span> <span class="o">.</span><span class="kt">Right</span><span class="p">)</span>
                <span class="p">{</span>
                    <span class="k">if</span> <span class="p">(</span><span class="n">dataSet</span><span class="o">.</span><span class="n">yMin</span> <span class="o">&lt;</span> <span class="n">_rightAxisMin</span><span class="p">)</span>
                    <span class="p">{</span>
                        <span class="n">_rightAxisMin</span> <span class="o">=</span> <span class="n">dataSet</span><span class="o">.</span><span class="n">yMin</span>
                    <span class="p">}</span>

                    <span class="k">if</span> <span class="p">(</span><span class="n">dataSet</span><span class="o">.</span><span class="n">yMax</span> <span class="o">&gt;</span> <span class="n">_rightAxisMax</span><span class="p">)</span>
                    <span class="p">{</span>
                        <span class="n">_rightAxisMax</span> <span class="o">=</span> <span class="n">dataSet</span><span class="o">.</span><span class="n">yMax</span>
                    <span class="p">}</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="p">}</span>

        <span class="c1">// in case there is only one axis, adjust the second axis</span>
        <span class="nf">handleEmptyAxis</span><span class="p">(</span><span class="n">firstLeft</span><span class="p">,</span> <span class="nv">firstRight</span><span class="p">:</span> <span class="n">firstRight</span><span class="p">)</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// calculates the sum of all y-values in all datasets</span>
<span class="kd">internal</span> <span class="kd">func</span> <span class="nf">calcYValueSum</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">_yValueSum</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span>
    <span class="p">}</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_yValueSum</span> <span class="o">+=</span> <span class="nf">fabs</span><span class="p">(</span><span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">yValueSum</span><span class="p">)</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// Calculates the total number of y-values across all ChartDataSets the ChartData represents.</span>
<span class="kd">internal</span> <span class="kd">func</span> <span class="nf">calcYValueCount</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">_yValCount</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span>
    <span class="p">}</span>

    <span class="k">var</span> <span class="nv">count</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">count</span> <span class="o">+=</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">entryCount</span>
    <span class="p">}</span>

    <span class="n">_yValCount</span> <span class="o">=</span> <span class="n">count</span>
<span class="p">}</span>

<span class="c1">/// returns the number of LineDataSets this object contains</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">dataSetCount</span><span class="p">:</span> <span class="kt">Int</span>
<span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="mi">0</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span>
<span class="p">}</span>

<span class="c1">/// returns the smallest y-value the data object contains.</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">yMin</span><span class="p">:</span> <span class="kt">Double</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="n">_yMin</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">getYMin</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Double</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">_yMin</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">getYMin</span><span class="p">(</span><span class="nv">axis</span><span class="p">:</span> <span class="kt">ChartYAxis</span><span class="o">.</span><span class="kt">AxisDependency</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Double</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">axis</span> <span class="o">==</span> <span class="o">.</span><span class="kt">Left</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="n">_leftAxisMin</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="n">_rightAxisMin</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// returns the greatest y-value the data object contains.</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">yMax</span><span class="p">:</span> <span class="kt">Double</span>
<span class="p">{</span>
        <span class="k">return</span> <span class="n">_yMax</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">getYMax</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Double</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">_yMax</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">getYMax</span><span class="p">(</span><span class="nv">axis</span><span class="p">:</span> <span class="kt">ChartYAxis</span><span class="o">.</span><span class="kt">AxisDependency</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Double</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">axis</span> <span class="o">==</span> <span class="o">.</span><span class="kt">Left</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="n">_leftAxisMax</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="n">_rightAxisMax</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// returns the average length (in characters) across all values in the x-vals array</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">xValAverageLength</span><span class="p">:</span> <span class="kt">Double</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">_xValAverageLength</span>
<span class="p">}</span>

<span class="c1">/// returns the total y-value sum across all DataSet objects the this object represents.</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">yValueSum</span><span class="p">:</span> <span class="kt">Double</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">_yValueSum</span>
<span class="p">}</span>

<span class="c1">/// Returns the total number of y-values across all DataSet objects the this object represents.</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">yValCount</span><span class="p">:</span> <span class="kt">Int</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">_yValCount</span>
<span class="p">}</span>

<span class="c1">/// returns the x-values the chart represents</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">xVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">String</span><span class="p">?]</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">_xVals</span>
<span class="p">}</span>

<span class="c1">///Adds a new x-value to the chart data.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">addXValue</span><span class="p">(</span><span class="nv">xVal</span><span class="p">:</span> <span class="kt">String</span><span class="p">?)</span>
<span class="p">{</span>
    <span class="n">_xVals</span><span class="o">.</span><span class="nf">append</span><span class="p">(</span><span class="n">xVal</span><span class="p">)</span>
<span class="p">}</span>

<span class="c1">/// Removes the x-value at the specified index.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">removeXValue</span><span class="p">(</span><span class="nv">index</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">_xVals</span><span class="o">.</span><span class="nf">removeAtIndex</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>
<span class="p">}</span>

<span class="c1">/// Returns the array of ChartDataSets this object holds.</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">dataSets</span><span class="p">:</span> <span class="p">[</span><span class="kt">ChartDataSet</span><span class="p">]</span>
<span class="p">{</span>
    <span class="k">get</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="n">_dataSets</span>
    <span class="p">}</span>
    <span class="k">set</span>
    <span class="p">{</span>
        <span class="n">_dataSets</span> <span class="o">=</span> <span class="n">newValue</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// Retrieve the index of a ChartDataSet with a specific label from the ChartData. Search can be case sensitive or not.</span>
<span class="c1">/// IMPORTANT: This method does calculations at runtime, do not over-use in performance critical situations.</span>
<span class="c1">///</span>
<span class="c1">/// :param: dataSets the DataSet array to search</span>
<span class="c1">/// :param: type</span>
<span class="c1">/// :param: ignorecase if true, the search is not case-sensitive</span>
<span class="c1">/// :returns:</span>
<span class="kd">internal</span> <span class="kd">func</span> <span class="nf">getDataSetIndexByLabel</span><span class="p">(</span><span class="nv">label</span><span class="p">:</span> <span class="kt">String</span><span class="p">,</span> <span class="nv">ignorecase</span><span class="p">:</span> <span class="kt">Bool</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">ignorecase</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">label</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="k">continue</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">label</span><span class="o">.</span><span class="nf">caseInsensitiveCompare</span><span class="p">(</span><span class="n">dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">label</span><span class="o">!</span><span class="p">)</span> <span class="o">==</span> <span class="kt">NSComparisonResult</span><span class="o">.</span><span class="kt">OrderedSame</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="k">return</span> <span class="n">i</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">label</span> <span class="o">==</span> <span class="n">dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">label</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="k">return</span> <span class="n">i</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
<span class="p">}</span>

<span class="c1">/// returns the total number of x-values this ChartData object represents (the size of the x-values array)</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">xValCount</span><span class="p">:</span> <span class="kt">Int</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">_xVals</span><span class="o">.</span><span class="n">count</span>
<span class="p">}</span>

<span class="c1">/// Returns the labels of all DataSets as a string array.</span>
<span class="kd">internal</span> <span class="kd">func</span> <span class="nf">dataSetLabels</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="p">[</span><span class="kt">String</span><span class="p">]</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">types</span> <span class="o">=</span> <span class="p">[</span><span class="kt">String</span><span class="p">]()</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">label</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">continue</span>
        <span class="p">}</span>

        <span class="n">types</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">label</span><span class="o">!</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="n">types</span>
<span class="p">}</span>

<span class="c1">/// Get the Entry for a corresponding highlight object</span>
<span class="c1">///</span>
<span class="c1">/// :param: highlight</span>
<span class="c1">/// :returns: the entry that is highlighted</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">getEntryForHighlight</span><span class="p">(</span><span class="nv">highlight</span><span class="p">:</span> <span class="kt">ChartHighlight</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">ChartDataEntry</span><span class="p">?</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="n">highlight</span><span class="o">.</span><span class="n">dataSetIndex</span> <span class="o">&gt;=</span> <span class="n">dataSets</span><span class="o">.</span><span class="n">count</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">nil</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">highlight</span><span class="o">.</span><span class="n">dataSetIndex</span><span class="p">]</span><span class="o">.</span><span class="nf">entryForXIndex</span><span class="p">(</span><span class="n">highlight</span><span class="o">.</span><span class="n">xIndex</span><span class="p">)</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// Returns the DataSet object with the given label.</span>
<span class="c1">/// sensitive or not.</span>
<span class="c1">/// IMPORTANT: This method does calculations at runtime. Use with care in performance critical situations.</span>
<span class="c1">///</span>
<span class="c1">/// :param: label</span>
<span class="c1">/// :param: ignorecase</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">getDataSetByLabel</span><span class="p">(</span><span class="nv">label</span><span class="p">:</span> <span class="kt">String</span><span class="p">,</span> <span class="nv">ignorecase</span><span class="p">:</span> <span class="kt">Bool</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">ChartDataSet</span><span class="p">?</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">index</span> <span class="o">=</span> <span class="nf">getDataSetIndexByLabel</span><span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="nv">ignorecase</span><span class="p">:</span> <span class="n">ignorecase</span><span class="p">)</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">index</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">index</span> <span class="o">&gt;=</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">nil</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">getDataSetByIndex</span><span class="p">(</span><span class="nv">index</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">ChartDataSet</span><span class="o">!</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span> <span class="o">==</span> <span class="kc">nil</span> <span class="o">||</span> <span class="n">index</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="o">||</span> <span class="n">index</span> <span class="o">&gt;=</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">nil</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">addDataSet</span><span class="p">(</span><span class="nv">d</span><span class="p">:</span> <span class="kt">ChartDataSet</span><span class="o">!</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span>
    <span class="p">}</span>

    <span class="n">_yValCount</span> <span class="o">+=</span> <span class="n">d</span><span class="o">.</span><span class="n">entryCount</span>
    <span class="n">_yValueSum</span> <span class="o">+=</span> <span class="n">d</span><span class="o">.</span><span class="n">yValueSum</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_yMax</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">yMax</span>
        <span class="n">_yMin</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">yMin</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">axisDependency</span> <span class="o">==</span> <span class="o">.</span><span class="kt">Left</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_leftAxisMax</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">yMax</span>
            <span class="n">_leftAxisMin</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">yMin</span>
        <span class="p">}</span>
        <span class="k">else</span>
        <span class="p">{</span>
            <span class="n">_rightAxisMax</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">yMax</span>
            <span class="n">_rightAxisMin</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">yMin</span>
        <span class="p">}</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">_yMax</span> <span class="o">&lt;</span> <span class="n">d</span><span class="o">.</span><span class="n">yMax</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_yMax</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">yMax</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">_yMin</span> <span class="o">&gt;</span> <span class="n">d</span><span class="o">.</span><span class="n">yMin</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_yMin</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">yMin</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">axisDependency</span> <span class="o">==</span> <span class="o">.</span><span class="kt">Left</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">_leftAxisMax</span> <span class="o">&lt;</span> <span class="n">d</span><span class="o">.</span><span class="n">yMax</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">_leftAxisMax</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">yMax</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">_leftAxisMin</span> <span class="o">&gt;</span> <span class="n">d</span><span class="o">.</span><span class="n">yMin</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">_leftAxisMin</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">yMin</span>
            <span class="p">}</span>
        <span class="p">}</span>
        <span class="k">else</span>
        <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">_rightAxisMax</span> <span class="o">&lt;</span> <span class="n">d</span><span class="o">.</span><span class="n">yMax</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">_rightAxisMax</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">yMax</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">_rightAxisMin</span> <span class="o">&gt;</span> <span class="n">d</span><span class="o">.</span><span class="n">yMin</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">_rightAxisMin</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">yMin</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="n">_dataSets</span><span class="o">.</span><span class="nf">append</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>

    <span class="nf">handleEmptyAxis</span><span class="p">(</span><span class="nf">getFirstLeft</span><span class="p">(),</span> <span class="nv">firstRight</span><span class="p">:</span> <span class="nf">getFirstRight</span><span class="p">())</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">handleEmptyAxis</span><span class="p">(</span><span class="nv">firstLeft</span><span class="p">:</span> <span class="kt">ChartDataSet</span><span class="p">?,</span> <span class="nv">firstRight</span><span class="p">:</span> <span class="kt">ChartDataSet</span><span class="p">?)</span>
<span class="p">{</span>
    <span class="c1">// in case there is only one axis, adjust the second axis</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">firstLeft</span> <span class="o">===</span> <span class="kc">nil</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_leftAxisMax</span> <span class="o">=</span> <span class="n">_rightAxisMax</span>
        <span class="n">_leftAxisMin</span> <span class="o">=</span> <span class="n">_rightAxisMin</span>
    <span class="p">}</span>
    <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">firstRight</span> <span class="o">===</span> <span class="kc">nil</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">_rightAxisMax</span> <span class="o">=</span> <span class="n">_leftAxisMax</span>
        <span class="n">_rightAxisMin</span> <span class="o">=</span> <span class="n">_leftAxisMin</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// Removes the given DataSet from this data object.</span>
<span class="c1">/// Also recalculates all minimum and maximum values.</span>
<span class="c1">///</span>
<span class="c1">/// :returns: true if a DataSet was removed, false if no DataSet could be removed.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">removeDataSet</span><span class="p">(</span><span class="nv">dataSet</span><span class="p">:</span> <span class="kt">ChartDataSet</span><span class="o">!</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span> <span class="o">==</span> <span class="kc">nil</span> <span class="o">||</span> <span class="n">dataSet</span> <span class="o">===</span> <span class="kc">nil</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">false</span>
    <span class="p">}</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">===</span> <span class="n">dataSet</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="nf">removeDataSetByIndex</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="kc">false</span>
<span class="p">}</span>

<span class="c1">/// Removes the DataSet at the given index in the DataSet array from the data object.</span>
<span class="c1">/// Also recalculates all minimum and maximum values.</span>
<span class="c1">///</span>
<span class="c1">/// :returns: true if a DataSet was removed, false if no DataSet could be removed.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">removeDataSetByIndex</span><span class="p">(</span><span class="nv">index</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span> <span class="o">==</span> <span class="kc">nil</span> <span class="o">||</span> <span class="n">index</span> <span class="o">&gt;=</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span> <span class="o">||</span> <span class="n">index</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">false</span>
    <span class="p">}</span>

    <span class="k">var</span> <span class="nv">d</span> <span class="o">=</span> <span class="n">_dataSets</span><span class="o">.</span><span class="nf">removeAtIndex</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>
    <span class="n">_yValCount</span> <span class="o">-=</span> <span class="n">d</span><span class="o">.</span><span class="n">entryCount</span>
    <span class="n">_yValueSum</span> <span class="o">-=</span> <span class="n">d</span><span class="o">.</span><span class="n">yValueSum</span>

    <span class="nf">calcMinMax</span><span class="p">(</span><span class="nv">start</span><span class="p">:</span> <span class="n">_lastStart</span><span class="p">,</span> <span class="nv">end</span><span class="p">:</span> <span class="n">_lastEnd</span><span class="p">)</span>

    <span class="k">return</span> <span class="kc">true</span>
<span class="p">}</span>

<span class="c1">/// Adds an Entry to the DataSet at the specified index. Entries are added to the end of the list.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">addEntry</span><span class="p">(</span><span class="nv">e</span><span class="p">:</span> <span class="kt">ChartDataEntry</span><span class="p">,</span> <span class="nv">dataSetIndex</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="o">&amp;&amp;</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span> <span class="o">&gt;</span> <span class="n">dataSetIndex</span> <span class="o">&amp;&amp;</span> <span class="n">dataSetIndex</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">var</span> <span class="nv">val</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>
        <span class="k">var</span> <span class="nv">set</span> <span class="o">=</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">dataSetIndex</span><span class="p">]</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">_yValCount</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_yMin</span> <span class="o">=</span> <span class="n">val</span>
            <span class="n">_yMax</span> <span class="o">=</span> <span class="n">val</span>

            <span class="k">if</span> <span class="p">(</span><span class="k">set</span><span class="o">.</span><span class="n">axisDependency</span> <span class="o">==</span> <span class="o">.</span><span class="kt">Left</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">_leftAxisMax</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>
                <span class="n">_leftAxisMin</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>
            <span class="p">}</span>
            <span class="k">else</span>
            <span class="p">{</span>
                <span class="n">_rightAxisMax</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>
                <span class="n">_rightAxisMin</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>
            <span class="p">}</span>
        <span class="p">}</span>
        <span class="k">else</span>
        <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">_yMax</span> <span class="o">&lt;</span> <span class="n">val</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">_yMax</span> <span class="o">=</span> <span class="n">val</span>
            <span class="p">}</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">_yMin</span> <span class="o">&gt;</span> <span class="n">val</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="n">_yMin</span> <span class="o">=</span> <span class="n">val</span>
            <span class="p">}</span>

            <span class="k">if</span> <span class="p">(</span><span class="k">set</span><span class="o">.</span><span class="n">axisDependency</span> <span class="o">==</span> <span class="o">.</span><span class="kt">Left</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">_leftAxisMax</span> <span class="o">&lt;</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
                <span class="p">{</span>
                    <span class="n">_leftAxisMax</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>
                <span class="p">}</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">_leftAxisMin</span> <span class="o">&gt;</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
                <span class="p">{</span>
                    <span class="n">_leftAxisMin</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>
                <span class="p">}</span>
            <span class="p">}</span>
            <span class="k">else</span>
            <span class="p">{</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">_rightAxisMax</span> <span class="o">&lt;</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
                <span class="p">{</span>
                    <span class="n">_rightAxisMax</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>
                <span class="p">}</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">_rightAxisMin</span> <span class="o">&gt;</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span><span class="p">)</span>
                <span class="p">{</span>
                    <span class="n">_rightAxisMin</span> <span class="o">=</span> <span class="n">e</span><span class="o">.</span><span class="n">value</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="p">}</span>

        <span class="n">_yValCount</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">_yValueSum</span> <span class="o">+=</span> <span class="n">val</span>

        <span class="nf">handleEmptyAxis</span><span class="p">(</span><span class="nf">getFirstLeft</span><span class="p">(),</span> <span class="nv">firstRight</span><span class="p">:</span> <span class="nf">getFirstRight</span><span class="p">())</span>

        <span class="k">set</span><span class="o">.</span><span class="nf">addEntry</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
    <span class="p">}</span>
    <span class="k">else</span>
    <span class="p">{</span>
        <span class="nf">println</span><span class="p">(</span><span class="s">"ChartData.addEntry() - dataSetIndex our of range."</span><span class="p">)</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// Removes the given Entry object from the DataSet at the specified index.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">removeEntry</span><span class="p">(</span><span class="nv">entry</span><span class="p">:</span> <span class="kt">ChartDataEntry</span><span class="o">!</span><span class="p">,</span> <span class="nv">dataSetIndex</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="c1">// entry null, outofbounds</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">entry</span> <span class="o">===</span> <span class="kc">nil</span> <span class="o">||</span> <span class="n">dataSetIndex</span> <span class="o">&gt;=</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">false</span>
    <span class="p">}</span>

    <span class="c1">// remove the entry from the dataset</span>
    <span class="k">var</span> <span class="nv">removed</span> <span class="o">=</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">dataSetIndex</span><span class="p">]</span><span class="o">.</span><span class="nf">removeEntry</span><span class="p">(</span><span class="nv">xIndex</span><span class="p">:</span> <span class="n">entry</span><span class="o">.</span><span class="n">xIndex</span><span class="p">)</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">removed</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">var</span> <span class="nv">val</span> <span class="o">=</span> <span class="n">entry</span><span class="o">.</span><span class="n">value</span>

        <span class="n">_yValCount</span> <span class="o">-=</span> <span class="mi">1</span>
        <span class="n">_yValueSum</span> <span class="o">-=</span> <span class="n">val</span>

        <span class="nf">calcMinMax</span><span class="p">(</span><span class="nv">start</span><span class="p">:</span> <span class="n">_lastStart</span><span class="p">,</span> <span class="nv">end</span><span class="p">:</span> <span class="n">_lastEnd</span><span class="p">)</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="n">removed</span>
<span class="p">}</span>

<span class="c1">/// Removes the Entry object at the given xIndex from the ChartDataSet at the</span>
<span class="c1">/// specified index. Returns true if an entry was removed, false if no Entry</span>
<span class="c1">/// was found that meets the specified requirements.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">removeEntryByXIndex</span><span class="p">(</span><span class="nv">xIndex</span><span class="p">:</span> <span class="kt">Int</span><span class="p">,</span> <span class="nv">dataSetIndex</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">dataSetIndex</span> <span class="o">&gt;=</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">false</span>
    <span class="p">}</span>

    <span class="k">var</span> <span class="nv">entry</span> <span class="o">=</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">dataSetIndex</span><span class="p">]</span><span class="o">.</span><span class="nf">entryForXIndex</span><span class="p">(</span><span class="n">xIndex</span><span class="p">)</span>

    <span class="k">if</span> <span class="p">(</span><span class="n">entry</span><span class="p">?</span><span class="o">.</span><span class="n">xIndex</span> <span class="o">!=</span> <span class="n">xIndex</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">false</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="nf">removeEntry</span><span class="p">(</span><span class="n">entry</span><span class="p">,</span> <span class="nv">dataSetIndex</span><span class="p">:</span> <span class="n">dataSetIndex</span><span class="p">)</span>
<span class="p">}</span>

<span class="c1">/// Returns the DataSet that contains the provided Entry, or null, if no DataSet contains this entry.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">getDataSetForEntry</span><span class="p">(</span><span class="nv">e</span><span class="p">:</span> <span class="kt">ChartDataEntry</span><span class="o">!</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">ChartDataSet</span><span class="p">?</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">e</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">nil</span>
    <span class="p">}</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">var</span> <span class="nv">set</span> <span class="o">=</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

        <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="k">set</span><span class="o">.</span><span class="n">entryCount</span><span class="p">;</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">e</span> <span class="o">===</span> <span class="k">set</span><span class="o">.</span><span class="nf">entryForXIndex</span><span class="p">(</span><span class="n">e</span><span class="o">.</span><span class="n">xIndex</span><span class="p">))</span>
            <span class="p">{</span>
                <span class="k">return</span> <span class="k">set</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="kc">nil</span>
<span class="p">}</span>

<span class="c1">/// Returns the index of the provided DataSet inside the DataSets array of</span>
<span class="c1">/// this data object. Returns -1 if the DataSet was not found.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">indexOfDataSet</span><span class="p">(</span><span class="nv">dataSet</span><span class="p">:</span> <span class="kt">ChartDataSet</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Int</span>
<span class="p">{</span>
    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">===</span> <span class="n">dataSet</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="n">i</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">getFirstLeft</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">ChartDataSet</span><span class="p">?</span>
<span class="p">{</span>
    <span class="k">for</span> <span class="n">dataSet</span> <span class="k">in</span> <span class="n">_dataSets</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">dataSet</span><span class="o">.</span><span class="n">axisDependency</span> <span class="o">==</span> <span class="o">.</span><span class="kt">Left</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="n">dataSet</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="kc">nil</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">getFirstRight</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">ChartDataSet</span><span class="p">?</span>
<span class="p">{</span>
    <span class="k">for</span> <span class="n">dataSet</span> <span class="k">in</span> <span class="n">_dataSets</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">dataSet</span><span class="o">.</span><span class="n">axisDependency</span> <span class="o">==</span> <span class="o">.</span><span class="kt">Right</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="n">dataSet</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="kc">nil</span>
<span class="p">}</span>

<span class="c1">/// Returns all colors used across all DataSet objects this object represents.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">getColors</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="p">[</span><span class="kt">UIColor</span><span class="p">]?</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">_dataSets</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="kc">nil</span>
    <span class="p">}</span>

    <span class="k">var</span> <span class="nv">clrcnt</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">clrcnt</span> <span class="o">+=</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">colors</span><span class="o">.</span><span class="n">count</span>
    <span class="p">}</span>

    <span class="k">var</span> <span class="nv">colors</span> <span class="o">=</span> <span class="p">[</span><span class="kt">UIColor</span><span class="p">]()</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">_dataSets</span><span class="o">.</span><span class="n">count</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="k">var</span> <span class="nv">clrs</span> <span class="o">=</span> <span class="n">_dataSets</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">colors</span>

        <span class="k">for</span> <span class="n">clr</span> <span class="k">in</span> <span class="n">clrs</span>
        <span class="p">{</span>
            <span class="n">colors</span><span class="o">.</span><span class="nf">append</span><span class="p">(</span><span class="n">clr</span><span class="p">)</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="n">colors</span>
<span class="p">}</span>

<span class="c1">/// Generates an x-values array filled with numbers in range specified by the parameters. Can be used for convenience.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">generateXVals</span><span class="p">(</span><span class="nv">from</span><span class="p">:</span> <span class="kt">Int</span><span class="p">,</span> <span class="nv">to</span><span class="p">:</span> <span class="kt">Int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="p">[</span><span class="kt">String</span><span class="p">]</span>
<span class="p">{</span>
    <span class="k">var</span> <span class="nv">xvals</span> <span class="o">=</span> <span class="p">[</span><span class="kt">String</span><span class="p">]()</span>

    <span class="k">for</span> <span class="p">(</span><span class="k">var</span> <span class="nv">i</span> <span class="o">=</span> <span class="n">from</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">to</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">xvals</span><span class="o">.</span><span class="nf">append</span><span class="p">(</span><span class="kt">String</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="n">xvals</span>
<span class="p">}</span>

<span class="c1">/// Sets a custom ValueFormatter for all DataSets this data object contains.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">setValueFormatter</span><span class="p">(</span><span class="nv">formatter</span><span class="p">:</span> <span class="kt">NSNumberFormatter</span><span class="o">!</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">for</span> <span class="k">set</span> <span class="k">in</span> <span class="n">dataSets</span>
    <span class="p">{</span>
        <span class="k">set</span><span class="o">.</span><span class="n">valueFormatter</span> <span class="o">=</span> <span class="n">formatter</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// Sets the color of the value-text (color in which the value-labels are drawn) for all DataSets this data object contains.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">setValueTextColor</span><span class="p">(</span><span class="nv">color</span><span class="p">:</span> <span class="kt">UIColor</span><span class="o">!</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">for</span> <span class="k">set</span> <span class="k">in</span> <span class="n">dataSets</span>
    <span class="p">{</span>
        <span class="k">set</span><span class="o">.</span><span class="n">valueTextColor</span> <span class="o">=</span> <span class="n">color</span> <span class="p">??</span> <span class="k">set</span><span class="o">.</span><span class="n">valueTextColor</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// Sets the font for all value-labels for all DataSets this data object contains.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">setValueFont</span><span class="p">(</span><span class="nv">font</span><span class="p">:</span> <span class="kt">UIFont</span><span class="o">!</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">for</span> <span class="k">set</span> <span class="k">in</span> <span class="n">dataSets</span>
    <span class="p">{</span>
        <span class="k">set</span><span class="o">.</span><span class="n">valueFont</span> <span class="o">=</span> <span class="n">font</span> <span class="p">??</span> <span class="k">set</span><span class="o">.</span><span class="n">valueFont</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// Enables / disables drawing values (value-text) for all DataSets this data object contains.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">setDrawValues</span><span class="p">(</span><span class="nv">enabled</span><span class="p">:</span> <span class="kt">Bool</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">for</span> <span class="k">set</span> <span class="k">in</span> <span class="n">dataSets</span>
    <span class="p">{</span>
        <span class="k">set</span><span class="o">.</span><span class="n">drawValuesEnabled</span> <span class="o">=</span> <span class="n">enabled</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// Enables / disables highlighting values for all DataSets this data object contains.</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">highlightEnabled</span><span class="p">:</span> <span class="kt">Bool</span>
    <span class="p">{</span>
    <span class="k">get</span>
    <span class="p">{</span>
        <span class="k">for</span> <span class="k">set</span> <span class="k">in</span> <span class="n">dataSets</span>
        <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="k">set</span><span class="o">.</span><span class="n">highlightEnabled</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="k">return</span> <span class="kc">false</span>
            <span class="p">}</span>
        <span class="p">}</span>

        <span class="k">return</span> <span class="kc">true</span>
    <span class="p">}</span>
    <span class="k">set</span>
    <span class="p">{</span>
        <span class="k">for</span> <span class="k">set</span> <span class="k">in</span> <span class="n">dataSets</span>
        <span class="p">{</span>
            <span class="k">set</span><span class="o">.</span><span class="n">highlightEnabled</span> <span class="o">=</span> <span class="n">newValue</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// if true, value highlightning is enabled</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">isHighlightEnabled</span><span class="p">:</span> <span class="kt">Bool</span> <span class="p">{</span> <span class="k">return</span> <span class="n">highlightEnabled</span> <span class="p">}</span>

<span class="c1">/// Clears this data object from all DataSets and removes all Entries.</span>
<span class="c1">/// Don't forget to invalidate the chart after this.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">clearValues</span><span class="p">()</span>
<span class="p">{</span>
    <span class="n">dataSets</span><span class="o">.</span><span class="nf">removeAll</span><span class="p">(</span><span class="nv">keepCapacity</span><span class="p">:</span> <span class="kc">false</span><span class="p">)</span>
    <span class="nf">notifyDataChanged</span><span class="p">()</span>
<span class="p">}</span>

<span class="c1">/// Checks if this data object contains the specified Entry. Returns true if so, false if not.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">contains</span><span class="p">(</span><span class="nv">#entry</span><span class="p">:</span> <span class="kt">ChartDataEntry</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="k">for</span> <span class="k">set</span> <span class="k">in</span> <span class="n">dataSets</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="k">set</span><span class="o">.</span><span class="nf">contains</span><span class="p">(</span><span class="n">entry</span><span class="p">))</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="kc">true</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="kc">false</span>
<span class="p">}</span>

<span class="c1">/// Checks if this data object contains the specified DataSet. Returns true if so, false if not.</span>
<span class="kd">public</span> <span class="kd">func</span> <span class="nf">contains</span><span class="p">(</span><span class="nv">#dataSet</span><span class="p">:</span> <span class="kt">ChartDataSet</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="k">for</span> <span class="k">set</span> <span class="k">in</span> <span class="n">dataSets</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="p">(</span><span class="k">set</span><span class="o">.</span><span class="nf">isEqual</span><span class="p">(</span><span class="n">dataSet</span><span class="p">))</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="kc">true</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="kc">false</span>
<span class="p">}</span>

<span class="c1">/// MARK: - ObjC compatibility</span>

<span class="c1">/// returns the average length (in characters) across all values in the x-vals array</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">xValsObjc</span><span class="p">:</span> <span class="p">[</span><span class="kt">NSObject</span><span class="p">]</span> <span class="p">{</span> <span class="k">return</span> <span class="kt">ChartUtils</span><span class="o">.</span><span class="nf">bridgedObjCGetStringArray</span><span class="p">(</span><span class="nv">swift</span><span class="p">:</span> <span class="n">_xVals</span><span class="p">);</span> <span class="p">}</span>

}



blog comments powered by Disqus