C#ColorLine未显示在TChart中

本文关键字:TChart 显示 C#ColorLine | 更新日期: 2023-09-27 18:20:30

我目前有一个TChart,我想在其中引入一条可拖动的水平线,它可以更改线下点的颜色。为此,我选择使用ColorLine,但该行未显示在TChart中。我是在工作中使用正确的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
    }
}

C#ColorLine未显示在TChart中

下面的代码在这里对我来说很好。如果你的问题仍然存在,请发送一个简短的、自给自足的、正确的(可编译的)示例项目,在这里重现问题。您可以在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);
      }
    }
  }
}

事实证明,虽然行被正确添加,但我用来显示图表的代码没有正确更新图表,而是显示了在它被传递到我的高亮显示函数之前的图表版本。