06 August 2015

条形图


  • BarChartDataEntry 条形图条目
  • BarChartDataSet 条形图数据集合
  • BarChartData 条形图数据

BarChartDataEntry


public class BarChartDataEntry: ChartDataEntry
{
    /// the values the stacked barchart holds
    /// 堆积条形图
    private var _values: [Double]?

<span class="c1">/// the sum of all negative values this entry (if stacked) contains</span>
<span class="c1">// 负数条目之和(如果是堆积条形图)</span>
<span class="kd">private</span> <span class="k">var</span> <span class="nv">_negativeSum</span><span class="p">:</span> <span class="kt">Double</span> <span class="o">=</span> <span class="mf">0.0</span>

<span class="c1">/// the sum of all positive values this entry (if stacked) contains</span>
<span class="c1">// 正数条目之和(如果是堆积条形图)</span>
<span class="kd">private</span> <span class="k">var</span> <span class="nv">_positiveSum</span><span class="p">:</span> <span class="kt">Double</span> <span class="o">=</span> <span class="mf">0.0</span>

<span class="c1">/// Constructor for stacked bar entries.</span>
<span class="kd">public</span> <span class="nf">init</span><span class="p">(</span><span class="nv">values</span><span class="p">:</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="nv">value</span><span class="p">:</span> <span class="kt">BarChartDataEntry</span><span class="o">.</span><span class="nf">calcSum</span><span class="p">(</span><span class="n">values</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">self</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">values</span>
    <span class="nf">calcPosNegSum</span><span class="p">()</span>
<span class="p">}</span>

<span class="c1">/// Constructor for normal bars (not stacked).</span>
<span class="kd">public</span> <span class="k">override</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="nv">value</span><span class="p">:</span> <span class="n">value</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="p">}</span>

<span class="c1">/// Constructor for stacked bar entries.</span>
<span class="kd">public</span> <span class="nf">init</span><span class="p">(</span><span class="nv">values</span><span class="p">:</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">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="nv">value</span><span class="p">:</span> <span class="kt">BarChartDataEntry</span><span class="o">.</span><span class="nf">calcSum</span><span class="p">(</span><span class="n">values</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="nv">data</span><span class="p">:</span> <span class="n">label</span><span class="p">)</span>
    <span class="k">self</span><span class="o">.</span><span class="n">values</span> <span class="o">=</span> <span class="n">values</span>
<span class="p">}</span>

<span class="c1">/// Constructor for normal bars (not stacked).</span>
<span class="kd">public</span> <span class="k">override</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="nv">value</span><span class="p">:</span> <span class="n">value</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="nv">data</span><span class="p">:</span> <span class="n">data</span><span class="p">)</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">getBelowSum</span><span class="p">(</span><span class="nv">stackIndex</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">if</span> <span class="p">(</span><span class="n">values</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">var</span> <span class="nv">remainder</span><span class="p">:</span> <span class="kt">Double</span> <span class="o">=</span> <span class="mf">0.0</span>
    <span class="k">var</span> <span class="nv">index</span> <span class="o">=</span> <span class="n">values</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">index</span> <span class="o">&gt;</span> <span class="n">stackIndex</span> <span class="o">&amp;&amp;</span> <span class="n">index</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">remainder</span> <span class="o">+=</span> <span class="n">values</span><span class="o">!</span><span class="p">[</span><span class="n">index</span><span class="p">]</span>
        <span class="n">index</span><span class="o">--</span>
    <span class="p">}</span>

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

<span class="c1">/// :returns: the sum of all negative values this entry (if stacked) contains. (this is a positive number)</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">negativeSum</span><span class="p">:</span> <span class="kt">Double</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">_negativeSum</span>
<span class="p">}</span>

