2014-09-03 58 views
0

我目前有一個TChart,我想引入一個可拖動的水平線,它可以改變線下點的顏色。我已選擇使用ColorLine來達到此目的,但該行不會出現在TChart中。我是否正在使用正確的TChart工具,或者我錯過了什麼?C#ColorLine沒有顯示在TChart中

下面是我當前代碼的精簡版。

public class testClass 
{ 
    private ColorLine line; 
    private double lineYVal = 5; 
    private TChart savedChart; 

    public testClass() 
    { 
     line = new Colorline(); 
     line.AllowDrag = true; 
     line.Pen.Color = Color.Red; 
     line.EndDragLine += lineDragHandler; 
    } 

    public void foo(TChart chart)   //chart is prepopulated with datapoints from 0->10 
    { 
     savedChart = chart; 
     //existing code which assigns colors 
     chart.Series[0].ColorRange(chart.Series[0].YValues, double.MinValue, lineYVal, Color.Red); 
     chart.Series[0].ColorRange(chart.Series[0].YValues, lineYVal, double.MaxValue, Color.Blue); 

     //my attempt to add a line 
     chart.Tools.Add(line); 
     line.Active = true; 
     line.Axis = chart.Axes.Left; 
     line.Value = lineYVal; 
    } 

    private void lineDragHandler(object sender) 
    { 
     lineYVal = line.Value; 
     savedChart.Tools.Clear();  //remove existing line from chart 
     foo(savedChart);    //redo colors and re-add line 
    } 
} 

回答

0

事實證明,雖然正在正確添加行了,我的代碼是用在圖表中顯示不正確更新圖表,並顯示一個版本的圖表從之前它被傳遞到我的亮點功能。

1

下面的代碼適合我在這裏工作。如果問題仍然存在,請發送a Short, Self Contained, Correct (Compilable), Example project以在此處重現問題。您可以在www.steema.net/upload上發佈您的文件。

using Steema.TeeChart; 
using Steema.TeeChart.Styles; 
using Steema.TeeChart.Tools; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
    public Form1() 
    { 
     InitializeComponent(); 
     InitializeChart(); 
    } 

    private void InitializeChart() 
    { 
     testClass(); 

     //existing code which assigns colors 
     tChart1.Series.Add(new Steema.TeeChart.Styles.Bar()).FillSampleValues(); 

     tChart1.Series[0].ColorRange(tChart1.Series[0].YValues, double.MinValue, lineYVal, Color.Red); 
     tChart1.Series[0].ColorRange(tChart1.Series[0].YValues, lineYVal, double.MaxValue, Color.Blue); 

     //my attempt to add a line 
     tChart1.Tools.Add(line); 
     line.Active = true; 
     line.Axis = tChart1.Axes.Left; 
     line.Value = lineYVal; 
    } 

    private ColorLine line; 
    private double lineYVal = 5; 
    private TChart savedChart; 

    public void testClass() 
    { 
     line = new ColorLine(); 
     line.AllowDrag = true; 
     line.Pen.Color = Color.Red; 
     line.EndDragLine += lineDragHandler; 
    } 

    public void foo(TChart chart)   //chart is prepopulated with datapoints from 0->10 
    { 
     savedChart = chart; 
     //existing code which assigns colors 
     chart.Series[0].ColorRange(chart.Series[0].YValues, double.MinValue, lineYVal, Color.Red); 
     chart.Series[0].ColorRange(chart.Series[0].YValues, lineYVal, double.MaxValue, Color.Blue); 

     //my attempt to add a line 
     chart.Tools.Add(line); 
     line.Active = true; 
     line.Axis = chart.Axes.Left; 
     line.Value = lineYVal; 
    } 

    private void lineDragHandler(object sender) 
    { 
     lineYVal = line.Value; 
     if (savedChart != null) 
     { 
     savedChart.Tools.Clear(); //remove existing line from chart 
     foo(savedChart);    //redo colors and re-add line 
     } 
     else 
     { 
     foo(tChart1); 
     } 
    } 

    } 
}