2014-09-03 62 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); 
     } 
    } 

    } 
}