<span class="c1">/// :returns: the sum of all positive values this entry (if stacked) contains.</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">positiveSum</span><span class="p">:</span> <span class="kt">Double</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">_positiveSum</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="kd">func</span> <span class="nf">calcPosNegSum</span><span class="p">()</span>
<span class="p">{</span>
    <span class="k">if</span> <span class="n">_values</span> <span class="o">==</span> <span class="kc">nil</span>
    <span class="p">{</span>
        <span class="n">_positiveSum</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">_negativeSum</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">return</span>
    <span class="p">}</span>

    <span class="k">var</span> <span class="nv">sumNeg</span><span class="p">:</span> <span class="kt">Double</span> <span class="o">=</span> <span class="mf">0.0</span>
    <span class="k">var</span> <span class="nv">sumPos</span><span class="p">:</span> <span class="kt">Double</span> <span class="o">=</span> <span class="mf">0.0</span>

    <span class="k">for</span> <span class="n">f</span> <span class="k">in</span> <span class="n">_values</span><span class="o">!</span>
    <span class="p">{</span>
        <span class="k">if</span> <span class="n">f</span> <span class="o">&lt;</span> <span class="mf">0.0</span>
        <span class="p">{</span>
            <span class="n">sumNeg</span> <span class="o">+=</span> <span class="o">-</span><span class="n">f</span>
        <span class="p">}</span>
        <span class="k">else</span>
        <span class="p">{</span>
            <span class="n">sumPos</span> <span class="o">+=</span> <span class="n">f</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="n">_negativeSum</span> <span class="o">=</span> <span class="n">sumNeg</span>
    <span class="n">_positiveSum</span> <span class="o">=</span> <span class="n">sumPos</span>
<span class="p">}</span>

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

<span class="c1">/// the values the stacked barchart holds</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">isStacked</span><span class="p">:</span> <span class="kt">Bool</span> <span class="p">{</span> <span class="k">return</span> <span class="n">_values</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">}</span>

<span class="c1">/// the values the stacked barchart holds</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">values</span><span class="p">:</span> <span class="p">[</span><span class="kt">Double</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="k">self</span><span class="o">.</span><span class="n">_values</span> <span class="p">}</span>
    <span class="k">set</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="kt">BarChartDataEntry</span><span class="o">.</span><span class="nf">calcSum</span><span class="p">(</span><span class="n">newValue</span><span class="p">)</span>
        <span class="k">self</span><span class="o">.</span><span class="n">_values</span> <span class="o">=</span> <span class="n">newValue</span>
        <span class="nf">calcPosNegSum</span><span class="p">()</span>
    <span class="p">}</span>
<span class="p">}</span>

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

<span class="kd">public</span> <span class="k">override</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">super</span><span class="o">.</span><span class="nf">copyWithZone</span><span class="p">(</span><span class="n">zone</span><span class="p">)</span> <span class="k">as!</span> <span class="kt">BarChartDataEntry</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">_values</span> <span class="o">=</span> <span class="n">_values</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">_negativeSum</span> <span class="o">=</span> <span class="n">_negativeSum</span>
    <span class="k">return</span> <span class="n">copy</span>
<span class="p">}</span>

<span class="c1">/// Calculates the sum across all values of the given stack.</span>
<span class="c1">///</span>
<span class="c1">/// :param: vals</span>
<span class="c1">/// :returns:</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="kd">func</span> <span class="nf">calcSum</span><span class="p">(</span><span class="nv">vals</span><span class="p">:</span> <span class="p">[</span><span class="kt">Double</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="n">vals</span> <span class="o">==</span> <span class="kc">nil</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="mf">0.0</span>
    <span class="p">}</span>

    <span class="k">var</span> <span class="nv">sum</span> <span class="o">=</span> <span class="mf">0.0</span>

    <span class="k">for</span> <span class="n">f</span> <span class="k">in</span> <span class="n">vals</span><span class="o">!</span>
    <span class="p">{</span>
        <span class="n">sum</span> <span class="o">+=</span> <span class="n">f</span>
    <span class="p">}</span>

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

}

BarChartDataSet


