07 August 2015

蜡烛图


  • CandleChartDataEntry 蜡烛图 数据条目
  • CandleChartDataSet 蜡烛图集合
  • CandleChartData 蜡烛图数据

CandleChartDataEntry


public class CandleChartDataEntry: ChartDataEntry
{
    /// shadow-high value
    // 最高值
    public var high = Double(0.0)

<span class="c1">/// shadow-low value</span>
<span class="c1">// 最低值</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">low</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="c1">/// close value</span>
<span class="c1">// 收盘值</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">close</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="c1">/// open value</span>
<span class="c1">// 开盘值</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">open</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">public</span> <span class="nf">init</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">shadowH</span><span class="p">:</span> <span class="kt">Double</span><span class="p">,</span> <span class="nv">shadowL</span><span class="p">:</span> <span class="kt">Double</span><span class="p">,</span> <span class="nv">open</span><span class="p">:</span> <span class="kt">Double</span><span class="p">,</span> <span class="nv">close</span><span class="p">:</span> <span class="kt">Double</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="p">(</span><span class="n">shadowH</span> <span class="o">+</span> <span class="n">shadowL</span><span class="p">)</span> <span class="o">/</span> <span class="mf">2.0</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">high</span> <span class="o">=</span> <span class="n">shadowH</span>
    <span class="k">self</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="n">shadowL</span>
    <span class="k">self</span><span class="o">.</span><span class="n">open</span> <span class="o">=</span> <span class="n">open</span>
    <span class="k">self</span><span class="o">.</span><span class="n">close</span> <span class="o">=</span> <span class="n">close</span>
<span class="p">}</span>

<span class="kd">public</span> <span class="nf">init</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">shadowH</span><span class="p">:</span> <span class="kt">Double</span><span class="p">,</span> <span class="nv">shadowL</span><span class="p">:</span> <span class="kt">Double</span><span class="p">,</span> <span class="nv">open</span><span class="p">:</span> <span class="kt">Double</span><span class="p">,</span> <span class="nv">close</span><span class="p">:</span> <span class="kt">Double</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="p">(</span><span class="n">shadowH</span> <span class="o">+</span> <span class="n">shadowL</span><span class="p">)</span> <span class="o">/</span> <span class="mf">2.0</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="k">self</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">shadowH</span>
    <span class="k">self</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="n">shadowL</span>
    <span class="k">self</span><span class="o">.</span><span class="n">open</span> <span class="o">=</span> <span class="n">open</span>
    <span class="k">self</span><span class="o">.</span><span class="n">close</span> <span class="o">=</span> <span class="n">close</span>
<span class="p">}</span>

<span class="c1">/// Returns the overall range (difference) between shadow-high and shadow-low.</span>
<span class="c1">// 最高值和最低值差值</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">shadowRange</span><span class="p">:</span> <span class="kt">Double</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="nf">abs</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="p">}</span>

<span class="c1">/// Returns the body size (difference between open and close).</span>
<span class="c1">// 开盘与收盘值 之差</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">bodyRange</span><span class="p">:</span> <span class="kt">Double</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="nf">abs</span><span class="p">(</span><span class="n">open</span> <span class="o">-</span> <span class="n">close</span><span class="p">)</span>
<span class="p">}</span>

<span class="c1">/// the center value of the candle. (Middle value between high and low)</span>
<span class="c1">// 高低盘中心</span>
<span class="kd">public</span> <span class="k">override</span> <span class="k">var</span> <span class="nv">value</span><span class="p">:</span> <span class="kt">Double</span>
<span class="p">{</span>
    <span class="k">get</span>
    <span class="p">{</span>
        <span class="k">return</span> <span class="k">super</span><span class="o">.</span><span class="n">value</span>
    <span class="p">}</span>
    <span class="k">set</span>
    <span class="p">{</span>
        <span class="k">super</span><span class="o">.</span><span class="n">value</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="mf">2.0</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">CandleChartDataEntry</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">high</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">low</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">open</span>
    <span class="n">copy</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">close</span>
    <span class="k">return</span> <span class="n">copy</span>
<span class="p">}</span>

}

CandleChartDataSet