public class BarChartDataSet: BarLineScatterCandleChartDataSet
{
    /// space indicator between the bars in percentage of the whole width of one value (0.15 == 15% of bar width)
    /// Bar之间的间距,相对于整个Bar的百分比
    public var barSpace: CGFloat = 0.15

<span class="c1">/// the maximum number of bars that are stacked upon each other, this value</span>
<span class="c1">/// is calculated from the Entries that are added to the DataSet</span>
<span class="kd">private</span> <span class="k">var</span> <span class="nv">_stackSize</span> <span class="o">=</span> <span class="mi">1</span>

<span class="c1">/// the color used for drawing the bar-shadows. The bar shadows is a surface behind the bar that indicates the maximum value</span>
<span class="c1">/// Bar 阴影颜色</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">barShadowColor</span> <span class="o">=</span> <span class="kt">UIColor</span><span class="p">(</span><span class="nv">red</span><span class="p">:</span> <span class="mf">215.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">215.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">215.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="c1">/// the alpha value (transparency) that is used for drawing the highlight indicator bar. min = 0.0 (fully transparent), max = 1.0 (fully opaque)</span>
<span class="c1">/// 标识 高亮 的透明度</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">highLightAlpha</span> <span class="o">=</span> <span class="kt">CGFloat</span><span class="p">(</span><span class="mf">120.0</span> <span class="o">/</span> <span class="mf">255.0</span><span class="p">)</span>

<span class="c1">/// the overall entry count, including counting each stack-value individually</span>
<span class="kd">private</span> <span class="k">var</span> <span class="nv">_entryCountStacks</span> <span class="o">=</span> <span class="mi">0</span>

<span class="c1">/// array of labels used to describe the different values of the stacked bars</span>
<span class="c1">// 堆积条形图标签数组</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">stackLabels</span><span class="p">:</span> <span class="p">[</span><span class="kt">String</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s">"Stack"</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="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="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="n">label</span><span class="p">)</span>

    <span class="k">self</span><span class="o">.</span><span class="n">highlightColor</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="k">self</span><span class="o">.</span><span class="nf">calcStackSize</span><span class="p">(</span><span class="n">yVals</span> <span class="k">as!</span> <span class="p">[</span><span class="kt">BarChartDataEntry</span><span class="p">]?)</span>
    <span class="k">self</span><span class="o">.</span><span class="nf">calcEntryCountIncludingStacks</span><span class="p">(</span><span class="n">yVals</span> <span class="k">as!</span> <span class="p">[</span><span class="kt">BarChartDataEntry</span><span class="p">]?)</span>
<span class="p">}</span>

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

<span class="kd">public</span> <span class="k">override</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">super</span><span class="o">.</span><span class="nf">copyWithZone</span><span class="p">(</span><span class="n">zone</span><span class="p">)</span> <span class="k">as!</span> <span class="kt">BarChartDataSet</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">barSpace</span> <span class="o">=</span> <span class="n">barSpace</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">_stackSize</span> <span class="o">=</span> <span class="n">_stackSize</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">barShadowColor</span> <span class="o">=</span> <span class="n">barShadowColor</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">highLightAlpha</span> <span class="o">=</span> <span class="n">highLightAlpha</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">_entryCountStacks</span> <span class="o">=</span> <span class="n">_entryCountStacks</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">stackLabels</span> <span class="o">=</span> <span class="n">stackLabels</span>
    <span class="k">return</span> <span class="n">copy</span>
<span class="p">}</span>