public class CandleChartDataSet: LineScatterCandleChartDataSet
{
    /// the width of the candle-shadow-line in pixels.
    /// :default: 3.0
    // 高低盘值的宽度
    public var shadowWidth = CGFloat(1.5)

<span class="c1">/// the space between the candle entries</span>
<span class="c1">/// :default: 0.1 (10%)</span>
<span class="c1">// 两个条目之间的距离</span>
<span class="kd">private</span> <span class="k">var</span> <span class="nv">_bodySpace</span> <span class="o">=</span> <span class="kt">CGFloat</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span>

<span class="c1">/// the color of the shadow line</span>
<span class="c1">/// 阴影颜色</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">shadowColor</span><span class="p">:</span> <span class="kt">UIColor</span><span class="p">?</span>

<span class="c1">/// use candle color for the shadow</span>
<span class="c1">// 是否使用 蜡烛图的颜色作为阴影颜色</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">shadowColorSameAsCandle</span> <span class="o">=</span> <span class="kc">false</span>

<span class="c1">/// color for open &lt;= close</span>
<span class="c1">// 跌盘的颜色</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">decreasingColor</span><span class="p">:</span> <span class="kt">UIColor</span><span class="p">?</span>

<span class="c1">/// color for open &gt; close</span>
<span class="c1">// 涨盘的颜色</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">increasingColor</span><span class="p">:</span> <span class="kt">UIColor</span><span class="p">?</span>

<span class="c1">/// Are decreasing values drawn as filled?</span>
<span class="c1">// 跌盘是否填充</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">decreasingFilled</span> <span class="o">=</span> <span class="kc">false</span>

<span class="c1">/// Are increasing values drawn as filled?</span>
<span class="c1">// 涨盘是否填充</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">increasingFilled</span> <span class="o">=</span> <span class="kc">true</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="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">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="k">return</span>
    <span class="p">}</span>

    <span class="k">var</span> <span class="nv">entries</span> <span class="o">=</span> <span class="n">yVals</span> <span class="k">as!</span> <span class="p">[</span><span class="kt">CandleChartDataEntry</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="p">{</span>
        <span class="n">endValue</span> <span class="o">=</span> <span class="n">entries</span><span class="o">.</span><span class="n">count</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">end</span>

    <span class="n">_yMin</span> <span class="o">=</span> <span class="n">entries</span><span class="p">[</span><span class="n">start</span><span class="p">]</span><span class="o">.</span><span class="n">low</span>
    <span class="n">_yMax</span> <span class="o">=</span> <span class="n">entries</span><span class="p">[</span><span class="n">start</span><span class="p">]</span><span class="o">.</span><span class="n">high</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="o">+</span> <span class="mi">1</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">var</span> <span class="nv">e</span> <span class="o">=</span> <span class="n">entries</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="n">e</span><span class="o">.</span><span class="n">low</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">low</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">high</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">high</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// the space that is left out on the left and right side of each candle,</span>
<span class="c1">/// :default: 0.1 (10%), max 0.45, min 0.0</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">bodySpace</span><span class="p">:</span> <span class="kt">CGFloat</span>
<span class="p">{</span>
    <span class="k">set</span>
    <span class="p">{</span>
        <span class="n">_bodySpace</span> <span class="o">=</span> <span class="n">newValue</span>

        <span class="k">if</span> <span class="p">(</span><span class="n">_bodySpace</span> <span class="o">&lt;</span> <span class="mf">0.0</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_bodySpace</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="p">}</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">_bodySpace</span> <span class="o">&gt;</span> <span class="mf">0.45</span><span class="p">)</span>
        <span class="p">{</span>
            <span class="n">_bodySpace</span> <span class="o">=</span> <span class="mf">0.45</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">_bodySpace</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">/// Is the shadow color same as the candle color?</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">isShadowColorSameAsCandle</span><span class="p">:</span> <span class="kt">Bool</span> <span class="p">{</span> <span class="k">return</span> <span class="n">shadowColorSameAsCandle</span> <span class="p">}</span>

<span class="c1">/// Are increasing values drawn as filled?</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">isIncreasingFilled</span><span class="p">:</span> <span class="kt">Bool</span> <span class="p">{</span> <span class="k">return</span> <span class="n">increasingFilled</span><span class="p">;</span> <span class="p">}</span>

<span class="c1">/// Are decreasing values drawn as filled?</span>
<span class="kd">public</span> <span class="k">var</span> <span class="nv">isDecreasingFilled</span><span class="p">:</span> <span class="kt">Bool</span> <span class="p">{</span> <span class="k">return</span> <span class="n">decreasingFilled</span><span class="p">;</span> <span class="p">}</span>

}

CandleChartData


public class CandleChartData: 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>

}



blog comments powered by Disqus