<span class="c1">/// Calculates the total number of entries this DataSet represents, including</span>
<span class="c1">/// stacks. All values belonging to a stack are calculated separately.</span>
<span class="kd">private</span> <span class="kd">func</span> <span class="nf">calcEntryCountIncludingStacks</span><span class="p">(</span><span class="nv">yVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">BarChartDataEntry</span><span class="p">]</span><span class="o">!</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">_entryCountStacks</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">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">var</span> <span class="nv">vals</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">values</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">vals</span> <span class="o">==</span> <span class="kc">nil</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_entryCountStacks</span><span class="o">++</span>
        <span class="p">}</span>
        <span class="k">else</span>
        <span class="p">{</span>
            <span class="n">_entryCountStacks</span> <span class="o">+=</span> <span class="n">vals</span><span class="o">!.</span><span class="n">count</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// calculates the maximum stacksize that occurs in the Entries array of this DataSet</span>
<span class="kd">private</span> <span class="kd">func</span> <span class="nf">calcStackSize</span><span class="p">(</span><span class="nv">yVals</span><span class="p">:</span> <span class="p">[</span><span class="kt">BarChartDataEntry</span><span class="p">]</span><span class="o">!</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="k">let</span> <span class="nv">vals</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">values</span>
        <span class="p">{</span>
            <span class="k">if</span> <span class="n">vals</span><span class="o">.</span><span class="n">count</span> <span class="o">&gt;</span> <span class="n">_stackSize</span>
            <span class="p">{</span>
                <span class="n">_stackSize</span> <span class="o">=</span> <span class="n">vals</span><span class="o">.</span><span class="n">count</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="kd">internal</span> <span class="k">override</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">if</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">as?</span> <span class="kt">BarChartDataEntry</span>
        <span class="p">{</span>
            <span class="k">if</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="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">values</span> <span class="o">==</span> <span class="kc">nil</span>
                <span class="p">{</span>
                    <span class="k">if</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="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="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="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="k">else</span>
                <span class="p">{</span>
                    <span class="k">if</span> <span class="o">-</span><span class="n">e</span><span class="o">.</span><span class="n">negativeSum</span> <span class="o">&lt;</span> <span class="n">_yMin</span>
                    <span class="p">{</span>
                        <span class="n">_yMin</span> <span class="o">=</span> <span class="o">-</span><span class="n">e</span><span class="o">.</span><span class="n">negativeSum</span>
                    <span class="p">}</span>

                    <span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">positiveSum</span> <span class="o">&gt;</span> <span class="n">_yMax</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">positiveSum</span>
                    <span class="p">}</span>
                <span class="p">}</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="c1">/// Returns the maximum number of bars that can be stacked upon another in this DataSet.</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">stackSize</span><span class="p">:</span> <span class="kt">Int</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">_stackSize</span>
<span class="p">}</span>

<span class="c1">/// Returns true if this DataSet is stacked (stacksize &gt; 1) or not.</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">isStacked</span><span class="p">:</span> <span class="kt">Bool</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">_stackSize</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="p">?</span> <span class="nv">true</span> <span class="p">:</span> <span class="kc">false</span>
<span class="p">}</span>

<span class="c1">/// returns the overall entry count, including counting each stack-value individually</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">entryCountStacks</span><span class="p">:</span> <span class="kt">Int</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">_entryCountStacks</span>
<span class="p">}</span>

}

BarChartData


public class BarChartData: BarLineScatterCandleChartData
{
    public override init()
    {
        super.init()
    }

<span class="kd">public</span> <span class="k">override</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="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">dataSets</span><span class="p">)</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="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="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">dataSets</span><span class="p">)</span>
<span class="p">}</span>

<span class="c1">// 组之间的距离</span>
<span class="kd">private</span> <span class="k">var</span> <span class="nv">_groupSpace</span> <span class="o">=</span> <span class="kt">CGFloat</span><span class="p">(</span><span class="mf">0.8</span><span class="p">)</span>

<span class="c1">/// The spacing is relative to a full bar width</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">groupSpace</span><span class="p">:</span> <span class="kt">CGFloat</span>
<span class="p">{</span>
    <span class="k">get</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="n">count</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="k">return</span> <span class="mf">0.0</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="n">_groupSpace</span>
    <span class="p">}</span>
    <span class="k">set</span>
    <span class="p">{</span>
        <span class="n">_groupSpace</span> <span class="o">=</span> <span class="n">newValue</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// Returns true if this BarData object contains grouped DataSets (more than 1 DataSet).</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">isGrouped</span><span class="p">:</span> <span class="kt">Bool</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="o">&gt;</span> <span class="mi">1</span> <span class="p">?</span> <span class="nv">true</span> <span class="p">:</span> <span class="kc">false</span>
<span class="p">}</span>

}



blog comments powered by Disqus